diff options
author | Joshua Bakita <jbakita@cs.unc.edu> | 2025-03-20 16:28:52 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2025-03-20 16:46:21 -0400 |
commit | 39c57bca3cbb42b1939a28377d8ef6cfab872450 (patch) | |
tree | 8a2ac5d15c420c7df3dff989a1d749194cda552c | |
parent | 5173f55d57a944c4f6a3e018892fc1da55e15571 (diff) |
Makefile improvements
- Add an "all" build target
- Fix build if libcuda.so is not on linker search path
- Do not assume that nvcc is available on $PATH
- Allow specifying CFLAGS and LDFLAGS when running make
- Allow passing non-standard CUDA build locations to make
Suggested usage if CUDA is installed in a non-standard location,
say, /playpen/jbakita/CUDA/cuda-archive/cuda-12.2:
make CUDA=/playpen/jbakita/CUDA/cuda-archive/cuda-12.2
-rw-r--r-- | Makefile | 24 | ||||
-rw-r--r-- | README.md | 8 |
2 files changed, 20 insertions, 12 deletions
@@ -1,22 +1,28 @@ | |||
1 | # Set this if CUDA is installed in a different location | ||
2 | CUDA ?= /usr/local/cuda | ||
1 | # Note that CXX and CC are predefined as g++ and cc (respectively) by Make | 3 | # Note that CXX and CC are predefined as g++ and cc (respectively) by Make |
2 | NVCC ?= nvcc | 4 | NVCC ?= $(CUDA)/bin/nvcc |
3 | # -fPIC is needed in all cases, as we may be linked into another shared library | 5 | # Everything has to have -lcuda, as it's needed for libsmctrl |
4 | CFLAGS = -fPIC | 6 | LDFLAGS := -lcuda -I$(CUDA)/include -L$(CUDA)/lib64 |
5 | LDFLAGS = -lcuda -I/usr/local/cuda/include | ||
6 | 7 | ||
7 | .PHONY: clean tests | 8 | .PHONY: clean tests all |
8 | 9 | ||
10 | # ----- Main Library ----- | ||
9 | libsmctrl.so: libsmctrl.c libsmctrl.h | 11 | libsmctrl.so: libsmctrl.c libsmctrl.h |
10 | $(CC) $< -shared -o $@ $(CFLAGS) $(LDFLAGS) | 12 | $(CC) $< -shared -o $@ -fPIC $(CFLAGS) $(LDFLAGS) |
11 | 13 | ||
14 | # -fPIC is needed even if built as a static library, in case we are linked into | ||
15 | # another shared library | ||
12 | libsmctrl.a: libsmctrl.c libsmctrl.h | 16 | libsmctrl.a: libsmctrl.c libsmctrl.h |
13 | $(CC) $< -c -o libsmctrl.o $(CFLAGS) $(LDFLAGS) | 17 | $(CC) $< -c -o libsmctrl.o -fPIC $(CFLAGS) $(LDFLAGS) |
14 | ar rcs $@ libsmctrl.o | 18 | ar rcs $@ libsmctrl.o |
15 | 19 | ||
20 | # ----- Utilities ----- | ||
16 | # Use static linking with tests to avoid LD_LIBRARY_PATH issues | 21 | # Use static linking with tests to avoid LD_LIBRARY_PATH issues |
17 | libsmctrl_test_gpc_info: libsmctrl_test_gpc_info.c libsmctrl.a testbench.h | 22 | libsmctrl_test_gpc_info: libsmctrl_test_gpc_info.c libsmctrl.a testbench.h |
18 | $(CC) $< -o $@ -g -L. -l:libsmctrl.a $(LDFLAGS) | 23 | $(CC) $< -o $@ -g -L. -l:libsmctrl.a $(CFLAGS) $(LDFLAGS) |
19 | 24 | ||
25 | # ----- Tests ----- | ||
20 | libsmctrl_test_mask_shared.o: libsmctrl_test_mask_shared.cu testbench.h | 26 | libsmctrl_test_mask_shared.o: libsmctrl_test_mask_shared.cu testbench.h |
21 | $(NVCC) -ccbin $(CXX) $< -c -g | 27 | $(NVCC) -ccbin $(CXX) $< -c -g |
22 | 28 | ||
@@ -37,6 +43,8 @@ libsmctrl_test_next_mask_override: libsmctrl_test_next_mask_override.c libsmctrl | |||
37 | 43 | ||
38 | tests: libsmctrl_test_gpc_info libsmctrl_test_global_mask libsmctrl_test_stream_mask libsmctrl_test_stream_mask_override libsmctrl_test_next_mask libsmctrl_test_next_mask_override | 44 | tests: libsmctrl_test_gpc_info libsmctrl_test_global_mask libsmctrl_test_stream_mask libsmctrl_test_stream_mask_override libsmctrl_test_next_mask libsmctrl_test_next_mask_override |
39 | 45 | ||
46 | all: libsmctrl.so tests | ||
47 | |||
40 | clean: | 48 | clean: |
41 | rm -f libsmctrl.so libsmctrl.a libsmctrl_test_gpu_info \ | 49 | rm -f libsmctrl.so libsmctrl.a libsmctrl_test_gpu_info \ |
42 | libsmctrl_test_mask_shared.o libmsctrl_test_global_mask \ | 50 | libsmctrl_test_mask_shared.o libmsctrl_test_global_mask \ |
@@ -29,10 +29,10 @@ To build, ensure that you have `gcc` installed and access to the CUDA SDK includ | |||
29 | make libsmctrl.a | 29 | make libsmctrl.a |
30 | ``` | 30 | ``` |
31 | 31 | ||
32 | If you see an error that the command `nvcc` was not found, `nvcc` is not available on your `PATH`. | 32 | If you see errors about CUDA headers or libraries not being found, your CUDA installation may be in a non-standard location. |
33 | Correct this error by explictly specifying the location of `nvcc` to `make`, e.g.: | 33 | Correct this error by explictly specifying the location of the CUDA install `make`, e.g.: |
34 | ``` | 34 | ``` |
35 | make NVCC=/playpen/jbakita/CUDA/cuda-archive/cuda-10.2/bin/nvcc libsmctrl.a | 35 | make CUDA=/playpen/jbakita/CUDA/cuda-archive/cuda-10.2/ libsmctrl.a |
36 | ``` | 36 | ``` |
37 | 37 | ||
38 | For binary backwards-compatibility to old versions of the NVIDIA GPU driver, we recommend building with an old version of the CUDA SDK. | 38 | For binary backwards-compatibility to old versions of the NVIDIA GPU driver, we recommend building with an old version of the CUDA SDK. |
@@ -41,7 +41,7 @@ For example, by building against CUDA 10.2, the binary will be compatible with a | |||
41 | Older versions of `nvcc` may require you to use an older version of `g++`. | 41 | Older versions of `nvcc` may require you to use an older version of `g++`. |
42 | This can be explictly specified via the `CXX` variable, e.g.: | 42 | This can be explictly specified via the `CXX` variable, e.g.: |
43 | ``` | 43 | ``` |
44 | make NVCC=/playpen/jbakita/CUDA/cuda-archive/cuda-8.0/bin/nvcc CXX=g++-5 libsmctrl.a | 44 | make CUDA=/playpen/jbakita/CUDA/cuda-archive/cuda-8.0/ CXX=g++-5 libsmctrl.a |
45 | ``` | 45 | ``` |
46 | 46 | ||
47 | `libsmctrl` supports being built as a shared library. | 47 | `libsmctrl` supports being built as a shared library. |