[CMake] Setting compile defs with: Cuda, add_subdirectory, and set_target_properties (COMPILE_DEFINITIONS)

James Bigler jamesbigler at gmail.com
Mon Aug 16 15:28:20 EDT 2010


On Mon, Aug 16, 2010 at 12:21 PM, Brian Davis <bitminer at gmail.com> wrote:

>
> From my current research into this problem it looks like the issue can be
> traced to  these lines in FindCuda.cmake
>
> --snip--
>   # Get the list of definitions from the directory property
>   get_directory_property(CUDA_NVCC_DEFINITIONS COMPILE_DEFINITIONS)
> message( "CUDA_NVCC_DEFINITIONS = ${CUDA_NVCC_DEFINITIONS}" )
>
>   if(CUDA_NVCC_DEFINITIONS)
>     foreach(_definition ${CUDA_NVCC_DEFINITIONS})
>       list(APPEND nvcc_flags "-D${_definition}")
>     endforeach()
>   endif()
> message( "nvcc_flags = ${nvcc_flags}" )
> --end snip--
>
> output from cmake as a result of added message line in Find.Cuda.
>
> -- snip --
> CUDA_NVCC_DEFINITIONS =
> gpuReconPlugin_EXPORTS=yes;GPU_RECON=yes;CUDADeviceProperties_DLLExport=yes;GPU_RECON=yes;CUDADeviceProperties_DLLExport=yes;CPU_RECON=yes
> nvcc_flags =
> -m64;-DgpuReconPlugin_EXPORTS=yes;-DGPU_RECON=yes;-DCUDADeviceProperties_DLLExport=yes;-DGPU_RECON=yes;-DCUDADeviceProperties_DLLExport=yes;-DCPU_RECON=yes
> -- end snip --
>
> note the use of get_directory property.  Have I mentioned to anyone lately
> why I can't understand CMake's (poor)  design choice in correlating
> directory structure to target build specification. It would appear to me
> from this (if unmodified) that I must create a new directory in order to get
> CMake to build 2 dlls in the same directory based on the same source using
> diffrent compiler defined.   I have reached a state of boondogglement.
>

This is how CMake works.  ADD_DEFINITIONS is a directory property and not a
target property.  If you wish to have per target definitions, there are
other ways to accomplish this:

    set(my_nvcc_flags)
    foreach( DEFINE_STR ${ARG_DEFINES} )
        message( "DEFINE_STR = ${DEFINE_STR}" )
        list(APPEND my_nvcc_flags -D${DEFINE_STR} )
    endforeach()

    if(DEFINED ARG_CU_SOURCES )
        message( "ARG_CU_SOURCES is defined as = ${ARG_CU_SOURCES}" )
        CUDA_ADD_LIBRARY( ${LIB_NAME} SHARED ${MATLAB_CUDA_FILES} OPTIONS
${my_nvcc_flags} )
    else()
        message( "ARG_CU_SOURCES is not defined" )
       ADD_LIBRARY( ${LIB_NAME} SHARED ${MATLAB_CUDA_FILES} )
    endif()

And don't forget to add the definitions to the target, since you are no
longer using add_definitions.

    if( DEFINED ARG_DEFINES )
    SET_TARGET_PROPERTIES( ${LIB_NAME} PROPERTIES
        PREFIX ""
#        COMPILE_DEFINITIONS ${ARG_DEFINES} )
#        COMPILE_DEFINITIONS MY_DEFINE=DEFINE_THIS_GOOP
        COMPILE_FLAGS -DMY_DEFINE=DEFINE_THIS_GOOP
        )
    endif()

FindCUDA.cmake can't use the target properties, because target properties
can be set anytime, and the FindCUDA.cmake script needs the properties at
the time cuda_add_libraries is called.  This is because FindCUDA.cmake
generates build scripts at configure time and not at generate time.

James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20100816/34c2bd2b/attachment.htm>


More information about the CMake mailing list