CMakeLists.txt 7.77 KB
Newer Older
1 2 3 4 5 6
# this test creates a static library and an executable
# the source to the library is then changed 
# 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
# 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# Xcode needs some help with the fancy dependencies in this test.
if("${CMAKE_GENERATOR}" MATCHES "Xcode")
  set(HELP_XCODE 1)
endif("${CMAKE_GENERATOR}" MATCHES "Xcode")
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})
    endif(MACRO_OBJS)
  endif(HELP_XCODE)
endfunction(help_xcode_depends)

31 32 33 34
if("${CMAKE_GENERATOR}" MATCHES "Ninja")
  set(HELP_NINJA 1) # TODO Why is this needed?
endif()

35 36 37 38 39 40
# 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()

41 42 43 44 45 46
if("${CMAKE_GENERATOR}" MATCHES "Make")
  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}")

47 48
list(APPEND _cmake_options "-DCMAKE_FORCE_DEPFILES=1")

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

54 55
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  "static const char* zot = \"zot\";\n")
56 57
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  "static const char* zot_custom = \"zot_custom\";\n")
58 59 60 61 62 63
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")

help_xcode_depends()
64

65
message("Building project first time")
66 67 68 69
try_compile(RESULT 
  ${BuildDepends_BINARY_DIR}/Project
  ${BuildDepends_SOURCE_DIR}/Project
  testRebuild
70
  CMAKE_FLAGS ${_cmake_options}
71
  OUTPUT_VARIABLE OUTPUT)
72
if(HELP_XCODE)
73 74 75 76 77 78 79 80 81 82
  try_compile(RESULT 
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
  try_compile(RESULT 
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
83
endif(HELP_XCODE)
84

85
message("Output from first build:\n${OUTPUT}")
86
if(NOT RESULT)
87
  message(SEND_ERROR "Could not build test project (1)!")
88 89
endif(NOT RESULT)

90 91 92 93 94 95 96 97
set(bar ${BuildDepends_BINARY_DIR}/Project/bar${CMAKE_EXECUTABLE_SUFFIX})
if(EXISTS 
    "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
  set(bar 
    "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
endif(EXISTS 
  "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
98 99 100 101 102 103 104 105
set(zot ${BuildDepends_BINARY_DIR}/Project/zot${CMAKE_EXECUTABLE_SUFFIX})
if(EXISTS 
    "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
  set(zot 
    "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
endif(EXISTS 
  "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
106 107 108

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

112
if("${out}" STREQUAL "foo ")
113
  message("Worked!")
114
else("${out}" STREQUAL "foo ")
115
  message(SEND_ERROR "Project did not initially build properly: ${out}")
116
endif("${out}" STREQUAL "foo ")
117

118 119 120 121 122
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}\"")

123 124
set(VALUE_UNCHANGED "[zot] [zot_custom] [zot_macro_dir] [zot_macro_tgt] ")
if("${out}" STREQUAL "${VALUE_UNCHANGED}")
125
  message("Worked!")
126
else("${out}" STREQUAL "${VALUE_UNCHANGED}")
127
  message(SEND_ERROR "Project did not initially build properly: ${out}")
128
endif("${out}" STREQUAL "${VALUE_UNCHANGED}")
129

130 131 132 133 134
message("Waiting 3 seconds...")
# any additional argument will cause ${bar} to wait forever
execute_process(COMMAND ${bar} -infinite TIMEOUT 3 OUTPUT_VARIABLE out)

message("Modifying Project/foo.cxx")
135
write_file(${BuildDepends_BINARY_DIR}/Project/foo.cxx
136
  "const char* foo() { return \"foo changed\";}" )
137 138
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot.hxx.in
  "static const char* zot = \"zot changed\";\n")
139 140
file(WRITE ${BuildDepends_BINARY_DIR}/Project/zot_custom.hxx.in
  "static const char* zot_custom = \"zot_custom changed\";\n")
141 142 143 144 145
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")

146 147 148 149
if(TEST_LINK_DEPENDS)
  file(WRITE ${TEST_LINK_DEPENDS} "2")
endif()

150
help_xcode_depends()
151 152

message("Building project second time")
153 154 155 156
try_compile(RESULT 
  ${BuildDepends_BINARY_DIR}/Project
  ${BuildDepends_SOURCE_DIR}/Project
  testRebuild
157
  CMAKE_FLAGS ${_cmake_options}
158 159
  OUTPUT_VARIABLE OUTPUT)

160
# Xcode is in serious need of help here
161
if(HELP_XCODE OR HELP_NINJA)
162 163 164 165 166 167 168 169 170 171
  try_compile(RESULT 
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
  try_compile(RESULT 
    ${BuildDepends_BINARY_DIR}/Project
    ${BuildDepends_SOURCE_DIR}/Project
    testRebuild
    OUTPUT_VARIABLE OUTPUT)
172
endif()
173

174
message("Output from second build:\n${OUTPUT}")
175
if(NOT RESULT)
176
  message(SEND_ERROR "Could not build test project (2)!")
177
endif(NOT RESULT)
178 179 180 181 182
if(EXISTS 
    "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
endif(EXISTS 
  "${BuildDepends_BINARY_DIR}/Project/Debug/bar${CMAKE_EXECUTABLE_SUFFIX}")
183 184 185 186 187
if(EXISTS 
    "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}" )
  message("found debug")
endif(EXISTS 
  "${BuildDepends_BINARY_DIR}/Project/Debug/zot${CMAKE_EXECUTABLE_SUFFIX}")
188

189 190
message("Running ${bar}  ")
execute_process(COMMAND ${bar} OUTPUT_VARIABLE out RESULT_VARIABLE runResult)
191
string(REGEX REPLACE "[\r\n]" " " out "${out}")
192
message("Run result: ${runResult} Output: \"${out}\"")
193

194
if("${out}" STREQUAL "foo changed ")
195
  message("Worked!")
196
else("${out}" STREQUAL "foo changed ")
197
  message(SEND_ERROR "Project did not rebuild properly!")
198
endif("${out}" STREQUAL "foo changed ")
199 200 201 202 203 204

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}\"")

205 206 207 208
set(VALUE_CHANGED
  "[zot changed] [zot_custom changed] [zot_macro_dir changed] [zot_macro_tgt changed] "
  )
if("${out}" STREQUAL "${VALUE_CHANGED}")
209
  message("Worked!")
210
else("${out}" STREQUAL "${VALUE_CHANGED}")
211
  message(SEND_ERROR "Project did not rebuild properly!")
212
endif("${out}" STREQUAL "${VALUE_CHANGED}")
213 214 215 216 217 218 219 220 221 222 223 224 225

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}
")
  endif()
endif()