From 557aef0b94c86d13e802e6e8e34a491304d7be2f Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 13 Nov 2014 19:26:36 -0500 Subject: [PATCH] ExternalProject: Add options to specify BYPRODUCTS (#14963) The external project's build process may generate byproducts on which other rules in the driving project's build later depend. Provide a way for the driving project to specify what byproducts it expects to be made available by the custom commands that drive the external project. --- Modules/ExternalProject.cmake | 13 ++++++++++ Tests/CustomCommandByproducts/CMakeLists.txt | 24 +++++++++++++++++++ .../CustomCommandByproducts.c | 2 ++ .../External/CMakeLists.txt | 4 ++++ .../External/ExternalLibrary.c | 1 + 5 files changed, 44 insertions(+) create mode 100644 Tests/CustomCommandByproducts/External/CMakeLists.txt create mode 100644 Tests/CustomCommandByproducts/External/ExternalLibrary.c diff --git a/Modules/ExternalProject.cmake b/Modules/ExternalProject.cmake index 32f6d2ce8..e5616b1bc 100644 --- a/Modules/ExternalProject.cmake +++ b/Modules/ExternalProject.cmake @@ -132,6 +132,9 @@ Create custom targets to build projects in external trees Use source dir for build dir ``BUILD_ALWAYS 1`` No stamp file, build step always runs + ``BUILD_BYPRODUCTS ...`` + Files that will be generated by the build command but may or may + not have their modification time updated by subsequent builds. Install step options are: @@ -234,6 +237,9 @@ Create custom targets to build projects in external trees Steps that depend on this step ``DEPENDS ...`` Files on which this step depends + ``BYPRODUCTS ...`` + Files that will be generated by this step but may or may not + have their modification time updated by subsequent builds. ``ALWAYS 1`` No stamp file, step always runs ``EXCLUDE_FROM_MAIN 1`` @@ -1409,6 +1415,9 @@ function(ExternalProject_Add_Step name step) # Dependencies on files. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS) + # Byproducts of the step. + get_property(byproducts TARGET ${name} PROPERTY _EP_${step}_BYPRODUCTS) + # Dependencies on steps. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES) foreach(dependee IN LISTS dependees) @@ -1466,6 +1475,7 @@ function(ExternalProject_Add_Step name step) add_custom_command( OUTPUT ${stamp_file} + BYPRODUCTS ${byproducts} COMMENT ${comment} COMMAND ${command} COMMAND ${touch} @@ -2139,8 +2149,11 @@ function(_ep_add_build_command name) set(always 0) endif() + get_property(build_byproducts TARGET ${name} PROPERTY _EP_BUILD_BYPRODUCTS) + ExternalProject_Add_Step(${name} build COMMAND ${cmd} + BYPRODUCTS ${build_byproducts} WORKING_DIRECTORY ${binary_dir} DEPENDEES configure ALWAYS ${always} diff --git a/Tests/CustomCommandByproducts/CMakeLists.txt b/Tests/CustomCommandByproducts/CMakeLists.txt index c39a5366a..884f8c231 100644 --- a/Tests/CustomCommandByproducts/CMakeLists.txt +++ b/Tests/CustomCommandByproducts/CMakeLists.txt @@ -79,6 +79,29 @@ add_custom_command(OUTPUT timestamp8.txt ${CMAKE_CURRENT_SOURCE_DIR}/byproduct8.c.in ) +# Generate the library file of an imported target as a byproduct +# of an external project. +if(CMAKE_CONFIGURATION_TYPES) + set(cfg /${CMAKE_CFG_INTDIR}) +else() + set(cfg) +endif() +set(ExternalLibrary_LIBRARY + ${CMAKE_CURRENT_BINARY_DIR}/External-build${cfg}/${CMAKE_STATIC_LIBRARY_PREFIX}ExternalLibrary${CMAKE_STATIC_LIBRARY_SUFFIX} + ) +include(ExternalProject) +ExternalProject_Add(ExternalTarget + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/External" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/External-build" + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/External-build/root" + DOWNLOAD_COMMAND "" + INSTALL_COMMAND "" + BUILD_BYPRODUCTS "${ExternalLibrary_LIBRARY}" + ) +add_library(ExternalLibrary STATIC IMPORTED) +set_property(TARGET ExternalLibrary PROPERTY IMPORTED_LOCATION ${ExternalLibrary_LIBRARY}) +add_dependencies(ExternalLibrary ExternalTarget) + # Add an executable consuming all the byproducts. add_executable(CustomCommandByproducts CustomCommandByproducts.c @@ -94,6 +117,7 @@ add_executable(CustomCommandByproducts add_dependencies(CustomCommandByproducts Producer2) add_dependencies(CustomCommandByproducts Producer3_4) add_dependencies(CustomCommandByproducts ProducerExe) +target_link_libraries(CustomCommandByproducts ExternalLibrary) if(CMAKE_GENERATOR STREQUAL "Ninja") add_custom_target(CheckNinja ALL diff --git a/Tests/CustomCommandByproducts/CustomCommandByproducts.c b/Tests/CustomCommandByproducts/CustomCommandByproducts.c index d9db9e6f7..1916427b7 100644 --- a/Tests/CustomCommandByproducts/CustomCommandByproducts.c +++ b/Tests/CustomCommandByproducts/CustomCommandByproducts.c @@ -6,6 +6,7 @@ extern int byproduct5(void); extern int byproduct6(void); extern int byproduct7(void); extern int byproduct8(void); +extern int ExternalLibrary(void); int main(void) { return ( @@ -17,5 +18,6 @@ int main(void) byproduct6() + byproduct7() + byproduct8() + + ExternalLibrary() + 0); } diff --git a/Tests/CustomCommandByproducts/External/CMakeLists.txt b/Tests/CustomCommandByproducts/External/CMakeLists.txt new file mode 100644 index 000000000..feaa12ea4 --- /dev/null +++ b/Tests/CustomCommandByproducts/External/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.1) +project(External C) + +add_library(ExternalLibrary STATIC ExternalLibrary.c) diff --git a/Tests/CustomCommandByproducts/External/ExternalLibrary.c b/Tests/CustomCommandByproducts/External/ExternalLibrary.c new file mode 100644 index 000000000..a1dacf0c1 --- /dev/null +++ b/Tests/CustomCommandByproducts/External/ExternalLibrary.c @@ -0,0 +1 @@ +int ExternalLibrary(void) { return 0; }