summaryrefslogtreecommitdiffstats
path: root/Tests/CustomCommandByproducts
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2014-11-13 23:54:52 (GMT)
committerBrad King <brad.king@kitware.com>2014-11-14 21:16:00 (GMT)
commite15a7075b58aef6fe7b6eb56f810d0f33bc31feb (patch)
treeffd22a2e663b4d0c0511e90f0718dce2cda51356 /Tests/CustomCommandByproducts
parent67bd514adce689f4c7f537cdc446c01f7373f5a5 (diff)
downloadCMake-e15a7075b58aef6fe7b6eb56f810d0f33bc31feb.zip
CMake-e15a7075b58aef6fe7b6eb56f810d0f33bc31feb.tar.gz
CMake-e15a7075b58aef6fe7b6eb56f810d0f33bc31feb.tar.bz2
Add an option for explicit BYPRODUCTS of custom commands (#14963)
A common idiom in CMake-based build systems is to have custom commands that generate files not listed explicitly as outputs so that these files do not have to be newer than the inputs. The file modification times of such "byproducts" are updated only when their content changes. Then other build rules can depend on the byproducts explicitly so that their dependents rebuild when the content of the original byproducts really does change. This "undeclared byproduct" approach is necessary for Makefile, VS, and Xcode build tools because if a byproduct were listed as an output of a rule then the rule would always rerun when the input is newer than the byproduct but the byproduct may never be updated. Ninja solves this problem by offering a 'restat' feature to check whether an output was really modified after running a rule and tracking the fact that it is up to date separately from its timestamp. However, Ninja also stats all dependencies up front and will only restat files that are listed as outputs of rules with the 'restat' option enabled. Therefore an undeclared byproduct that does not exist at the start of the build will be considered missing and the build will fail even if other dependencies would cause the byproduct to be available before its dependents build. CMake works around this limitation by adding 'phony' build rules for custom command dependencies in the build tree that do not have any explicit specification of what produces them. This is not optimal because it prevents Ninja from reporting an error when an input to a rule really is missing. A better approach is to allow projects to explicitly specify the byproducts of their custom commands so that no phony rules are needed for them. In order to work with the non-Ninja generators, the byproducts must be known separately from the outputs. Add a new "BYPRODUCTS" option to the add_custom_command and add_custom_target commands to specify byproducts explicitly. Teach the Ninja generator to specify byproducts as outputs of the custom commands. In the case of POST_BUILD, PRE_LINK, and PRE_BUILD events on targets that link, the byproducts must be specified as outputs of the link rule that runs the commands. Activate 'restat' for such rules so that Ninja knows it needs to check the byproducts, but not for link rules that have no byproducts.
Diffstat (limited to 'Tests/CustomCommandByproducts')
-rw-r--r--Tests/CustomCommandByproducts/CMakeLists.txt103
-rw-r--r--Tests/CustomCommandByproducts/CustomCommandByproducts.c21
-rw-r--r--Tests/CustomCommandByproducts/ProducerExe.c1
-rw-r--r--Tests/CustomCommandByproducts/byproduct1.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct2.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct3.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct4.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct5.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct6.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct7.c.in1
-rw-r--r--Tests/CustomCommandByproducts/byproduct8.c.in1
-rw-r--r--Tests/CustomCommandByproducts/ninja-check.cmake20
12 files changed, 153 insertions, 0 deletions
diff --git a/Tests/CustomCommandByproducts/CMakeLists.txt b/Tests/CustomCommandByproducts/CMakeLists.txt
new file mode 100644
index 0000000..c39a536
--- /dev/null
+++ b/Tests/CustomCommandByproducts/CMakeLists.txt
@@ -0,0 +1,103 @@
+cmake_minimum_required(VERSION 3.1)
+project(CustomCommandByproducts C)
+
+# Generate a byproduct in a rule that runs in the target consuming it.
+add_custom_command(
+ OUTPUT timestamp1.txt
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct1.c.in byproduct1.c
+ BYPRODUCTS byproduct1.c
+ COMMAND ${CMAKE_COMMAND} -E touch timestamp1.txt
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct1.c.in
+ )
+
+# Generate a byproduct in a rule that runs in a dependency of the consumer.
+add_custom_command(
+ OUTPUT timestamp2.txt
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct2.c.in byproduct2.c
+ BYPRODUCTS byproduct2.c
+ COMMAND ${CMAKE_COMMAND} -E touch timestamp2.txt
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct2.c.in
+ )
+add_custom_target(Producer2 DEPENDS timestamp2.txt)
+
+# Generate a byproduct in a custom target.
+add_custom_target(Producer3_4
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct3.c.in byproduct3.c
+ BYPRODUCTS byproduct3.c
+ )
+
+# Generate a byproduct in a custom target POST_BUILD command.
+add_custom_command(
+ TARGET Producer3_4 POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct4.c.in byproduct4.c
+ BYPRODUCTS byproduct4.c
+ )
+
+add_executable(ProducerExe ProducerExe.c)
+
+# Generate a byproduct in an executable POST_BUILD command.
+add_custom_command(
+ TARGET ProducerExe POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct5.c.in byproduct5.c
+ BYPRODUCTS byproduct5.c
+ )
+
+# Generate a byproduct in an executable PRE_LINK command.
+add_custom_command(
+ TARGET ProducerExe PRE_LINK
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct6.c.in byproduct6.c
+ BYPRODUCTS byproduct6.c
+ )
+
+# Generate a byproduct in an executable PRE_BUILD command.
+add_custom_command(
+ TARGET ProducerExe PRE_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct7.c.in byproduct7.c
+ BYPRODUCTS byproduct7.c
+ )
+
+# Generate a byproduct in a custom command that consumes other byproducts.
+add_custom_command(OUTPUT timestamp8.txt
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct8.c.in byproduct8.c
+ COMMAND ${CMAKE_COMMAND} -E touch timestamp8.txt
+ BYPRODUCTS byproduct8.c
+ DEPENDS
+ ${CMAKE_CURRENT_BINARY_DIR}/byproduct2.c
+ ${CMAKE_CURRENT_BINARY_DIR}/byproduct3.c
+ ${CMAKE_CURRENT_BINARY_DIR}/byproduct4.c
+ ${CMAKE_CURRENT_BINARY_DIR}/byproduct5.c
+ ${CMAKE_CURRENT_BINARY_DIR}/byproduct6.c
+ ${CMAKE_CURRENT_BINARY_DIR}/byproduct7.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/byproduct8.c.in
+ )
+
+# Add an executable consuming all the byproducts.
+add_executable(CustomCommandByproducts
+ CustomCommandByproducts.c
+ byproduct1.c timestamp1.txt
+ byproduct2.c
+ byproduct3.c
+ byproduct4.c
+ byproduct5.c
+ byproduct6.c
+ byproduct7.c
+ byproduct8.c timestamp8.txt
+ )
+add_dependencies(CustomCommandByproducts Producer2)
+add_dependencies(CustomCommandByproducts Producer3_4)
+add_dependencies(CustomCommandByproducts ProducerExe)
+
+if(CMAKE_GENERATOR STREQUAL "Ninja")
+ add_custom_target(CheckNinja ALL
+ COMMENT "Checking build.ninja"
+ COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/ninja-check.cmake
+ )
+endif()
diff --git a/Tests/CustomCommandByproducts/CustomCommandByproducts.c b/Tests/CustomCommandByproducts/CustomCommandByproducts.c
new file mode 100644
index 0000000..d9db9e6
--- /dev/null
+++ b/Tests/CustomCommandByproducts/CustomCommandByproducts.c
@@ -0,0 +1,21 @@
+extern int byproduct1(void);
+extern int byproduct2(void);
+extern int byproduct3(void);
+extern int byproduct4(void);
+extern int byproduct5(void);
+extern int byproduct6(void);
+extern int byproduct7(void);
+extern int byproduct8(void);
+int main(void)
+{
+ return (
+ byproduct1() +
+ byproduct2() +
+ byproduct3() +
+ byproduct4() +
+ byproduct5() +
+ byproduct6() +
+ byproduct7() +
+ byproduct8() +
+ 0);
+}
diff --git a/Tests/CustomCommandByproducts/ProducerExe.c b/Tests/CustomCommandByproducts/ProducerExe.c
new file mode 100644
index 0000000..78f2de1
--- /dev/null
+++ b/Tests/CustomCommandByproducts/ProducerExe.c
@@ -0,0 +1 @@
+int main(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct1.c.in b/Tests/CustomCommandByproducts/byproduct1.c.in
new file mode 100644
index 0000000..5c3cc24
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct1.c.in
@@ -0,0 +1 @@
+int byproduct1(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct2.c.in b/Tests/CustomCommandByproducts/byproduct2.c.in
new file mode 100644
index 0000000..eeb69ef
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct2.c.in
@@ -0,0 +1 @@
+int byproduct2(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct3.c.in b/Tests/CustomCommandByproducts/byproduct3.c.in
new file mode 100644
index 0000000..7d15310
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct3.c.in
@@ -0,0 +1 @@
+int byproduct3(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct4.c.in b/Tests/CustomCommandByproducts/byproduct4.c.in
new file mode 100644
index 0000000..8b243dd
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct4.c.in
@@ -0,0 +1 @@
+int byproduct4(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct5.c.in b/Tests/CustomCommandByproducts/byproduct5.c.in
new file mode 100644
index 0000000..47f5990
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct5.c.in
@@ -0,0 +1 @@
+int byproduct5(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct6.c.in b/Tests/CustomCommandByproducts/byproduct6.c.in
new file mode 100644
index 0000000..d70c14f
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct6.c.in
@@ -0,0 +1 @@
+int byproduct6(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct7.c.in b/Tests/CustomCommandByproducts/byproduct7.c.in
new file mode 100644
index 0000000..0be5006
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct7.c.in
@@ -0,0 +1 @@
+int byproduct7(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/byproduct8.c.in b/Tests/CustomCommandByproducts/byproduct8.c.in
new file mode 100644
index 0000000..abefd62
--- /dev/null
+++ b/Tests/CustomCommandByproducts/byproduct8.c.in
@@ -0,0 +1 @@
+int byproduct8(void) { return 0; }
diff --git a/Tests/CustomCommandByproducts/ninja-check.cmake b/Tests/CustomCommandByproducts/ninja-check.cmake
new file mode 100644
index 0000000..2fc3cc2
--- /dev/null
+++ b/Tests/CustomCommandByproducts/ninja-check.cmake
@@ -0,0 +1,20 @@
+file(READ build.ninja build_ninja)
+if("${build_ninja}" MATCHES [====[
+# Unknown Build Time Dependencies.
+# Tell Ninja that they may appear as side effects of build rules
+# otherwise ordered by order-only dependencies.
+
+((build [^:]*: phony [^\n]*
+)*)# ========]====])
+ set(phony "${CMAKE_MATCH_1}")
+ if(NOT phony)
+ message(STATUS "build.ninja correctly does not have extra phony rules")
+ else()
+ string(REGEX REPLACE "\n+$" "" phony "${phony}")
+ string(REGEX REPLACE "\n" "\n " phony " ${phony}")
+ message(FATAL_ERROR "build.ninja incorrectly has extra phony rules:\n"
+ "${phony}")
+ endif()
+else()
+ message(FATAL_ERROR "build.ninja is incorrectly missing expected block")
+endif()