CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. cmake_minimum_required (VERSION 3.9)
  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")
  10. # Skip generating the rpath pointing at the build tree to make sure
  11. # the executable is installed with the proper rpath in the install
  12. # tree.
  13. set(CMAKE_SKIP_BUILD_RPATH 1)
  14. # Make sure the executable can run from the install tree.
  15. set(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
  16. # Skip the dependency that causes a build when installing. This
  17. # avoids infinite loops when the post-build rule below installs.
  18. set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY 1)
  19. set(CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY 1)
  20. set(CMAKE_DEBUG_POSTFIX "_test_debug_postfix")
  21. set(EXTRA_INSTALL_FLAGS)
  22. message("Extra install: ${EXTRA_INSTALL_FLAGS}")
  23. if(STAGE2)
  24. set(LIBPATHS
  25. ${CMAKE_INSTALL_PREFIX}/MyTest/lib/static
  26. ${CMAKE_INSTALL_PREFIX}/MyTest/lib
  27. )
  28. set(t1NAMES test1 test1${CMAKE_DEBUG_POSTFIX} test1rel)
  29. set(t2NAMES test2 test2${CMAKE_DEBUG_POSTFIX})
  30. set(t4NAMES test4out test4out${CMAKE_DEBUG_POSTFIX})
  31. # Make sure the install script ran.
  32. set(CMAKE_INSTALL_SCRIPT_DID_RUN 0)
  33. include(${CMAKE_INSTALL_PREFIX}/MyTest/InstallScriptOut.cmake OPTIONAL)
  34. if(CMAKE_INSTALL_SCRIPT_DID_RUN)
  35. message(STATUS "Stage 1 did run install script 2.")
  36. else()
  37. message(SEND_ERROR "Stage 1 did not run install script 2.")
  38. endif()
  39. if(CYGWIN OR MINGW)
  40. set(LIBPATHS ${LIBPATHS} "${CMAKE_INSTALL_PREFIX}/MyTest/bin")
  41. endif()
  42. message("Search for library in: ${LIBPATHS}")
  43. set(TEST1_LIBRARY "TEST1_LIBRARY-NOTFOUND" CACHE FILEPATH "Force find." FORCE)
  44. set(TEST2_LIBRARY "TEST2_LIBRARY-NOTFOUND" CACHE FILEPATH "Force find." FORCE)
  45. set(TEST4_LIBRARY "TEST4_LIBRARY-NOTFOUND" CACHE FILEPATH "Force find." FORCE)
  46. find_library(TEST1_LIBRARY
  47. NAMES ${t1NAMES}
  48. PATHS ${LIBPATHS}
  49. DOC "First library")
  50. find_library(TEST2_LIBRARY
  51. NAMES ${t2NAMES}
  52. PATHS ${LIBPATHS}
  53. DOC "Second library")
  54. find_library(TEST4_LIBRARY
  55. NAMES ${t4NAMES}
  56. PATHS ${LIBPATHS}
  57. DOC "Fourth library")
  58. # Test importing a library found on disk.
  59. add_library(lib_test4 UNKNOWN IMPORTED)
  60. set_property(TARGET lib_test4 PROPERTY IMPORTED_LOCATION ${TEST4_LIBRARY})
  61. include_directories(${CMAKE_INSTALL_PREFIX}/MyTest/include)
  62. add_executable (SimpleInstExeS2 inst2.cxx foo.c foo.h)
  63. target_link_libraries(SimpleInstExeS2 ${TEST1_LIBRARY} ${TEST2_LIBRARY} lib_test4)
  64. set(install_target SimpleInstExeS2)
  65. if("${TEST1_LIBRARY}" MATCHES "static")
  66. message(STATUS "test1 correctly found in lib/static")
  67. else()
  68. message(SEND_ERROR "test1 not found in lib/static!")
  69. endif()
  70. # Check for failure of configuration-specific installation.
  71. if(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/include/Release/lib1debug.h")
  72. message(FATAL_ERROR "Debug-configuration file installed for Release!")
  73. endif()
  74. if(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/include/Debug/lib1release.h")
  75. message(FATAL_ERROR "Release-configuration file installed for Debug!")
  76. endif()
  77. # Check for failure of directory installation.
  78. if(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/TSD.h")
  79. message(FATAL_ERROR "Directory installation did not install TSD.h")
  80. endif()
  81. if(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/CVS")
  82. message(FATAL_ERROR "Directory installation installed CVS directory.")
  83. endif()
  84. if(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/CVS")
  85. message(FATAL_ERROR "Directory installation installed CVS directory.")
  86. endif()
  87. if(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/CMakeLists.txt")
  88. message(FATAL_ERROR "Directory installation installed CMakeLists.txt.")
  89. endif()
  90. if(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/alt/TestSubDir/TSD.h")
  91. message(FATAL_ERROR "Directory installation did not install alternate TSD.h")
  92. endif()
  93. if(EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/alt/TestSubDir/TSD.cxx")
  94. message(FATAL_ERROR "Directory installation installed alternate TSD.cxx")
  95. endif()
  96. # Check that scripts properly installed.
  97. if(WIN32 AND NOT CYGWIN)
  98. set(BAT .bat)
  99. else()
  100. set(BAT)
  101. endif()
  102. foreach(loc share share/old1 share/old2 share/old3 share/alt)
  103. set(CUR_SCRIPT "${CMAKE_INSTALL_PREFIX}/MyTest/${loc}/sample_script${BAT}")
  104. execute_process(
  105. COMMAND ${CUR_SCRIPT}
  106. RESULT_VARIABLE SAMPLE_SCRIPT_RESULT
  107. OUTPUT_VARIABLE SAMPLE_SCRIPT_OUTPUT
  108. )
  109. if(NOT "${SAMPLE_SCRIPT_RESULT}" STREQUAL "0")
  110. message(FATAL_ERROR
  111. "Sample script [${CUR_SCRIPT}] failed: [${SAMPLE_SCRIPT_RESULT}]")
  112. endif()
  113. if(NOT "${SAMPLE_SCRIPT_OUTPUT}" MATCHES "Sample Script Output")
  114. message(FATAL_ERROR
  115. "Bad sample script [${CUR_SCRIPT}] output: [${SAMPLE_SCRIPT_OUTPUT}]")
  116. endif()
  117. endforeach()
  118. # Check for failure of empty directory installation.
  119. if(NOT EXISTS "${CMAKE_INSTALL_PREFIX}/MyTest/share/empty")
  120. message(FATAL_ERROR "Empty directory installation did not install.")
  121. endif()
  122. file(GLOB EMPTY_FILES "${CMAKE_INSTALL_PREFIX}/MyTest/share/empty/*")
  123. if(EMPTY_FILES)
  124. message(FATAL_ERROR "Empty directory installed [${EMPTY_FILES}].")
  125. endif()
  126. # Make sure the test executable can run from the install tree.
  127. set_target_properties(SimpleInstExeS2 PROPERTIES
  128. INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
  129. install_targets(/MyTest/bin SimpleInstExeS2)
  130. # try to import the exported targets again
  131. set(SimpleInstallS1_DIR ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
  132. find_package(SimpleInstallS1 REQUIRED)
  133. get_target_property(simpleInstallImported S1_SimpleInstall IMPORTED)
  134. if(NOT simpleInstallImported)
  135. message(FATAL_ERROR "Target S1_SimpleInstall could not be imported")
  136. endif()
  137. else()
  138. # Wipe out the install directory to do a fresh test.
  139. file(REMOVE_RECURSE ${CMAKE_INSTALL_PREFIX}/MyTest)
  140. # this is stage 1, so create libraries and modules and install everything
  141. add_library(test1 STATIC lib1.cxx)
  142. add_library(test2 SHARED lib2.cxx)
  143. add_library(test3 MODULE lib3.cxx)
  144. add_library(test4 SHARED lib4.cxx)
  145. # Test <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME properties.
  146. set_property(TARGET test4 PROPERTY ARCHIVE_OUTPUT_NAME test4out)
  147. set_property(TARGET test4 PROPERTY LIBRARY_OUTPUT_NAME test4out)
  148. add_executable (SimpleInstall inst.cxx foo.c foo.h)
  149. target_link_libraries(SimpleInstall test1 test2 test4)
  150. set(install_target SimpleInstall)
  151. set_target_properties(SimpleInstall PROPERTIES OUTPUT_NAME SimpleInstExe)
  152. # Disable VERSION test until it is implemented in the Xcode generator.
  153. if(NOT XCODE)
  154. set_target_properties(SimpleInstall PROPERTIES VERSION 1.2)
  155. endif()
  156. # Make sure the test executable can run from the install tree.
  157. set_target_properties(SimpleInstall PROPERTIES
  158. INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/MyTest/lib)
  159. # Test per-configuration output name.
  160. set_target_properties(test1 PROPERTIES RELEASE_OUTPUT_NAME test1rel)
  161. set_target_properties(test2 PROPERTIES PUBLIC_HEADER foo.h)
  162. if(CMAKE_GENERATOR MATCHES "Makefiles")
  163. add_subdirectory(TestSubDir)
  164. add_dependencies(SimpleInstall TSD)
  165. endif()
  166. add_dependencies(SimpleInstall test3)
  167. add_dependencies(test2 test3)
  168. add_dependencies(test4 test2)
  169. install(TARGETS SimpleInstall test1 test2 test3 EXPORT SimpleInstallS1
  170. RUNTIME DESTINATION MyTest/bin COMPONENT Runtime # .exe, .dll
  171. LIBRARY DESTINATION MyTest/lib COMPONENT Runtime # .so, mod.dll
  172. ARCHIVE DESTINATION MyTest/lib/static COMPONENT Development # .a, .lib
  173. PUBLIC_HEADER DESTINATION MyTest/include COMPONENT Development
  174. )
  175. install(TARGETS test4 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
  176. RUNTIME DESTINATION MyTest/bin
  177. LIBRARY DESTINATION MyTest/lib
  178. ARCHIVE DESTINATION MyTest/lib/static
  179. OPTIONAL # for coverage...target should always exist
  180. )
  181. install(FILES lib1.h DESTINATION MyTest/include/$<1:foo>$<0:/wrong>)
  182. install(FILES lib2.h
  183. DESTINATION $<1:MyTest/include/foo>$<0:/wrong>
  184. COMPONENT Development
  185. PERMISSIONS OWNER_READ OWNER_WRITE
  186. RENAME lib2renamed.h
  187. )
  188. # Test old-style install commands.
  189. install_files(/MyTest/include FILES lib3.h)
  190. install_files(/MyTest/include/old .h lib3)
  191. install_files(/MyTest/include/old "^lib2.h$")
  192. install_programs(/MyTest/share/old1 FILES
  193. scripts/sample_script scripts/sample_script.bat)
  194. install_programs(/MyTest/share/old2
  195. scripts/sample_script scripts/sample_script.bat)
  196. # "export" the targets collected in "SimpleInstallS1"
  197. install(EXPORT SimpleInstallS1 FILE SimpleInstallS1Config.cmake
  198. DESTINATION MyTest/lib
  199. NAMESPACE S1_ )
  200. export(TARGETS SimpleInstall test1 test2 test3
  201. FILE "${CMAKE_CURRENT_BINARY_DIR}/SimpleInstallS1Config.cmake"
  202. NAMESPACE S2_ )
  203. add_subdirectory(scripts)
  204. # Test optional installation.
  205. install(FILES does_not_exist.h DESTINATION MyTest/include/foo OPTIONAL)
  206. # Test configuration-specific installation.
  207. install(FILES lib1.h RENAME lib1release.h CONFIGURATIONS Release
  208. DESTINATION MyTest/include/Release
  209. )
  210. install(FILES lib1.h RENAME lib1debug.h CONFIGURATIONS Debug
  211. DESTINATION MyTest/include/Debug
  212. )
  213. # Test directory installation.
  214. file(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/MyTest/share/CVS")
  215. file(REMOVE_RECURSE "${CMAKE_INSTALL_PREFIX}/MyTest/share/TestSubDir/CVS")
  216. install(
  217. DIRECTORY TestSubDir $<1:scripts/>$<0:/wrong> DESTINATION $<1:MyTest/share>$<0:/wrong>
  218. FILE_PERMISSIONS OWNER_READ OWNER_WRITE
  219. DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
  220. GROUP_READ GROUP_EXECUTE
  221. PATTERN "CVS" EXCLUDE
  222. REGEX "\\.txt$" EXCLUDE
  223. PATTERN "scripts/*" PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
  224. )
  225. # Alternate directory installation for coverage.
  226. install(
  227. DIRECTORY $<1:scripts/>$<0:/wrong> DESTINATION $<1:MyTest/share/alt>$<0:/wrong>
  228. COMPONENT Development
  229. USE_SOURCE_PERMISSIONS
  230. PATTERN "CVS" EXCLUDE
  231. REGEX "\\.txt$" EXCLUDE
  232. )
  233. install(
  234. DIRECTORY TestSubDir DESTINATION $<1:MyTest/share/alt>$<0:/wrong>
  235. FILE_PERMISSIONS OWNER_READ OWNER_WRITE
  236. DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
  237. GROUP_READ GROUP_EXECUTE
  238. FILES_MATCHING PATTERN "*.h"
  239. )
  240. # Test empty directory installation.
  241. install(DIRECTORY DESTINATION MyTest/share/empty)
  242. # Test optional directory installation.
  243. install(DIRECTORY does-not-exist DESTINATION MyTest/share OPTIONAL)
  244. # Test user-specified install scripts, with and without COMPONENT.
  245. install(
  246. SCRIPT InstallScript1.cmake
  247. CODE "set(INSTALL_CODE_DID_RUN 1)"
  248. SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/InstallScript2.cmake
  249. )
  250. install(
  251. SCRIPT InstallScript3.cmake
  252. CODE "set(INSTALL_CODE_WITH_COMPONENT_DID_RUN 1)"
  253. SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/InstallScript4.cmake
  254. COMPONENT Development
  255. )
  256. set_directory_properties(PROPERTIES
  257. ADDITIONAL_MAKE_CLEAN_FILES
  258. "${CMAKE_INSTALL_PREFIX}/InstallScriptOut.cmake;${CMAKE_INSTALL_PREFIX}/InstallScript4Out.cmake")
  259. set_target_properties(SimpleInstall PROPERTIES PRE_INSTALL_SCRIPT
  260. ${CMAKE_CURRENT_SOURCE_DIR}/PreInstall.cmake)
  261. set_target_properties(SimpleInstall PROPERTIES POST_INSTALL_SCRIPT
  262. ${CMAKE_CURRENT_SOURCE_DIR}/PostInstall.cmake)
  263. set_target_properties(test4 PROPERTIES VERSION 1.2 SOVERSION 3
  264. INSTALL_NAME_DIR @executable_path/../lib)
  265. endif()
  266. get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  267. if(_isMultiConfig)
  268. set(SI_CONFIG --config $<CONFIGURATION>)
  269. else()
  270. set(SI_CONFIG)
  271. endif()
  272. # Dummy test of CPack
  273. set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Test of packaging with cpack")
  274. set(CPACK_PACKAGE_VENDOR "Kitware")
  275. set(CPACK_INSTALL_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/PackageScript.cmake")
  276. if(WIN32 AND NOT UNIX)
  277. find_program(NSIS_MAKENSIS NAMES makensis
  278. PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\NSIS]
  279. DOC "Where is makensis.exe located"
  280. )
  281. if(NOT NSIS_MAKENSIS)
  282. set(CPACK_GENERATOR TGZ)
  283. endif()
  284. endif()
  285. if(UNIX AND NOT APPLE)
  286. set(CPACK_GENERATOR "TGZ;STGZ;TZ")
  287. # find_program(found_compress
  288. # NAMES compress)
  289. # if(found_compress)
  290. # find_program(file_command NAMES file)
  291. # if(NOT file_command)
  292. # set(file_command file)
  293. # endif()
  294. # execute_process(COMMAND ${file_command} ${found_compress}
  295. # OUTPUT_VARIABLE output)
  296. # set(SKIP_TZ FALSE)
  297. # if("${output}" MATCHES "script")
  298. # set(SKIP_TZ TRUE)
  299. # endif()
  300. # if("${output}" MATCHES "dummy.sh")
  301. # set(SKIP_TZ TRUE)
  302. # endif()
  303. # if(NOT SKIP_TZ)
  304. # message("compress found and it was not a script")
  305. # message("output from file command: [${output}]")
  306. # list(APPEND CPACK_GENERATOR "TZ")
  307. # else()
  308. # message("compress found, but it was a script so don't use it")
  309. # message("output from file command: [${output}]")
  310. # endif()
  311. # endif()
  312. find_program(found_bz2
  313. NAMES bzip2)
  314. if(found_bz2)
  315. list(APPEND CPACK_GENERATOR "TBZ2")
  316. endif()
  317. endif()
  318. set(CPACK_PACKAGE_EXECUTABLES "SimpleInstall" "Simple Install")
  319. set(CMAKE_INSTALL_MFC_LIBRARIES 1)
  320. set(CMAKE_INSTALL_DEBUG_LIBRARIES 1)
  321. set(CMAKE_INSTALL_UCRT_LIBRARIES 1)
  322. include(InstallRequiredSystemLibraries)
  323. if(CTEST_TEST_CPACK)
  324. set(package_command COMMAND
  325. ${CMAKE_COMMAND} --build . --target package ${SI_CONFIG}
  326. )
  327. # Avoid settings that require the .zip file command line tools...
  328. # (just build an NSIS installer without component support)
  329. #
  330. set(CPACK_BINARY_ZIP OFF)
  331. set(CPACK_MONOLITHIC_INSTALL ON)
  332. else()
  333. set(package_command)
  334. endif()
  335. include(CPack)
  336. set(install_command COMMAND
  337. ${CMAKE_COMMAND} --build . --target install ${SI_CONFIG}
  338. )
  339. add_custom_command(
  340. TARGET ${install_target}
  341. POST_BUILD
  342. ${install_command}
  343. ${package_command}
  344. COMMENT "Install Project"
  345. )