From d4e9edbbc84358957987ee47683e28a76ce2ae8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Thu, 28 Sep 2023 21:02:40 +0300 Subject: [PATCH] Rename CMake package name to upa, and add `NAMESPACE upa::` Now, to find the library from a CMake project, use: `find_package(upa)` To link to the library, use: `target_link_libraries(exe-target upa::url)` --- CMakeLists.txt | 68 ++++++++++++++++++++--------------- cmake/upa-config.cmake.in | 6 ++++ cmake/upa_url-config.cmake.in | 3 -- 3 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 cmake/upa-config.cmake.in delete mode 100644 cmake/upa_url-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cab262..8d0a73e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,16 @@ endif() # Project settings project(upa_url VERSION 0.0.1 LANGUAGES CXX) +# The ${lib_name} is used to create the config file name: ${lib_name}-config.cmake +# It also must be used as the package name argument to find_package +set(lib_name upa) +# Exported name for target files; also used to create an alias +# target: upa::${lib_export} +set(lib_export url) +# The ${lib_target} is the logical library target name; also used +# to create the library filename +set(lib_target upa_url) + set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard") # Upa URL is top level project? @@ -89,31 +99,31 @@ include_directories(deps) # Are Upa URL and ICU libraries needed? if (URL_BUILD_TESTS OR URL_BUILD_FUZZER OR URL_BUILD_EXAMPLES OR URL_INSTALL OR NOT URL_BUILD_TOOLS) - # Upa URL library name - set(upa_url_lib upa_url) - + # This library depends on ICU find_package(ICU REQUIRED COMPONENTS i18n uc) if (URL_AMALGAMATED) - add_library(${upa_url_lib} STATIC + add_library(${lib_target} STATIC single_include/upa/url.cpp) - target_include_directories(${upa_url_lib} + target_include_directories(${lib_target} INTERFACE single_include) else() - add_library(${upa_url_lib} STATIC + add_library(${lib_target} STATIC src/url.cpp src/url_idna.cpp src/url_ip.cpp src/url_percent_encode.cpp src/url_search_params.cpp src/url_utf.cpp) - target_include_directories(${upa_url_lib} PUBLIC + target_include_directories(${lib_target} PUBLIC $ $) endif() - add_library(upa::url ALIAS ${upa_url_lib}) - target_include_directories(${upa_url_lib} PRIVATE ${ICU_INCLUDE_DIR}) - target_link_libraries(${upa_url_lib} INTERFACE ICU::i18n ICU::uc) + add_library(upa::${lib_export} ALIAS ${lib_target}) + set_target_properties(${lib_target} PROPERTIES + EXPORT_NAME ${lib_export}) + target_include_directories(${lib_target} PRIVATE ${ICU_INCLUDE_DIR}) + target_link_libraries(${lib_target} INTERFACE ICU::i18n ICU::uc) endif() # Test targets @@ -149,7 +159,7 @@ if (URL_BUILD_TESTS) get_filename_component(test_name ${file} NAME_WE) add_executable(${test_name} ${file}) - target_link_libraries(${test_name} ${upa_url_lib}) + target_link_libraries(${test_name} ${lib_target}) add_test(NAME ${test_name} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test @@ -177,7 +187,7 @@ if (URL_BUILD_FUZZER) else() add_executable(${fuzz_name} ${file}) endif() - target_link_libraries(${fuzz_name} ${upa_url_lib}) + target_link_libraries(${fuzz_name} ${lib_target}) endforeach() endif() @@ -185,7 +195,7 @@ endif() if (URL_BUILD_EXAMPLES) add_executable(urlparse examples/urlparse.cpp) - target_link_libraries(urlparse ${upa_url_lib}) + target_link_libraries(urlparse ${lib_target}) endif() # Tool's targets @@ -203,47 +213,49 @@ if (URL_INSTALL AND NOT URL_AMALGAMATED) include(CMakePackageConfigHelpers) install( - DIRECTORY include/upa + DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) install( - TARGETS ${upa_url_lib} - EXPORT ${upa_url_lib}-targets + TARGETS ${lib_target} + EXPORT ${lib_target}-targets DESTINATION ${CMAKE_INSTALL_LIBDIR} ) install( - EXPORT ${upa_url_lib}-targets - FILE ${upa_url_lib}-targets.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${upa_url_lib} + EXPORT ${lib_target}-targets + FILE ${lib_name}-targets.cmake + NAMESPACE upa:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${lib_name} ) # generate the config file that includes the exports configure_package_config_file( - ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${upa_url_lib}-config.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/${upa_url_lib}-config.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${upa_url_lib} + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${lib_name}-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${lib_name}-config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${lib_name} NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO ) # generate the version file for the config file write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/${upa_url_lib}-config-version.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${lib_name}-config-version.cmake COMPATIBILITY SameMinorVersion ) # install the generated configuration files install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/${upa_url_lib}-config.cmake - ${CMAKE_CURRENT_BINARY_DIR}/${upa_url_lib}-config-version.cmake - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${upa_url_lib}" + ${CMAKE_CURRENT_BINARY_DIR}/${lib_name}-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${lib_name}-config-version.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${lib_name} ) # generate the export targets for the build tree # needs to be after the install(TARGETS) command export( - EXPORT ${upa_url_lib}-targets - FILE ${CMAKE_CURRENT_BINARY_DIR}/${upa_url_lib}-targets.cmake + EXPORT ${lib_target}-targets + FILE ${CMAKE_CURRENT_BINARY_DIR}/${lib_name}-targets.cmake + NAMESPACE upa:: ) endif() diff --git a/cmake/upa-config.cmake.in b/cmake/upa-config.cmake.in new file mode 100644 index 0000000..ad8d69b --- /dev/null +++ b/cmake/upa-config.cmake.in @@ -0,0 +1,6 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) +find_dependency(ICU REQUIRED COMPONENTS i18n uc) + +include("${CMAKE_CURRENT_LIST_DIR}/upa-targets.cmake") diff --git a/cmake/upa_url-config.cmake.in b/cmake/upa_url-config.cmake.in deleted file mode 100644 index d1348a4..0000000 --- a/cmake/upa_url-config.cmake.in +++ /dev/null @@ -1,3 +0,0 @@ -@PACKAGE_INIT@ - -include("${CMAKE_CURRENT_LIST_DIR}/upa_url-targets.cmake")