Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / RunCMake / BuildDepends / RunCMakeTest.cmake
1 include(RunCMake)
2
3 if(RunCMake_GENERATOR MATCHES "Ninja")
4   # Detect ninja version so we know what tests can be supported.
5   execute_process(
6     COMMAND "${RunCMake_MAKE_PROGRAM}" --version
7     OUTPUT_VARIABLE ninja_out
8     ERROR_VARIABLE ninja_out
9     RESULT_VARIABLE ninja_res
10     OUTPUT_STRIP_TRAILING_WHITESPACE
11     )
12   if(ninja_res EQUAL 0 AND "x${ninja_out}" MATCHES "^x[0-9]+\\.[0-9]+")
13     set(ninja_version "${ninja_out}")
14     message(STATUS "ninja version: ${ninja_version}")
15   else()
16     message(FATAL_ERROR "'ninja --version' reported:\n${ninja_out}")
17   endif()
18 endif()
19
20 if(RunCMake_GENERATOR STREQUAL "Borland Makefiles" OR
21    RunCMake_GENERATOR STREQUAL "Watcom WMake")
22   set(fs_delay 3)
23 else()
24   set(fs_delay 1.125)
25 endif()
26
27 function(run_BuildDepends CASE)
28   # Use a single build tree for a few tests without cleaning.
29   if(NOT RunCMake_TEST_BINARY_DIR)
30     set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
31   endif()
32   set(RunCMake_TEST_NO_CLEAN 1)
33   if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
34     set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
35   endif()
36   file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
37   file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
38   include(${RunCMake_SOURCE_DIR}/${CASE}.step1.cmake OPTIONAL)
39   run_cmake(${CASE})
40   set(RunCMake-check-file check.cmake)
41   set(check_step 1)
42   run_cmake_command(${CASE}-build1 ${CMAKE_COMMAND} --build . --config Debug)
43   if(run_BuildDepends_skip_step_2)
44     return()
45   endif()
46   execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay}) # handle 1s resolution
47   include(${RunCMake_SOURCE_DIR}/${CASE}.step2.cmake OPTIONAL)
48   set(check_step 2)
49   run_cmake_command(${CASE}-build2 ${CMAKE_COMMAND} --build . --config Debug)
50   if(run_BuildDepends_skip_step_3)
51     return()
52   endif()
53   execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay}) # handle 1s resolution
54   include(${RunCMake_SOURCE_DIR}/${CASE}.step3.cmake OPTIONAL)
55   set(check_step 3)
56   run_cmake_command(${CASE}-build3 ${CMAKE_COMMAND} --build . --config Debug)
57 endfunction()
58
59 set(run_BuildDepends_skip_step_3 1)
60
61 run_BuildDepends(C-Exe)
62 if(NOT RunCMake_GENERATOR STREQUAL "Xcode")
63   if(RunCMake_GENERATOR_TOOLSET MATCHES "^(v80|v90|v100)$")
64     # VS 10 forgets to re-link when a manifest changes
65     set(run_BuildDepends_skip_step_2 1)
66   endif()
67   run_BuildDepends(C-Exe-Manifest)
68   unset(run_BuildDepends_skip_step_2)
69 endif()
70
71 run_BuildDepends(Custom-Symbolic-and-Byproduct)
72 run_BuildDepends(Custom-Always)
73
74 set(RunCMake_TEST_OUTPUT_MERGE_save "${RunCMake_TEST_OUTPUT_MERGE}")
75 set(RunCMake_TEST_OUTPUT_MERGE 1)
76 run_BuildDepends(ExternalProjectCacheArgs)
77 set(RunCMake_TEST_OUTPUT_MERGE "${RunCMake_TEST_OUTPUT_MERGE_save}")
78
79 # Test header dependencies with a build tree underneath a source tree.
80 set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/BuildUnderSource")
81 set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/BuildUnderSource/build")
82 file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
83 file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}/include")
84 foreach(f CMakeLists.txt BuildUnderSource.cmake BuildUnderSource.c)
85   configure_file("${RunCMake_SOURCE_DIR}/${f}" "${RunCMake_TEST_SOURCE_DIR}/${f}" COPYONLY)
86 endforeach()
87 run_BuildDepends(BuildUnderSource)
88 unset(RunCMake_TEST_BINARY_DIR)
89 unset(RunCMake_TEST_SOURCE_DIR)
90
91 if(RunCMake_GENERATOR MATCHES "Make")
92   run_BuildDepends(MakeCustomIncludes)
93   if(NOT "${RunCMake_BINARY_DIR}" STREQUAL "${RunCMake_SOURCE_DIR}")
94     run_BuildDepends(MakeInProjectOnly)
95   endif()
96 endif()
97
98 function(run_RepeatCMake CASE)
99   set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${CASE}-build)
100   if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
101     set(RunCMake_TEST_OPTIONS -DCMAKE_CONFIGURATION_TYPES=Debug)
102   else()
103     set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
104   endif()
105   run_cmake(${CASE})
106   set(RunCMake_TEST_NO_CLEAN 1)
107   run_cmake_command(${CASE}-build1 ${CMAKE_COMMAND} --build . --config Debug)
108   run_cmake_command(${CASE}-rerun1 ${CMAKE_COMMAND} .)
109   file(WRITE ${RunCMake_TEST_BINARY_DIR}/exists-for-build2 "")
110   run_cmake_command(${CASE}-build2 ${CMAKE_COMMAND} --build . --config Debug)
111 endfunction()
112
113 run_RepeatCMake(RepeatCMake-Custom)
114
115 function(run_ReGeneration)
116   # test re-generation of project even if CMakeLists.txt files disappeared
117
118   # Use a single build tree for a few tests without cleaning.
119   set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/regenerate-project-build)
120   set(RunCMake_TEST_SOURCE_DIR ${RunCMake_BINARY_DIR}/regenerate-project-source)
121   set(RunCMake_TEST_NO_CLEAN 1)
122   file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
123   file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
124   set(ProjectHeader [=[
125     cmake_minimum_required(VERSION 3.5)
126     project(Regenerate-Project NONE)
127   ]=])
128
129   # create project with subdirectory
130   file(WRITE "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" "${ProjectHeader}"
131     "add_subdirectory(mysubdir)")
132   file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}/mysubdir")
133   file(WRITE "${RunCMake_TEST_SOURCE_DIR}/mysubdir/CMakeLists.txt" "# empty")
134
135   run_cmake(Regenerate-Project)
136   execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${fs_delay})
137
138   # now we delete the subdirectory and adjust the CMakeLists.txt
139   file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}/mysubdir")
140   file(WRITE "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" "${ProjectHeader}")
141
142   run_cmake_command(Regenerate-Project-Directory-Removed
143     ${CMAKE_COMMAND} --build "${RunCMake_TEST_BINARY_DIR}")
144
145   unset(RunCMake_TEST_BINARY_DIR)
146   unset(RunCMake_TEST_SOURCE_DIR)
147   unset(RunCMake_TEST_NO_CLEAN)
148 endfunction()
149
150 if(RunCMake_GENERATOR STREQUAL "Xcode")
151   run_ReGeneration(regenerate-project)
152 endif()
153
154 if(CMake_TEST_BuildDepends_GNU_AS)
155   set(ENV{ASM} "${CMake_TEST_BuildDepends_GNU_AS}")
156   run_BuildDepends(GNU-AS)
157 endif()
158
159 if ((RunCMake_GENERATOR STREQUAL "Unix Makefiles"
160       AND (CMAKE_C_COMPILER_ID STREQUAL "GNU"
161         OR CMAKE_C_COMPILER_ID STREQUAL "LCC"
162         OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
163         OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang"))
164     OR (RunCMake_GENERATOR STREQUAL "NMake Makefiles"
165       AND MSVC_VERSION GREATER 1300
166       AND CMAKE_C_COMPILER_ID STREQUAL "MSVC"))
167   run_BuildDepends(CompilerDependencies)
168   run_BuildDepends(CustomCommandDependencies)
169 endif()
170
171 if (RunCMake_GENERATOR MATCHES "Makefiles")
172   run_cmake(CustomCommandDependencies-BadArgs)
173   run_cmake_with_options(CustomCommandDependencies-compiler-deps-legacy -DCMAKE_DEPENDS_USE_COMPILER=FALSE)
174   set(RunCMake_TEST_NO_CLEAN 1)
175   run_cmake_command(CustomCommandDependencies-compiler-deps-legacy ${CMAKE_COMMAND} --build . --config Debug)
176   unset(RunCMake_TEST_NO_CLEAN)
177 endif()
178
179 if(RunCMake_GENERATOR MATCHES "Make|Ninja|Visual Studio|Xcode" AND
180     NOT RunCMake_GENERATOR MATCHES "Visual Studio (9|10)( |$)")
181   unset(run_BuildDepends_skip_step_3)
182   run_BuildDepends(CustomCommandDepfile)
183   set(run_BuildDepends_skip_step_3 1)
184 endif()
185
186 if(RunCMake_GENERATOR MATCHES "Make")
187   run_BuildDepends(MakeDependencies)
188 endif()
189
190 if(RunCMake_GENERATOR MATCHES "^Visual Studio 9 " OR
191    (RunCMake_GENERATOR MATCHES "Ninja" AND ninja_version VERSION_LESS 1.7))
192   # This build tool misses the dependency.
193   set(run_BuildDepends_skip_step_2 1)
194 endif()
195 run_BuildDepends(CustomCommandUnityBuild)
196 unset(run_BuildDepends_skip_step_2)