如何从cmake目标自动生成pkgconfig文件

f87krz0w  于 2022-12-23  发布在  其他
关注(0)|答案(2)|浏览(449)

我想在cmake中从目标生成pkgconfig文件。我开始写了这样的代码:

function(auto_pkgconfig TARGET)

    get_target_property(INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)
    string(REPLACE "$<BUILD_INTERFACE:" "$<0:" INCLUDE_DIRS "${INCLUDE_DIRS}")
    string(REPLACE "$<INSTALL_INTERFACE:" "$<1:" INCLUDE_DIRS "${INCLUDE_DIRS}")
    string(REPLACE "$<INSTALL_PREFIX>" "${CMAKE_INSTALL_PREFIX}" INCLUDE_DIRS "${INCLUDE_DIRS}")

    file(GENERATE OUTPUT ${TARGET}.pc CONTENT "
Name: ${TARGET}
Cflags: -I$<JOIN:${INCLUDE_DIRS}, -I>
Libs: -L${CMAKE_INSTALL_PREFIX}/lib -l${TARGET}
")
    install(FILES ${TARGET}.pc DESTINATION lib/pkgconfig)
endfunction()

这是一个简化的版本,但它基本上读取INTERFACE_INCLUDE_DIRECTORIES属性并处理生成器表达式的INSTALL_INTERFACE,只要在调用auto_pkgconfig之前设置了include目录,就可以很好地工作,如下所示:

add_library(foo foo.cpp)

target_include_directories(foo PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>
    ${OTHER_INCLUDE_DIRS}
)

auto_pkgconfig(foo)

但是,有时属性是在调用auto_pkgconfig之后设置的,如下所示:

add_library(foo foo.cpp)
auto_pkgconfig(foo)

target_include_directories(foo PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>
    ${OTHER_INCLUDE_DIRS}
)

但是,这将无法正确读取include目录。我希望auto_pkgconfig在设置完所有目标属性后运行。我可以使用生成器表达式,将auto_pkgconfig修改为:

function(auto_pkgconfig TARGET)

    file(GENERATE OUTPUT ${TARGET}.pc CONTENT "
Name: ${TARGET}
Cflags: -I$<JOIN:$<TARGET_PROPERTY:${TARGET},INTERFACE_INCLUDE_DIRECTORIES>, -I>
Libs: -L$<TARGET_FILE_DIR:${TARGET}> -l${TARGET}
")
    install(FILES ${TARGET}.pc DESTINATION lib/pkgconfig)
endfunction()

但是,这将读取BUILD_INTERFACE而不是INSTALL_INTERFACE。因此,在设置目标属性之后,是否有其他方法可以读取它们?

jmo0nnb3

jmo0nnb31#

根据CMake文档,INSTALL_INTERFACE的内容只有在调用install(EXPORT)时才可用。除非它们扩展了CMake,否则最好执行其他操作来生成PkgConfig文件。理想情况下,您应该对安装布局有足够的控制权,以使此操作变得容易。

  • 然而 *,这并不意味着你不能做你要求的事情;这只是"Tony the Pony"级别的邪恶。我其实很犹豫要不要发布这个。请不要把这个当作推荐。

我们的想法是使用install(EXPORT)让CMake生成适当的脚本,然后 * 生成一个虚拟的CMake项目 *,它使用上面给出的file(GENERATE OUTPUT ...)代码;虚拟项目将看到导出的属性,即INSTALL_INTERFACE属性。
我最初尝试使用install(CODE [[ ... ]])来实现这一点,但是它 * 也 * 看到$<BUILD_INTERFACE:...>视图,我在CMake Discourse上询问过这一点。

cmake_minimum_required(VERSION 3.16)
project(example)

# Dummy library for demo

add_library(example SHARED example.cpp)

target_compile_definitions(example
  PUBLIC $<BUILD_INTERFACE:BUILD>
         $<INSTALL_INTERFACE:INSTALL>)

target_include_directories(example
  PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
         $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>)

# Here be dragons...

function(auto_pc TARGET)
  file(CONFIGURE OUTPUT "pc.${TARGET}/CMakeLists.txt"
       CONTENT [[
cmake_minimum_required(VERSION 3.16)
project(pc_@TARGET@)

find_package(pc_@TARGET@ REQUIRED CONFIG)

file(GENERATE OUTPUT @TARGET@.pc
     CONTENT [=[
Name: @TARGET@
Cflags: -I$<JOIN:$<TARGET_PROPERTY:INTERFACE_INCLUDE_DIRECTORIES>, -I> -D$<JOIN:$<TARGET_PROPERTY:INTERFACE_COMPILE_DEFINITIONS>, -D>
Libs: -L$<TARGET_FILE_DIR:@TARGET@> -l@TARGET@
]=]   TARGET "@TARGET@")
]] @ONLY NEWLINE_STYLE LF)

  install(TARGETS ${TARGET} EXPORT pc_${TARGET})
  install(EXPORT pc_${TARGET} DESTINATION "_auto_pc" FILE pc_${TARGET}-config.cmake)

  file(CONFIGURE OUTPUT "pc.${TARGET}/post-install.cmake"
       CONTENT [[
file(REAL_PATH "${CMAKE_INSTALL_PREFIX}" prefix)
set(proj "@CMAKE_CURRENT_BINARY_DIR@/pc.@TARGET@")
execute_process(COMMAND "@CMAKE_COMMAND@" "-Dpc_@TARGET@_DIR=${prefix}/_auto_pc" -S "${proj}" -B "${proj}/build")
file(COPY "${proj}/build/@TARGET@.pc" DESTINATION "${prefix}")
]] @ONLY NEWLINE_STYLE LF)

  install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/pc.${TARGET}/post-install.cmake")
endfunction()

auto_pc(example)

# Clean up install path
install(CODE [[ file(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/_auto_pc") ]])

结果如下:

alex@Alex-Desktop:~/test$ cmake -S . -B build
...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/build
alex@Alex-Desktop:~/test$ cmake --build build/
...
alex@Alex-Desktop:~/test$ cmake --install build --prefix install
-- Install configuration: ""
-- Installing: /home/alex/test/install/lib/libexample.so
-- Installing: /home/alex/test/install/_auto_pc/pc_example-config.cmake
-- Installing: /home/alex/test/install/_auto_pc/pc_example-config-noconfig.cmake
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/build/pc.example/build
alex@Alex-Desktop:~/test$ ls install/
example.pc  lib
alex@Alex-Desktop:~/test$ cat install/example.pc
Name: example
Cflags: -I/home/alex/test/install/include -DINSTALL
Libs: -L/home/alex/test/install/lib -lexample

你应该难过才对。我也难过。

pqwbnv8z

pqwbnv8z2#

编辑:离题,因为这里,pc文件是手动生成的

pkgconfig模板文件

动机:

  • CMakeLists.txt应该是唯一的真实来源(名称、版本)
  • pkgconfig文件大约比cmake文件小10倍(cmake到pkgconfig是一个有损的转换)

模板文件:第一个月

prefix="@CMAKE_INSTALL_PREFIX@"
exec_prefix="${prefix}"
libdir="${prefix}/lib"
includedir="${prefix}/include"

Name: @PROJECT_NAME@
Description: @CMAKE_PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@
Cflags: -I${includedir}
Libs: -L${libdir} -l@target1@

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(my_library VERSION 1.1.2 LANGUAGES C
  DESCRIPTION "example library")

add_library(my_library src/my_library.c)

# generate pc file for pkg-config
set(target1 my_library)
configure_file(my_package.pc.in
  lib/pkgconfig/my_package.pc @ONLY)

基于:CMake generate pkg-config .pc
相关:将目标导出到cmake文件

相关问题