CMakeLists.txt 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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,sm_30,sm_35\\\"")
  11. string(APPEND CMAKE_CUDA_FLAGS " --generate-code=arch=compute_50,code=[compute_50,sm_50,sm_52]")
  12. set(CMAKE_CXX_STANDARD 11)
  13. set(CMAKE_CUDA_STANDARD 11)
  14. set(CMAKE_CUDA_SEPARABLE_COMPILATION ON)
  15. add_library(CUDASeparateLibA STATIC file1.cu file2.cu file3.cu)
  16. get_property(sep_comp TARGET CUDASeparateLibA PROPERTY CUDA_SEPARABLE_COMPILATION)
  17. if(NOT sep_comp)
  18. message(FATAL_ERROR "CUDA_SEPARABLE_COMPILATION not initialized")
  19. endif()
  20. unset(CMAKE_CUDA_SEPARABLE_COMPILATION)
  21. if(CMAKE_CUDA_SIMULATE_ID STREQUAL "MSVC")
  22. # Test adding a flag that is not in our CUDA flag table for VS.
  23. if(NOT CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 8)
  24. string(APPEND CMAKE_CUDA_FLAGS " --ftemplate-depth 50")
  25. endif()
  26. # Test adding a flag that nvcc should pass to the host compiler.
  27. target_compile_options(CUDASeparateLibA PRIVATE -Xcompiler=-bigobj)
  28. endif()
  29. #Having file4/file5 in a shared library causes serious problems
  30. #with the nvcc linker and it will generate bad entries that will
  31. #cause a segv when trying to run the executable
  32. #
  33. add_library(CUDASeparateLibB STATIC file4.cu file5.cu)
  34. target_link_libraries(CUDASeparateLibB PRIVATE CUDASeparateLibA)
  35. add_executable(CudaOnlySeparateCompilation main.cu)
  36. target_link_libraries(CudaOnlySeparateCompilation
  37. PRIVATE CUDASeparateLibB)
  38. set_target_properties(CUDASeparateLibA
  39. CUDASeparateLibB
  40. PROPERTIES CUDA_SEPARABLE_COMPILATION ON
  41. POSITION_INDEPENDENT_CODE ON)
  42. if (CMAKE_GENERATOR MATCHES "^Visual Studio")
  43. #Visual Studio CUDA integration will not perform device linking
  44. #on a target that itself does not have GenerateRelocatableDeviceCode
  45. #enabled.
  46. set_target_properties(CudaOnlySeparateCompilation
  47. PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
  48. endif()
  49. if(APPLE)
  50. # Help the static cuda runtime find the driver (libcuda.dyllib) at runtime.
  51. set_property(TARGET CudaOnlySeparateCompilation PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
  52. endif()