Updated formatting of documentation plus a little reorganization.
[cmake.git] / Tests / SimpleInstallS2 / CMakeLists.txt
blob7a249d870909b116efcbaae9a54d9c667ff01b1e
1 cmake_minimum_required (VERSION 2.6)
2 PROJECT (TestSimpleInstall)
3 SET(CMAKE_VERBOSE_MAKEFILE 1)
4 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY
5   "${TestSimpleInstall_BINARY_DIR}/bin")
6 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
7   "${TestSimpleInstall_BINARY_DIR}/lib/static")
8 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY
9   "${TestSimpleInstall_BINARY_DIR}/lib")
11 # Skip generating the rpath pointing at the build tree to make sure
12 # the executable is installed with the proper rpath in the install
13 # tree.
14 SET(CMAKE_SKIP_BUILD_RPATH 1)
16 # Make sure the executable can run from the install tree.
17 SET(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
19 # Skip the dependency that causes a build when installing.  This
20 # avoids infinite loops when the post-build rule below installs.
21 SET(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY 1)
22 SET(CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY 1)
24 SET(CMAKE_DEBUG_POSTFIX "_test_debug_postfix")
26 SET(EXTRA_INSTALL_FLAGS)
27 MESSAGE("Extra install: ${EXTRA_INSTALL_FLAGS}")
29 IF(STAGE2)
30   SET(LIBPATHS
31     ${CMAKE_INSTALL_PREFIX}/MyTest/lib/static
32     ${CMAKE_INSTALL_PREFIX}/MyTest/lib
33     )
34   SET(t1NAMES test1 test1${CMAKE_DEBUG_POSTFIX} test1rel)
35   SET(t2NAMES test2 test2${CMAKE_DEBUG_POSTFIX})
36   SET(t4NAMES test4out test4out${CMAKE_DEBUG_POSTFIX})
38   # Make sure the install script ran.
39   SET(CMAKE_INSTALL_SCRIPT_DID_RUN 0)
40   INCLUDE(${CMAKE_INSTALL_PREFIX}/MyTest/InstallScriptOut.cmake OPTIONAL)
41   IF(CMAKE_INSTALL_SCRIPT_DID_RUN)
42     MESSAGE(STATUS "Stage 1 did run install script 2.")
43   ELSE(CMAKE_INSTALL_SCRIPT_DID_RUN)
44     MESSAGE(SEND_ERROR "Stage 1 did not run install script 2.")
45   ENDIF(CMAKE_INSTALL_SCRIPT_DID_RUN)
47   IF(CYGWIN OR MINGW)
48     SET(LIBPATHS ${LIBPATHS} "${CMAKE_INSTALL_PREFIX}/MyTest/bin")
49   ENDIF(CYGWIN OR MINGW)
50   MESSAGE("Search for library in: ${LIBPATHS}")
52   SET(TEST1_LIBRARY "TEST1_LIBRARY-NOTFOUND" CACHE FILEPATH "Force find." FORCE)
53   SET(TEST2_LIBRARY "TEST2_LIBRARY-NOTFOUND" CACHE FILEPATH "Force find." FORCE)
54   SET(TEST4_LIBRARY "TEST4_LIBRARY-NOTFOUND" CACHE FILEPATH "Force find." FORCE)
56   FIND_LIBRARY(TEST1_LIBRARY 
57     NAMES ${t1NAMES}
58     PATHS ${LIBPATHS}
59     DOC "First library")
60   FIND_LIBRARY(TEST2_LIBRARY 
61     NAMES ${t2NAMES}
62     PATHS ${LIBPATHS}
63     DOC "Second library")
64   FIND_LIBRARY(TEST4_LIBRARY 
65     NAMES ${t4NAMES}
66     PATHS ${LIBPATHS}
67     DOC "Fourth library")
69   # Test importing a library found on disk.
70   ADD_LIBRARY(lib_test4 UNKNOWN IMPORTED)
71   SET_PROPERTY(TARGET lib_test4 PROPERTY IMPORTED_LOCATION ${TEST4_LIBRARY})
73   INCLUDE_DIRECTORIES(${CMAKE_INSTALL_PREFIX}/MyTest/include)
74   ADD_EXECUTABLE (SimpleInstExeS2 inst2.cxx foo.c foo.h)
75   TARGET_LINK_LIBRARIES(SimpleInstExeS2 ${TEST1_LIBRARY} ${TEST2_LIBRARY} lib_test4)
76   SET(install_target SimpleInstExeS2)
78   IF("${TEST1_LIBRARY}" MATCHES "static")
79     MESSAGE(STATUS "test1 correctly found in lib/static")
80   ELSE("${TEST1_LIBRARY}" MATCHES "static")
81     MESSAGE(SEND_ERROR "test1 not found in lib/static!")
82   ENDIF("${TEST1_LIBRARY}" MATCHES "static")
84   # Check for failure of configuration-specific installation.
85   IF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/include/Release/lib1debug.h")
86     MESSAGE(FATAL_ERROR "Debug-configuration file installed for Release!")
87   ENDIF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/include/Release/lib1debug.h")
88   IF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/include/Debug/lib1release.h")
89     MESSAGE(FATAL_ERROR "Release-configuration file installed for Debug!")
90   ENDIF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/include/Debug/lib1release.h")
92   # Check for failure of directory installation.
93   IF(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/TSD.h")
94     MESSAGE(FATAL_ERROR "Directory installation did not install TSD.h")
95   ENDIF(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/TSD.h")
96   IF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/CVS")
97     MESSAGE(FATAL_ERROR "Directory installation installed CVS directory.")
98   ENDIF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/CVS")
99   IF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/CVS")
100     MESSAGE(FATAL_ERROR "Directory installation installed CVS directory.")
101   ENDIF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/CVS")
102   IF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/CMakeLists.txt")
103     MESSAGE(FATAL_ERROR "Directory installation installed CMakeLists.txt.")
104   ENDIF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/CMakeLists.txt")
105   IF(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/alt/TestSubDir/TSD.h")
106     MESSAGE(FATAL_ERROR "Directory installation did not install alternate TSD.h")
107   ENDIF(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/alt/TestSubDir/TSD.h")
108   IF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/alt/TestSubDir/TSD.cxx")
109     MESSAGE(FATAL_ERROR "Directory installation installed alternate TSD.cxx")
110   ENDIF(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/alt/TestSubDir/TSD.cxx")
112   # Check that scripts properly installed.
113   IF(WIN32 AND NOT CYGWIN)
114     SET(BAT .bat)
115   ELSE(WIN32 AND NOT CYGWIN)
116     SET(BAT)
117   ENDIF(WIN32 AND NOT CYGWIN)
118   FOREACH(loc share share/old1 share/old2 share/old3 share/alt)
119     SET(CUR_SCRIPT "${CMAKE_INSTALL_PREFIX}/MyTest/${loc}/sample_script${BAT}")
120     EXECUTE_PROCESS(
121       COMMAND ${CUR_SCRIPT}
122       RESULT_VARIABLE SAMPLE_SCRIPT_RESULT
123       OUTPUT_VARIABLE SAMPLE_SCRIPT_OUTPUT
124       )
125     IF(NOT "${SAMPLE_SCRIPT_RESULT}" MATCHES "^0$")
126       MESSAGE(FATAL_ERROR
127         "Sample script [${CUR_SCRIPT}] failed: [${SAMPLE_SCRIPT_RESULT}]")
128     ENDIF(NOT "${SAMPLE_SCRIPT_RESULT}" MATCHES "^0$")
129     IF(NOT "${SAMPLE_SCRIPT_OUTPUT}" MATCHES "Sample Script Output")
130       MESSAGE(FATAL_ERROR
131         "Bad sample script [${CUR_SCRIPT}] output: [${SAMPLE_SCRIPT_OUTPUT}]")
132     ENDIF(NOT "${SAMPLE_SCRIPT_OUTPUT}" MATCHES "Sample Script Output")
133   ENDFOREACH(loc)
135   # Check for failure of empty directory installation.
136   IF(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/empty")
137     MESSAGE(FATAL_ERROR "Empty directory installation did not install.")
138   ENDIF(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/empty")
139   FILE(GLOB EMPTY_FILES "${CMAKE_INSTALL_PREFIX}/MyTest/share/empty/*")
140   IF(EMPTY_FILES)
141     MESSAGE(FATAL_ERROR "Empty directory installed [${EMPTY_FILES}].")
142   ENDIF(EMPTY_FILES)
144   # Make sure the test executable can run from the install tree.
145   SET_TARGET_PROPERTIES(SimpleInstExeS2 PROPERTIES
146     INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
148   INSTALL_TARGETS(/MyTest/bin SimpleInstExeS2)
150 # try to import the exported targets again
151   SET(SimpleInstallS1_DIR ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
152   FIND_PACKAGE(SimpleInstallS1 REQUIRED)
153   GET_TARGET_PROPERTY(simpleInstallImported S1_SimpleInstall IMPORTED)
154   IF(NOT simpleInstallImported)
155     MESSAGE(FATAL_ERROR "Target S1_SimpleInstall could not be imported")
156   ENDIF(NOT simpleInstallImported)
158 ELSE(STAGE2)
159   # Wipe out the install directory to do a fresh test.
160   FILE(REMOVE_RECURSE ${CMAKE_INSTALL_PREFIX}/MyTest)
162   # this is stage 1, so create libraries and modules and install everything
163   ADD_LIBRARY(test1 STATIC lib1.cxx)
164   ADD_LIBRARY(test2 SHARED lib2.cxx)
165   ADD_LIBRARY(test3 MODULE lib3.cxx)
166   ADD_LIBRARY(test4 SHARED lib4.cxx)
168   # Test <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME properties.
169   SET_PROPERTY(TARGET test4 PROPERTY ARCHIVE_OUTPUT_NAME test4out)
170   SET_PROPERTY(TARGET test4 PROPERTY LIBRARY_OUTPUT_NAME test4out)
172   ADD_EXECUTABLE (SimpleInstall inst.cxx foo.c foo.h)
173   TARGET_LINK_LIBRARIES(SimpleInstall test1 test2 test4)
174   SET(install_target SimpleInstall)
176   # Make sure the test executable can run from the install tree.
177   SET_TARGET_PROPERTIES(SimpleInstall PROPERTIES
178     INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
180   # Test per-configuration output name.
181   SET_TARGET_PROPERTIES(test1 PROPERTIES RELEASE_OUTPUT_NAME test1rel)
182   SET_TARGET_PROPERTIES(test2 PROPERTIES PUBLIC_HEADER foo.h)
184   IF(CMAKE_GENERATOR MATCHES "Makefiles")
185     ADD_SUBDIRECTORY(TestSubDir)
186     ADD_DEPENDENCIES(SimpleInstall TSD)
187   ENDIF(CMAKE_GENERATOR MATCHES "Makefiles")
189   ADD_DEPENDENCIES(SimpleInstall test3)
190   ADD_DEPENDENCIES(test2 test3)
191   ADD_DEPENDENCIES(test4 test2)
193   INSTALL(TARGETS SimpleInstall test1 test2 test3 EXPORT SimpleInstallS1
194     RUNTIME DESTINATION MyTest/bin        COMPONENT Runtime     # .exe, .dll
195     LIBRARY DESTINATION MyTest/lib        COMPONENT Runtime     # .so, mod.dll
196     ARCHIVE DESTINATION MyTest/lib/static COMPONENT Development # .a, .lib
197     PUBLIC_HEADER DESTINATION MyTest/include COMPONENT Development 
198     )
200   INSTALL(TARGETS test4 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
201     RUNTIME DESTINATION MyTest/bin
202     LIBRARY DESTINATION MyTest/lib
203     ARCHIVE DESTINATION MyTest/lib/static
204     OPTIONAL # for coverage...target should always exist
205     )
206   INSTALL(FILES lib1.h DESTINATION MyTest/include/foo)
207   INSTALL(FILES lib2.h
208     DESTINATION MyTest/include/foo
209     COMPONENT Development
210     PERMISSIONS OWNER_READ OWNER_WRITE
211     RENAME lib2renamed.h
212     )
214   # Test old-style install commands.
215   INSTALL_FILES(/MyTest/include FILES lib3.h)
216   INSTALL_FILES(/MyTest/include/old .h lib3)
217   INSTALL_FILES(/MyTest/include/old "^lib2.h$")
218   INSTALL_PROGRAMS(/MyTest/share/old1 FILES
219     scripts/sample_script scripts/sample_script.bat)
220   INSTALL_PROGRAMS(/MyTest/share/old2
221     scripts/sample_script scripts/sample_script.bat)
223 # "export" the targets collected in "SimpleInstallS1"
224   INSTALL(EXPORT SimpleInstallS1 FILE SimpleInstallS1Config.cmake 
225                                  DESTINATION MyTest/lib 
226                                  NAMESPACE S1_ )
228   EXPORT(TARGETS SimpleInstall test1 test2 test3 
229          FILE "${CMAKE_CURRENT_BINARY_DIR}/SimpleInstallS1Config.cmake"
230          NAMESPACE S2_ )
232   ADD_SUBDIRECTORY(scripts)
234   # Test optional installation.
235   INSTALL(FILES does_not_exist.h DESTINATION MyTest/include/foo OPTIONAL)
237   # Test configuration-specific installation.
238   INSTALL(FILES lib1.h RENAME lib1release.h CONFIGURATIONS Release
239     DESTINATION MyTest/include/Release
240     )
241   INSTALL(FILES lib1.h RENAME lib1debug.h CONFIGURATIONS Debug
242     DESTINATION MyTest/include/Debug
243     )
245   # Test directory installation.
246   FILE(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/MyTest/share/CVS")
247   FILE(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/CVS")
248   INSTALL(
249     DIRECTORY TestSubDir scripts/ DESTINATION MyTest/share
250     FILE_PERMISSIONS OWNER_READ OWNER_WRITE
251     DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
252                           GROUP_READ GROUP_EXECUTE
253     PATTERN "CVS" EXCLUDE
254     REGEX "\\.txt$" EXCLUDE
255     PATTERN "scripts/*" PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
256     )
258   # Alternate directory installation for coverage.
259   INSTALL(
260     DIRECTORY scripts/ DESTINATION MyTest/share/alt
261     COMPONENT Development
262     USE_SOURCE_PERMISSIONS
263     PATTERN "CVS" EXCLUDE
264     REGEX "\\.txt$" EXCLUDE
265     )
266   INSTALL(
267     DIRECTORY TestSubDir DESTINATION MyTest/share/alt
268     FILE_PERMISSIONS OWNER_READ OWNER_WRITE
269     DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
270                           GROUP_READ GROUP_EXECUTE
271     FILES_MATCHING PATTERN "*.h"
272     )
274   # Test empty directory installation.
275   INSTALL(DIRECTORY DESTINATION MyTest/share/empty)
277   # Test optional directory installation.
278   INSTALL(DIRECTORY does-not-exist DESTINATION MyTest/share OPTIONAL)
280   # Test user-specified install scripts, with and without COMPONENT.
281   INSTALL(
282     SCRIPT InstallScript1.cmake
283     CODE "SET(INSTALL_CODE_DID_RUN 1)"
284     SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/InstallScript2.cmake
285     )
286   INSTALL(
287     SCRIPT InstallScript3.cmake
288     CODE "SET(INSTALL_CODE_WITH_COMPONENT_DID_RUN 1)"
289     SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/InstallScript4.cmake
290     COMPONENT Development
291     )
292   SET_DIRECTORY_PROPERTIES(PROPERTIES
293     ADDITIONAL_MAKE_CLEAN_FILES
294     "${CMAKE_INSTALL_PREFIX}/InstallScriptOut.cmake;${CMAKE_INSTALL_PREFIX}/InstallScript4Out.cmake")
296   SET_TARGET_PROPERTIES(SimpleInstall PROPERTIES OUTPUT_NAME SimpleInstExe)
297   # Disable VERSION test until it is implemented in the XCode generator.
298   IF(NOT XCODE)
299     SET_TARGET_PROPERTIES(SimpleInstall PROPERTIES VERSION 1.2)
300   ENDIF(NOT XCODE)
301   SET_TARGET_PROPERTIES(SimpleInstall PROPERTIES PRE_INSTALL_SCRIPT 
302     ${CMAKE_CURRENT_SOURCE_DIR}/PreInstall.cmake)
303   SET_TARGET_PROPERTIES(SimpleInstall PROPERTIES POST_INSTALL_SCRIPT 
304     ${CMAKE_CURRENT_SOURCE_DIR}/PostInstall.cmake)
305   SET_TARGET_PROPERTIES(test4 PROPERTIES VERSION 1.2 SOVERSION 3
306         INSTALL_NAME_DIR @executable_path/../lib)
307 ENDIF(STAGE2)
309 IF(CMAKE_CONFIGURATION_TYPES)
310   SET(SI_CONFIG -C ${CMAKE_CFG_INTDIR})
311 ELSE(CMAKE_CONFIGURATION_TYPES)
312   SET(SI_CONFIG)
313 ENDIF(CMAKE_CONFIGURATION_TYPES)
315 # Dummy test of CPack
316 SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Test of packaging with cpack")
317 SET(CPACK_PACKAGE_VENDOR "Kitware")
318 SET(CPACK_INSTALL_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/PackageScript.cmake")
320 IF(WIN32 AND NOT UNIX)
321   FIND_PROGRAM(NSIS_MAKENSIS NAMES makensis
322     PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS]
323     DOC "Where is makensis.exe located"
324     )
325   IF(NOT NSIS_MAKENSIS)
326     SET(CPACK_GENERATOR TGZ)
327   ENDIF(NOT NSIS_MAKENSIS)
328 ENDIF(WIN32 AND NOT UNIX)
329 IF(UNIX AND NOT APPLE)
330   SET(CPACK_GENERATOR "TGZ;STGZ;TZ")
331   #  FIND_PROGRAM(found_compress
332   #    NAMES compress)
333   #  IF(found_compress)
334   #    FIND_PROGRAM(file_command NAMES file)
335   #    IF(NOT file_command)
336   #      set(file_command file)
337   #    ENDIF(NOT file_command)
338   #    EXECUTE_PROCESS(COMMAND ${file_command} ${found_compress}
339   #      OUTPUT_VARIABLE output)
340   #    set(SKIP_TZ FALSE)
341   #    if("${output}" MATCHES "script")
342   #      set(SKIP_TZ TRUE)
343   #    endif("${output}" MATCHES "script")
344   #    if("${output}" MATCHES "dummy.sh")
345   #      set(SKIP_TZ TRUE)
346   #    endif("${output}" MATCHES "dummy.sh")
347   #    if(NOT SKIP_TZ)
348   #      message("compress found and it was not a script")
349   #      message("output from file command: [${output}]")
350   #      SET(CPACK_GENERATOR "${CPACK_GENERATOR};TZ")
351   #    else(NOT SKIP_TZ)
352   #      message("compress found, but it was a script so dont use it")
353   #      message("output from file command: [${output}]")
354   #    endif(NOT SKIP_TZ)
355   #  ENDIF(found_compress)
356   FIND_PROGRAM(found_bz2
357     NAMES bzip2)
358   IF(found_bz2)
359     SET(CPACK_GENERATOR "${CPACK_GENERATOR};TBZ2")
360   ENDIF(found_bz2)
361 ENDIF(UNIX AND NOT APPLE)
363 SET(CPACK_PACKAGE_EXECUTABLES "SimpleInstall" "Simple Install")
364 SET(CMAKE_INSTALL_MFC_LIBRARIES 1)
365 SET(CMAKE_INSTALL_DEBUG_LIBRARIES 1)
366 INCLUDE(InstallRequiredSystemLibraries)
367 INCLUDE(CPack)
369 IF(CTEST_TEST_CPACK)
370   SET(PACKAGE_TARGET --build-target package)
371 ELSE(CTEST_TEST_CPACK)
372   SET(PACKAGE_TARGET)
373 ENDIF(CTEST_TEST_CPACK)
375 ADD_CUSTOM_COMMAND(
376   TARGET ${install_target}
377   POST_BUILD
378   COMMAND ${CMAKE_CTEST_COMMAND}
379   ARGS ${SI_CONFIG}
380   --build-and-test
381   ${CMAKE_SOURCE_DIR}
382   ${CMAKE_BINARY_DIR}
383   --build-generator ${CMAKE_GENERATOR}
384   --build-project ${PROJECT_NAME}
385   --build-makeprogram ${CMAKE_MAKE_PROGRAM}
386   --build-noclean
387   --build-target install
388   ${PACKAGE_TARGET}
389   COMMENT "Install Project"
390   )