ACGCommon.cmake 10.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5
if (EXISTS ${CMAKE_SOURCE_DIR}/${CMAKE_PROJECT_NAME}.cmake)
  include (${CMAKE_SOURCE_DIR}/${CMAKE_PROJECT_NAME}.cmake)
endif ()

# prevent build in source directory
Jan Möbius's avatar
Jan Möbius committed
6 7 8 9
  if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
      message (SEND_ERROR "Building in the source directory is not supported.")
      message (FATAL_ERROR "Please remove the created \"CMakeCache.txt\" file, the \"CMakeFiles\" directory and create a build directory and call \"${CMAKE_COMMAND} <path to the sources>\".")
  endif ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
Jan Möbius's avatar
Jan Möbius committed
10 11

# allow only Debug and Release builds
12
set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "")
Jan Möbius's avatar
Jan Möbius committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
mark_as_advanced (CMAKE_CONFIGURATION_TYPES)

# set Debus as default build target
if (NOT CMAKE_BUILD_TYPE)
  set (CMAKE_BUILD_TYPE Debug CACHE STRING
      "Choose the type of build, options are: Debug, Release."
      FORCE)
endif ()

# create our output directroy
if (NOT EXISTS ${CMAKE_BINARY_DIR}/Build)
  file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Build)
endif ()

# read version from file
macro (acg_get_version)
Jan Möbius's avatar
Jan Möbius committed
29 30 31 32 33 34 35 36 37 38 39
    if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${ARGN}/VERSION")
      file (READ "${CMAKE_CURRENT_SOURCE_DIR}/${ARGN}/VERSION" _file)
    else ()
      file (READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" _file)
    endif ()

    string (
        REGEX REPLACE
        "^.*ID=([^\n]*).*$" "\\1"
        _id ${_file}
    )
Jan Möbius's avatar
Jan Möbius committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    string (
        REGEX REPLACE
        "^.*VERSION=([^\n]*).*$" "\\1"
        _version ${_file}
    )
    string (
        REGEX REPLACE
        "^.*MAJOR=([^\n]*).*$" "\\1"
        _major ${_file}
    )
    string (
        REGEX REPLACE
        "^.*MINOR=([^\n]*).*$" "\\1"
        _minor ${_file}
    )
    string (
        REGEX REPLACE
        "^.*PATCH=([^\n]*).*$" "\\1"
        _patch ${_file}
    )
Jan Möbius's avatar
Jan Möbius committed
60 61 62 63 64

    set (${_id}_VERSION ${_version})
    set (${_id}_VERSION_MAJOR ${_major})
    set (${_id}_VERSION_MINOR ${_minor})
    set (${_id}_VERSION_PATCH ${_patch})
Jan Möbius's avatar
Jan Möbius committed
65 66 67 68
endmacro ()


# set directory structures for the different platforms
69
if (CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
Jan Möbius's avatar
Jan Möbius committed
70
  set (ACG_PROJECT_DATADIR ".")
Jan Möbius's avatar
Jan Möbius committed
71 72
  set (ACG_PROJECT_LIBDIR "lib")
  set (ACG_PROJECT_BINDIR ".")
Jan Möbius's avatar
Jan Möbius committed
73
  set (ACG_PROJECT_PLUGINDIR "Plugins")
Jan Möbius's avatar
Jan Möbius committed
74 75
  if (NOT EXISTS ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR})
    file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR})
Jan Möbius's avatar
Jan Möbius committed
76
  endif ()
77 78
elseif (APPLE)
  set (ACG_PROJECT_DATADIR "share/${CMAKE_PROJECT_NAME}")
Matthias Möller's avatar
Matthias Möller committed
79
  set (ACG_PROJECT_LIBDIR "lib${LIB_SUFFIX}")
80
  set (CMAKE_LIBRARY_OUTPUT_DIR "${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}")
Matthias Möller's avatar
Matthias Möller committed
81
  set (ACG_PROJECT_PLUGINDIR "${ACG_PROJECT_LIBDIR}/plugins")
82
  set (ACG_PROJECT_BINDIR "bin")
Jan Möbius's avatar
Jan Möbius committed
83 84
else ()
  set (ACG_PROJECT_DATADIR "share/${CMAKE_PROJECT_NAME}")
Matthias Möller's avatar
Matthias Möller committed
85 86
  set (ACG_PROJECT_LIBDIR "lib${LIB_SUFFIX}")
  set (ACG_PROJECT_PLUGINDIR "${ACG_PROJECT_LIBDIR}/plugins")
Jan Möbius's avatar
Jan Möbius committed
87 88 89 90 91 92 93 94
  set (ACG_PROJECT_BINDIR "bin")
endif ()

# allow a project to modify the directories
if (COMMAND acg_modify_project_dirs)
  acg_modify_project_dirs ()
endif ()

Jan Möbius's avatar
Jan Möbius committed
95 96 97 98 99
if (NOT EXISTS ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_DATADIR})
 file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_DATADIR})
endif ()


Jan Möbius's avatar
Jan Möbius committed
100 101 102 103 104 105 106 107 108
# sets default build properties
macro (acg_set_target_props target)
  if (WIN32)
    set_target_properties (
      ${target} PROPERTIES
      BUILD_WITH_INSTALL_RPATH 1
      SKIP_BUILD_RPATH 0
    )
  elseif (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE)
Jan Möbius's avatar
Jan Möbius committed
109 110 111 112 113 114 115 116
    # save rpath
    set_target_properties (
      ${target} PROPERTIES
      INSTALL_RPATH "@executable_path/../${ACG_PROJECT_LIBDIR}"
      MACOSX_RPATH 1
      #BUILD_WITH_INSTALL_RPATH 1
      SKIP_BUILD_RPATH 0
    )  
Jan Möbius's avatar
Jan Möbius committed
117
  elseif (NOT APPLE)
118

Jan Möbius's avatar
Jan Möbius committed
119 120
    set_target_properties (
      ${target} PROPERTIES
Matthias Möller's avatar
Matthias Möller committed
121
      INSTALL_RPATH "$ORIGIN/../${ACG_PROJECT_LIBDIR}"
Jan Möbius's avatar
Jan Möbius committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      BUILD_WITH_INSTALL_RPATH 1
      SKIP_BUILD_RPATH 0
      RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_BINDIR}"
      LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}"
    )
  endif ()
endmacro ()

include (AddFileDependencies)
include (ACGCompiler)

# define INCLUDE_TEMPLATES for everything we build
add_definitions (-DINCLUDE_TEMPLATES)

# unsets the given variable
macro (acg_unset var)
    set (${var} "" CACHE INTERNAL "")
endmacro ()

# sets the given variable
macro (acg_set var value)
    set (${var} ${value} CACHE INTERNAL "")
endmacro ()

# append all files with extension "ext" in the "dirs" directories to "ret"
147
# excludes all files starting with a '.' (dot)
Jan Möbius's avatar
Jan Möbius committed
148 149 150
macro (acg_append_files ret ext)
  foreach (_dir ${ARGN})
    file (GLOB _files "${_dir}/${ext}")
151 152 153 154 155 156
    foreach (_file ${_files})
      get_filename_component (_filename ${_file} NAME)
      if (_filename MATCHES "^[.]")
	list (REMOVE_ITEM _files ${_file})
      endif ()
    endforeach ()
Jan Möbius's avatar
Jan Möbius committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    list (APPEND ${ret} ${_files})
  endforeach ()
endmacro ()

# drop all "*T.cc" files from list
macro (acg_drop_templates list)
  foreach (_file ${${list}})
    if (_file MATCHES "T.cc$")
      list (REMOVE_ITEM ${list} ${_file})
    endif ()
  endforeach ()
endmacro ()

# get all files in directory, but ignore svn
macro (acg_get_files_in_dir ret dir)
  file (GLOB_RECURSE __files RELATIVE "${dir}" "${dir}/*")
  foreach (_file ${__files})
Jan Möbius's avatar
Jan Möbius committed
174
    if ( (NOT _file MATCHES ".*svn.*") AND (NOT _file MATCHES ".DS_Store") )
Jan Möbius's avatar
Jan Möbius committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
      list (APPEND ${ret} "${_file}")
    endif ()
  endforeach ()
endmacro ()

# copy the whole directory without svn files
function (acg_copy_after_build target src dst)
  acg_unset (_files)
  acg_get_files_in_dir (_files ${src})
  foreach (_file ${_files})
    add_custom_command(TARGET ${target} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different "${src}/${_file}" "${dst}/${_file}"
    )
  endforeach ()
endfunction ()

# extended version of add_executable that also copies output to out Build directory
function (acg_add_executable _target)
  add_executable (${_target} ${ARGN})

  # set common target properties defined in common.cmake
  acg_set_target_props (${_target})
Jan Möbius's avatar
Jan Möbius committed
197
  
198
  if (WIN32 OR (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE))
Jan Möbius's avatar
Jan Möbius committed
199 200 201
    add_custom_command (TARGET ${_target} POST_BUILD
                        COMMAND ${CMAKE_COMMAND} -E
                        copy_if_different
202 203 204 205
                          $<TARGET_FILE:${_target}>
                          ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_BINDIR}/$<TARGET_FILE_NAME:${_target}>)
  endif (WIN32 OR (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE))
  
206
  if (NOT ACG_PROJECT_MACOS_BUNDLE OR NOT APPLE)
Jan Möbius's avatar
Jan Möbius committed
207 208 209 210 211
    install (TARGETS ${_target} DESTINATION ${ACG_PROJECT_BINDIR})
  endif ()
endfunction ()

# extended version of add_library that also copies output to out Build directory
212 213 214 215
function (acg_add_library _target _libtype)

  if (${_libtype} STREQUAL SHAREDANDSTATIC)
    set (_type SHARED)
216
    if (NOT WIN32 OR MINGW)
217 218 219 220 221 222 223 224 225
      set (_and_static 1)
    else ()
      set (_and_static 0)
    endif ()    
  else ()
    set (_type ${_libtype})
    set (_and_static 0)
  endif ()

226 227
  add_library (${_target} ${_type} ${ARGN} )

Jan Möbius's avatar
Jan Möbius committed
228 229 230
  # set common target properties defined in common.cmake
  acg_set_target_props (${_target})

231 232 233 234 235
  if (_and_static)
    add_library (${_target}Static STATIC ${ARGN})

    # set common target properties defined in common.cmake
    acg_set_target_props (${_target}Static)
236 237

    set_target_properties(${_target}Static PROPERTIES OUTPUT_NAME ${_target})
238 239 240
    
    if (NOT APPLE)
      set_target_properties (${_target}Static PROPERTIES 
241 242
                             LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
                            )
243 244 245
    endif ()
  endif ()

246
  if ( (WIN32 AND MSVC) OR (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE))
Jan Möbius's avatar
Jan Möbius committed
247 248 249 250
    if (${_type} STREQUAL SHARED)
      add_custom_command (TARGET ${_target} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
251 252
                            $<TARGET_FILE:${_target}>
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/$<TARGET_FILE_NAME:${_target}>)
Jan Möbius's avatar
Jan Möbius committed
253
    add_custom_command (TARGET ${_target} POST_BUILD
Jan Möbius's avatar
Jan Möbius committed
254 255
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
256 257
                            $<TARGET_LINKER_FILE:${_target}>
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/$<TARGET_LINKER_FILE_NAME:${_target}>)
Jan Möbius's avatar
Jan Möbius committed
258 259 260 261
    elseif (${_type} STREQUAL MODULE)
      if (NOT EXISTS ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR})
        file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR})
      endif ()
262
      add_custom_command (TARGET ${_target} POST_BUILD
Jan Möbius's avatar
Jan Möbius committed
263 264
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
265 266
                            $<TARGET_FILE:${_target}>
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR}/$<TARGET_FILE_NAME:${_target}>)
267
    elseif (${_type} STREQUAL STATIC)
Jan Möbius's avatar
Jan Möbius committed
268
    add_custom_command (TARGET ${_target} POST_BUILD
269 270
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
271 272
                            $<TARGET_FILE:${_target}>
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/$<TARGET_FILE_NAME:${_target}>)
Jan Möbius's avatar
Jan Möbius committed
273 274 275 276
  endif()
  
   
  # make an extra copy for windows into the binary directory
277 278
    if (${_type} STREQUAL SHARED AND WIN32)
      add_custom_command (TARGET ${_target} POST_BUILD
279
                          COMMAND ${CMAKE_COMMAND} -E
280 281 282
                          copy_if_different 
                            $<TARGET_FILE:${_target}>
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_BINDIR}/$<TARGET_FILE_NAME:${_target}>)
Jan Möbius's avatar
Jan Möbius committed
283
  endif () 
284
    
285
  endif( (WIN32 AND MSVC) OR (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE))
286 287
  
  if (_and_static)
288 289 290 291
    add_custom_command (TARGET ${_target}Static POST_BUILD
                        COMMAND ${CMAKE_COMMAND} -E
                        copy_if_different
                          $<TARGET_FILE:${_target}Static>
292
                          ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/$<TARGET_FILE_NAME:${_target}Static>)
293

Jan Möbius's avatar
Jan Möbius committed
294
  endif ()
295 296 297 298 299 300 301 302 303 304 305
 

  # Block installation of libraries by setting ACG_NO_LIBRARY_INSTALL
  if ( NOT ACG_NO_LIBRARY_INSTALL ) 
    if (NOT ACG_PROJECT_MACOS_BUNDLE OR NOT APPLE)
      if (${_type} STREQUAL SHARED OR ${_type} STREQUAL STATIC )
        install (TARGETS ${_target}
                 RUNTIME DESTINATION ${ACG_PROJECT_BINDIR}
                 LIBRARY DESTINATION ${ACG_PROJECT_LIBDIR}
                 ARCHIVE DESTINATION ${ACG_PROJECT_LIBDIR})
        if (_and_static)
306 307
          install (TARGETS ${_target}Static
                   DESTINATION ${ACG_PROJECT_LIBDIR})
308 309 310
        endif ()
      elseif (${_type} STREQUAL MODULE)
        install (TARGETS ${_target} DESTINATION ${ACG_PROJECT_PLUGINDIR})
311
      endif ()
Jan Möbius's avatar
Jan Möbius committed
312
    endif ()
313
  endif()
314 315 316

endfunction ()