CMake自定义目标不会构建

时间:2018-05-06 09:20:11

标签: cmake post-build

我使用以下方法构建两个目标:

add_library(tgt1 SHARED a.cpp)
add_library(tgt2 SHARED b.cpp)

在构建两者之后,我需要运行一个取决于两个目标的后期构建步骤。我尝试了以下几种组合但没有成功:

add_custom_target(final_tgt DEPENDS tgt1 tgt2)
add_custom_command(TARGET final_tgt POST_BUILD COMMAND <command> ARGS <args>)

即使build.make包含自定义命令,也不会构建最终目标。

尝试使用ALL作为自定义目标,但是尝试在首次构建目标时首先构建它。

我无法使用add_library或add_executable作为最终目标,因为它们需要指定源文件。

这样做的正确方法是什么?

===================================

编辑:下面是一个最小的可验证源代码。 它尝试做的是在两个体系结构中编译代码(对于Mac),并作为后构建使用lipo创建通用二进制文件:

cmake_minimum_required(VERSION 2.8)
set(icpc_req_path "/usr/local/bin/icpc-16.0.146")

set(CMAKE_CXX_COMPILER "${icpc_req_path}")

project("CMakeTest")
set(SOURCE_FILES a.cpp)

set (TARGET_NAME "TGT")
set(TARGETS "")
set(ARCHITECTURES i386 x86_64)

foreach(ar ${ARCHITECTURES})
    set(CMAKE_CXX_FLAGS_RELEASE "")
    set(CMAKE_CXX_FLAGS_DEBUG "")
    set(CMAKE_CXX_FLAGS "")

    add_library(TGT_${ar} SHARED ${SOURCE_FILES})
    set_target_properties(${TARGET_NAME}_${ar}
        PROPERTIES COMPILE_FLAGS "-arch ${ar} -xSSE3")
    set_target_properties(${TARGET_NAME}_${ar}
        PROPERTIES LINK_FLAGS "-arch ${ar}")
    set(TARGETS "${TARGETS};lib${TARGET_NAME}_${ar}.dylib")
endforeach(ar)

message("Targets: ${TARGETS}")
add_custom_target(${TARGET_NAME} DEPENDS ${TARGETS})
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
     COMMAND "lipo"
     ARGS "-create" ${TARGETS} "-output" "${TARGET_NAME}.dylib")

a.cpp的内容是:

int main(){}

2 个答案:

答案 0 :(得分:1)

以下示例适用于我。

两个目标彼此独立。一个自定义目标取决于两者。

add_library(Big SHARED ${SOURCES_BIG})
add_library(Foo SHARED ${SOURCES_FOO})

add_custom_target(pack ALL
    COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}Big${CMAKE_SHARED_LIBRARY_SUFFIX}
    COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}Foo${CMAKE_SHARED_LIBRARY_SUFFIX}
    DEPENDS Big Foo
    COMMENT Build target pack
)

注意:计算代码的哈希只是为了显示目标是否在更改时重建。

答案 1 :(得分:0)

add_custom_command有两种形式:生成新输出,并作用于单个目标。 Docs

您正在呼叫的命令是什么?它以任何方式产生结果(新文件等)?如果是这样,请使用add_custom_command,如下所示:

add_custom_command(
  OUTPUT <output-file>
  DEPENDS tgt1 tgt2
  COMMAND <command>
  ARGS <args>
  COMMENT "Running  <command> on targets tgt1 and tgt2."
)

使用没有add_custom_command参数的OUTPUT的第二个变体,因为它将<target>更改为POST_BUILD(或预构建,预链接)步骤,需要一个目标。那么,tgt1tgt2中哪一个被<command>修改了?

我们假设在POST_BUILD步骤中修改tgt1并且tgt2未被触及。然后你就可以这样做:

add_library(tgt2 SHARED b.cpp)

add_library(tgt1 SHARED a.cpp)
add_custom_command(
  TARGET tgt1 POST_BUILD
  COMMAND <command>
  ARGS <args>
)
add_dependencies(tgt1 tgt2) # tgt1 depends on tgt2 because
# POST_BUILD-step is just the final step to build 'tgt1'
# NOTE: It is incorrect to modify 'tgt2' as POST_BUILD step for tgt1.
#       So this example expects no changes to tgt2 in add_custom_command.

- 在有关详细信息后编辑:

工作示例

的CMakeLists.txt

# I don't have 'icpc' and could not find it easily available for macOS.
# Instead, let's create a file "TGT" where contents are the two hashes of the two
# libraries, like done in 'Th.Thielemann's answer.
cmake_minimum_required(VERSION 3.10)
project(q50198141)

add_library(Big SHARED library1.cpp)
add_library(Foo SHARED library2.cpp)

add_custom_command(OUTPUT combined
    COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/combine.sh
    ARGS $<TARGET_FILE:Big> $<TARGET_FILE:Foo> combined
    DEPENDS Big Foo combine.sh
    COMMENT Build output 'combined'
)
add_custom_target(run_combined ALL DEPENDS combined)

combined.sh(确保是可以办理的!)

#!/bin/bash

# Hardcoded for q50198141    
# Args: In1 In2 Out
md5sum $1 $2 > $3