CMakeLists.txt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. cmake_minimum_required(VERSION 3.7)
  2. project (CudaOnlySeparateCompilation CUDA)
  3. #Goal for this example:
  4. #Build a static library that defines multiple methods and kernels that
  5. #use each other.
  6. #After that confirm that we can call those methods from dynamic libraries
  7. #and executables.
  8. #We complicate the matter by also testing that multiple static libraries
  9. #all containing cuda separable compilation code links properly
  10. string(APPEND CMAKE_CUDA_FLAGS " -gencode arch=compute_30,code=compute_30")
  11. set(CMAKE_CXX_STANDARD 11)
  12. set(CMAKE_CUDA_STANDARD 11)
  13. set(CMAKE_CUDA_SEPARABLE_COMPILATION ON)
  14. add_library(CUDASeparateLibA STATIC file1.cu file2.cu file3.cu)
  15. get_property(sep_comp TARGET CUDASeparateLibA PROPERTY CUDA_SEPARABLE_COMPILATION)
  16. if(NOT sep_comp)
  17. message(FATAL_ERROR "CUDA_SEPARABLE_COMPILATION not initialized")
  18. endif()
  19. unset(CMAKE_CUDA_SEPARABLE_COMPILATION)
  20. if(CMAKE_CUDA_SIMULATE_ID STREQUAL "MSVC")
  21. # Test adding a flag that is not in our CUDA flag table for VS.
  22. if(NOT CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 8)
  23. string(APPEND CMAKE_CUDA_FLAGS " --ftemplate-depth 50")
  24. endif()
  25. # Test adding a flag that nvcc should pass to the host compiler.
  26. target_compile_options(CUDASeparateLibA PRIVATE -Xcompiler=-bigobj)
  27. endif()
  28. #Having file4/file5 in a shared library causes serious problems
  29. #with the nvcc linker and it will generate bad entries that will
  30. #cause a segv when trying to run the executable
  31. #
  32. add_library(CUDASeparateLibB STATIC file4.cu file5.cu)
  33. target_link_libraries(CUDASeparateLibB PRIVATE CUDASeparateLibA)
  34. add_executable(CudaOnlySeparateCompilation main.cu)
  35. target_link_libraries(CudaOnlySeparateCompilation
  36. PRIVATE CUDASeparateLibB)
  37. set_target_properties(CUDASeparateLibA
  38. CUDASeparateLibB
  39. PROPERTIES CUDA_SEPARABLE_COMPILATION ON
  40. POSITION_INDEPENDENT_CODE ON)
  41. if (CMAKE_GENERATOR MATCHES "^Visual Studio")
  42. #Visual Studio CUDA integration will not perform device linking
  43. #on a target that itself does not have GenerateRelocatableDeviceCode
  44. #enabled.
  45. set_target_properties(CudaOnlySeparateCompilation
  46. PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
  47. endif()
  48. if(APPLE)
  49. # Help the static cuda runtime find the driver (libcuda.dyllib) at runtime.
  50. set_property(TARGET CudaOnlySeparateCompilation PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
  51. endif()