Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to install the library for use in other CMake projects #8

Merged
merged 9 commits into from
Sep 30, 2023
119 changes: 96 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@ if(POLICY CMP0074)
endif()

# Project settings
project(tests LANGUAGES CXX)
project(upa_url VERSION 0.0.1 LANGUAGES CXX)
# The ${upa_lib_name} is used to create the config file name:
# ${upa_lib_name}-config.cmake
# It also must be used as the package name argument to find_package
set(upa_lib_name upa)
# Exported name for library target files; also used to create an alias
# target: upa::${upa_lib_export}
set(upa_lib_export url)
# The ${upa_lib_target} is the logical library target name; also used
# to create the library filename
set(upa_lib_target upa_url)

set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard")

# Upa URL is top level project?
if (NOT DEFINED URL_MAIN_PROJECT)
set(URL_MAIN_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(URL_MAIN_PROJECT ON)
endif()
endif()

# Options
option(URL_BUILD_TESTS "Build the URL tests." ON)
option(URL_BUILD_TESTS "Build the URL tests." ${URL_MAIN_PROJECT})
option(URL_BUILD_FUZZER "Build the URL fuzzer." OFF)
option(URL_BUILD_EXAMPLES "Build the URL examples." OFF)
option(URL_BUILD_TOOLS "Build tools." OFF)
option(URL_INSTALL "Generate the install target." ON)
# library options
option(URL_AMALGAMATED "Use amalgamated URL library source." OFF)
# tests build options
Expand Down Expand Up @@ -76,37 +96,35 @@ if (URL_TEST_VALGRIND)
separate_arguments(MEMORYCHECK_COMMAND)
endif()

# are URL and ICU libraries needed
if (URL_BUILD_TESTS OR URL_BUILD_FUZZER OR URL_BUILD_EXAMPLES)
set(URL_USE_LIBS ON)
endif()

if (URL_AMALGAMATED)
include_directories(
deps
single_include)
else()
include_directories(
deps
include)
endif()
include_directories(deps)

if (URL_USE_LIBS)
# 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)
# This library depends on ICU
find_package(ICU REQUIRED COMPONENTS i18n uc)

if (URL_AMALGAMATED)
add_library(cppurl STATIC
add_library(${upa_lib_target} STATIC
single_include/upa/url.cpp)
target_include_directories(${upa_lib_target}
INTERFACE single_include)
else()
add_library(cppurl STATIC
add_library(${upa_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_lib_target} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
endif()
target_include_directories(cppurl PRIVATE ${ICU_INCLUDE_DIR})
add_library(upa::${upa_lib_export} ALIAS ${upa_lib_target})
set_target_properties(${upa_lib_target} PROPERTIES
EXPORT_NAME ${upa_lib_export})
target_include_directories(${upa_lib_target} PRIVATE ${ICU_INCLUDE_DIR})
target_link_libraries(${upa_lib_target} INTERFACE ICU::i18n ICU::uc)
endif()

# Test targets
Expand Down Expand Up @@ -142,7 +160,7 @@ if (URL_BUILD_TESTS)
get_filename_component(test_name ${file} NAME_WE)

add_executable(${test_name} ${file})
target_link_libraries(${test_name} cppurl ICU::i18n ICU::uc)
target_link_libraries(${test_name} ${upa_lib_target})

add_test(NAME ${test_name}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
Expand Down Expand Up @@ -170,20 +188,75 @@ if (URL_BUILD_FUZZER)
else()
add_executable(${fuzz_name} ${file})
endif()
target_link_libraries(${fuzz_name} cppurl ICU::i18n ICU::uc)
target_link_libraries(${fuzz_name} ${upa_lib_target})
endforeach()
endif()

# Example's targets

if (URL_BUILD_EXAMPLES)
add_executable(urlparse examples/urlparse.cpp)
target_link_libraries(urlparse cppurl ICU::i18n ICU::uc)
target_link_libraries(urlparse ${upa_lib_target})
endif()

# Tool's targets

if (URL_BUILD_TOOLS)
add_executable(dumpCharBitSets tools/dumpCharBitSets.cpp)
set_property(TARGET dumpCharBitSets PROPERTY CXX_STANDARD 17)
target_include_directories(dumpCharBitSets PRIVATE include)
endif()

# Install

if (URL_INSTALL AND NOT URL_AMALGAMATED)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

install(
DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(
TARGETS ${upa_lib_target}
EXPORT ${upa_lib_target}-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

install(
EXPORT ${upa_lib_target}-targets
FILE ${upa_lib_name}-targets.cmake
NAMESPACE upa::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${upa_lib_name}
)

# generate the config file that includes the exports
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${upa_lib_name}-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${upa_lib_name}-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${upa_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_lib_name}-config-version.cmake
COMPATIBILITY SameMinorVersion
)

# install the generated configuration files
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${upa_lib_name}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${upa_lib_name}-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${upa_lib_name}
)

# generate the export targets for the build tree
# needs to be after the install(TARGETS) command
export(
EXPORT ${upa_lib_target}-targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/${upa_lib_name}-targets.cmake
NAMESPACE upa::
)
endif()
6 changes: 6 additions & 0 deletions cmake/upa-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(ICU REQUIRED COMPONENTS i18n uc)

include("${CMAKE_CURRENT_LIST_DIR}/upa-targets.cmake")