SuperBuild_Macro.cmake 5.84 KB
Newer Older
1 2 3 4 5
include(CMakeParseArguments)

# Macro SETUP_SYSTEM_LIBRARY : defines USE_SYSTEM_XXX option, initialize
# internal variables
macro(SETUP_SYSTEM_LIBRARY)
6
  cmake_parse_arguments(NEW_SYSLIB  "" "PROJECT;DEFAULT" "" ${ARGN})
7 8 9 10
  option(USE_SYSTEM_${NEW_SYSLIB_PROJECT}
    "  Use a system build of ${NEW_SYSLIB_PROJECT}."
    ${NEW_SYSLIB_DEFAULT}
    )
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  set(SYSTEM_${NEW_SYSLIB_PROJECT}_CMAKE_CACHE)
endmacro(SETUP_SYSTEM_LIBRARY)

# Macro ADD_SYSTEM_LOCATION : define variables to specific system locations
macro(ADD_SYSTEM_LOCATION)
  cmake_parse_arguments(NEW_SYSLOC  "" "PROJECT" "VARIABLES" ${ARGN})
  foreach(var ${NEW_SYSLOC_VARIABLES})
    set(${var} "" CACHE FILEPATH "Set a custom system location")
    mark_as_advanced(${var})
    if(USE_SYSTEM_${NEW_SYSLOC_PROJECT})
      set_property(CACHE ${var} PROPERTY TYPE FILEPATH)
    else()
      set_property(CACHE ${var} PROPERTY TYPE INTERNAL)
    endif()
    # add variable to cache if not empty
    if(NOT ${var} STREQUAL "")
      # additional argument for CMake cache
      set(ITEM_FOR_CMAKE_CACHE "-D${var}:STRING=${${var}}")
      list(APPEND SYSTEM_${NEW_SYSLOC_PROJECT}_CMAKE_CACHE ${ITEM_FOR_CMAKE_CACHE})
    endif()
  endforeach(var)
endmacro(ADD_SYSTEM_LOCATION)
33 34 35

# Macro ADD_SYSTEM_PREFIX : defines a system prefix for the given project
#   - creates a cache variable SYSTEM_${PROJECT}_PREFIX
36 37
#   - if CMAKE_ALIAS is defined, it is used to add an entry in cache
#       -D${CMAKE_ALIAS}:PATH=${SYSTEM_${PROJECT}_PREFIX}
38
macro(ADD_SYSTEM_PREFIX)
39
  cmake_parse_arguments(NEW_SYSPREFIX  "" "PROJECT;CMAKE_ALIAS" "" ${ARGN})
40 41 42 43 44 45 46
  set(SYSTEM_${NEW_SYSPREFIX_PROJECT}_PREFIX "" CACHE PATH "Set a custom system prefix")
  mark_as_advanced(SYSTEM_${NEW_SYSPREFIX_PROJECT}_PREFIX)
  if(USE_SYSTEM_${NEW_SYSPREFIX_PROJECT})
    set_property(CACHE SYSTEM_${NEW_SYSPREFIX_PROJECT}_PREFIX PROPERTY TYPE PATH)
  else()
    set_property(CACHE SYSTEM_${NEW_SYSPREFIX_PROJECT}_PREFIX PROPERTY TYPE INTERNAL)
  endif()
47 48 49 50 51 52 53
  if(NOT ${NEW_SYSPREFIX_CMAKE_ALIAS} STREQUAL "")
    if(NOT SYSTEM_${NEW_SYSPREFIX_PROJECT}_PREFIX STREQUAL "")
      set(ITEM_FOR_CMAKE_CACHE
        "-D${NEW_SYSPREFIX_CMAKE_ALIAS}:PATH=${SYSTEM_${NEW_SYSPREFIX_PROJECT}_PREFIX}")
      list(APPEND SYSTEM_${NEW_SYSLOC_PROJECT}_CMAKE_CACHE ${ITEM_FOR_CMAKE_CACHE})
    endif()
  endif()
54
endmacro(ADD_SYSTEM_PREFIX)
55 56 57 58 59

# Macro SETUP_SUPERBUILD
# Initialize usefull variables to build a superbuild project
macro(SETUP_SUPERBUILD)
  cmake_parse_arguments(NEW_SB "" "PROJECT" "" ${ARGN})
Rashad Kanavath's avatar
Rashad Kanavath committed
60 61
  #set_property(GLOBAL PROPERTY prop_${project}_DEPENDENCIES "")
  set(${NEW_SB_PROJECT}_DEPENDENCIES "")
62 63 64
  set(${NEW_SB_PROJECT}_SB_BUILD_DIR ${CMAKE_BINARY_DIR}/${NEW_SB_PROJECT}/build)
  set(${NEW_SB_PROJECT}_SB_SRC ${CMAKE_BINARY_DIR}/${NEW_SB_PROJECT}/src/${NEW_SB_PROJECT})
  set(${NEW_SB_PROJECT}_SB_CONFIG)
65
  set(_SB_${NEW_SB_PROJECT}_ROOT ${SB_INSTALL_PREFIX})
66
endmacro(SETUP_SUPERBUILD)
67

68
macro(ADDTO_DEPENDENCIES_IF_NOT_SYSTEM project)
69 70
  foreach(dep ${ARGN})
    if(NOT USE_SYSTEM_${dep})
71
      #      get_property(old_${project}_DEPENDENCIES GLOBAL PROPERTY prop_${project}_DEPENDENCIES)
Rashad Kanavath's avatar
Rashad Kanavath committed
72
      list(APPEND ${project}_DEPENDENCIES "${dep}")
73
      #     set_property(GLOBAL PROPERTY prop_${project}_DEPENDENCIES "${old_${project}_DEPENDENCIES}")
74
    endif()
75 76
    string(TOLOWER ${dep} dep_lower)
    include(External_${dep_lower})
77 78 79
  endforeach()
endmacro(ADDTO_DEPENDENCIES_IF_NOT_SYSTEM)

80
# Macro to add a cmake variable to ${proj}_SB_CONFIG (var type: string)
Rashad Kanavath's avatar
Rashad Kanavath committed
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
macro(ADD_SUPERBUILD_CMAKE_VAR project var_name)
  set (extra_macro_args ${ARGN})
  # Did we get any optional args?
  list(LENGTH extra_macro_args num_extra_args)
  if (${num_extra_args} GREATER 0)
    list(GET extra_macro_args 0 optional_arg)
    set(var_type ${optional_arg})
  else()
    #try a guess for type from variable name using
    #CMake's variable naming convention!
    #convert to string to list so we ZLIB_INCLUDE_DIR as ZLIB, INCLUDE, DIR
    string(REPLACE "_" ";" var_name_parts ${var_name})
    #reverse the list.
    #We are only interested in the last part _DIR(s) or _LIBRARY, _ROOT etc..
    list(REVERSE var_name_parts)
    #simply pop first item to have the last part of var_name
    list(GET var_name_parts 0 var_name_last_part)

    #set var_type.
    if( "${var_name_last_part}" STREQUAL "DIR")
      set(var_type PATH)
    elseif("${var_name_last_part}" STREQUAL "LIBRARY")
      set(var_type FILEPATH)
    else()
      set(var_type STRING)
    endif()
  endif ()
  if(DEFINED _SB_${var_name})
    list(APPEND ${project}_SB_CONFIG
      -D${var_name}:${var_type}=${_SB_${var_name}}
111
      )
Rashad Kanavath's avatar
Rashad Kanavath committed
112 113 114
  elseif(DEFINED ${var_name})
    list(APPEND ${project}_SB_CONFIG
      -D${var_name}:${var_type}=${${var_name}}
115 116
      )
  endif()
Rashad Kanavath's avatar
Rashad Kanavath committed
117

118
endmacro(ADD_SUPERBUILD_CMAKE_VAR)
119

120
# Macro to add a configure variable to ${proj}_SB_CONFIG
121
# optional 3rd argument : suffix to the variable
Rashad Kanavath's avatar
Rashad Kanavath committed
122 123
macro(ADD_SUPERBUILD_CONFIGURE_VAR project var name)
  set(suffix "${ARGV3}")
124
  if(DEFINED _SB_${var})
Rashad Kanavath's avatar
Rashad Kanavath committed
125
    list(APPEND ${project}_SB_CONFIG
126
      ${name}=${_SB_${var}}${suffix}
127 128
      )
  elseif(DEFINED ${var})
Rashad Kanavath's avatar
Rashad Kanavath committed
129
    list(APPEND ${project}_SB_CONFIG
130
      ${name}=${${var}}${suffix}
131 132 133
      )
  endif()
endmacro(ADD_SUPERBUILD_CONFIGURE_VAR)
134 135

macro(SUPERBUILD_PATCH_SOURCE project external_project_step_name)
136 137 138 139 140 141 142 143 144 145 146
  set (extra_args ${ARGN})
  # Did we get any optional args?
  list(LENGTH extra_args num_extra_args)
  if (${num_extra_args} GREATER 0)
    list(GET extra_args 0 patch_dir_arg)
    #if there is third argument set is as ${project}_PATCH_DIR
    set(${project}_PATCH_DIR ${patch_dir_arg})
  else()
    #default value
    set(${project}_PATCH_DIR ${CMAKE_SOURCE_DIR}/patches/${project})
  endif()
147 148 149 150
  ExternalProject_Add_Step(${project} ${external_project_step_name}
    COMMAND
    ${CMAKE_COMMAND}
    -DSOURCE_DIR=${${project}_SB_SRC}
151
    -DPATCH_DIR=${${project}_PATCH_DIR}
152 153 154 155
    -P ${CMAKE_SOURCE_DIR}/CMake/patch.cmake
    DEPENDEES patch update
    DEPENDERS configure
    )
156
endmacro(SUPERBUILD_PATCH_SOURCE)