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