blob: 778f2c111e2d61b6c6b224aaf065d5697449ef04 (
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
include(RunCMake)
# Detect ninja version so we know what tests can be supported.
execute_process(
COMMAND "${RunCMake_MAKE_PROGRAM}" --version
OUTPUT_VARIABLE ninja_out
ERROR_VARIABLE ninja_out
RESULT_VARIABLE ninja_res
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(ninja_res EQUAL 0 AND "x${ninja_out}" MATCHES "^x[0-9]+\\.[0-9]+")
set(ninja_version "${ninja_out}")
message(STATUS "ninja version: ${ninja_version}")
else()
message(FATAL_ERROR "'ninja --version' reported:\n${ninja_out}")
endif()
function(run_CMP0058 case)
# Use a single build tree for a few tests without cleaning.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0058-${case}-build)
set(RunCMake_TEST_NO_CLEAN 1)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
run_cmake(CMP0058-${case})
run_cmake_command(CMP0058-${case}-build ${CMAKE_COMMAND} --build .)
endfunction()
run_CMP0058(OLD-no)
run_CMP0058(OLD-by)
run_CMP0058(WARN-no)
run_CMP0058(WARN-by)
run_CMP0058(NEW-no)
run_CMP0058(NEW-by)
run_cmake(CustomCommandDepfile)
function(run_SubDir)
# Use a single build tree for a few tests without cleaning.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SubDir-build)
set(RunCMake_TEST_NO_CLEAN 1)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
run_cmake(SubDir)
if(WIN32)
set(SubDir_all [[SubDir\all]])
set(SubDir_test [[SubDir\test]])
set(SubDir_install [[SubDir\install]])
else()
set(SubDir_all [[SubDir/all]])
set(SubDir_test [[SubDir/test]])
set(SubDir_install [[SubDir/install]])
endif()
run_cmake_command(SubDir-build ${CMAKE_COMMAND} --build . --target ${SubDir_all})
run_cmake_command(SubDir-test ${CMAKE_COMMAND} --build . --target ${SubDir_test})
run_cmake_command(SubDir-install ${CMAKE_COMMAND} --build . --target ${SubDir_install})
endfunction()
run_SubDir()
function(run_ninja dir)
execute_process(
COMMAND "${RunCMake_MAKE_PROGRAM}"
WORKING_DIRECTORY "${dir}"
OUTPUT_VARIABLE ninja_stdout
ERROR_VARIABLE ninja_stderr
RESULT_VARIABLE ninja_result
)
if(NOT ninja_result EQUAL 0)
message(STATUS "
============ beginning of ninja's stdout ============
${ninja_stdout}
=============== end of ninja's stdout ===============
")
message(STATUS "
============ beginning of ninja's stderr ============
${ninja_stderr}
=============== end of ninja's stderr ===============
")
message(FATAL_ERROR
"top ninja build failed exited with status ${ninja_result}")
endif()
endfunction(run_ninja)
function(sleep delay)
execute_process(
COMMAND ${CMAKE_COMMAND} -E sleep ${delay}
RESULT_VARIABLE result
)
if(NOT result EQUAL 0)
message(FATAL_ERROR "failed to sleep for ${delay} second.")
endif()
endfunction(sleep)
function(touch path)
execute_process(
COMMAND ${CMAKE_COMMAND} -E touch ${path}
RESULT_VARIABLE result
)
if(NOT result EQUAL 0)
message(FATAL_ERROR "failed to touch main ${path} file.")
endif()
endfunction(touch)
macro(ninja_escape_path path out)
string(REPLACE "\$ " "\$\$" "${out}" "${path}")
string(REPLACE " " "\$ " "${out}" "${${out}}")
string(REPLACE ":" "\$:" "${out}" "${${out}}")
endmacro(ninja_escape_path)
macro(shell_escape string out)
string(REPLACE "\"" "\\\"" "${out}" "${string}")
endmacro(shell_escape)
function(run_sub_cmake test ninja_output_path_prefix)
set(top_build_dir "${RunCMake_BINARY_DIR}/${test}-build/")
file(REMOVE_RECURSE "${top_build_dir}")
file(MAKE_DIRECTORY "${top_build_dir}")
ninja_escape_path("${ninja_output_path_prefix}"
escaped_ninja_output_path_prefix)
# Generate top build ninja file.
set(top_build_ninja "${top_build_dir}/build.ninja")
shell_escape("${top_build_ninja}" escaped_top_build_ninja)
set(build_ninja_dep "${top_build_dir}/build_ninja_dep")
ninja_escape_path("${build_ninja_dep}" escaped_build_ninja_dep)
shell_escape("${CMAKE_COMMAND}" escaped_CMAKE_COMMAND)
file(WRITE "${build_ninja_dep}" "fake dependency of top build.ninja file\n")
if(WIN32)
set(cmd_prefix "cmd.exe /C \"")
set(cmd_suffix "\"")
else()
set(cmd_prefix "")
set(cmd_suffix "")
endif()
file(WRITE "${top_build_ninja}" "\
subninja ${escaped_ninja_output_path_prefix}/build.ninja
default ${escaped_ninja_output_path_prefix}/all
# Sleep for 1 second before to regenerate to make sure the timestamp of
# the top build.ninja will be strictly greater than the timestamp of the
# sub/build.ninja file. We assume the system as 1 sec timestamp resolution.
rule RERUN
command = ${cmd_prefix}\"${escaped_CMAKE_COMMAND}\" -E sleep 1 && \"${escaped_CMAKE_COMMAND}\" -E touch \"${escaped_top_build_ninja}\"${cmd_suffix}
description = Testing regeneration
generator = 1
build build.ninja: RERUN ${escaped_build_ninja_dep} || ${escaped_ninja_output_path_prefix}/build.ninja
pool = console
")
# Run sub cmake project.
set(RunCMake_TEST_OPTIONS "-DCMAKE_NINJA_OUTPUT_PATH_PREFIX=${ninja_output_path_prefix}")
set(RunCMake_TEST_BINARY_DIR "${top_build_dir}/${ninja_output_path_prefix}")
run_cmake(${test})
# Check there is no 'default' statement in Ninja file generated by CMake.
set(sub_build_ninja "${RunCMake_TEST_BINARY_DIR}/build.ninja")
file(READ "${sub_build_ninja}" sub_build_ninja_file)
if(sub_build_ninja_file MATCHES "\ndefault [^\n][^\n]*all\n")
message(FATAL_ERROR
"unexpected 'default' statement found in '${sub_build_ninja}'")
endif()
# Run ninja from the top build directory.
run_ninja("${top_build_dir}")
# Test regeneration rules run in order.
set(main_cmakelists "${RunCMake_SOURCE_DIR}/CMakeLists.txt")
sleep(1) # Assume the system as 1 sec timestamp resolution.
touch("${main_cmakelists}")
touch("${build_ninja_dep}")
run_ninja("${top_build_dir}")
file(TIMESTAMP "${main_cmakelists}" mtime_main_cmakelists UTC)
file(TIMESTAMP "${sub_build_ninja}" mtime_sub_build_ninja UTC)
file(TIMESTAMP "${top_build_ninja}" mtime_top_build_ninja UTC)
# Check sub build.ninja is regenerated.
if(mtime_main_cmakelists STRGREATER mtime_sub_build_ninja)
message(FATAL_ERROR
"sub build.ninja not regenerated:
CMakeLists.txt = ${mtime_main_cmakelists}
sub/build.ninja = ${mtime_sub_build_ninja}")
endif()
# Check top build.ninja is regenerated after sub build.ninja.
if(NOT mtime_top_build_ninja STRGREATER mtime_sub_build_ninja)
message(FATAL_ERROR
"top build.ninja not regenerated strictly after sub build.ninja:
sub/build.ninja = ${mtime_sub_build_ninja}
build.ninja = ${mtime_top_build_ninja}")
endif()
endfunction(run_sub_cmake)
if("${ninja_version}" VERSION_LESS 1.6)
message(WARNING "Ninja is too old; skipping rest of test.")
return()
endif()
foreach(ninja_output_path_prefix "sub space" "sub")
run_sub_cmake(Executable "${ninja_output_path_prefix}")
run_sub_cmake(StaticLib "${ninja_output_path_prefix}")
run_sub_cmake(SharedLib "${ninja_output_path_prefix}")
run_sub_cmake(TwoLibs "${ninja_output_path_prefix}")
run_sub_cmake(SubDirPrefix "${ninja_output_path_prefix}")
run_sub_cmake(CustomCommandWorkingDirectory "${ninja_output_path_prefix}")
endforeach(ninja_output_path_prefix)
|