diff --git a/.cmake-format b/.cmake-format index 8c355cf..86664ab 100644 --- a/.cmake-format +++ b/.cmake-format @@ -54,4 +54,6 @@ parse: BINARY_DIR: 1 COMPATIBILITY: 1 VERSION_HEADER: 1 + EXPORT_HEADER: 1 + DISABLE_VERSION_SUFFIX: 1 DEPENDENCIES: + diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index 35d2f15..2b079f1 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -1,3 +1,4 @@ +--- name: Style on: @@ -12,17 +13,17 @@ jobs: style: runs-on: macos-latest - + steps: - - uses: actions/checkout@v1 - - - name: Install format dependencies - run: | - brew install clang-format - pip3 install cmake_format==0.6.11 pyyaml + - uses: actions/checkout@v1 + + - name: Install format dependencies + run: | + brew install clang-format + pip3 install cmake_format==0.6.13 pyyaml - - name: configure - run: cmake -Stest/style -Bbuild/style + - name: configure + run: cmake -S test/style -B build/style - - name: check style - run: cmake --build build/style --target check-format + - name: check style + run: cmake --build build/style --target check-format diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 599b8db..8801446 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,3 +1,4 @@ +--- name: Test on: @@ -27,12 +28,12 @@ jobs: - name: test and install local build run: | cmake -S test -B build/local - cmake --build build/local - ./build/local/test + cmake --build build/local + cmake --build build/local --target test sudo cmake --build build/local --target install - name: test installed build run: | cmake -S test -B build/installed -D TEST_INSTALLED_VERSION=1 - cmake --build build/installed - ./build/installed/test + cmake --build build/installed + cmake --build build/installed --target test diff --git a/CMakeLists.txt b/CMakeLists.txt index e348e4e..1c41657 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ function(packageProject) cmake_parse_arguments( PROJECT "" - "NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT" + "NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;EXPORT_HEADER;VERSION_HEADER;NAMESPACE;DISABLE_VERSION_SUFFIX;ARCH_INDEPENDENT" "DEPENDENCIES" ${ARGN} ) @@ -26,7 +26,6 @@ function(packageProject) set(PROJECT_VERSION_SUFFIX -${PROJECT_VERSION}) endif() - # handle default arguments: if(NOT DEFINED PROJECT_COMPATIBILITY) set(PROJECT_COMPATIBILITY AnyNewerVersion) endif() @@ -39,14 +38,23 @@ function(packageProject) add_library(${PROJECT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME}) endif() - if(DEFINED PROJECT_VERSION_HEADER) + if(DEFINED PROJECT_VERSION_HEADER OR DEFINED PROJECT_EXPORT_HEADER) set(PROJECT_VERSION_INCLUDE_DIR ${PROJECT_BINARY_DIR}/PackageProjectInclude) - string(TOUPPER ${PROJECT_NAME} UPPERCASE_PROJECT_NAME) - configure_file( - ${PACKAGE_PROJECT_ROOT_PATH}/version.h.in - ${PROJECT_VERSION_INCLUDE_DIR}/${PROJECT_VERSION_HEADER} @ONLY - ) + if(DEFINED PROJECT_EXPORT_HEADER) + include(GenerateExportHeader) + generate_export_header( + ${PROJECT_NAME} EXPORT_FILE_NAME ${PROJECT_VERSION_INCLUDE_DIR}/${PROJECT_EXPORT_HEADER} + ) + endif() + + if(DEFINED PROJECT_VERSION_HEADER) + string(TOUPPER ${PROJECT_NAME} UPPERCASE_PROJECT_NAME) + configure_file( + ${PACKAGE_PROJECT_ROOT_PATH}/version.h.in + ${PROJECT_VERSION_INCLUDE_DIR}/${PROJECT_VERSION_HEADER} @ONLY + ) + endif() get_target_property(target_type ${PROJECT_NAME} TYPE) if(target_type STREQUAL "INTERFACE_LIBRARY") diff --git a/README.md b/README.md index 9bb3de8..1b6540d 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ packageProject( # (optional) create a header containing the version info # Note: that the path to headers should be lowercase VERSION_HEADER "${PROJECT_NAME}/version.h" + # (optional) create a export header using GenerateExportHeader module + EXPORT_HEADER "${PROJECT_NAME}/export.h" # (optional) install your library with a namespace (Note: do NOT add extra '::') NAMESPACE ${PROJECT_NAMESPACE} # (optional) define the project's version compatibility, defaults to `AnyNewerVersion` diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a59ce21..8c0201d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.14) option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF) @@ -18,8 +18,11 @@ else() add_subdirectory(transitive_dependency) endif() -add_executable(test main.cpp) +add_executable(main main.cpp) target_link_libraries( - test dependency ns::namespaced_dependency transitive_dependency::transitive_dependency + main dependency ns::namespaced_dependency transitive_dependency::transitive_dependency ) + +enable_testing() +add_test(NAME test COMMAND main) diff --git a/test/dependency/CMakeLists.txt b/test/dependency/CMakeLists.txt index a9f0fa7..ad23315 100644 --- a/test/dependency/CMakeLists.txt +++ b/test/dependency/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.14) project( dependency @@ -6,14 +6,14 @@ project( LANGUAGES CXX ) -add_library(dependency source/dependency.cpp) +add_library(dependency STATIC source/dependency.cpp) target_include_directories( dependency PUBLIC $ $ ) -add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/PackageProject) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject) packageProject( NAME ${PROJECT_NAME} diff --git a/test/namespaced_dependency/CMakeLists.txt b/test/namespaced_dependency/CMakeLists.txt index 6eeb212..6c154b3 100644 --- a/test/namespaced_dependency/CMakeLists.txt +++ b/test/namespaced_dependency/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.14) project( namespaced_dependency @@ -7,7 +7,7 @@ project( ) set(PROJECT_NAMESPACE "ns") -add_library(${PROJECT_NAME} source/namespaced_dependency.cpp) +add_library(${PROJECT_NAME} STATIC source/namespaced_dependency.cpp) # the alias ${PROJECT_NAMESPACE}::${PROJECT_NAME} is automatically provided by PackageProject.cmake # if we use the `NAMESPACE` parameter @@ -16,7 +16,7 @@ target_include_directories( $ ) -add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/PackageProject) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject) packageProject( NAME ${PROJECT_NAME} diff --git a/test/style/CMakeLists.txt b/test/style/CMakeLists.txt index 575b86e..650acd0 100644 --- a/test/style/CMakeLists.txt +++ b/test/style/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.14) project(StyleCheck) diff --git a/test/transitive_dependency/CMakeLists.txt b/test/transitive_dependency/CMakeLists.txt index f0d1a20..d2f9a0a 100644 --- a/test/transitive_dependency/CMakeLists.txt +++ b/test/transitive_dependency/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.14) project( transitive_dependency @@ -22,16 +22,20 @@ CPMAddPackage( OPTIONS "CXXOPTS_BUILD_EXAMPLES Off" "CXXOPTS_BUILD_TESTS Off" ) -add_library(${PROJECT_NAME} source/transitive_dependency.cpp) +# Set default visibility to hidden for all targets +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN YES) + +add_library(${PROJECT_NAME} SHARED source/transitive_dependency.cpp) target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) -target_link_libraries(${PROJECT_NAME} fmt cxxopts) +target_link_libraries(${PROJECT_NAME} PUBLIC fmt::fmt-header-only cxxopts) target_include_directories( transitive_dependency PUBLIC $ $ ) -add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/PackageProject) +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../.. PackageProject) packageProject( NAME ${PROJECT_NAME} @@ -41,5 +45,6 @@ packageProject( INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} VERSION_HEADER "transitive_dependency/version.h" + EXPORT_HEADER "transitive_dependency/export.h" DEPENDENCIES "fmt 7.1.3;cxxopts 2.2.0" ) diff --git a/test/transitive_dependency/cmake/CPM.cmake b/test/transitive_dependency/cmake/CPM.cmake index 467d0e0..5df65a1 100644 --- a/test/transitive_dependency/cmake/CPM.cmake +++ b/test/transitive_dependency/cmake/CPM.cmake @@ -1,4 +1,4 @@ -set(CPM_DOWNLOAD_VERSION 0.28.3) +set(CPM_DOWNLOAD_VERSION 0.30.0) if(CPM_SOURCE_CACHE) # Expand relative path. This is important if the provided path contains a tilde (~) diff --git a/test/transitive_dependency/source/transitive_dependency.cpp b/test/transitive_dependency/source/transitive_dependency.cpp index 9f6ab5d..1e782fe 100644 --- a/test/transitive_dependency/source/transitive_dependency.cpp +++ b/test/transitive_dependency/source/transitive_dependency.cpp @@ -1,6 +1,7 @@ #include +#include #include -void transitiveDependencyFunction() { +void TRANSITIVE_DEPENDENCY_EXPORT transitiveDependencyFunction() { fmt::print("Using transitive_dependency version {}\n", TRANSITIVE_DEPENDENCY_VERSION); }