ACGCommon.cmake 15.5 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
if (EXISTS ${CMAKE_SOURCE_DIR}/${CMAKE_PROJECT_NAME}.cmake)
  include (${CMAKE_SOURCE_DIR}/${CMAKE_PROJECT_NAME}.cmake)
endif ()

# prevent build in source directory
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}")

# allow only Debug and Release builds
set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
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)
    file (READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" _file)
    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}
    )
    set (VERSION ${_version})
    set (VERSION_MAJOR ${_major})
    set (VERSION_MINOR ${_minor})
    set (VERSION_PATCH ${_patch})
endmacro ()


# set directory structures for the different platforms
if (WIN32)
  set (ACG_PROJECT_DATADIR ".")
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
60 61
  set (ACG_PROJECT_LIBDIR "lib")
  set (ACG_PROJECT_BINDIR ".")
Jan Möbius's avatar
 
Jan Möbius committed
62
  set (ACG_PROJECT_PLUGINDIR "Plugins")
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
63 64
  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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  endif ()
else ()
  set (ACG_PROJECT_DATADIR "share/${CMAKE_PROJECT_NAME}")
  set (ACG_PROJECT_LIBDIR "lib/${CMAKE_PROJECT_NAME}")
  set (ACG_PROJECT_PLUGINDIR "lib/${CMAKE_PROJECT_NAME}/plugins")
  set (ACG_PROJECT_BINDIR "bin")
endif ()

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

# 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)
    set_target_properties (
      ${target} PROPERTIES
      INSTALL_NAME_DIR "@executable_path/../lib/${CMAKE_PROJECT_NAME}"
      BUILD_WITH_INSTALL_RPATH 1
      SKIP_BUILD_RPATH 0
    )
  elseif (NOT APPLE)
    set_target_properties (
      ${target} PROPERTIES
      INSTALL_RPATH "$ORIGIN/../lib/${CMAKE_PROJECT_NAME}"
      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)

# look for selected qt dependencies
macro (acg_qt4)
  if (NOT QT4_FOUND)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
114
    find_package (Qt4 COMPONENTS QtCore QtGui ${ARGN})
Jan Möbius's avatar
 
Jan Möbius committed
115 116 117 118 119

    set (QT_USE_QTOPENGL 1)
    set (QT_USE_QTNETWORK 1)
    set (QT_USE_QTSCRIPT 1)
    set (QT_USE_QTSQL 1)
Jan Möbius's avatar
Jan Möbius committed
120
    set (QT_USE_QTXML 1)
Jan Möbius's avatar
 
Jan Möbius committed
121 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    set (QT_USE_QTHELP 1)
    set (QT_USE_QTWEBKIT 1)
    set (QT_USE_QTUITOOLS 1)

    include (${QT_USE_FILE})
  endif ()
endmacro ()

# 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 ()

# test for OpenMP
macro (acg_openmp)
  if (NOT OPENMP_NOTFOUND)
    find_package(OpenMP)
    if (OPENMP_FOUND)
      set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
      set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
      add_definitions(-DUSE_OPENMP)
    else ()
      set (OPENMP_NOTFOUND 1)
    endif ()
  endif ()
endmacro ()

# test for FTGL
macro (acg_ftgl)
  find_package (Freetype)

  if (FREETYPE_FOUND)
    find_package (FTGL)

    if (FTGL_FOUND)
      add_definitions (-DUSE_FTGL)
      include_directories (${FTGL_INCLUDE_DIR} ${FREETYPE_INCLUDE_DIR_freetype2})
      set (FTGL_LIBS ${FREETYPE_LIBRARIES} ${FTGL_LIBRARIES})
    endif ()
  endif ()
endmacro ()

# append all files with extension "ext" in the "dirs" directories to "ret"
macro (acg_append_files ret ext)
  foreach (_dir ${ARGN})
    file (GLOB _files "${_dir}/${ext}")
    list (APPEND ${ret} ${_files})
  endforeach ()
endmacro ()

# append all files with extension "ext" in the "dirs" directories and its subdirectories to "ret"
macro (acg_append_files_recursive ret ext)
  foreach (_dir ${ARGN})
    file (GLOB_RECURSE _files "${_dir}/${ext}")
    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 ()

# generate moc targets for sources in list
macro (acg_qt4_automoc moc_SRCS)
  qt4_get_moc_flags (_moc_INCS)

  set (_matching_FILES )
  foreach (_current_FILE ${ARGN})

     get_filename_component (_abs_FILE ${_current_FILE} ABSOLUTE)
     # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
     # here. this is required to make bouic work correctly:
     # we need to add generated .cpp files to the sources (to compile them),
     # but we cannot let automoc handle them, as the .cpp files don't exist yet when
     # cmake is run for the very first time on them -> however the .cpp files might
     # exist at a later run. at that time we need to skip them, so that we don't add two
     # different rules for the same moc file
     get_source_file_property (_skip ${_abs_FILE} SKIP_AUTOMOC)

     if ( NOT _skip AND EXISTS ${_abs_FILE} )

        file (READ ${_abs_FILE} _contents)

        get_filename_component (_abs_PATH ${_abs_FILE} PATH)

        string (REGEX MATCHALL "Q_OBJECT" _match "${_contents}")
        if (_match)
            get_filename_component (_basename ${_current_FILE} NAME_WE)
            set (_header ${_abs_FILE})
            set (_moc    ${CMAKE_CURRENT_BINARY_DIR}/moc_${_basename}.cpp)

            add_custom_command (OUTPUT ${_moc}
                COMMAND ${QT_MOC_EXECUTABLE}
                ARGS ${_moc_INCS} ${_header} -o ${_moc}
                DEPENDS ${_header}
            )

            add_file_dependencies (${_abs_FILE} ${_moc})
            set (${moc_SRCS} ${${moc_SRCS}} ${_moc})
            
        endif ()
     endif ()
  endforeach ()
endmacro ()

# generate uic targets for sources in list
macro (acg_qt4_autouic uic_SRCS)

  set (_matching_FILES )
  foreach (_current_FILE ${ARGN})

     get_filename_component (_abs_FILE ${_current_FILE} ABSOLUTE)

     if ( EXISTS ${_abs_FILE} )

        file (READ ${_abs_FILE} _contents)

        get_filename_component (_abs_PATH ${_abs_FILE} PATH)

        get_filename_component (_basename ${_current_FILE} NAME_WE)
        string (REGEX REPLACE "Ui$" "" _cbasename ${_basename})
        set (_outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${_basename}.hh)
        set (_header ${_basename}.hh)
        set (_source ${_abs_PATH}/${_cbasename}.cc)
        
        add_custom_command (OUTPUT ${_outfile}
            COMMAND ${QT_UIC_EXECUTABLE}
            ARGS -o ${_outfile} ${_abs_FILE}
            DEPENDS ${_abs_FILE})

        add_file_dependencies (${_source} ${_outfile})
        set (${uic_SRCS} ${${uic_SRCS}} ${_outfile})
            
     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})
    if (NOT _file MATCHES ".*svn.*")
      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 ()

# install the whole directory without svn files
function (acg_install_dir src dst)
  acg_unset (_files)
  acg_get_files_in_dir (_files ${src})
  foreach (_file ${_files})
    get_filename_component (_file_PATH ${_file} PATH)
    install(FILES "${src}/${_file}"
      DESTINATION "${dst}/${_file_PATH}"
    )
  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})

  if (WIN32)
    # copy exe file to "Build" directory
    # Visual studio will create this file in a subdirectory so we can't use
    # RUNTIME_OUTPUT_DIRECTORY directly here
    add_custom_command (TARGET ${_target} POST_BUILD
                        COMMAND ${CMAKE_COMMAND} -E
                        copy_if_different
                          ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_target}.exe
                          ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_BINDIR}/${_target}.exe)
  elseif (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE)
    add_custom_command (TARGET ${_target} POST_BUILD
                        COMMAND ${CMAKE_COMMAND} -E
                        copy_if_different
                          ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_target}
                          ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_BINDIR}/${_target})
  endif ()
324
  if (NOT ACG_PROJECT_MACOS_BUNDLE OR NOT APPLE)
Jan Möbius's avatar
 
Jan Möbius committed
325 326 327 328 329
    install (TARGETS ${_target} DESTINATION ${ACG_PROJECT_BINDIR})
  endif ()
endfunction ()

# extended version of add_library that also copies output to out Build directory
330 331 332 333 334 335 336 337 338 339 340 341 342 343
function (acg_add_library _target _libtype)

  if (${_libtype} STREQUAL SHAREDANDSTATIC)
    set (_type SHARED)
    if (NOT WIN32)
      set (_and_static 1)
    else ()
      set (_and_static 0)
    endif ()    
  else ()
    set (_type ${_libtype})
    set (_and_static 0)
  endif ()

344 345 346 347 348 349 350
  add_library (${_target} ${_type} ${ARGN} )

#  acg_get_version()
#
#  set_target_properties (${_target} PROPERTIES VERSION    ${VERSION-MAJOR}.${VERSION-MINOR} 
#                                               SOVERSION  ${VERSION-MAJOR}.${VERSION-MINOR} )
#
Jan Möbius's avatar
 
Jan Möbius committed
351 352 353 354

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

355 356 357 358 359 360 361
  if (_and_static)
    add_library (${_target}Static STATIC ${ARGN})

    # set common target properties defined in common.cmake
    acg_set_target_props (${_target}Static)
    
    if (NOT APPLE)
362
# todo : create correct so names
363
      set_target_properties (${_target}Static PROPERTIES 
364 365 366 367 368 369 370 371 372
                             LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
                            )

# todo : rename the lib to omit the static name extension
      install (TARGETS ${_target}Static
               RUNTIME DESTINATION ${ACG_PROJECT_BINDIR}
               LIBRARY DESTINATION ${ACG_PROJECT_LIBDIR}
               ARCHIVE DESTINATION ${ACG_PROJECT_LIBDIR} )

373 374 375
    endif ()
  endif ()

Jan Möbius's avatar
 
Jan Möbius committed
376 377 378 379 380 381 382 383 384
  if (WIN32)
    # copy exe file to "Build" directory
    # Visual studio will create this file in a subdirectory so we can't use
    # RUNTIME_OUTPUT_DIRECTORY directly here
    if (${_type} STREQUAL SHARED)
      add_custom_command (TARGET ${_target} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_target}.dll
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
385
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_BINDIR}/${_target}.dll)
Jan Möbius's avatar
 
Jan Möbius committed
386 387 388 389 390 391 392 393 394 395
    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 ()
      add_custom_command (TARGET ${_target} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_target}.dll
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR}/${_target}.dll)
    endif ()
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
396 397 398 399 400 401 402
    if (${_type} STREQUAL SHARED OR ${_type} STREQUAL STATIC)
      add_custom_command (TARGET ${_target} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_target}.lib
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/${_target}.lib)
    endif ()
Jan Möbius's avatar
 
Jan Möbius committed
403 404
  elseif (APPLE AND NOT ACG_PROJECT_MACOS_BUNDLE)
    if (${_type} STREQUAL SHARED)
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
405
      add_custom_command (TARGET ${_target} POST_BUILD
Jan Möbius's avatar
 
Jan Möbius committed
406 407
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
408 409
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${_target}.dylib
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/lib${_target}.dylib)
Jan Möbius's avatar
 
Jan Möbius committed
410 411 412 413
    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 ()
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
414
      add_custom_command (TARGET ${_target} POST_BUILD
Jan Möbius's avatar
 
Jan Möbius committed
415 416
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
417 418
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${_target}.so
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR}/lib${_target}.so)
419 420 421 422 423 424 425 426 427 428 429 430 431
    elseif (${_type} STREQUAL STATIC)
      add_custom_command (TARGET ${_target} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${_target}.a
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/lib${_target}.a)
    endif ()
    if (_and_static)
      add_custom_command (TARGET ${_target}Static POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${_target}Static.a
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/lib${_target}.a)
Jan Möbius's avatar
 
Jan Möbius committed
432
    endif ()
433 434 435 436 437 438 439 440

  elseif (NOT APPLE AND _and_static)
      add_custom_command (TARGET ${_target}Static POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${_target}Static.a
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_LIBDIR}/lib${_target}.a)

Jan Möbius's avatar
 
Jan Möbius committed
441
  endif ()
442 443
  
  if (NOT ACG_PROJECT_MACOS_BUNDLE OR NOT APPLE)
444
    if (${_type} STREQUAL SHARED OR ${_type} STREQUAL STATIC )
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
445 446 447 448
      install (TARGETS ${_target}
               RUNTIME DESTINATION ${ACG_PROJECT_BINDIR}
               LIBRARY DESTINATION ${ACG_PROJECT_LIBDIR}
               ARCHIVE DESTINATION ${ACG_PROJECT_LIBDIR})
Jan Möbius's avatar
 
Jan Möbius committed
449 450 451 452 453
    elseif (${_type} STREQUAL MODULE)
      install (TARGETS ${_target} DESTINATION ${ACG_PROJECT_PLUGINDIR})
    endif ()
  endif ()
endfunction ()