I need to build some internal libraries that mix pure C files and CUDA files. In addition, different libraries may be built from the same sources with different preprocessor options.
I've managed to get something that works, but what happens is that I don't like the fact that I have to explicitly define the CUDA objects when they could be easily inferred from the _SOURCES var:
$ cat Makefile.am
AUTOMAKE_OPTIONS = foreign
NVCC=cc
cuda_libs = libfoobar1.a libfoobar2.a
noinst_LIBRARIES = $(cuda_libs)
foobar_sources = foo.c bar.cu
libfoobar1_a_SOURCES = $(foobar_sources)
libfoobar1_a_CPPFLAGS = -DLIB1
libfoobar1_a_LIBADD = libfoobar1_a-bar.o # CUDA object explicit definition
libfoobar2_a_SOURCES = $(foobar_sources)
libfoobar2_a_CPPFLAGS = -DLIB2
libfoobar2_a_LIBADD = libfoobar2_a-bar.o # CUDA object explicit definition
$(eval include cuda.mk) # why eval? Check comment #1
clean-local:
rm -rf libfoobar2_a-bar.o libfoobar1_a-bar.o
$ cat cuda.mk
# $(get_conincal names)
get_canonical = $(subst .,_,$(1))
# $(call cuda_rule prefix) http://blog.jgc.org/2012/01/using-gnu-makes-define-and-eval-to.html
define cuda_rule
$(1)-%.o: %.cu
$(NVCC) $(CUDA_CFLAGS) $($(1)_CPPFLAGS) --compiler-options="$($(1)_CFLAGS)" -c -o $$@ $$<
endef
$(foreach cuda_prefix,$(call get_canonical,$(cuda_libs)),$(eval $(call cuda_rule,$(cuda_prefix))))
If I try to generalize the CUDA objects definitions moving them to the rule generation with something like this:
define cuda_rule
$(1)_LIBADD = $(addsuffix .o,$(addprefix $(1)-,$(basename $(filter %.cu,$($(1)_SOURCES)))))
[...]
endef
make now seems to not get the rule, and does not know how to build the CUDA objects:
ar cru libfoobar1.a libfoobar1_a-foo.o libfoobar1_a-bar.o
ar: libfoobar1_a-bar.o: No such file or directory
Any ideas? Thanks in advance.
Albert
P.S. All files that you need if you want to play along with this