Hugo Maxwell
Hugo Maxwell

Reputation: 763

CMake variable ignored on first build (CUDA_NVCC_FLAGS)

In my CMakeLists.txt I am loading an environment variable as such:

cmake_minimum_required(VERSION 2.6)

set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} $ENV{CUDA_NVCC_FLAGS} -lineinfo --default-stream per-thread --disable-warnings")

message("CUDA_NVCC_FLAGS = ${CUDA_NVCC_FLAGS}")

However on the first build attempt after a clean (rm -r build) it will simply ignore the CUDA_NVCC_FLAGS variable, causing the build to fail:

cmake -D CMAKE_CXX_FLAGS="-g -O3 -fmax-errors=1" ../../

make -j8

Console ouput:

CUDA_NVCC_FLAGS =  -gencode arch=compute_61,code=sm_61 -lineinfo --default-stream per-thread --disable-warnings
...
/home/mad/workspace/automy-system/vision/src/DeBayerFilter.cu(132): warning: integer conversion resulted in truncation
/home/mad/workspace/automy-system/vision/src/HeightMapFilter.cu(115): error: identifier "__ldg" is undefined

The build fails because my CUDA code requires a certain compute capability which is enabled by the CUDA_NVCC_FLAGS.

On a second attempt and there after it works just fine.

cmake version 3.5.1

Upvotes: 1

Views: 1011

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

Like any other CMAKE_<LANG>_FLAGS variable, CUDA_NVCC_FLAGS variable is set at project(CUDA) call, when the compiler is detected. So appending to that variable should be performed after the project call:

project(MyProject CUDA)
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} $ENV{CUDA_NVCC_FLAGS} -lineinfo --default-stream per-thread --disable-warnings")

If CUDA is detected with find_package(CUDA) (in old CMake versions which doesn't natively support CUDA), then appending the flags should come after find_package(CUDA), which sets the CUDA_NVCC_FLAGS variable:

find_package(CUDA REQUIRED)
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} $ENV{CUDA_NVCC_FLAGS} -lineinfo --default-stream per-thread --disable-warnings")

The reason why inverse order works, but only on the non-first configuration, is that variable CUDA_NVCC_FLAGS is actually cached: The first project() call sets the variable's value and stores it into the cache. Futher project() calls detect that the compiler is already checked and do not re-set the variable.

Upvotes: 1

Related Questions