Skip to content

Commit 2f91a1d

Browse files
committed
Introduce CMake find modules for dependencies
While this approach maintains pkg-config as the preferred mechanism for discovering dependencies, it allows us to support the older config mechanism shipped with many of these dependencies on older distributions like CentOS 7. An additional part of this change is the switch to CMake targets for dependencies, which significantly reduces boilerplate in the main CMakeLists.txt.
1 parent 992fa5f commit 2f91a1d

File tree

8 files changed

+276
-43
lines changed

8 files changed

+276
-43
lines changed

CMakeLists.txt

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,24 @@ endif()
1414

1515
add_compile_options(-Wall -Wextra -Wconversion -Wpedantic)
1616

17-
include(FindPkgConfig)
18-
pkg_check_modules(CREATEREPO_C REQUIRED createrepo_c)
19-
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
20-
pkg_check_modules(GPG_ERROR REQUIRED gpg-error)
21-
pkg_check_modules(GPGME REQUIRED gpgme)
22-
pkg_check_modules(LIBASSUAN REQUIRED libassuan)
17+
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
18+
find_package(createrepo_c 0.13.0 MODULE REQUIRED)
19+
find_package(glib-2.0 MODULE REQUIRED)
20+
find_package(gpg-error 1.13 MODULE REQUIRED)
21+
find_package(gpgme 1.7.0 MODULE REQUIRED)
22+
find_package(assuan 2.2.0 MODULE REQUIRED)
2323

2424
# Repository operations
2525
add_library(createrepo-cache STATIC
2626
src/createrepo-cache/coordinator.c
2727
src/createrepo-cache/repo_cache.c)
2828
target_include_directories(createrepo-cache PUBLIC
29-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
30-
$<INSTALL_INTERFACE:include>)
31-
target_include_directories(createrepo-cache SYSTEM PUBLIC
32-
${CREATEREPO_C_INCLUDE_DIRS}
33-
${GLIB2_INCLUDE_DIRS}
34-
${GPG_ERROR_INCLUDE_DIRS}
35-
${GPGME_INCLUDE_DIRS})
36-
target_link_libraries(createrepo-cache PRIVATE
37-
${CREATEREPO_C_LIBRARIES}
38-
${GLIB2_LIBRARIES}
39-
${GPG_ERROR_LIBRARIES}
40-
${GPGME_LIBRARIES})
29+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>)
30+
target_link_libraries(createrepo-cache PUBLIC
31+
createrepo_c
32+
glib-2.0
33+
gpg-error
34+
gpgme)
4135
target_compile_definitions(createrepo-cache PRIVATE
4236
-DG_LOG_DOMAIN="CREATEREPO_CACHE")
4337
set_target_properties(createrepo-cache PROPERTIES
@@ -57,38 +51,26 @@ add_library(createrepo-agent-lib OBJECT
5751
target_include_directories(createrepo-agent-lib PUBLIC
5852
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
5953
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/gen>)
60-
target_include_directories(createrepo-agent-lib SYSTEM PRIVATE
61-
${CREATEREPO_C_INCLUDE_DIRS})
62-
target_include_directories(createrepo-agent-lib SYSTEM PUBLIC
63-
${GLIB2_INCLUDE_DIRS}
64-
${GPG_ERROR_INCLUDE_DIRS}
65-
${LIBASSUAN_INCLUDE_DIRS})
6654
target_link_libraries(createrepo-agent-lib PRIVATE
6755
createrepo-cache
68-
${CREATEREPO_C_LIBRARIES})
56+
createrepo_c)
6957
target_link_libraries(createrepo-agent-lib PUBLIC
70-
${GLIB2_LIBRARIES}
71-
${GPG_ERROR_LIBRARIES}
72-
${LIBASSUAN_LIBRARIES})
58+
assuan
59+
gpg-error
60+
glib-2.0)
7361
target_compile_definitions(createrepo-agent-lib PRIVATE
7462
-DG_LOG_DOMAIN="CREATEREPO_AGENT")
7563

7664
# Executable
7765
add_executable(${PROJECT_NAME}
7866
src/${PROJECT_NAME}/agent.c)
79-
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE
80-
${CREATEREPO_C_INCLUDE_DIRS}
81-
${GLIB2_INCLUDE_DIRS}
82-
${GPG_ERROR_INCLUDE_DIRS}
83-
${GPGME_INCLUDE_DIRS}
84-
${LIBASSUAN_INCLUDE_DIRS})
8567
target_link_libraries(${PROJECT_NAME} PRIVATE
68+
assuan
8669
createrepo-agent-lib
87-
${CREATEREPO_C_LIBRARIES}
88-
${GLIB2_LIBRARIES}
89-
${GPG_ERROR_LIBRARIES}
90-
${GPGME_LIBRARIES}
91-
${LIBASSUAN_LIBRARIES})
70+
createrepo_c
71+
glib-2.0
72+
gpg-error
73+
gpgme)
9274
install(TARGETS ${PROJECT_NAME}
9375
RUNTIME DESTINATION bin
9476
COMPONENT bin)

cmake/Findassuan.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
include(FindPackageHandleStandardArgs)
4+
5+
find_package(PkgConfig QUIET)
6+
if(PKG_CONFIG_FOUND)
7+
pkg_check_modules(assuan QUIET IMPORTED_TARGET libassuan)
8+
list(GET assuan_LINK_LIBRARIES 0 assuan_LIBRARY)
9+
endif()
10+
11+
if(NOT assuan_FOUND)
12+
find_program(LIBASSUAN_CONFIG NAMES libassuan-config)
13+
if(LIBASSUAN_CONFIG)
14+
include(use_config_util)
15+
16+
extract_libs(assuan_LINK_LIBRARIES
17+
${LIBASSUAN_CONFIG} --libs)
18+
list(GET assuan_LINK_LIBRARIES 0 assuan_LIBRARY)
19+
20+
extract_includes(assuan_INCLUDE_DIRS
21+
${LIBASSUAN_CONFIG} --cflags)
22+
23+
execute_process(
24+
COMMAND ${LIBASSUAN_CONFIG} --version
25+
OUTPUT_VARIABLE assuan_VERSION
26+
OUTPUT_STRIP_TRAILING_WHITESPACE)
27+
endif()
28+
endif()
29+
30+
find_package_handle_standard_args(assuan
31+
REQUIRED_VARS
32+
assuan_LIBRARY
33+
assuan_VERSION
34+
VERSION_VAR assuan_VERSION)
35+
36+
if(TARGET PkgConfig::assuan)
37+
add_library(assuan ALIAS PkgConfig::assuan)
38+
else()
39+
add_library(assuan UNKNOWN IMPORTED)
40+
set_target_properties(assuan PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${assuan_INCLUDE_DIRS}")
41+
set(assuan_OTHER_LIBRARIES ${assuan_LINK_LIBRARIES})
42+
list(POP_FRONT assuan_OTHER_LIBRARIES)
43+
set_target_properties(assuan PROPERTIES INTERFACE_LINK_LIBRARIES "${assuan_OTHER_LIBRARIES}")
44+
unset(assuan_OTHER_LIBRARIES)
45+
set_property(TARGET assuan APPEND PROPERTY IMPORTED_LOCATION "${assuan_LIBRARY}")
46+
endif()
47+
48+
mark_as_advanced(assuan_LIBRARY)

cmake/Findcreaterepo_c.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
include(FindPackageHandleStandardArgs)
4+
5+
find_package(PkgConfig QUIET)
6+
if(PKG_CONFIG_FOUND)
7+
pkg_check_modules(createrepo_c QUIET REQUIRED IMPORTED_TARGET createrepo_c)
8+
list(GET createrepo_c_LINK_LIBRARIES 0 createrepo_c_LIBRARY)
9+
mark_as_advanced(createrepo_c_LIBRARY)
10+
endif()
11+
12+
find_package_handle_standard_args(createrepo_c
13+
REQUIRED_VARS
14+
createrepo_c_LIBRARY
15+
createrepo_c_VERSION
16+
VERSION_VAR createrepo_c_VERSION)
17+
18+
add_library(createrepo_c ALIAS PkgConfig::createrepo_c)

cmake/Findglib-2.0.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
include(FindPackageHandleStandardArgs)
4+
5+
find_package(PkgConfig QUIET)
6+
if(PKG_CONFIG_FOUND)
7+
pkg_check_modules(glib-2.0 QUIET REQUIRED IMPORTED_TARGET glib-2.0)
8+
list(GET glib-2.0_LINK_LIBRARIES 0 glib-2.0_LIBRARY)
9+
mark_as_advanced(glib-2.0_LIBRARY)
10+
endif()
11+
12+
find_package_handle_standard_args(glib-2.0
13+
REQUIRED_VARS
14+
glib-2.0_LIBRARY
15+
glib-2.0_VERSION
16+
VERSION_VAR glib-2.0_VERSION)
17+
18+
add_library(glib-2.0 ALIAS PkgConfig::glib-2.0)

cmake/Findgpg-error.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
include(FindPackageHandleStandardArgs)
4+
5+
find_package(PkgConfig QUIET)
6+
if(PKG_CONFIG_FOUND)
7+
pkg_check_modules(gpg-error QUIET IMPORTED_TARGET gpg-error)
8+
list(GET gpg-error_LINK_LIBRARIES 0 gpg-error_LIBRARY)
9+
endif()
10+
11+
if(NOT gpg-error_FOUND)
12+
find_program(GPG_ERROR_CONFIG NAMES gpg-error-config)
13+
if(GPG_ERROR_CONFIG)
14+
include(use_config_util)
15+
16+
extract_libs(gpg-error_LINK_LIBRARIES
17+
${GPG_ERROR_CONFIG} --libs)
18+
list(GET gpg-error_LINK_LIBRARIES 0 gpg-error_LIBRARY)
19+
20+
extract_includes(gpg-error_INCLUDE_DIRS
21+
${GPG_ERROR_CONFIG} --cflags)
22+
23+
execute_process(
24+
COMMAND ${GPG_ERROR_CONFIG} --version
25+
OUTPUT_VARIABLE gpg-error_VERSION
26+
OUTPUT_STRIP_TRAILING_WHITESPACE)
27+
endif()
28+
endif()
29+
30+
find_package_handle_standard_args(gpg-error
31+
REQUIRED_VARS
32+
gpg-error_LIBRARY
33+
gpg-error_VERSION
34+
VERSION_VAR gpg-error_VERSION)
35+
36+
if(TARGET PkgConfig::gpg-error)
37+
add_library(gpg-error ALIAS PkgConfig::gpg-error)
38+
else()
39+
add_library(gpg-error UNKNOWN IMPORTED)
40+
set_target_properties(gpg-error PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${gpg-error_INCLUDE_DIRS}")
41+
set(gpg-error_OTHER_LIBRARIES ${gpg-error_LINK_LIBRARIES})
42+
list(POP_FRONT gpg-error_OTHER_LIBRARIES)
43+
set_target_properties(gpg-error PROPERTIES INTERFACE_LINK_LIBRARIES "${gpg-error_OTHER_LIBRARIES}")
44+
unset(gpg-error_OTHER_LIBRARIES)
45+
set_property(TARGET gpg-error APPEND PROPERTY IMPORTED_LOCATION "${gpg-error_LIBRARY}")
46+
endif()
47+
48+
mark_as_advanced(gpg-error_LIBRARY)

cmake/Findgpgme.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
include(FindPackageHandleStandardArgs)
4+
5+
find_package(PkgConfig QUIET)
6+
if(PKG_CONFIG_FOUND)
7+
pkg_check_modules(gpgme QUIET IMPORTED_TARGET gpgme)
8+
list(GET gpgme_LINK_LIBRARIES 0 gpgme_LIBRARY)
9+
endif()
10+
11+
if(NOT gpgme_FOUND)
12+
find_program(GPGME_CONFIG NAMES gpgme-config)
13+
if(GPGME_CONFIG)
14+
include(use_config_util)
15+
16+
extract_libs(gpgme_LINK_LIBRARIES
17+
${GPGME_CONFIG} --libs)
18+
list(GET gpgme_LINK_LIBRARIES 0 gpgme_LIBRARY)
19+
20+
extract_includes(gpgme_INCLUDE_DIRS
21+
${GPGME_CONFIG} --cflags)
22+
23+
execute_process(
24+
COMMAND ${GPGME_CONFIG} --version
25+
OUTPUT_VARIABLE gpgme_VERSION
26+
OUTPUT_STRIP_TRAILING_WHITESPACE)
27+
endif()
28+
endif()
29+
30+
find_package_handle_standard_args(gpgme
31+
REQUIRED_VARS
32+
gpgme_LIBRARY
33+
gpgme_VERSION
34+
VERSION_VAR gpgme_VERSION)
35+
36+
if(TARGET PkgConfig::gpgme)
37+
add_library(gpgme ALIAS PkgConfig::gpgme)
38+
else()
39+
add_library(gpgme UNKNOWN IMPORTED)
40+
set_target_properties(gpgme PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${gpgme_INCLUDE_DIRS}")
41+
set(gpgme_OTHER_LIBRARIES ${gpgme_LINK_LIBRARIES})
42+
list(POP_FRONT gpgme_OTHER_LIBRARIES)
43+
set_target_properties(gpgme PROPERTIES INTERFACE_LINK_LIBRARIES "${gpgme_OTHER_LIBRARIES}")
44+
unset(gpgme_OTHER_LIBRARIES)
45+
set_property(TARGET gpgme APPEND PROPERTY IMPORTED_LOCATION "${gpgme_LIBRARY}")
46+
endif()
47+
48+
mark_as_advanced(gpgme_LIBRARY)

cmake/use_config_util.cmake

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function(extract_includes OUT_VAR)
2+
list(SUBLIST ARGV 1 -1 CMDARGS)
3+
execute_process(
4+
COMMAND ${CMDARGS}
5+
OUTPUT_VARIABLE STANDARD_OUTPUT
6+
OUTPUT_STRIP_TRAILING_WHITESPACE
7+
RESULT_VARIABLE COMMAND_RESULT)
8+
if(NOT "${COMMAND_RESULT}" STREQUAL "0")
9+
return()
10+
endif()
11+
12+
separate_arguments(STANDARD_OUTPUT)
13+
14+
set(INCDIRS)
15+
foreach(arg ${STANDARD_OUTPUT})
16+
if("${arg}" MATCHES "^-I(.+)")
17+
list(APPEND INCDIRS "${CMAKE_MATCH_1}")
18+
endif()
19+
endforeach()
20+
21+
set(FULL_INCDIRS)
22+
foreach(incdir ${INCDIRS})
23+
file(TO_CMAKE_PATH "${incdir}" incdir)
24+
list(APPEND FULL_INCDIRS "${incdir}")
25+
endforeach()
26+
27+
set(${OUT_VAR} ${FULL_INCDIRS} PARENT_SCOPE)
28+
endfunction()
29+
30+
function(extract_libs OUT_VAR)
31+
list(SUBLIST ARGV 1 -1 CMDARGS)
32+
execute_process(
33+
COMMAND ${CMDARGS}
34+
OUTPUT_VARIABLE STANDARD_OUTPUT
35+
OUTPUT_STRIP_TRAILING_WHITESPACE
36+
RESULT_VARIABLE COMMAND_RESULT)
37+
if(NOT "${COMMAND_RESULT}" STREQUAL "0")
38+
return()
39+
endif()
40+
41+
separate_arguments(STANDARD_OUTPUT)
42+
43+
set(LIBDIRS)
44+
set(LIBS)
45+
foreach(arg ${STANDARD_OUTPUT})
46+
if("${arg}" MATCHES "^-L(.+)")
47+
list(APPEND LIBDIRS "${CMAKE_MATCH_1}")
48+
elseif("${arg}" MATCHES "^-l(.+)")
49+
list(APPEND LIBS "${CMAKE_MATCH_1}")
50+
endif()
51+
endforeach()
52+
53+
set(HINTS)
54+
foreach(libdir ${LIBDIRS})
55+
file(TO_CMAKE_PATH "${libdir}" libdir)
56+
list(APPEND HINTS "${libdir}")
57+
endforeach()
58+
59+
set(FULL_LIBS)
60+
foreach(lib ${LIBS})
61+
find_library(${lib}_path
62+
NAMES ${lib}
63+
HINTS ${HINTS})
64+
if(NOT ${${lib}_path_FOUND})
65+
return()
66+
endif()
67+
list(APPEND FULL_LIBS ${${lib}_path})
68+
endforeach()
69+
70+
set(${OUT_VAR} ${FULL_LIBS} PARENT_SCOPE)
71+
endfunction()

test/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ find_package(GTest REQUIRED)
1010
macro(add_cache_test NAME)
1111
add_executable(test_${NAME} "main.cpp" "test_${NAME}.cpp")
1212
target_link_libraries(test_${NAME}
13-
${GTEST_LIBRARIES}
14-
createrepo-cache)
13+
createrepo-cache
14+
GTest::gtest)
1515
add_test(NAME ${NAME} COMMAND test_${NAME})
1616
endmacro()
1717

1818
macro(add_integration_test NAME)
1919
add_executable(test_${NAME} "integration_main.cpp" "test_${NAME}.cpp")
2020
target_link_libraries(test_${NAME}
21-
${GTEST_LIBRARIES}
22-
createrepo-agent-lib)
21+
createrepo-agent-lib
22+
GTest::gtest)
2323
add_test(NAME ${NAME} COMMAND test_${NAME})
2424
endmacro()
2525

0 commit comments

Comments
 (0)