dist.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # LuaDist CMake utility library.
  2. # Provides sane project defaults and macros common to LuaDist CMake builds.
  3. #
  4. # Copyright (C) 2007-2012 LuaDist.
  5. # by David Manura, Peter Drahoš
  6. # Redistribution and use of this file is allowed according to the terms of the MIT license.
  7. # For details see the COPYRIGHT file distributed with LuaDist.
  8. # Please note that the package source code is licensed under its own license.
  9. ## Extract information from dist.info
  10. if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/dist.info )
  11. message ( FATAL_ERROR
  12. "Missing dist.info file (${CMAKE_CURRENT_SOURCE_DIR}/dist.info)." )
  13. endif ()
  14. file ( READ ${CMAKE_CURRENT_SOURCE_DIR}/dist.info DIST_INFO )
  15. if ( "${DIST_INFO}" STREQUAL "" )
  16. message ( FATAL_ERROR "Failed to load dist.info." )
  17. endif ()
  18. # Reads field `name` from dist.info string `DIST_INFO` into variable `var`.
  19. macro ( _parse_dist_field name var )
  20. string ( REGEX REPLACE ".*${name}[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1"
  21. ${var} "${DIST_INFO}" )
  22. if ( ${var} STREQUAL DIST_INFO )
  23. message ( FATAL_ERROR "Failed to extract \"${var}\" from dist.info" )
  24. endif ()
  25. endmacro ()
  26. #
  27. _parse_dist_field ( name DIST_NAME )
  28. _parse_dist_field ( version DIST_VERSION )
  29. _parse_dist_field ( license DIST_LICENSE )
  30. _parse_dist_field ( author DIST_AUTHOR )
  31. _parse_dist_field ( maintainer DIST_MAINTAINER )
  32. _parse_dist_field ( url DIST_URL )
  33. _parse_dist_field ( desc DIST_DESC )
  34. #message ( STATUS "DIST_NAME: ${DIST_NAME}")
  35. #message ( STATUS "DIST_VERSION: ${DIST_VERSION}")
  36. #message ( STATUS "DIST_LICENSE: ${DIST_LICENSE}")
  37. #message ( STATUS "DIST_AUTHOR: ${DIST_AUTHOR}")
  38. #message ( STATUS "DIST_MAINTAINER: ${DIST_MAINTAINER}")
  39. #message ( STATUS "DIST_URL: ${DIST_URL}")
  40. #message ( STATUS "DIST_DESC: ${DIST_DESC}")
  41. string ( REGEX REPLACE ".*depends[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1"
  42. DIST_DEPENDS ${DIST_INFO} )
  43. if ( DIST_DEPENDS STREQUAL DIST_INFO )
  44. set ( DIST_DEPENDS "" )
  45. endif ()
  46. #message ( STATUS "DIST_DEPENDS: ${DIST_DEPENDS}")
  47. ## 2DO: Parse DIST_DEPENDS and try to install Dependencies with automatically using externalproject_add
  48. ## INSTALL DEFAULTS (Relative to CMAKE_INSTALL_PREFIX)
  49. # Primary paths
  50. set ( INSTALL_BIN bin CACHE PATH "Where to install binaries to." )
  51. set ( INSTALL_LIB lib CACHE PATH "Where to install libraries to." )
  52. set ( INSTALL_INC include CACHE PATH "Where to install headers to." )
  53. set ( INSTALL_ETC etc CACHE PATH "Where to store configuration files" )
  54. set ( INSTALL_SHARE share CACHE PATH "Directory for shared data." )
  55. # Secondary paths
  56. option ( INSTALL_VERSION
  57. "Install runtime libraries and executables with version information." OFF)
  58. set ( INSTALL_DATA ${INSTALL_SHARE}/${DIST_NAME} CACHE PATH
  59. "Directory the package can store documentation, tests or other data in.")
  60. set ( INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH
  61. "Recommended directory to install documentation into.")
  62. set ( INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH
  63. "Recommended directory to install examples into.")
  64. set ( INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH
  65. "Recommended directory to install tests into.")
  66. set ( INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH
  67. "Where to install additional files")
  68. # Tweaks and other defaults
  69. # Setting CMAKE to use loose block and search for find modules in source directory
  70. set ( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true )
  71. set ( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH} )
  72. option ( BUILD_SHARED_LIBS "Build shared libraries" ON )
  73. # In MSVC, prevent warnings that can occur when using standard libraries.
  74. if ( MSVC )
  75. add_definitions ( -D_CRT_SECURE_NO_WARNINGS )
  76. endif ()
  77. # RPath and relative linking
  78. option ( USE_RPATH "Use relative linking." ON)
  79. if ( USE_RPATH )
  80. string ( REGEX REPLACE "[^!/]+" ".." UP_DIR ${INSTALL_BIN} )
  81. set ( CMAKE_SKIP_BUILD_RPATH FALSE CACHE STRING "" FORCE )
  82. set ( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE CACHE STRING "" FORCE )
  83. set ( CMAKE_INSTALL_RPATH $ORIGIN/${UP_DIR}/${INSTALL_LIB}
  84. CACHE STRING "" FORCE )
  85. set ( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE STRING "" FORCE )
  86. set ( CMAKE_INSTALL_NAME_DIR @executable_path/${UP_DIR}/${INSTALL_LIB}
  87. CACHE STRING "" FORCE )
  88. endif ()
  89. ## MACROS
  90. # Parser macro
  91. macro ( parse_arguments prefix arg_names option_names)
  92. set ( DEFAULT_ARGS )
  93. foreach ( arg_name ${arg_names} )
  94. set ( ${prefix}_${arg_name} )
  95. endforeach ()
  96. foreach ( option ${option_names} )
  97. set ( ${prefix}_${option} FALSE )
  98. endforeach ()
  99. set ( current_arg_name DEFAULT_ARGS )
  100. set ( current_arg_list )
  101. foreach ( arg ${ARGN} )
  102. set ( larg_names ${arg_names} )
  103. list ( FIND larg_names "${arg}" is_arg_name )
  104. if ( is_arg_name GREATER -1 )
  105. set ( ${prefix}_${current_arg_name} ${current_arg_list} )
  106. set ( current_arg_name ${arg} )
  107. set ( current_arg_list )
  108. else ()
  109. set ( loption_names ${option_names} )
  110. list ( FIND loption_names "${arg}" is_option )
  111. if ( is_option GREATER -1 )
  112. set ( ${prefix}_${arg} TRUE )
  113. else ()
  114. set ( current_arg_list ${current_arg_list} ${arg} )
  115. endif ()
  116. endif ()
  117. endforeach ()
  118. set ( ${prefix}_${current_arg_name} ${current_arg_list} )
  119. endmacro ()
  120. # install_executable ( executable_targets )
  121. # Installs any executables generated using "add_executable".
  122. # USE: install_executable ( lua )
  123. # NOTE: subdirectories are NOT supported
  124. set ( CPACK_COMPONENT_RUNTIME_DISPLAY_NAME "${DIST_NAME} Runtime" )
  125. set ( CPACK_COMPONENT_RUNTIME_DESCRIPTION
  126. "Executables and runtime libraries. Installed into ${INSTALL_BIN}." )
  127. macro ( install_executable )
  128. foreach ( _file ${ARGN} )
  129. if ( INSTALL_VERSION )
  130. set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION}
  131. SOVERSION ${DIST_VERSION} )
  132. endif ()
  133. install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN}
  134. COMPONENT Runtime )
  135. endforeach()
  136. endmacro ()
  137. # install_library ( library_targets )
  138. # Installs any libraries generated using "add_library" into apropriate places.
  139. # USE: install_library ( libexpat )
  140. # NOTE: subdirectories are NOT supported
  141. set ( CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "${DIST_NAME} Development Libraries" )
  142. set ( CPACK_COMPONENT_LIBRARY_DESCRIPTION
  143. "Static and import libraries needed for development. Installed into ${INSTALL_LIB} or ${INSTALL_BIN}." )
  144. macro ( install_library )
  145. foreach ( _file ${ARGN} )
  146. if ( INSTALL_VERSION )
  147. set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION}
  148. SOVERSION ${DIST_VERSION} )
  149. endif ()
  150. install ( TARGETS ${_file}
  151. RUNTIME DESTINATION . COMPONENT Runtime )
  152. endforeach()
  153. endmacro ()
  154. # helper function for various install_* functions, for PATTERN/REGEX args.
  155. macro ( _complete_install_args )
  156. if ( NOT("${_ARG_PATTERN}" STREQUAL "") )
  157. set ( _ARG_PATTERN PATTERN ${_ARG_PATTERN} )
  158. endif ()
  159. if ( NOT("${_ARG_REGEX}" STREQUAL "") )
  160. set ( _ARG_REGEX REGEX ${_ARG_REGEX} )
  161. endif ()
  162. endmacro ()
  163. # install_header ( files/directories [INTO destination] )
  164. # Install a directories or files into header destination.
  165. # USE: install_header ( lua.h luaconf.h ) or install_header ( GL )
  166. # USE: install_header ( mylib.h INTO mylib )
  167. # For directories, supports optional PATTERN/REGEX arguments like install().
  168. set ( CPACK_COMPONENT_HEADER_DISPLAY_NAME "${DIST_NAME} Development Headers" )
  169. set ( CPACK_COMPONENT_HEADER_DESCRIPTION
  170. "Headers needed for development. Installed into ${INSTALL_INC}." )
  171. macro ( install_header )
  172. parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
  173. _complete_install_args()
  174. foreach ( _file ${_ARG_DEFAULT_ARGS} )
  175. if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
  176. install ( DIRECTORY ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO}
  177. COMPONENT Header ${_ARG_PATTERN} ${_ARG_REGEX} )
  178. else ()
  179. install ( FILES ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO}
  180. COMPONENT Header )
  181. endif ()
  182. endforeach()
  183. endmacro ()
  184. # install_data ( files/directories [INTO destination] )
  185. # This installs additional data files or directories.
  186. # USE: install_data ( extra data.dat )
  187. # USE: install_data ( image1.png image2.png INTO images )
  188. # For directories, supports optional PATTERN/REGEX arguments like install().
  189. set ( CPACK_COMPONENT_DATA_DISPLAY_NAME "${DIST_NAME} Data" )
  190. set ( CPACK_COMPONENT_DATA_DESCRIPTION
  191. "Application data. Installed into ${INSTALL_DATA}." )
  192. macro ( install_data )
  193. parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
  194. _complete_install_args()
  195. foreach ( _file ${_ARG_DEFAULT_ARGS} )
  196. if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
  197. install ( DIRECTORY ${_file}
  198. DESTINATION ${INSTALL_DATA}/${_ARG_INTO}
  199. COMPONENT Data ${_ARG_PATTERN} ${_ARG_REGEX} )
  200. else ()
  201. install ( FILES ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO}
  202. COMPONENT Data )
  203. endif ()
  204. endforeach()
  205. endmacro ()
  206. # INSTALL_DOC ( files/directories [INTO destination] )
  207. # This installs documentation content
  208. # USE: install_doc ( doc/ doc.pdf )
  209. # USE: install_doc ( index.html INTO html )
  210. # For directories, supports optional PATTERN/REGEX arguments like install().
  211. set ( CPACK_COMPONENT_DOCUMENTATION_DISPLAY_NAME "${DIST_NAME} Documentation" )
  212. set ( CPACK_COMPONENT_DOCUMENTATION_DESCRIPTION
  213. "Application documentation. Installed into ${INSTALL_DOC}." )
  214. macro ( install_doc )
  215. parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
  216. _complete_install_args()
  217. foreach ( _file ${_ARG_DEFAULT_ARGS} )
  218. if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
  219. install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO}
  220. COMPONENT Documentation ${_ARG_PATTERN} ${_ARG_REGEX} )
  221. else ()
  222. install ( FILES ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO}
  223. COMPONENT Documentation )
  224. endif ()
  225. endforeach()
  226. endmacro ()
  227. # install_example ( files/directories [INTO destination] )
  228. # This installs additional examples
  229. # USE: install_example ( examples/ exampleA )
  230. # USE: install_example ( super_example super_data INTO super)
  231. # For directories, supports optional PATTERN/REGEX argument like install().
  232. set ( CPACK_COMPONENT_EXAMPLE_DISPLAY_NAME "${DIST_NAME} Examples" )
  233. set ( CPACK_COMPONENT_EXAMPLE_DESCRIPTION
  234. "Examples and their associated data. Installed into ${INSTALL_EXAMPLE}." )
  235. macro ( install_example )
  236. parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
  237. _complete_install_args()
  238. foreach ( _file ${_ARG_DEFAULT_ARGS} )
  239. if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
  240. install ( DIRECTORY ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO}
  241. COMPONENT Example ${_ARG_PATTERN} ${_ARG_REGEX} )
  242. else ()
  243. install ( FILES ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO}
  244. COMPONENT Example )
  245. endif ()
  246. endforeach()
  247. endmacro ()
  248. # install_test ( files/directories [INTO destination] )
  249. # This installs tests and test files, DOES NOT EXECUTE TESTS
  250. # USE: install_test ( my_test data.sql )
  251. # USE: install_test ( feature_x_test INTO x )
  252. # For directories, supports optional PATTERN/REGEX argument like install().
  253. set ( CPACK_COMPONENT_TEST_DISPLAY_NAME "${DIST_NAME} Tests" )
  254. set ( CPACK_COMPONENT_TEST_DESCRIPTION
  255. "Tests and associated data. Installed into ${INSTALL_TEST}." )
  256. macro ( install_test )
  257. parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
  258. _complete_install_args()
  259. foreach ( _file ${_ARG_DEFAULT_ARGS} )
  260. if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
  261. install ( DIRECTORY ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO}
  262. COMPONENT Test ${_ARG_PATTERN} ${_ARG_REGEX} )
  263. else ()
  264. install ( FILES ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO}
  265. COMPONENT Test )
  266. endif ()
  267. endforeach()
  268. endmacro ()
  269. # install_foo ( files/directories [INTO destination] )
  270. # This installs optional or otherwise unneeded content
  271. # USE: install_foo ( etc/ example.doc )
  272. # USE: install_foo ( icon.png logo.png INTO icons)
  273. # For directories, supports optional PATTERN/REGEX argument like install().
  274. set ( CPACK_COMPONENT_OTHER_DISPLAY_NAME "${DIST_NAME} Unspecified Content" )
  275. set ( CPACK_COMPONENT_OTHER_DESCRIPTION
  276. "Other unspecified content. Installed into ${INSTALL_FOO}." )
  277. macro ( install_foo )
  278. parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
  279. _complete_install_args()
  280. foreach ( _file ${_ARG_DEFAULT_ARGS} )
  281. if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
  282. install ( DIRECTORY ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO}
  283. COMPONENT Other ${_ARG_PATTERN} ${_ARG_REGEX} )
  284. else ()
  285. install ( FILES ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO}
  286. COMPONENT Other )
  287. endif ()
  288. endforeach()
  289. endmacro ()
  290. ## CTest defaults
  291. ## CPack defaults
  292. set ( CPACK_GENERATOR "ZIP" )
  293. set ( CPACK_STRIP_FILES TRUE )
  294. set ( CPACK_PACKAGE_NAME "${DIST_NAME}" )
  295. set ( CPACK_PACKAGE_VERSION "${DIST_VERSION}")
  296. set ( CPACK_PACKAGE_VENDOR "LuaDist" )
  297. set ( CPACK_COMPONENTS_ALL Runtime Library Header Data Documentation Example Other )
  298. include ( CPack )