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 fb27a355..2070805b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,26 +5,84 @@ 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") +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) -# --- 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 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 + 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) -include_directories(./include/) -include_directories(${CUDAToolkit_INCLUDE_DIRS}) +configure_file( + cmake/${PROJECT_NAME}-config.cmake.in ${PROJECT_NAME}-config.cmake @ONLY +) -add_library(cudawrappers SHARED ${SOURCE_FILES}) +write_basic_package_version_file( + ${PROJECT_NAME}-config-version.cmake + VERSION ${cudawrappers_VERSION} + 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} +) # Including linters rules include(cmake/linter-tools.cmake) # Tests add_subdirectory(tests) + +# --- auto-ignore build directory +if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore) + file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*") +endif() 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.in b/cmake/cudawrappers-config.cmake.in new file mode 100644 index 00000000..f6701027 --- /dev/null +++ b/cmake/cudawrappers-config.cmake.in @@ -0,0 +1,7 @@ +include(CMakeFindDependencyMacro) + +find_package(CUDAToolkit @CUDA_MIN_VERSION@ REQUIRED) + +foreach(component ${@PROJECT_NAME@_FIND_COMPONENTS}) + include(${CMAKE_CURRENT_LIST_DIR}/${component}-config.cmake) +endforeach() 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 @@ -