CMake + CUDA + separable compilation -> "nvcc doesn't know what to do with ' ' " - cmake

I use CMake for a project of mine involving CUDA. Recently I have had to turn on "separable compilation" of the some of my CUDA code:
set(CUDA_SEPARABLE_COMPILATION ON)
but then, building started to fail. As an example, the following would happen:
/usr/local/cuda/bin/nvcc -gencode arch=compute_30,code=compute_30 --std=c++11 \
-Xcompiler -Wall -O3 -DNDEBUG "" "" "" "" -m64 -ccbin /usr/bin/cc \
-dlink /some/where/generated_foo.cu.o -o /some/where/foo_intermediate_link.o
nvcc fatal : Don't know what to do with ''
(lines broken and names shortened for readability)
So, the problem is that something triggers CMake to add some empty (quoted) strings to the command-line, which nvcc doesn't like. Other than that the command seems fine.
Now, -O3 -DNDEBUG are my nvcc compilation flags for release builds. But I've certainly not added any empty-string flags anywhere. I tried looking into how FindCUDA constructs the nvcc invocation, but couldn't quite figure out where these empty strings are coming.
Without going into the specifics of my CMakeLists.txt, could this possibly be a well-known issue with CMake's FindaCUDA module which has a general workaround?
Note: I use GNU/Linux Mint 18.3, CMake 3.5, and CUDA 9.1.

It turns out this is a known CMake issue.
The workaround is to only set build-config-specific compilation flags for the active build config, e.g. instead of having:
set(CUDA_NVCC_FLAGS_RELEASE ${CUDA_NVCC_FLAGS_RELEASE} -O3)
set(CUDA_NVCC_FLAGS_DEBUG ${CUDA_NVCC_FLAGS_DEBUG} -g --generate-line-info)
in your CMakeLists.txt, use:
if (CMAKE_BUILD_TYPE_UPPER STREQUAL "RELEASE")
set(CUDA_NVCC_FLAGS_RELEASE ${CUDA_NVCC_FLAGS_RELEASE} -O3)
elseif (CMAKE_BUILD_TYPE_UPPER STREQUAL "DEBUG")
set(CUDA_NVCC_FLAGS_DEBUG ${CUDA_NVCC_FLAGS_DEBUG} -g --generate-line-info)
endif (CMAKE_BUILD_TYPE_UPPER STREQUAL "RELEASE")
instead (or more cases if you have multiple build types).
Another possible alternative is to not use FindCUDA at all, as CMake has added support for CUDA as a "first-class" language beginning with some 3.X release (not sure what X is).

Related

CMake get all flags, includes and defines programatically

I'm using CMake 3.12 with linux and I try to extract all flags, includes and defines programatically from a target.
I found some help online, but it was always limited to the default settings induced by CMake.
For example with the variables :
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
...
Here I'm missing external flags.
I' stumbled upon the file "flags.make" located in the build folder of the project:
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.12
# compile C with /usr/bin/gcc-4.8
C_FLAGS = -O3 -DNDEBUG -fPIC
C_DEFINES = -DLINUX -DLINUX64 -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
C_INCLUDES =
Which is exactly what I need. Does anybody know how to access these variables within CMake?
Thank you!
Best regards
Fabian

How to get cmake generate -std=c++14 flag for qcc compiler

I'm trying to cross compile some c++ library for QNX neutrino using cmake. In CMakeLists.txt file I specified CMAKE_CXX_STANDARD 14 required, but the resulting compiler command line does not contain the -std=c++14 option.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
I've tried using target compile features:
target_compile_features(my_library PRIVATE cxx_std_14)
but that gives me the following error:
CMake Error at CMakeLists.txt:53 (target_compile_features):
target_compile_features no known features for CXX compiler
"QCC"
version 5.4.0.
When I'm using check_cxx_compiler_flag feature, it seems to recognize the option:
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-std=c++14 CXX14_SUPPORT)
if(CXX14_SUPPORT)
message("c++14 support found")
else()
message("c++14 unsupported")
endif()
This outputs message
c++14 support found
Running qcc manually it accepts the -std=c++14 option just fine and the code using std::make_unique compiles just fine.
Also using the native compiler (Ubuntu 18.04, gcc) everything work fine with cmake generated makefiles. make VERBOSE=1 displays the following command line (I removed some directories):
/usr/local/bin/c++ -Dshm_transfer_EXPORTS -I... -fPIC -std=gnu++14 -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp
as opposed to the command line using qcc toolchain:
.../qnx700/host/linux/x86_64/usr/bin/qcc -lang-c++ -Vgcc_ntox86_64 -lang-c++ -Dshm_transfer_EXPORTS -I... -fPIC -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp
I would have expected the cmake command to recognize that qcc supports the -std=c++14 option and generates the corresponding command lines because of the CMAKE_CXX_STANDARD setting.
Use
set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
. Using this you can stick the compiler setting to the target, while global flags are dis encouraged and can be overwritten by other cmake consumers. This the reason I assume why the deprecated set(CMAKE_CXX_STANDARD 14) did not help you: I can not see your full CMakeLists.txt and bet you have many sub folders and other targets, which could reset the CMAKE_CXX_STANDARD them selfs. Also make sure of the ordering of the CMake commands.
And you can replace ${PROJECT_NAME} with my_library if you want.
add_compile_options(-std=gnu++14)
Add this to your project level CMakeLists.txt file, not in toolchain.

CMake not respecting command line options

Ubuntu 14_04, gcc 4.8.4, cmake 2.8.12.2
I wish to add additional options to UnitTest++. I added code coverage via UTPP_CODE_COVERAGE but leave it off for regular builds. This seemed to fail - nothing is in the Makefile that looks to be specified via the set(CMAKE_CXX_FLAGS option.
So I looked at whether the UTPP_AMPLIFY_WARNINGS command works
Whether I compile with it on or off, no difference is made to the Makefile.
At the terminal I compile with
$ cmake -G "Unix Makefiles" -DUTPP_AMPLIFY_WARNINGS=ON ../
But when I analyse the generated Makefile -Wall is nowhere to be found in the output. It's not even in the CMakeCache.txt
What am I doing wrong?
I can confirm what you have observed. There seems that the required warning level is already default in the CMakeLists.txt file and also the command set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") setting the -Wall flag is reached and executed (have tested this using message(). If there is no other answer here, at least you know you are not alone wondering how it comes.
cmake_minimum_required(VERSION 2.8.1)
project(UnitTest++)
option(UTPP_USE_PLUS_SIGN
"Set this to OFF if you wish to use '-cpp' instead of '++' in lib/include paths"
ON)
option(UTPP_INCLUDE_TESTS_IN_BUILD
"Set this to OFF if you do not wish to automatically build or run unit tests as part of the default cmake --build"
ON)
option(UTPP_AMPLIFY_WARNINGS
"Set this to OFF if you wish to use CMake default warning levels; should generally only use to work around support issues for your specific compiler"
ON)
...
# up warning level for project
if (${UTPP_AMPLIFY_WARNINGS})
# instead of getting compiler specific, we're going to try making an assumption that an existing /W# means
# we are dealing with an MSVC or MSVC-like compiler (e.g. Intel on Windows)
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
# message(STATUS "CMAKE_CXX_FLAGS MATCHES")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
else()
# message(STATUS "set(CMAKE_CXX_FLAGS")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
endif()
endif()

How to disable cmake setting CXX_FLAGS = -std=c++1y -g -O3 -std=gnu++11

cmake generates the flags.make file with
CXX_FLAGS = -std=c++1y -g -O3 -std=gnu++11
The first -std is set by me and the second is generated by cmake. I can't understand where the second one comes from or how to disable it.
Has someone encountered the same issue and know how to resolve this?
Without having your code/project it's difficult to tell. Just a guess: you are including/depending on an external library that does need C++11 (e.g. Boost) and has requested it from CMake.
So to override this with your own need for C++14 you can either set globally CMAKE_CXX_STANDARD with something like:
set(CMAKE_CXX_STANDARD 14)
or with naming specific compiler features needed in your library/executable.

CMake, static library and link time optimization

I'm trying to create static library with link time optimization using cmake and g++.
set(
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -pedantic -std=c++11"
)
if (CMAKE_COMPILER_IS_GNUCXX)
set(
CMAKE_STATIC_LINKER_FLAGS_RELEASE
"${CMAKE_STATIC_LINKER_FLAGS_RELEASE} -flto -fwhole-program"
)
endif()
add_library(
mylib STATIC
mylib.cpp
)
But when running typical
cmake -DCMAKE_BUILD_TYPE=Release ..
make
I'm getting following error:
/usr/bin/ar: two different operation options specified
link.txt file contains following commands:
/usr/bin/ar cq libmylib.a -flto -fwhole-program CMakeFiles/mylib.cpp.o
/usr/bin/ranlib libmylib.a
From what I understand from running ar --help the -flto -fwhole-program should be before libmylib.a in the first line. But I have no idea how to force CMake to put it there.
Is my assumption correct? And how can I resolve this?
EDIT: I'd like to add that I'm completely new to using LTO so if it doesn't make sense to use it for static libraries, please do tell me so.
-flto isn't a valid option for ar. You should instead use these flags for CMAKE_EXE_LINKER_FLAGS.