summaryrefslogtreecommitdiffstats
path: root/Tests/CustomCommandByproducts/CMakeLists.txt
blob: 3289e8f9f761a69f6feeed95fa27b0082e51cd37 (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
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
  )

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

# 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(CMAKE_CONFIGURATION_TYPES)
  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
  )
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
    COMMENT "Checking build.ninja"
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/ninja-check.cmake
    )
endif()