summaryrefslogtreecommitdiff
path: root/Tests/CustomCommandByproducts/CMakeLists.txt
blob: 08c897c2d87835b0ddc1e4e42ca29b8b5b39a99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0058 OLD)
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 ${CMAKE_CURRENT_BINARY_DIR}/../CustomCommandByproducts/timestamp2.txt
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/byproduct2.c.in byproduct2.c
  BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/../CustomCommandByproducts/byproduct2.c
  COMMAND ${CMAKE_COMMAND} -E touch timestamp2.txt
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct2.c.in
  )
add_custom_target(Producer2 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/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 ${CMAKE_CURRENT_BINARY_DIR}/../CustomCommandByproducts/byproduct3.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct3.c.in
  )

# 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 ${CMAKE_CURRENT_BINARY_DIR}/../CustomCommandByproducts/byproduct4.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct4.c.in
  )

add_executable(ProducerExe5_6_7 ProducerExe.c)

# Generate a byproduct in an executable POST_BUILD command.
add_custom_command(
  TARGET ProducerExe5_6_7 POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/byproduct5.c.in byproduct5.c
  BYPRODUCTS byproduct5.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct5.c.in
  )

# Generate a byproduct in an executable PRE_LINK command.
add_custom_command(
  TARGET ProducerExe5_6_7 PRE_LINK
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/byproduct6.c.in byproduct6.c
  BYPRODUCTS byproduct6.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct6.c.in
  )

# Generate a byproduct in an executable PRE_BUILD command.
add_custom_command(
  TARGET ProducerExe5_6_7 PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/byproduct7.c.in byproduct7.c
  BYPRODUCTS byproduct7.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct7.c.in
  )

# 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_executable(ProducerExe9 ProducerExe.c)

# Generate a byproduct in a custom target which depends on a byproduct of a
# POST_BUILD command (test if dependency of custom target Producer9 to
# ProducerExe9 is added).
add_custom_command(
  TARGET ProducerExe9 POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${CMAKE_CURRENT_SOURCE_DIR}/byproduct9.c.in byproduct9a.c
  BYPRODUCTS byproduct9a.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/byproduct9.c.in
  )
add_custom_target(Producer9
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    byproduct9a.c byproduct9.c
  BYPRODUCTS byproduct9.c
  DEPENDS byproduct9a.c
  )

# Generate the library file of an imported target as a byproduct
# of an external project.
get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(_isMultiConfig)
  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)

# Generate the library file of an imported target as a byproduct
# of an external project. The byproduct uses <BINARY_DIR> that is substituted
# by the real binary path
if(_isMultiConfig)
  set(cfg /${CMAKE_CFG_INTDIR})
else()
  set(cfg)
endif()
include(ExternalProject)
ExternalProject_Add(ExtTargetSubst
  SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/External"
  DOWNLOAD_COMMAND ""
  INSTALL_COMMAND ""
  BUILD_BYPRODUCTS "<BINARY_DIR>${cfg}/${CMAKE_STATIC_LIBRARY_PREFIX}ExternalLibrary${CMAKE_STATIC_LIBRARY_SUFFIX}"
  )
ExternalProject_Get_Property(ExtTargetSubst binary_dir)
add_library(ExternalLibraryWithSubstitution STATIC IMPORTED)
set_property(TARGET ExternalLibraryWithSubstitution PROPERTY IMPORTED_LOCATION
  ${binary_dir}${cfg}/${CMAKE_STATIC_LIBRARY_PREFIX}ExternalLibrary${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(ExternalLibraryWithSubstitution ExtTargetSubst)

# 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
  byproduct9.c
  )

# Dependencies to byproducts of custom commands other than build events are not
# yet traced (see issue #19005).
add_dependencies(CustomCommandByproducts Producer2)

target_link_libraries(CustomCommandByproducts ExternalLibrary)

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()