File tree Expand file tree Collapse file tree
include/namespaced_dependency Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010
1111env :
1212 CTEST_OUTPUT_ON_FAILURE : 1
13- CODECOV_TOKEN : ${{ secrets.CODECOV_TOKEN }}
1413
1514jobs :
1615 build :
1716
1817 runs-on : ubuntu-latest
1918
2019 steps :
21- - uses : actions/checkout@v1
20+ - uses : actions/checkout@v2
2221
2322 - name : test local build
2423 run : |
25- cmake -Htest -Bbuild /local
24+ cmake -S test -B build /local
2625 cmake --build build/local
2726 ./build/local/test
2827
29- - name : install dependency
28+ - name : install dependencies
3029 run : |
31- cmake -Htest /dependency -Bbuild /dependency
30+ cmake -S test /dependency -B build /dependency
3231 sudo cmake --build build/dependency --target install
32+ cmake -S test/namespaced_dependency -B build/namespaced_dependency
33+ sudo cmake --build build/namespaced_dependency --target install
3334
3435 - name : test installed build
3536 run : |
36- cmake -Htest -Bbuild /installed -DTEST_INSTALLED_VERSION =1
37+ cmake -S test -B build /installed -D TEST_INSTALLED_VERSION =1
3738 cmake --build build/installed
3839 ./build/installed/test
Original file line number Diff line number Diff line change 1- /build
1+ /build
2+ /out
3+ /.vs
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ function(packageProject)
1313 cmake_parse_arguments (
1414 PROJECT
1515 ""
16- "NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;VERSION_HEADER"
16+ "NAME;VERSION;INCLUDE_DIR;INCLUDE_DESTINATION;BINARY_DIR;COMPATIBILITY;VERSION_HEADER;NAMESPACE "
1717 "DEPENDENCIES"
1818 ${ARGN}
1919 )
@@ -23,6 +23,13 @@ function(packageProject)
2323 set (PROJECT_COMPATIBILITY AnyNewerVersion)
2424 endif ()
2525
26+ # we wanto to automatically add :: to our namespace, so only append if a namespace was given in the first place
27+ # we also provide an alias to ensure that local and installed versions have the same name
28+ if (DEFINED PROJECT_NAMESPACE)
29+ set (PROJECT_NAMESPACE ${PROJECT_NAMESPACE} ::)
30+ add_library (${PROJECT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME} )
31+ endif ()
32+
2633 if (DEFINED PROJECT_VERSION_HEADER)
2734 set (PROJECT_VERSION_INCLUDE_DIR ${PROJECT_BINARY_DIR} /PackageProjectInclude)
2835 string (TOUPPER ${PROJECT_NAME} UPPERCASE_PROJECT_NAME)
@@ -69,6 +76,7 @@ function(packageProject)
6976 install (
7077 EXPORT ${PROJECT_NAME} Targets
7178 DESTINATION lib/cmake/${PROJECT_NAME} -${PROJECT_VERSION}
79+ NAMESPACE ${PROJECT_NAMESPACE}
7280 )
7381
7482 install (
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ packageProject(
3535 # (optional) create a header containing the version info
3636 # note that the path should be lowercase
3737 VERSION_HEADER "${PROJECT_NAME}/version.h"
38+ # (optional) install your library with a namespace (do NOT add extra '::')
39+ NAMESPACE ${PROJECT_NAMESPACE}
3840)
3941```
4042
Original file line number Diff line number Diff line change @@ -9,9 +9,11 @@ project(PackageProjectTest
99
1010if (TEST_INSTALLED_VERSION)
1111 find_package (dependency 1.2.3 REQUIRED )
12+ find_package (namespaced_dependency 4.5.6 REQUIRED )
1213else ()
1314 add_subdirectory (dependency )
15+ add_subdirectory (namespaced_dependency )
1416endif ()
1517
1618add_executable (test main.cpp )
17- target_link_libraries (test dependency )
19+ target_link_libraries (test dependency ns::namespaced_dependency )
Original file line number Diff line number Diff line change 11#include < dependency/dependency.h>
22#include < dependency/version.h>
3+ #include < namespaced_dependency/namespaced_dependency.h>
4+ #include < namespaced_dependency/version.h>
35#include < string>
46
57int main () {
68 dependencyFunction ();
7- return DEPENDENCY_VERSION == std::string (" 1.2.3" ) ? 0 : 1 ;
9+ ns::namespacedDependencyFunction ();
10+ return DEPENDENCY_VERSION == std::string (" 1.2.3" ) && NAMESPACED_DEPENDENCY_VERSION == std::string (" 4.5.6" ) ? 0 : 1 ;
811}
Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.14 FATAL_ERROR )
2+
3+ project (namespaced_dependency
4+ VERSION 4.5.6
5+ LANGUAGES CXX
6+ )
7+
8+ set (PROJECT_NAMESPACE "ns" )
9+ add_library (${PROJECT_NAME} source /namespaced_dependency.cpp )
10+ # the alias ${PROJECT_NAMESPACE}::${PROJECT_NAME} is automatically provided by PackageProject.cmake
11+ # if we use the `NAMESPACE` parameter
12+
13+ target_include_directories (namespaced_dependency
14+ PUBLIC
15+ $<BUILD_INTERFACE :${PROJECT_SOURCE_DIR} /include >
16+ $<INSTALL_INTERFACE :include /${PROJECT_NAME} -${PROJECT_VERSION} >
17+ )
18+
19+ add_subdirectory (${CMAKE_CURRENT_LIST_DIR} /../.. ${CMAKE_CURRENT_BINARY_DIR} /PackageProject )
20+
21+ packageProject (
22+ NAME ${PROJECT_NAME}
23+ VERSION ${PROJECT_VERSION}
24+ NAMESPACE ${PROJECT_NAMESPACE}
25+ BINARY_DIR ${PROJECT_BINARY_DIR}
26+ INCLUDE_DIR ${PROJECT_SOURCE_DIR} /include
27+ INCLUDE_DESTINATION include /${PROJECT_NAME}-${PROJECT_VERSION}
28+ VERSION_HEADER "namespaced_dependency/version.h"
29+ DEPENDENCIES ""
30+ )
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < iostream>
4+
5+ namespace ns {
6+
7+ void namespacedDependencyFunction ();
8+
9+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < namespaced_dependency/version.h>
3+
4+ namespace ns {
5+ void namespacedDependencyFunction () {
6+ std::cout << " Using namespaced_dependency version " << NAMESPACED_DEPENDENCY_VERSION << std::endl;
7+ }
8+ }
You can’t perform that action at this time.
0 commit comments