Debian Codegraph @

cmake/utils.cmake

raw
1 # Copyright 2022-2023 Timo Röhling <[email protected]>
2 # SPDX-License-Identifier: FSFAP
3 #
4 # Copying and distribution of this file, with or without modification, are
5 # permitted in any medium without royalty provided the copyright notice and
6 # this notice are preserved. This file is offered as-is, without any warranty.
7 #
8 include(CMakeParseArguments)
9 include(ExternalProject)
10 include(FetchContent)
11
12 function(add_autotools_dependency name)
13 cmake_parse_arguments(arg "" "LIBRARY_NAME;EXPORTED_TARGET" "" ${ARGN})
14 FetchContent_MakeAvailable(${name})
15 if(NOT TARGET ${arg_EXPORTED_TARGET})
16 find_program(MAKE_EXECUTABLE NAMES gmake make mingw32-make REQUIRED)
17 set(library_file
18 "${CMAKE_STATIC_LIBRARY_PREFIX}${arg_LIBRARY_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}"
19 )
20 string(TOLOWER "${name}" lc_name)
21 string(TOUPPER "${CMAKE_BUILD_TYPE}" uc_build_type)
22 if(CMAKE_C_COMPILER_AR)
23 set(ar_executable "${CMAKE_C_COMPILER_AR}")
24 else()
25 set(ar_executable "${CMAKE_AR}")
26 endif()
27 if(CMAKE_C_COMPILER_LAUNCHER)
28 set(cc_executable
29 "${CMAKE_C_COMPILER_LAUNCHER} ${CMAKE_C_COMPILER}"
30 )
31 else()
32 set(cc_executable "${CMAKE_C_COMPILER}")
33 endif()
34 ExternalProject_Add(
35 Ext${name}
36 SOURCE_DIR "${${lc_name}_SOURCE_DIR}"
37 UPDATE_DISCONNECTED TRUE
38 PATCH_COMMAND
39 command -v autoreconf && autoreconf <SOURCE_DIR>
40 CONFIGURE_COMMAND
41 <SOURCE_DIR>/configure --disable-shared --prefix=<INSTALL_DIR>
42 "CC=${cc_executable}" "AR=${ar_executable}"
43 "RANLIB=${CMAKE_RANLIB}" "MAKE=${MAKE_EXECUTABLE}"
44 "CFLAGS=${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${uc_build_type}}"
45 BUILD_COMMAND ${MAKE_EXECUTABLE} -j
46 INSTALL_COMMAND ${MAKE_EXECUTABLE} -j install
47 TEST_COMMAND ""
48 BUILD_BYPRODUCTS <INSTALL_DIR>/lib/${library_file}
49 )
50 ExternalProject_Get_Property(Ext${name} INSTALL_DIR)
51 add_library(${arg_EXPORTED_TARGET} STATIC IMPORTED)
52 set_target_properties(
53 ${arg_EXPORTED_TARGET}
54 PROPERTIES IMPORTED_LOCATION "${INSTALL_DIR}/lib/${library_file}"
55 INTERFACE_INCLUDE_DIRECTORIES "${INSTALL_DIR}/include"
56 )
57 add_dependencies(${arg_EXPORTED_TARGET} Ext${name})
58 file(MAKE_DIRECTORY "${INSTALL_DIR}/include")
59 endif()
60 endfunction()
61
62 function(find_systemd_unit_destination var)
63 if(CMAKE_INSTALL_PREFIX MATCHES "^/usr/?$")
64 find_package(PkgConfig QUIET)
65 if(PkgConfig_FOUND)
66 pkg_get_variable(unitdir systemd systemdsystemunitdir)
67 set(${var}
68 "${unitdir}"
69 PARENT_SCOPE
70 )
71 else()
72 set(${var}
73 "/usr/lib/systemd/system"
74 PARENT_SCOPE
75 )
76 endif()
77 else()
78 set(${var}
79 "/etc/systemd/system"
80 PARENT_SCOPE
81 )
82 endif()
83 endfunction()
84
85 function(find_systemd_sysusers_destination var)
86 if(CMAKE_INSTALL_PREFIX MATCHES "^/usr/?$")
87 find_package(PkgConfig QUIET)
88 if(PkgConfig_FOUND)
89 pkg_get_variable(sysusersdir systemd sysusersdir)
90 set(${var}
91 "${sysusersdir}"
92 PARENT_SCOPE
93 )
94 else()
95 set(${var}
96 "/usr/lib/sysusers.d"
97 PARENT_SCOPE
98 )
99 endif()
100 else()
101 set(${var}
102 "/etc/sysusers.d"
103 PARENT_SCOPE
104 )
105 endif()
106 endfunction()