CMake通过字符串处理现有的$ <TARGET来定义新的目标

我正在尝试更新一个较早的CMake脚本,它基本上执行以下操作来生成一个libtool文件:

get_target_property(target_location ${target} LOCATION)
get_filename_component(target_we ${target_location} NAME_WE)
get_target_property(target_deps ${target} LT_DEPENDENCY_LIBS)
# ...
# Get a bunch more properties...
# ...
set(la_target ${PROJECT_BINARY_DIR}/${target_we}.la)
# ...
# Do a bunch of file(WRITE...) file(APPEND...) etc
# ...
install(FILES ${la_target} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)

您无法从现代cmake中的目标获取LOCATION属性,并且我无法弄清楚如何在$ generator上执行get_filename_component()。

在add_custom_command()COMMAND语句中使用生成器只允许一行,所以我无法设置变量来执行所有必要的get_target_property()/ file(WRITE ...)处理。

或者,我可以添加一个COMMAND cmake -P script.cmake来运行一个脚本,它可以执行所有的字符串处理,但不能定义或引用目标,所以我似乎卡住了。

编辑:我在macOS上使用Makefile生成器,所讨论的目标是在同一个项目中生成的共享库。

编辑2:错误是:

The LOCATION property may not be read from target "mytarget".  
Use the target name directly with add_custom_command, or use 
the generator expression $<TARGET_FILE>, as appropriate.

你可以这样做:

  • 首先使用file命令展开生成器表达式并呈现二级模板
  • ...将在构建时通过configure_file进行渲染
  • 这是一个示例项目:

    cmake_minimum_required(VERSION 3.10)
    
    project(location-test VERSION 0.0.1 LANGUAGES CXX)
    
    add_executable(foo foo.cc)
    
    file(GENERATE OUTPUT blah.la.in INPUT blah.la.in.in)
    
    configure_file(render-variables.cmake.in render-variables.cmake @ONLY)
    
    add_custom_command(
        OUTPUT "${PROJECT_BINARY_DIR}/blah.la"
        COMMAND "${CMAKE_COMMAND}"
            -DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}"
            -DPROJECT_NAME="${PROJECT_NAME}"
            -DPROJECT_VERSION="${PROJECT_VERSION}"
            -P "${PROJECT_BINARY_DIR}/render-variables.cmake"
        MAIN_DEPENDENCY "${PROJECT_BINARY_DIR}/blah.la.in"
        DEPENDS "${PROJECT_BINARY_DIR}/render-variables.cmake"
    )
    
    add_custom_target(
        make-la-la-la ALL
        DEPENDS "${PROJECT_BINARY_DIR}/blah.la"
    )
    

    blah.la.in.in (2级blah.la.in.in %):

    # This file going to be rendered by the following pipeline:
    # - at CMake execution time it'll expand all generator expressions 
    # (e.g. $<TARGET_FILE:foo>)
    # - at build time, the `render-variables.cmake` script will substitute 
    # CMake variables (like @PROJECT_NAME@ or @PROJECT_VERSION@)
    

    为真实的.la文件编写模板... render-variables.cmake.in很简单:

    configure_file("${PROJECT_BINARY_DIR}/blah.la.in" "${PROJECT_BINARY_DIR}/blah.la" @ONLY)
    

    最后是一个虚拟的C ++文件foo.cc

    #include <iostream>
    
    int main()
    {
        std::cout << "Hello Africa!n";
    }
    

    建立它!

    localtion-sample$ mkdir build/
    localtion-sample$ cd build/
    localtion-sample/build$ cmake ..
    -- The CXX compiler identification is GNU 5.4.0
    -- Check for working CXX compiler: /usr/x86_64-pc-linux-gnu/gcc-bin/5.4.0/c++ -- works
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /tmp/localtion-sample/build
    
    localtion-sample/build$ make 
    Scanning dependencies of target foo
    [ 33%] Building CXX object CMakeFiles/foo.dir/foo.cc.o
    [ 66%] Linking CXX executable foo
    [ 66%] Built target foo
    Scanning dependencies of target make-la-la-la
    [100%] Generating blah.la
    [100%] Built target make-la-la-la
    
    localtion-sample/build$ ll
    total 52K
    drwxr-xr-x 6 zaufi zaufi  320 Feb  9 22:35 CMakeFiles/
    -rw-r--r-- 1 zaufi zaufi  287 Feb  9 22:35 blah.la
    -rw-r--r-- 1 zaufi zaufi  300 Feb  9 22:35 blah.la.in
    -rw-r--r-- 1 zaufi zaufi  11K Feb  9 22:35 CMakeCache.txt
    -rw-r--r-- 1 zaufi zaufi 1.5K Feb  9 22:35 cmake_install.cmake
    -rwxr-xr-x 1 zaufi zaufi  14K Feb  9 22:35 foo
    -rw-r--r-- 1 zaufi zaufi 5.0K Feb  9 22:35 Makefile
    -rw-r--r-- 1 zaufi zaufi   89 Feb  9 22:35 render-variables.cmake
    
    localtion-sample/build$ cat blah.la
    # This file going to be rendered by the following pipeline:
    # - at CMake execution time it'll expand all generator expressions
    # (e.g. /tmp/localtion-sample/build/foo)
    # - at build time, the `render-variables.cmake` script will substitute
    # CMake variables (like location-test or 0.0.1)
    
    链接地址: http://www.djcxy.com/p/31885.html

    上一篇: CMake define new target by string processing existing $<TARGET

    下一篇: CMake run custom command before build?