From 0b44f5c51cd21ba9dc03c40c3cbeeab3c7715083 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Wed, 6 Apr 2022 15:21:13 +0200 Subject: [PATCH 1/7] Fix include --- CMakeLists.txt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb27a355..b7b5403a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,8 @@ project( DESCRIPTION "C++ Wrappers for the CUDA Driver API" VERSION 0.3.0 HOMEPAGE_URL https://github.com/nlesc-recruit/cudawrappers - LANGUAGES CUDA CXX) + LANGUAGES CXX) + set(CMAKE_CXX_STANDARD 11) set(CMAKE_BUILD_TYPE "Release") @@ -18,11 +19,15 @@ endif() set(SOURCE_FILES src/cu.cpp src/nvrtc.cpp) -include_directories(./include/) -include_directories(${CUDAToolkit_INCLUDE_DIRS}) - add_library(cudawrappers SHARED ${SOURCE_FILES}) +target_include_directories( + cudawrappers + PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" + PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) + +target_link_libraries(cudawrappers PUBLIC CUDA::cuda_driver CUDA::nvrtc) + # Including linters rules include(cmake/linter-tools.cmake) From 53ae8c7f41bec4fc9b525d8fe730374bce51653c Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Fri, 8 Apr 2022 18:15:06 +0200 Subject: [PATCH 2/7] Add installation to support CMake find_package --- CMakeLists.txt | 22 +++++++++++++++++++++- README.md | 9 +++++++++ cmake/cudawrappers-config.cmake | 5 +++++ cudawrappers-config.cmake | 1 - 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 cmake/cudawrappers-config.cmake delete mode 100644 cudawrappers-config.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b7b5403a..070516f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,8 @@ add_library(cudawrappers SHARED ${SOURCE_FILES}) target_include_directories( cudawrappers - PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" + PUBLIC $ + $ PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) target_link_libraries(cudawrappers PUBLIC CUDA::cuda_driver CUDA::nvrtc) @@ -33,3 +34,22 @@ include(cmake/linter-tools.cmake) # Tests add_subdirectory(tests) + +# Installation +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cu.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/include/nvrtc.hpp + DESTINATION "include/cudawrappers") + +install( + TARGETS cudawrappers + EXPORT cudawrappers-export + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) + +install( + EXPORT cudawrappers-export + FILE cudawrappers-targets.cmake + DESTINATION lib/cmake/cudawrappers) + +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cudawrappers-config.cmake + DESTINATION "lib/cmake/cudawrappers") diff --git a/README.md b/README.md index 36c1eb74..cb2ec276 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,15 @@ make -C build This command will create a `build` folder, compile the code and generate the library `libcudawrappers.so` in the build directory. For more details on the building requirements and on testing, check the [developer documentation](README.dev.md). +To install to `~/.local`, use +```shell +git clone https://github.com/nlesc-recruit/cudawrappers +cd cudawrappers +cmake -DCMAKE_INSTALL_PREFIX=$HOME/.local -S . -B build +make -C build +make -C build install +``` + ### Usage examples You can include the cudawrappers library in your own projects in various ways. We have created a few repositories with example setups to get you started: diff --git a/cmake/cudawrappers-config.cmake b/cmake/cudawrappers-config.cmake new file mode 100644 index 00000000..4f787d2e --- /dev/null +++ b/cmake/cudawrappers-config.cmake @@ -0,0 +1,5 @@ +include(CMakeFindDependencyMacro) + +find_package(CUDAToolkit REQUIRED) + +include("${CMAKE_CURRENT_LIST_DIR}/cudawrappers-targets.cmake") diff --git a/cudawrappers-config.cmake b/cudawrappers-config.cmake deleted file mode 100644 index 8b137891..00000000 --- a/cudawrappers-config.cmake +++ /dev/null @@ -1 +0,0 @@ - From 5f04444bb619b8b5f6a787e6a951112cf98e0862 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Mon, 11 Apr 2022 11:25:28 +0200 Subject: [PATCH 3/7] Fix version number in find_package --- CMakeLists.txt | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 070516f2..981fcd18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.17 FATAL_ERROR) +set(project cudawrappers) project( - cudawrappers + ${project} DESCRIPTION "C++ Wrappers for the CUDA Driver API" VERSION 0.3.0 HOMEPAGE_URL https://github.com/nlesc-recruit/cudawrappers @@ -23,7 +24,7 @@ add_library(cudawrappers SHARED ${SOURCE_FILES}) target_include_directories( cudawrappers - PUBLIC $ + PUBLIC $ $ PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) @@ -38,18 +39,25 @@ add_subdirectory(tests) # Installation install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cu.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/nvrtc.hpp - DESTINATION "include/cudawrappers") + DESTINATION include/${project}) install( TARGETS cudawrappers - EXPORT cudawrappers-export + EXPORT cudawrappers-targets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) install( - EXPORT cudawrappers-export - FILE cudawrappers-targets.cmake - DESTINATION lib/cmake/cudawrappers) - -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cudawrappers-config.cmake - DESTINATION "lib/cmake/cudawrappers") + EXPORT cudawrappers-targets + FILE ${project}-targets.cmake + DESTINATION lib/cmake/${project}) + +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/cmake/${project}-config-version.cmake + VERSION ${cudawrappers_VERSION} + COMPATIBILITY AnyNewerVersion) + +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${project}-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/${project}-config-version.cmake + DESTINATION lib/cmake/${project}) From f7455ad4e66c5477993e0a0ba8186aee0f5c57cd Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Tue, 12 Apr 2022 14:31:57 +0200 Subject: [PATCH 4/7] Add support for components in find_package --- CMakeLists.txt | 94 +++++++++++++++++------------- cmake/cudawrappers-config.cmake | 5 -- cmake/cudawrappers-config.cmake.in | 7 +++ 3 files changed, 61 insertions(+), 45 deletions(-) delete mode 100644 cmake/cudawrappers-config.cmake create mode 100644 cmake/cudawrappers-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 981fcd18..1f26f1a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,34 +1,69 @@ cmake_minimum_required(VERSION 3.17 FATAL_ERROR) -set(project cudawrappers) project( - ${project} + cudawrappers DESCRIPTION "C++ Wrappers for the CUDA Driver API" VERSION 0.3.0 HOMEPAGE_URL https://github.com/nlesc-recruit/cudawrappers LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) -set(CMAKE_BUILD_TYPE "Release") +set(CMAKE_BUILD_TYPE Release) +option(BUILD_SHARED_LIBS "Create shared libraries" True) find_package(CUDAToolkit REQUIRED) -# --- auto-ignore build directory -if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore) - file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*") -endif() +# Define components +set(COMPONENTS cu nvrtc) +set(LINK_cu CUDA::cuda_driver) +set(LINK_nvrtc CUDA::cuda_driver CUDA::nvrtc) + +include(GNUInstallDirs) +foreach(component ${COMPONENTS}) + # Create library + add_library(${component}) + # Add sources + target_sources(${component} PRIVATE src/${component}.cpp) + # Add headers + set_target_properties(${component} PROPERTIES + PUBLIC_HEADER include/${component}.hpp) + # Add the project name as a prefix to the output libraries + set_target_properties(${component} PROPERTIES PREFIX ${PROJECT_NAME}-) + # Add includes + target_include_directories( + ${component} + PUBLIC $ + $ + PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) + # Add links + target_link_libraries(${component} PUBLIC ${LINK_${component}}) + # Install libraries and headers + install( + TARGETS ${component} + EXPORT ${component}-config # export component cmake targets + COMPONENT ${component} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) + # Install component cmake targets + install( + EXPORT ${component}-config + NAMESPACE ${PROJECT_NAME}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) +endforeach() -set(SOURCE_FILES src/cu.cpp src/nvrtc.cpp) +# Install project cmake targets +include(CMakePackageConfigHelpers) -add_library(cudawrappers SHARED ${SOURCE_FILES}) +configure_file(cmake/${PROJECT_NAME}-config.cmake.in + ${PROJECT_NAME}-config.cmake @ONLY) -target_include_directories( - cudawrappers - PUBLIC $ - $ - PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) +write_basic_package_version_file( + ${PROJECT_NAME}-config-version.cmake + VERSION ${cudawrappers_VERSION} + COMPATIBILITY AnyNewerVersion) -target_link_libraries(cudawrappers PUBLIC CUDA::cuda_driver CUDA::nvrtc) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) # Including linters rules include(cmake/linter-tools.cmake) @@ -36,28 +71,7 @@ include(cmake/linter-tools.cmake) # Tests add_subdirectory(tests) -# Installation -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cu.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/include/nvrtc.hpp - DESTINATION include/${project}) - -install( - TARGETS cudawrappers - EXPORT cudawrappers-targets - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) - -install( - EXPORT cudawrappers-targets - FILE ${project}-targets.cmake - DESTINATION lib/cmake/${project}) - -include(CMakePackageConfigHelpers) -write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/cmake/${project}-config-version.cmake - VERSION ${cudawrappers_VERSION} - COMPATIBILITY AnyNewerVersion) - -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${project}-config.cmake - ${CMAKE_CURRENT_BINARY_DIR}/cmake/${project}-config-version.cmake - DESTINATION lib/cmake/${project}) +# --- auto-ignore build directory +if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore) + file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*") +endif() diff --git a/cmake/cudawrappers-config.cmake b/cmake/cudawrappers-config.cmake deleted file mode 100644 index 4f787d2e..00000000 --- a/cmake/cudawrappers-config.cmake +++ /dev/null @@ -1,5 +0,0 @@ -include(CMakeFindDependencyMacro) - -find_package(CUDAToolkit REQUIRED) - -include("${CMAKE_CURRENT_LIST_DIR}/cudawrappers-targets.cmake") diff --git a/cmake/cudawrappers-config.cmake.in b/cmake/cudawrappers-config.cmake.in new file mode 100644 index 00000000..c8b86f40 --- /dev/null +++ b/cmake/cudawrappers-config.cmake.in @@ -0,0 +1,7 @@ +include(CMakeFindDependencyMacro) + +find_package(CUDAToolkit REQUIRED) + +foreach(component ${@PROJECT_NAME@_FIND_COMPONENTS}) + include(${CMAKE_CURRENT_LIST_DIR}/${component}-config.cmake) +endforeach() From 8ccbc24037c2e2fcbb66ecb6e63ef0b739da2276 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Tue, 12 Apr 2022 16:43:08 +0200 Subject: [PATCH 5/7] Add a pretend namespace for when the project is not installed --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f26f1a6..1d9f1470 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,8 @@ include(GNUInstallDirs) foreach(component ${COMPONENTS}) # Create library add_library(${component}) + # Add a pretend namespace for use cases where the project in not installed + add_library(${PROJECT_NAME}::${component} ALIAS ${component}) # Add sources target_sources(${component} PRIVATE src/${component}.cpp) # Add headers From bac1f886026e531ed75cde2a7eaac9e910a47735 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Thu, 14 Apr 2022 14:53:20 +0200 Subject: [PATCH 6/7] Nicer formatting --- .cmake-format.py | 2 +- CMakeLists.txt | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.cmake-format.py b/.cmake-format.py index e20a31f2..e06cf295 100644 --- a/.cmake-format.py +++ b/.cmake-format.py @@ -65,7 +65,7 @@ # If a statement is wrapped to more than one line, than dangle the closing # parenthesis on its own line. - dangle_parens = False + dangle_parens = True # If the trailing parenthesis must be 'dangled' on its on line, then align it # to this reference: `prefix`: the start of the statement, `prefix-indent`: diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d9f1470..4a628e4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,8 @@ project( DESCRIPTION "C++ Wrappers for the CUDA Driver API" VERSION 0.3.0 HOMEPAGE_URL https://github.com/nlesc-recruit/cudawrappers - LANGUAGES CXX) + LANGUAGES CXX +) set(CMAKE_CXX_STANDARD 11) set(CMAKE_BUILD_TYPE Release) @@ -27,8 +28,9 @@ foreach(component ${COMPONENTS}) # Add sources target_sources(${component} PRIVATE src/${component}.cpp) # Add headers - set_target_properties(${component} PROPERTIES - PUBLIC_HEADER include/${component}.hpp) + set_target_properties( + ${component} PROPERTIES PUBLIC_HEADER include/${component}.hpp + ) # Add the project name as a prefix to the output libraries set_target_properties(${component} PROPERTIES PREFIX ${PROJECT_NAME}-) # Add includes @@ -36,7 +38,8 @@ foreach(component ${COMPONENTS}) ${component} PUBLIC $ $ - PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) + PRIVATE ${CUDAToolkit_INCLUDE_DIRS} + ) # Add links target_link_libraries(${component} PUBLIC ${LINK_${component}}) # Install libraries and headers @@ -44,28 +47,33 @@ foreach(component ${COMPONENTS}) TARGETS ${component} EXPORT ${component}-config # export component cmake targets COMPONENT ${component} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} + ) # Install component cmake targets install( EXPORT ${component}-config NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + ) endforeach() # Install project cmake targets include(CMakePackageConfigHelpers) -configure_file(cmake/${PROJECT_NAME}-config.cmake.in - ${PROJECT_NAME}-config.cmake @ONLY) +configure_file( + cmake/${PROJECT_NAME}-config.cmake.in ${PROJECT_NAME}-config.cmake @ONLY +) write_basic_package_version_file( ${PROJECT_NAME}-config-version.cmake VERSION ${cudawrappers_VERSION} - COMPATIBILITY AnyNewerVersion) + COMPATIBILITY AnyNewerVersion +) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} +) # Including linters rules include(cmake/linter-tools.cmake) From 86d60a48bd817d8047c2fd0e35511051d05e712f Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Fri, 15 Apr 2022 10:25:35 +0200 Subject: [PATCH 7/7] Require at least CUDAToolkit version 10.0 --- CMakeLists.txt | 3 ++- cmake/cudawrappers-config.cmake.in | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a628e4a..2070805b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,8 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_BUILD_TYPE Release) option(BUILD_SHARED_LIBS "Create shared libraries" True) -find_package(CUDAToolkit REQUIRED) +set(CUDA_MIN_VERSION 10.0) +find_package(CUDAToolkit ${CUDA_MIN_VERSION} REQUIRED) # Define components set(COMPONENTS cu nvrtc) diff --git a/cmake/cudawrappers-config.cmake.in b/cmake/cudawrappers-config.cmake.in index c8b86f40..f6701027 100644 --- a/cmake/cudawrappers-config.cmake.in +++ b/cmake/cudawrappers-config.cmake.in @@ -1,7 +1,7 @@ include(CMakeFindDependencyMacro) -find_package(CUDAToolkit REQUIRED) +find_package(CUDAToolkit @CUDA_MIN_VERSION@ REQUIRED) foreach(component ${@PROJECT_NAME@_FIND_COMPONENTS}) - include(${CMAKE_CURRENT_LIST_DIR}/${component}-config.cmake) + include(${CMAKE_CURRENT_LIST_DIR}/${component}-config.cmake) endforeach()