| 1 |
# Porcelain tools for using pkg config. Better than cmake's builtins. |
| 2 |
|
| 3 |
# Execute pkg-config and store flags in the cache. Sets the variable `pkg_errno` |
| 4 |
# in the parent scope to the return value of the subprocess call. |
| 5 |
function(_pkg_query outvar arg) |
| 6 |
execute_process( |
| 7 |
COMMAND pkg-config --cflags-only-I ${arg} |
| 8 |
RESULT_VARIABLE _pkg_err |
| 9 |
OUTPUT_VARIABLE _pkg_out |
| 10 |
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) |
| 11 |
if(NOT _pkg_err EQUAL 0) |
| 12 |
set(pkg_errno |
| 13 |
1 |
| 14 |
PARENT_SCOPE) |
| 15 |
return() |
| 16 |
endif() |
| 17 |
|
| 18 |
# Convert space-separated list to semicolon-separated cmake-list |
| 19 |
string(REGEX REPLACE " +" ";" _includes_raw "${_pkg_out}") |
| 20 |
|
| 21 |
set(PKG_${outvar}_INCLUDEDIRS_RAW |
| 22 |
${_includes_raw} |
| 23 |
CACHE STRING "include directories for ${outvar}" FORCE) |
| 24 |
|
| 25 |
# Strip "-I" from include directories in the form of "-I/path/to" |
| 26 |
string(REGEX REPLACE "-I" "" _include_list "${_includes_raw}") |
| 27 |
|
| 28 |
set(PKG_${outvar}_INCLUDEDIRS |
| 29 |
${_include_list} |
| 30 |
CACHE STRING "include directories for ${outvar}" FORCE) |
| 31 |
|
| 32 |
execute_process( |
| 33 |
COMMAND pkg-config --cflags-only-other ${arg} |
| 34 |
RESULT_VARIABLE _pkg_err |
| 35 |
OUTPUT_VARIABLE _pkg_out |
| 36 |
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) |
| 37 |
if(NOT _pkg_err EQUAL 0) |
| 38 |
set(pkg_errno |
| 39 |
1 |
| 40 |
PARENT_SCOPE) |
| 41 |
return() |
| 42 |
endif() |
| 43 |
|
| 44 |
# Convert space-separated list to semicolon-separated cmake-list |
| 45 |
string(REGEX REPLACE " +" ";" _cflags_raw "${_pkg_out}") |
| 46 |
|
| 47 |
set(PKG_${outvar}_CFLAGS_RAW |
| 48 |
${_cflags} |
| 49 |
CACHE STRING "cflags directories for ${outvar}" FORCE) |
| 50 |
|
| 51 |
# Convert some C++ specific flags into a generator expression that will |
| 52 |
# nullify during C compiles. Specifically match replace strings like |
| 53 |
# "-std=c++11" to "$<$<COMPILE_LANGUAGE:CXX>:-std=c++11>". |
| 54 |
string(REGEX REPLACE "(-std=[^;]+)" "$<$<COMPILE_LANGUAGE:CXX>:\\1>" _cflags |
| 55 |
"${_cflags_raw}") |
| 56 |
|
| 57 |
set(PKG_${outvar}_CFLAGS |
| 58 |
${_cflags} |
| 59 |
CACHE STRING "cflags for ${outvar}" FORCE) |
| 60 |
|
| 61 |
set(PKG_${outvar}_CFLAGS_RAW |
| 62 |
${_includes_raw} ${_cflags_raw} |
| 63 |
CACHE STRING "raw cflags for ${outvar}" FORCE) |
| 64 |
|
| 65 |
execute_process( |
| 66 |
COMMAND pkg-config --libs-only-L ${arg} |
| 67 |
RESULT_VARIABLE _pkg_err |
| 68 |
OUTPUT_VARIABLE _pkg_out |
| 69 |
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) |
| 70 |
if(NOT _pkg_err EQUAL 0) |
| 71 |
set(pkg_errno |
| 72 |
1 |
| 73 |
PARENT_SCOPE) |
| 74 |
return() |
| 75 |
endif() |
| 76 |
|
| 77 |
# Convert space-separated list to semicolon-separated cmake-list |
| 78 |
string(REGEX REPLACE " +" ";" _libdirs_raw "${_pkg_out}") |
| 79 |
|
| 80 |
# Remove -L prefix |
| 81 |
string(REGEX REPLACE "-L" "" _libdirs "${_libdirs_raw}") |
| 82 |
|
| 83 |
set(PKG_${outvar}_LIBDIRS |
| 84 |
${_libdirs} |
| 85 |
CACHE STRING "library directories for ${outvar}" FORCE) |
| 86 |
|
| 87 |
execute_process( |
| 88 |
COMMAND pkg-config --libs-only-l ${arg} |
| 89 |
RESULT_VARIABLE _pkg_err |
| 90 |
OUTPUT_VARIABLE _pkg_out |
| 91 |
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) |
| 92 |
if(NOT _pkg_err EQUAL 0) |
| 93 |
set(pkg_errno |
| 94 |
1 |
| 95 |
PARENT_SCOPE) |
| 96 |
return() |
| 97 |
endif() |
| 98 |
|
| 99 |
# Convert space-separated list to semicolon-separated cmake-list |
| 100 |
string(REGEX REPLACE " +" ";" _libs_raw "${_pkg_out}") |
| 101 |
|
| 102 |
# Convert space-separated list to semicolon-separated cmake-list Strip "-l" |
| 103 |
# from libraries in the form of "-lfilename" so that they are just "filename". |
| 104 |
# Cmake will add the -l. |
| 105 |
string(REGEX REPLACE "-l" "" _libs "${_libs_raw}") |
| 106 |
|
| 107 |
set(PKG_${outvar}_LIBS |
| 108 |
${_libs} |
| 109 |
CACHE STRING "library directories for ${outvar}" FORCE) |
| 110 |
message(STATUS "PKG_${outvar}_LIBS: ${PKG_${outvar}_LIBS}") |
| 111 |
|
| 112 |
set(PKG_${outvar}_NAME |
| 113 |
${arg} |
| 114 |
CACHE STRING "selected name which worked as an argument to pkg-config" |
| 115 |
FORCE) |
| 116 |
|
| 117 |
set(PKG_${outvar}_LDFLAGS_RAW |
| 118 |
${_libdirs_raw} ${_libs_raw} |
| 119 |
CACHE STRING "raw ldflags for ${outvar}" FORCE) |
| 120 |
|
| 121 |
set(pkg_errno |
| 122 |
0 |
| 123 |
PARENT_SCOPE) |
| 124 |
endfunction() |
| 125 |
|
| 126 |
# Execute pkg-config for each name in the list Usage |
| 127 |
# _pkg_query_loop(<canonical_name> [<alt1> [<alt2> [...]]]) |
| 128 |
function(_pkg_query_loop name) |
| 129 |
if(PKG_${outvar}_FOUND) |
| 130 |
return() |
| 131 |
endif() |
| 132 |
set(outvar ${name}) |
| 133 |
set(names ${ARGN}) |
| 134 |
if(NOT names) |
| 135 |
set(names ${name}) |
| 136 |
endif() |
| 137 |
|
| 138 |
set(pkg_errno 1) |
| 139 |
foreach(qname ${names}) |
| 140 |
_pkg_query(${outvar} ${qname}) |
| 141 |
if(pkg_errno EQUAL 0) |
| 142 |
set(PKG_${outvar}_FOUND |
| 143 |
TRUE |
| 144 |
CACHE BOOL "${outvar} was found" FORCE) |
| 145 |
return() |
| 146 |
endif() |
| 147 |
endforeach() |
| 148 |
|
| 149 |
set(PKG_${outvar}_FOUND |
| 150 |
FALSE |
| 151 |
CACHE BOOL "${outvar} was not found" FORCE) |
| 152 |
endfunction() |
| 153 |
|
| 154 |
# Find system packages using pkg-config. |
| 155 |
# |
| 156 |
# usage: |
| 157 |
# ~~~ |
| 158 |
# pkg_find(PKG libfoo NAMES libfoo libfoo-1 libfoox |
| 159 |
# PKG libbar NAMES libarx |
| 160 |
# PKG libbaz) |
| 161 |
# ~~~ |
| 162 |
# |
| 163 |
# Execute pkg-config to get flags for each of the given library names. Each |
| 164 |
# library is specified with a line `PKG <name> [NAMES <name0>, ...]`. If the |
| 165 |
# library might have different names on different supported systems you may |
| 166 |
# specify a list of names to attempt. The first one that `pkg-config` exists |
| 167 |
# with `0` for will be used. If `NAMES` is provided, then `<name>` will not be |
| 168 |
# attempted (so duplicate it if you want to). `<name>` will be used for the key |
| 169 |
# in the pkg-config database cache. |
| 170 |
function(pkg_find) |
| 171 |
# cmake-lint: disable=C0201,R0912 |
| 172 |
set(state_ "PARSE_PKG") |
| 173 |
set(name_) |
| 174 |
set(names_) |
| 175 |
|
| 176 |
foreach(arg ${ARGN}) |
| 177 |
if(state_ STREQUAL "PARSE_PKG") |
| 178 |
if(arg STREQUAL "PKG") |
| 179 |
set(state_ "PARSE_NAME") |
| 180 |
set(name_) |
| 181 |
set(names_) |
| 182 |
else() |
| 183 |
message(FATAL_ERROR "malformed pkg_find, " |
| 184 |
"expected 'PKG' but got '${arg}'") |
| 185 |
endif() |
| 186 |
elseif(state_ STREQUAL "PARSE_NAME") |
| 187 |
set(name_ ${arg}) |
| 188 |
set(state_ "PARSE_KEYWORD") |
| 189 |
elseif(state_ STREQUAL "PARSE_KEYWORD") |
| 190 |
if(arg STREQUAL "PKG") |
| 191 |
_pkg_query_loop(${name_} ${names_}) |
| 192 |
set(name_) |
| 193 |
set(names_) |
| 194 |
set(state_ "PARSE_NAME") |
| 195 |
elseif(arg STREQUAL "NAMES") |
| 196 |
set(state_ "PARSE_NAMES") |
| 197 |
else() |
| 198 |
message(FATAL_ERROR "malformed pkg_find, expected keyword " |
| 199 |
"'PKG' or 'NAMES' but got '${arg}'") |
| 200 |
endif() |
| 201 |
elseif(state_ STREQUAL "PARSE_NAMES") |
| 202 |
if(arg STREQUAL "PKG") |
| 203 |
_pkg_query_loop(${name_} ${names_}) |
| 204 |
set(name_) |
| 205 |
set(names_) |
| 206 |
set(state_ "PARSE_NAME") |
| 207 |
elseif(arg STREQUAL "NAMES") |
| 208 |
message(FATAL_ERROR "malformed pkg_find, found literal " |
| 209 |
"'NAMES' when expecting library names") |
| 210 |
else() |
| 211 |
list(APPEND names_ ${arg}) |
| 212 |
endif() |
| 213 |
endif() |
| 214 |
endforeach() |
| 215 |
|
| 216 |
if(name_) |
| 217 |
_pkg_query_loop(${name_} ${names_}) |
| 218 |
endif() |
| 219 |
endfunction() |
| 220 |
|
| 221 |
# Add flags to compile/link options for the target according to the output of |
| 222 |
# pkg-config. Asserts that the given pkg-config packages were found |
| 223 |
function(target_pkg_depends target pkg0) |
| 224 |
foreach(pkgname ${pkg0} ${ARGN}) |
| 225 |
if(NOT PKG_${pkgname}_FOUND) |
| 226 |
message(FATAL_ERROR "pkg-config package ${pkgname} is not enumerated, but" |
| 227 |
" required by ${target}") |
| 228 |
endif() |
| 229 |
if(NOT ${PKG_${pkgname}_FOUND}) |
| 230 |
message(FATAL_ERROR "pkg-config package ${pkgname} is not found, but" |
| 231 |
" required by ${target}") |
| 232 |
endif() |
| 233 |
if(PKG_${pkgname}_INCLUDEDIRS) |
| 234 |
# TODO(josh): passthrough things like SYSTEM, BEFORE, |
| 235 |
# INTERFACE|PUBLIC|PRIVATE |
| 236 |
target_include_directories(${target} SYSTEM |
| 237 |
PUBLIC ${PKG_${pkgname}_INCLUDEDIRS}) |
| 238 |
endif() |
| 239 |
if(PKG_${pkgname}_CFLAGS) |
| 240 |
# TODO(josh): passthrough things like BEFORE, INTERFACE|PUBLIC|PRIVATE |
| 241 |
target_compile_options(${target} PUBLIC ${PKG_${pkgname}_CFLAGS}) |
| 242 |
endif() |
| 243 |
if(PKG_${pkgname}_LIBDIRS) |
| 244 |
get_target_property(lflags_ ${target} LINK_FLAGS) |
| 245 |
if(lflags_) |
| 246 |
list(APPEND lflags_ ${PKG_${pkgname}_lflags}) |
| 247 |
set_target_properties(${target} PROPERTIES LINK_FLAGS ${lflags_}) |
| 248 |
else() |
| 249 |
set_target_properties(${target} PROPERTIES LINK_FLAGS |
| 250 |
${PKG_${pkgname}_lflags}) |
| 251 |
endif() |
| 252 |
|
| 253 |
endif() |
| 254 |
if(PKG_${pkgname}_LIBS) |
| 255 |
# Passthrough options like INTERFACE|PUBLIC|PRIVATE and |
| 256 |
# debug|optimized|general |
| 257 |
target_link_libraries(${target} PUBLIC ${PKG_${pkgname}_LIBS}) |
| 258 |
endif() |
| 259 |
endforeach() |
| 260 |
endfunction() |