Bruno Golosio
Bruno Golosio

Reputation: 135

Generate a Makefile that compiles and links all the source files in a single step with Automake

I am new with automake. I am trying to write the Makefile.am files for my library, however due to some limitation of the nvcc compiler, I need to compile and link the source files in a single step, without producing the .o files, so that I directly generate the library without having to link separate files. I could find several examples of Makefile.am files in the web, but none of them seemed to fit my need. Here is my (working) Makefile:

CC=g++
NVCC=nvcc
CXXFLAGS= -O3 -Wall -fPIC
CUDAFLAGS= -arch sm_30 --ptxas-options=-v --relocatable-device-code true
LIBS= -lm
LDFLAGS = -shared   # linking flags

RM = rm -f   # rm command
TARGET_LIB = ./lib/libsharedcuda.so  # target lib

SRC_DIR   = src

SRC_FILES  = $(wildcard $(SRC_DIR)/*.cu)
SRC_FILES += $(wildcard $(SRC_DIR)/*.cpp)

H_FILES   = $(wildcard $(SRC_DIR)/*.h)
H_FILES += $(wildcard $(SRC_DIR)/*.cuh)

.PHONY: all
all: ${TARGET_LIB}

$(TARGET_LIB): $(SRC_FILES)
    $(NVCC) -ccbin=${CC} --compiler-options  '${CXXFLAGS}' ${CUDAFLAGS} ${LDFLAGS} -o $@ $^

.PHONY: clean
clean:
    -${RM} ${TARGET_LIB}

How should the Makefile.am be written in order to produce a makefile like this one?

Upvotes: 1

Views: 322

Answers (1)

Bruno Golosio
Bruno Golosio

Reputation: 135

The correct answer to my question is the one written by Sam Varshavchik: as he pointed out, it is not possible. However, for anyone who might be interested, I found a workaround that solves my problem, useful for instance if you want to build a CUDA-C/C++ shared library with Autotools.

The trick is to generate first the src/Makefile.in file using autotools, then modifing this file before the make command. With my versions of autoconf/automake, the relevant line is:

    $(AM_V_CXXLD)$(libexample_la_LINK) ... 

that should be replaced by something like:

    nvcc -ccbin=g++ --compiler-options  '-O3 -Wall -fPIC' -shared -arch sm_30 --ptxas-options=-v --relocatable-device-code true -o source-files -lm

where libexample is the name of the library and source-files is the list of files to be compiled. Be careful: the line is preceded by a tab, not by spaces. If you want to be able to use "make install", you should also modify the command that installs the library in the proper folder. In my case in:

    $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL) $(INSTALL_STRIP_FLAG) $$list2 "$(DESTDIR)$(libdir)";

"$$list2" should be replaced by "libexample.so"

This patch could be done automatically by a shell script launched at the end of the config script. I have placed a complete working example in https://github.com/golosio/sharedcuda_autotools

Upvotes: 1

Related Questions