CMakeLists.txt 16.7 KB
Newer Older
1
# this test creates a static library and an executable
2
# the source to the library is then changed
3 4 5
# and the build is done on the executable and if things
# are working the executable should relink with the new
# value.  The subdir Project contains the CMakelists.txt
6
# and source files for the test project.
Ken Martin's avatar
Ken Martin committed
7
cmake_minimum_required (VERSION 2.6)
8
project(BuildDepends)
9

10 11 12 13 14
# This entire test takes place during the initial
# configure step.  It should not run again when the
# project is built.
set(CMAKE_SUPPRESS_REGENERATION 1)

15
# Xcode needs some help with the fancy dependencies in this test.
16
if(XCODE AND XCODE_VERSION VERSION_LESS 5)
17
  set(HELP_XCODE 1)
18
endif()
19 20 21 22 23 24 25 26
function(help_xcode_depends)
  if(HELP_XCODE)
    file(GLOB_RECURSE MACRO_OBJS
      ${BuildDepends_BINARY_DIR}/Project/zot_macro_*.o*
      )
    if(MACRO_OBJS)
      message("Helping Xcode by removing objects [${MACRO_OBJS}]")
      file(REMOVE ${MACRO_OBJS})
27 28 29
    endif()
  endif()
endfunction()
30

31 32 33 34 35 36
# The Intel compiler causes the MSVC linker to crash during
# incremental linking, so avoid the /INCREMENTAL:YES flag.
if(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
  set(_cmake_options "-DCMAKE_EXE_LINKER_FLAGS=")
endif()

37
if("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
38 39 40 41 42
  set(TEST_LINK_DEPENDS ${BuildDepends_BINARY_DIR}/Project/linkdep.txt)
  file(WRITE ${TEST_LINK_DEPENDS} "1")
endif()
list(APPEND _cmake_options "-DTEST_LINK_DEPENDS=${TEST_LINK_DEPENDS}")

43 44
list(APPEND _cmake_options "-DCMAKE_FORCE_DEPFILES=1")

45
if(NOT CMAKE_GENERATOR MATCHES "Visual Studio ([^9]|9[0-9])")
46 47 48 49
  set(TEST_MULTI3 1)
  list(APPEND _cmake_options "-DTEST_MULTI3=1")
endif()

50 51
file(MAKE_DIRECTORY ${BuildDepends_BINARY_DIR}/Project)
message("Creating Project/foo.cxx")
52
write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
53
  "const char* foo() { return \"foo\";}" )
54

55 56
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  "static const char* zot = \"zot\";\n")
57 58
file(WRITE ${BuildDepends_BINARY_DIR}/Project/dir/header.txt
  "#define HEADER_STRING \"ninja\"\n" )
59 60
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  "static const char* zot_custom = \"zot_custom\";\n")
61 62 63 64
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  "static const char* zot_macro_dir = \"zot_macro_dir\";\n")
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  "static const char* zot_macro_tgt = \"zot_macro_tgt\";\n")
65 66 67 68 69 70
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_pch.hxx
  "#ifndef ZOT_PCH_HXX\n"
  "#define ZOT_PCH_HXX\n"
  "static const char* zot_pch = \"zot_pch\";\n"
  "#endif\n"
  )
71

72 73 74 75 76 77
file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  "#define link_depends_no_shared_lib_value 1\n")
file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_exe.h
  "#define link_depends_no_shared_exe_value 0\n")
set(link_depends_no_shared_check_txt ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_check.txt)

78 79 80
file(WRITE ${BuildDepends_BINARY_DIR}/Project/object_depends.txt "0\n")
set(object_depends_check_txt ${BuildDepends_BINARY_DIR}/Project/object_depends_check.txt)

81
file(WRITE ${BuildDepends_BINARY_DIR}/Project/external.in "external original\n")
82
file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi1-in.txt "multi1-in original\n")
83 84
file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt "multi2-stamp original\n")
file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt "multi3-stamp original\n")
85

86
help_xcode_depends()
87

88
message("Building project first time")
89
try_compile(RESULT
90 91 92
  ${BuildDepends_BINARY_DIR}/Project
  ${BuildDepends_SOURCE_DIR}/Project
  testRebuild
93
  CMAKE_FLAGS ${_cmake_options}
94
  OUTPUT_VARIABLE OUTPUT)
95
if(HELP_XCODE)
96
  try_compile(RESULT
97 98 99 100
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
101
  try_compile(RESULT
102 103 104 105
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
106
endif()
107

108
message("Output from first build:\n${OUTPUT}")
109
if(NOT RESULT)
110
  message(SEND_ERROR "Could not build test project (1)!")
111
endif()
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
# find and save the ninjadep executable
set(ninjadep ${BuildDepends_BINARY_DIR}/Project/ninjadep${CMAKE_EXECUTABLE_SUFFIX})
if(EXISTS
    "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
  set(ninjadep
    "${BuildDepends_BINARY_DIR}/Project/Debug/ninjadep${CMAKE_EXECUTABLE_SUFFIX}")
endif()
message("Running ${ninjadep}  ")
execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
string(REGEX REPLACE "[\r\n]" " " out "${out}")
message("Run result: ${runResult} Output: \"${out}\"")

if("${out}" STREQUAL "HEADER_STRING: ninja ")
  message("Worked!")
else()
  message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
    " expected [HEADER_STRING: ninja]")
endif()

133
set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
134
if(EXISTS
135 136
    "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
137
  set(bar
138
    "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
139
endif()
140
set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
141
if(EXISTS
142 143
    "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
144
  set(zot
145
    "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
146
endif()
147 148 149

message("Running ${bar}  ")
execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
150
string(REGEX REPLACE "[\r\n]" " " out "${out}")
151 152
message("Run result: ${runResult} Output: \"${out}\"")

153
if("${out}" STREQUAL "foo ")
154
  message("Worked!")
155
else()
156
  message(SEND_ERROR "Project did not initially build properly: ${out}")
157
endif()
158

159 160 161 162 163
message("Running ${zot}  ")
execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
string(REGEX REPLACE "[\r\n]" " " out "${out}")
message("Run result: ${runResult} Output: \"${out}\"")

164
set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] [zot_pch] ")
165
if("${out}" STREQUAL "${VALUE_UNCHANGED}")
166
  message("Worked!")
167
else()
168
  message(SEND_ERROR "Project did not initially build properly: ${out}")
169
endif()
170

171 172 173 174 175 176 177 178 179 180 181 182 183
if(EXISTS "${link_depends_no_shared_check_txt}")
  file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  if("${link_depends_no_shared_check}" STREQUAL "1")
    message(STATUS "link_depends_no_shared_exe is newer than link_depends_no_shared_lib as expected.")
  else()
    message(SEND_ERROR "Project did not initially build properly: "
      "link_depends_no_shared_exe is older than link_depends_no_shared_lib.")
  endif()
else()
  message(SEND_ERROR "Project did not initially build properly: "
    "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
endif()

184 185 186 187 188 189 190 191 192 193 194 195 196
if(EXISTS ${BuildDepends_BINARY_DIR}/Project/external.out)
  file(STRINGS ${BuildDepends_BINARY_DIR}/Project/external.out external_out)
  if("${external_out}" STREQUAL "external original")
    message(STATUS "external.out contains '${external_out}'")
  else()
    message(SEND_ERROR "Project did not initially build properly: "
      "external.out contains '${external_out}'")
  endif()
else()
  message(SEND_ERROR "Project did not initially build properly: "
    "external.out is missing")
endif()

197 198 199 200 201 202 203 204 205 206 207 208 209
if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt)
  file(STRINGS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt multi1_out)
  if("${multi1_out}" STREQUAL "multi1-in original")
    message(STATUS "multi1-out2-copy.txt contains '${multi1_out}'")
  else()
    message(SEND_ERROR "Project did not initially build properly: "
      "multi1-out2-copy.txt contains '${multi1_out}'")
  endif()
else()
  message(SEND_ERROR "Project did not initially build properly: "
    "multi1-out2-copy.txt is missing")
endif()

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi2-real.txt)
  if(${BuildDepends_BINARY_DIR}/Project/multi2-real.txt
      IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt)
    message(STATUS "multi2-real.txt is newer than multi2-stamp.txt")
  else()
    message(SEND_ERROR "Project did not initially build properly: "
      "multi2-real.txt is not newer than multi2-stamp.txt")
  endif()
else()
  message(SEND_ERROR "Project did not initially build properly: "
    "multi2-real.txt is missing")
endif()

if(TEST_MULTI3)
  if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi3-real.txt)
    if(${BuildDepends_BINARY_DIR}/Project/multi3-real.txt
        IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt)
      message(STATUS "multi3-real.txt is newer than multi3-stamp.txt")
    else()
      message(SEND_ERROR "Project did not initially build properly: "
        "multi3-real.txt is not newer than multi3-stamp.txt")
    endif()
  else()
    message(SEND_ERROR "Project did not initially build properly: "
      "multi3-real.txt is missing")
  endif()
endif()

238
message("Waiting 3 seconds...")
239
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 3)
240 241

message("Modifying Project/foo.cxx")
242
write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
243
  "const char* foo() { return \"foo changed\";}" )
244 245
file(WRITE "${BuildDepends_BINARY_DIR}/Project/dir/header.txt"
  "#define HEADER_STRING \"ninja changed\"\n" )
246 247
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  "static const char* zot = \"zot changed\";\n")
248 249
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  "static const char* zot_custom = \"zot_custom changed\";\n")
250 251 252 253
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_dir.hxx
  "static const char* zot_macro_dir = \"zot_macro_dir changed\";\n")
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_macro_tgt.hxx
  "static const char* zot_macro_tgt = \"zot_macro_tgt changed\";\n")
254 255 256 257 258 259
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_pch.hxx
  "#ifndef ZOT_PCH_HXX\n"
  "#define ZOT_PCH_HXX\n"
  "static const char* zot_pch = \"zot_pch changed\";\n"
  "#endif\n"
  )
260

261 262 263
file(WRITE ${BuildDepends_BINARY_DIR}/Project/link_depends_no_shared_lib.h
  "#define link_depends_no_shared_lib_value 0\n")

264 265
file(WRITE ${BuildDepends_BINARY_DIR}/Project/object_depends.txt "1\n")

266 267 268 269
if(TEST_LINK_DEPENDS)
  file(WRITE ${TEST_LINK_DEPENDS} "2")
endif()

270
file(WRITE ${BuildDepends_BINARY_DIR}/Project/external.in "external changed\n")
271
file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi1-in.txt "multi1-in changed\n")
272 273
file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt "multi2-stamp changed\n")
file(WRITE ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt "multi3-stamp changed\n")
274

275
help_xcode_depends()
276 277

message("Building project second time")
278
try_compile(RESULT
279 280 281
  ${BuildDepends_BINARY_DIR}/Project
  ${BuildDepends_SOURCE_DIR}/Project
  testRebuild
282
  CMAKE_FLAGS ${_cmake_options}
283 284
  OUTPUT_VARIABLE OUTPUT)

285
# Xcode is in serious need of help here
286
if(HELP_XCODE)
287
  try_compile(RESULT
288 289 290 291
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
292
  try_compile(RESULT
293 294 295 296
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
297
endif()
298

299
message("Output from second build:\n${OUTPUT}")
300
if(NOT RESULT)
301
  message(SEND_ERROR "Could not build test project (2)!")
302
endif()
303
if(EXISTS
304 305
    "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
306
endif()
307
if(EXISTS
308 309
    "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
310
endif()
311

312 313 314 315 316 317 318 319 320 321 322 323
message("Running ${ninjadep}  ")
execute_process(COMMAND ${ninjadep} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
string(REGEX REPLACE "[\r\n]" " " out "${out}")
message("Run result: ${runResult} Output: \"${out}\"")

if("${out}" STREQUAL "HEADER_STRING: ninja changed ")
  message("Worked!")
else()
  message(SEND_ERROR "Project did not rebuild properly. Output[${out}]\n"
    " expected [HEADER_STRING: ninja changed]")
endif()

324 325
message("Running ${bar}  ")
execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
326
string(REGEX REPLACE "[\r\n]" " " out "${out}")
327
message("Run result: ${runResult} Output: \"${out}\"")
328

329
if("${out}" STREQUAL "foo changed ")
330
  message("Worked!")
331
else()
332
  message(SEND_ERROR "Project did not rebuild properly!")
333
endif()
334 335 336 337 338 339

message("Running ${zot}  ")
execute_process(COMMAND ${zot} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
string(REGEX REPLACE "[\r\n]" " " out "${out}")
message("Run result: ${runResult} Output: \"${out}\"")

340
set(VALUE_CHANGED
341
  "[zot changed] [zot_custom changed] [zot_macro_dir changed] [zot_macro_tgt changed] [zot_pch changed] "
342 343
  )
if("${out}" STREQUAL "${VALUE_CHANGED}")
344
  message("Worked!")
345
else()
346
  message(SEND_ERROR "Project did not rebuild properly!")
347
endif()
348 349 350 351 352 353 354 355 356 357

if(TEST_LINK_DEPENDS)
  set(linkdep ${BuildDepends_BINARY_DIR}/Project/linkdep${CMAKE_EXECUTABLE_SUFFIX})
  if(${linkdep} IS_NEWER_THAN ${TEST_LINK_DEPENDS})
    message("LINK_DEPENDS worked")
  else()
    message(SEND_ERROR "LINK_DEPENDS failed.  Executable
  ${linkdep}
is not newer than dependency
  ${TEST_LINK_DEPENDS}
358 359 360 361 362 363 364 365 366 367 368
")
  endif()

  set(linkdep2 ${BuildDepends_BINARY_DIR}/Project/linkdep2${CMAKE_EXECUTABLE_SUFFIX})
  if(${linkdep2} IS_NEWER_THAN ${TEST_LINK_DEPENDS})
    message("INTERFACE_LINK_DEPENDS worked")
  else()
    message(SEND_ERROR "INTERFACE_LINK_DEPENDS failed.  Executable
  ${linkdep2}
is not newer than dependency
  ${TEST_LINK_DEPENDS}
369 370 371
")
  endif()
endif()
372 373 374 375 376

if(EXISTS "${link_depends_no_shared_check_txt}")
  file(STRINGS "${link_depends_no_shared_check_txt}" link_depends_no_shared_check LIMIT_COUNT 1)
  if("${link_depends_no_shared_check}" STREQUAL "0")
    message(STATUS "link_depends_no_shared_exe is older than link_depends_no_shared_lib as expected.")
377 378
  elseif(XCODE AND NOT XCODE_VERSION VERSION_LESS 5)
    message(STATUS "Known limitation: link_depends_no_shared_exe is newer than link_depends_no_shared_lib but we cannot stop Xcode ${XCODE_VERSION} from enforcing this dependency.")
379 380 381 382 383 384 385
  else()
    message(SEND_ERROR "Project did not rebuild properly: link_depends_no_shared_exe is newer than link_depends_no_shared_lib.")
  endif()
else()
  message(SEND_ERROR "Project did not rebuild properly.  "
    "Targets link_depends_no_shared_lib and link_depends_no_shared_exe not both built.")
endif()
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400
if(EXISTS "${object_depends_check_txt}")
  file(STRINGS "${object_depends_check_txt}" object_depends_check LIMIT_COUNT 1)
  if("${object_depends_check}" STREQUAL "1")
    message(STATUS "object_depends exe is newer than object_depends.txt as expected.")
  elseif(CMAKE_GENERATOR MATCHES "Visual Studio|Xcode")
    message(STATUS "Known limitation: OBJECT_DEPENDS does not work on ${CMAKE_GENERATOR}")
  else()
    message(SEND_ERROR "Project did not rebuild properly: object_depends exe is not newer than object_depends.txt.")
  endif()
else()
  message(SEND_ERROR "Project did not rebuild properly.  "
    "object_depends exe and object_depends.txt are not both present.")
endif()

401 402 403 404 405 406 407 408 409 410 411 412
if(EXISTS ${BuildDepends_BINARY_DIR}/Project/external.out)
  file(STRINGS ${BuildDepends_BINARY_DIR}/Project/external.out external_out)
  if("${external_out}" STREQUAL "external changed")
    message(STATUS "external.out contains '${external_out}'")
  else()
    message(SEND_ERROR "Project did not rebuild properly: "
      "external.out contains '${external_out}'")
  endif()
else()
  message(SEND_ERROR "Project did not rebuild properly: "
    "external.out is missing")
endif()
413 414 415 416 417 418 419 420 421 422 423 424 425

if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt)
  file(STRINGS ${BuildDepends_BINARY_DIR}/Project/multi1-out2-copy.txt multi1_out)
  if("${multi1_out}" STREQUAL "multi1-in changed")
    message(STATUS "multi1-out2-copy.txt contains '${multi1_out}'")
  else()
    message(SEND_ERROR "Project did not rebuild properly: "
      "multi1-out2-copy.txt contains '${multi1_out}'")
  endif()
else()
  message(SEND_ERROR "Project did not rebuild properly: "
    "multi1-out2-copy.txt is missing")
endif()
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi2-real.txt)
  if(${BuildDepends_BINARY_DIR}/Project/multi2-real.txt
      IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi2-stamp.txt)
    message(STATUS "multi2-real.txt is newer than multi2-stamp.txt")
  else()
    message(SEND_ERROR "Project did not rebuild properly: "
      "multi2-real.txt is not newer than multi2-stamp.txt")
  endif()
else()
  message(SEND_ERROR "Project did not rebuild properly: "
    "multi2-real.txt is missing")
endif()

if(TEST_MULTI3)
  if(EXISTS ${BuildDepends_BINARY_DIR}/Project/multi3-real.txt)
    if(${BuildDepends_BINARY_DIR}/Project/multi3-real.txt
        IS_NEWER_THAN ${BuildDepends_BINARY_DIR}/Project/multi3-stamp.txt)
      message(STATUS "multi3-real.txt is newer than multi3-stamp.txt")
    else()
      message(SEND_ERROR "Project did not rebuild properly: "
        "multi3-real.txt is not newer than multi3-stamp.txt")
    endif()
  else()
    message(SEND_ERROR "Project did not rebuild properly: "
      "multi3-real.txt is missing")
  endif()
endif()