0

I am on Visual Studio 2017, Windows 10, CMake 3.16.

I have a situation where I have a project that consists of cuda .cu files that links to a c++ based upstream library via cmake. For that upstream library, the target consists of a macro, WINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push,0)), that is stored in its cmake target via INTERFACE_COMPILE_DEFINITIONS. The main problem is nvcc is unable parse this because of the comma, I actually need to modify it to WINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push\,0)) in order for it to compile, otherwise the below error message will show up

nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified

but I couldn't find a proper way to do this in cmake. Please see below minimal examples of my failed attempts

cmake_minimum_required(VERSION 3.16)

project(sample)

add_library(aaatarget aaa.cpp)

target_compile_definitions(aaatarget PUBLIC MYMACRO="aaa")

add_executable(bbbtarget bbb.cpp)

target_link_libraries(bbbtarget aaatarget)

# this works but it will mess up other targets that links to aaatarget that actually wants 
# MYMACRO to be it's original aaa
set_target_properties(aaatarget PROPERTIES INTERFACE_COMPILE_DEFINITIONS MYMACRO="bbb")

# ideally I want to just change bbbtarget to make it work but none of the below works
set_target_properties(bbbtarget PROPERTIES INTERFACE_COMPILE_DEFINITIONS MYMACRO="bbb")
set_target_properties(bbbtarget PROPERTIES INTERFACE_COMPILE_OPTIONS MYMACRO="bbb")
set_target_properties(bbbtarget PROPERTIES COMPILE_DEFINITIONS MYMACRO="bbb")
set_target_properties(bbbtarget PROPERTIES COMPILE_OPTIONS MYMACRO="bbb")

# this has the potential to solve it for my specific use case but unfortunately COMPILE_LANGUAGE
# doesn't work on visual studio... otherwise, I could request the upstream vendor to make this change
target_compile_definitions(aaatarget PUBLIC "$<$<COMPILE_LANGUAGE:CXX>:MYMACRO=\"aaa\"")

Can someone tell me how to link to a target in CMake but partially modify some properties locally? Best solution is NVIDIA fix nvcc so it can parse the comma correctly just like cl but it will be great to have a workaround now before that happen.

talonmies
  • 70,661
  • 34
  • 192
  • 269
user3667089
  • 2,996
  • 5
  • 30
  • 56
  • I see, you want to override target properties only when linked to another specific target. Not sure if there is a mechanic to do this in CMake. There might be. – Jan Hošek Mar 10 '20 at 08:41
  • Maybe [this](https://stackoverflow.com/questions/41866614/how-to-overwrite-macro-definition-in-cmake) might help? – Jan Hošek Mar 10 '20 at 08:59
  • `unable parse this because of the comma` - can you show how do you use it and how does it fail? I guess you did `WINDOWS_DISABLE_ALL_WARNING="__pragma(warning(push,0))"`, which will not work, but `"WINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push,0))"` should work. – KamilCuk Mar 10 '20 at 10:00
  • @KamilCuk Easiest way to is to recreate the problem without cmake. `nvcc -c "-DWINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push,0))" main.cu`, `main.cu` is just a one-liner with `WINDOWS_DISABLE_ALL_WARNING`. You will see the compilation error. Vice versa, `cl -c "-DWINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push,0))" main.cpp`, `main.cpp` is also a one-liner with `WINDOWS_DISABLE_ALL_WARNING`, but it complies. – user3667089 Mar 10 '20 at 15:03
  • @JanHošek that link is actually a question asked by myself. It's a different problem and I don't think it helps for this one. – user3667089 Mar 10 '20 at 15:05
  • `You will see the compilation error.` - then are you sure this is related to cmake? What error? Och, I see that! `nvcc -E -c "-DWINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push,0))"` is missing a `,0))` part – KamilCuk Mar 10 '20 at 15:10
  • @KamilCuk same error as shown in the original question, but complies if I add the backslash, feel free to give it a try. This is a cmake problem before nvidia fixes this. I need to link to a upstream cmake target that has this definition, and I need a way for modify it locally for my nvcc related code. – user3667089 Mar 10 '20 at 15:15
  • `This is a cmake problem before nvidia fixes this` Seems like elements in [--define-macro](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#file-and-path-specifications-define-macro) are separated by a comma. But you nowhere set `cuda` as project language. How are you specifying cmake to use nvcc as compiler? If you think this is a cmake bug, you should take it – KamilCuk Mar 10 '20 at 15:16
  • Because [isn't it already solved](https://gitlab.kitware.com/cmake/cmake/issues/16510)? By properly quoting the compile definitions in [cua cmake templates](https://gitlab.kitware.com/cmake/cmake/-/commit/c1f4f13dbfa7caf6bbf4d8b70a7f09f786c7eed6)? – KamilCuk Mar 10 '20 at 15:21
  • @KamilCuk In my actual project, there is `project(myproject LANGUGUES CXX CUDA)`. That link is describing a different problem that has been solved, If you think you have the solution, can you please write up one? – user3667089 Mar 10 '20 at 15:29

0 Answers0