CMake C++ & CUDA: How to manipulate the compile options property only for a submodule and only for CUDA?

For example, a CMake submodule S has some compile flags inherited from its parent’s modules, like -std=C++14. But submodule S is a CUDA project that requires C++17.

I removed this flag like the following:

get_property(old_compile_opts·TARGET·SUBMODULE_S·PROPERTY·COMPILE_OPTIONS)
list(REMOVE_ITEM·old_compile_opts·"-std=c++14")·#·CUTE·requires·c++17,·avoid·overload
# reset
set_property(TARGET SUBMODULE_S PROPERTY COMPILE_OPTIONS ${old_compile_opts})
add_compile_options(-std=c++17)

But the CUDA FLAGS still has c++14, and only the CXX FLAGS does not have c++14.

Besides, I also have the same requirements for the sm_xx option, where the parent module uses sm_75, sm_80 while the submodule only uses sm_80.

Thus I think the root requirement is to manipulate compile flags only for a specific language and module.

Not to set CUDA FLAGS globally, but only for this submodule.

  • The modern way of specifying the C++ standard in CMake is through target_compile_features() on a target-by-target basis, see cmake-compile-features(7). For CUDA projects one can the use target_compile_features(mylib PUBLIC cuda_std_17), although ideally the dependency would have already done that. Can you change the parent module to use target_compile_features(mylib PUBLIC cxx_std_14)?

    – 




  • If you really need/want to use legacy-style CMake at module level there is also the COMPILE_FEATURES property which might be useful if you use it properly at parent and submodule level.

    – 

  • If using target_compile_features(submodule PUBLIC cuda_std_17), will the “flags.make” file generated during compilation for the submodule only have -std=c++17? Or it will have -std=c++17 -std=c++14 where the later flag is inherited from its parent module? Anyway, I’ll try this approach. Thank you!

    – 




  • I think that that will depend on how the -std=c++14 was added in the first place. If it uses the compile features API, it should only be used for C++, but that apparently isn’t the case. That is why I asked if you are able to change this in the parent module as well. Due to there being cxx_std_ and cuda_std_, CMake should be able to use the first for C++ sources and the second for CUDA sources.

    – 




  • The GPU architectures for which the CUDA code is compiled should not be hard-coded. Limits are ok but that doesn’t seem to be what you are asking. Just provide CMAKE_CUDA_ARCHITECTURE=80 at configuration time.

    – 

Leave a Comment