|
| 1 | +# utils file for projects came from visual studio solution with cmake-converter. |
| 2 | + |
| 3 | +################################################################################ |
| 4 | +# Wrap each token of the command with condition |
| 5 | +################################################################################ |
| 6 | +cmake_policy(PUSH) |
| 7 | +cmake_policy(SET CMP0054 NEW) |
| 8 | +macro(prepare_commands) |
| 9 | + unset(TOKEN_ROLE) |
| 10 | + unset(COMMANDS) |
| 11 | + foreach(TOKEN ${ARG_COMMANDS}) |
| 12 | + if("${TOKEN}" STREQUAL "COMMAND") |
| 13 | + set(TOKEN_ROLE "KEYWORD") |
| 14 | + elseif("${TOKEN_ROLE}" STREQUAL "KEYWORD") |
| 15 | + set(TOKEN_ROLE "CONDITION") |
| 16 | + elseif("${TOKEN_ROLE}" STREQUAL "CONDITION") |
| 17 | + set(TOKEN_ROLE "COMMAND") |
| 18 | + elseif("${TOKEN_ROLE}" STREQUAL "COMMAND") |
| 19 | + set(TOKEN_ROLE "ARG") |
| 20 | + endif() |
| 21 | + |
| 22 | + if("${TOKEN_ROLE}" STREQUAL "KEYWORD") |
| 23 | + list(APPEND COMMANDS "${TOKEN}") |
| 24 | + elseif("${TOKEN_ROLE}" STREQUAL "CONDITION") |
| 25 | + set(CONDITION ${TOKEN}) |
| 26 | + elseif("${TOKEN_ROLE}" STREQUAL "COMMAND") |
| 27 | + list(APPEND COMMANDS "$<$<NOT:${CONDITION}>:${DUMMY}>$<${CONDITION}:${TOKEN}>") |
| 28 | + elseif("${TOKEN_ROLE}" STREQUAL "ARG") |
| 29 | + list(APPEND COMMANDS "$<${CONDITION}:${TOKEN}>") |
| 30 | + endif() |
| 31 | + endforeach() |
| 32 | +endmacro() |
| 33 | +cmake_policy(POP) |
| 34 | + |
| 35 | +################################################################################ |
| 36 | +# Transform all the tokens to absolute paths |
| 37 | +################################################################################ |
| 38 | +macro(prepare_output) |
| 39 | + unset(OUTPUT) |
| 40 | + foreach(TOKEN ${ARG_OUTPUT}) |
| 41 | + if(IS_ABSOLUTE ${TOKEN}) |
| 42 | + list(APPEND OUTPUT "${TOKEN}") |
| 43 | + else() |
| 44 | + list(APPEND OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/${TOKEN}") |
| 45 | + endif() |
| 46 | + endforeach() |
| 47 | +endmacro() |
| 48 | + |
| 49 | +################################################################################ |
| 50 | +# Parse add_custom_command_if args. |
| 51 | +# |
| 52 | +# Input: |
| 53 | +# PRE_BUILD - Pre build event option |
| 54 | +# PRE_LINK - Pre link event option |
| 55 | +# POST_BUILD - Post build event option |
| 56 | +# TARGET - Target |
| 57 | +# OUTPUT - List of output files |
| 58 | +# DEPENDS - List of files on which the command depends |
| 59 | +# COMMANDS - List of commands(COMMAND condition1 commannd1 args1 COMMAND |
| 60 | +# condition2 commannd2 args2 ...) |
| 61 | +# Output: |
| 62 | +# OUTPUT - Output files |
| 63 | +# DEPENDS - Files on which the command depends |
| 64 | +# COMMENT - Comment |
| 65 | +# PRE_BUILD - TRUE/FALSE |
| 66 | +# PRE_LINK - TRUE/FALSE |
| 67 | +# POST_BUILD - TRUE/FALSE |
| 68 | +# TARGET - Target name |
| 69 | +# COMMANDS - Prepared commands(every token is wrapped in CONDITION) |
| 70 | +# NAME - Unique name for custom target |
| 71 | +# STEP - PRE_BUILD/PRE_LINK/POST_BUILD |
| 72 | +################################################################################ |
| 73 | +function(add_custom_command_if_parse_arguments) |
| 74 | + cmake_parse_arguments("ARG" "PRE_BUILD;PRE_LINK;POST_BUILD" "TARGET;COMMENT" "DEPENDS;OUTPUT;COMMANDS" ${ARGN}) |
| 75 | + |
| 76 | + if(WIN32) |
| 77 | + set(DUMMY "cd.") |
| 78 | + elseif(UNIX) |
| 79 | + set(DUMMY "true") |
| 80 | + endif() |
| 81 | + |
| 82 | + prepare_commands() |
| 83 | + prepare_output() |
| 84 | + |
| 85 | + set(DEPENDS "${ARG_DEPENDS}") |
| 86 | + set(COMMENT "${ARG_COMMENT}") |
| 87 | + set(PRE_BUILD "${ARG_PRE_BUILD}") |
| 88 | + set(PRE_LINK "${ARG_PRE_LINK}") |
| 89 | + set(POST_BUILD "${ARG_POST_BUILD}") |
| 90 | + set(TARGET "${ARG_TARGET}") |
| 91 | + if(PRE_BUILD) |
| 92 | + set(STEP "PRE_BUILD") |
| 93 | + elseif(PRE_LINK) |
| 94 | + set(STEP "PRE_LINK") |
| 95 | + elseif(POST_BUILD) |
| 96 | + set(STEP "POST_BUILD") |
| 97 | + endif() |
| 98 | + set(NAME "${TARGET}_${STEP}") |
| 99 | + |
| 100 | + set(OUTPUT "${OUTPUT}" PARENT_SCOPE) |
| 101 | + set(DEPENDS "${DEPENDS}" PARENT_SCOPE) |
| 102 | + set(COMMENT "${COMMENT}" PARENT_SCOPE) |
| 103 | + set(PRE_BUILD "${PRE_BUILD}" PARENT_SCOPE) |
| 104 | + set(PRE_LINK "${PRE_LINK}" PARENT_SCOPE) |
| 105 | + set(POST_BUILD "${POST_BUILD}" PARENT_SCOPE) |
| 106 | + set(TARGET "${TARGET}" PARENT_SCOPE) |
| 107 | + set(COMMANDS "${COMMANDS}" PARENT_SCOPE) |
| 108 | + set(STEP "${STEP}" PARENT_SCOPE) |
| 109 | + set(NAME "${NAME}" PARENT_SCOPE) |
| 110 | +endfunction() |
| 111 | + |
| 112 | +################################################################################ |
| 113 | +# Add conditional custom command |
| 114 | +# |
| 115 | +# Generating Files |
| 116 | +# The first signature is for adding a custom command to produce an output: |
| 117 | +# add_custom_command_if( |
| 118 | +# <OUTPUT output1 [output2 ...]> |
| 119 | +# <COMMANDS> |
| 120 | +# <COMMAND condition command1 [args1...]> |
| 121 | +# [COMMAND condition command2 [args2...]] |
| 122 | +# [DEPENDS [depends...]] |
| 123 | +# [COMMENT comment] |
| 124 | +# |
| 125 | +# Build Events |
| 126 | +# add_custom_command_if( |
| 127 | +# <TARGET target> |
| 128 | +# <PRE_BUILD | PRE_LINK | POST_BUILD> |
| 129 | +# <COMMAND condition command1 [args1...]> |
| 130 | +# [COMMAND condition command2 [args2...]] |
| 131 | +# [COMMENT comment] |
| 132 | +# |
| 133 | +# Input: |
| 134 | +# output - Output files the command is expected to produce |
| 135 | +# condition - Generator expression for wrapping the command |
| 136 | +# command - Command-line(s) to execute at build time. |
| 137 | +# args - Command`s args |
| 138 | +# depends - Files on which the command depends |
| 139 | +# comment - Display the given message before the commands are executed at |
| 140 | +# build time. |
| 141 | +# PRE_BUILD - Run before any other rules are executed within the target |
| 142 | +# PRE_LINK - Run after sources have been compiled but before linking the |
| 143 | +# binary |
| 144 | +# POST_BUILD - Run after all other rules within the target have been |
| 145 | +# executed |
| 146 | +################################################################################ |
| 147 | +function(add_custom_command_if) |
| 148 | + add_custom_command_if_parse_arguments(${ARGN}) |
| 149 | + |
| 150 | + if(OUTPUT AND TARGET) |
| 151 | + message(FATAL_ERROR "Wrong syntax. A TARGET and OUTPUT can not both be specified.") |
| 152 | + endif() |
| 153 | + |
| 154 | + if(OUTPUT) |
| 155 | + add_custom_command(OUTPUT ${OUTPUT} |
| 156 | + ${COMMANDS} |
| 157 | + DEPENDS ${DEPENDS} |
| 158 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 159 | + COMMENT ${COMMENT}) |
| 160 | + elseif(TARGET) |
| 161 | + if(PRE_BUILD AND NOT ${CMAKE_GENERATOR} MATCHES "Visual Studio") |
| 162 | + add_custom_target( |
| 163 | + ${NAME} |
| 164 | + ${COMMANDS} |
| 165 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 166 | + COMMENT ${COMMENT}) |
| 167 | + add_dependencies(${TARGET} ${NAME}) |
| 168 | + else() |
| 169 | + add_custom_command( |
| 170 | + TARGET ${TARGET} |
| 171 | + ${STEP} |
| 172 | + ${COMMANDS} |
| 173 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 174 | + COMMENT ${COMMENT}) |
| 175 | + endif() |
| 176 | + else() |
| 177 | + message(FATAL_ERROR "Wrong syntax. A TARGET or OUTPUT must be specified.") |
| 178 | + endif() |
| 179 | +endfunction() |
| 180 | + |
| 181 | +################################################################################ |
| 182 | +# Use props file for a target and configs |
| 183 | +# use_props(<target> <configs...> <props_file>) |
| 184 | +# Inside <props_file> there are following variables: |
| 185 | +# PROPS_TARGET - <target> |
| 186 | +# PROPS_CONFIG - One of <configs...> |
| 187 | +# PROPS_CONFIG_U - Uppercase PROPS_CONFIG |
| 188 | +# Input: |
| 189 | +# target - Target to apply props file |
| 190 | +# configs - Build configurations to apply props file |
| 191 | +# props_file - CMake script |
| 192 | +################################################################################ |
| 193 | +macro(use_props TARGET CONFIGS PROPS_FILE) |
| 194 | + set(PROPS_TARGET "${TARGET}") |
| 195 | + foreach(PROPS_CONFIG ${CONFIGS}) |
| 196 | + string(TOUPPER "${PROPS_CONFIG}" PROPS_CONFIG_U) |
| 197 | + |
| 198 | + get_filename_component(ABSOLUTE_PROPS_FILE "${PROPS_FILE}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_LIST_DIR}") |
| 199 | + if(EXISTS "${ABSOLUTE_PROPS_FILE}") |
| 200 | + include("${ABSOLUTE_PROPS_FILE}") |
| 201 | + else() |
| 202 | + message(WARNING "Corresponding cmake file from props \"${ABSOLUTE_PROPS_FILE}\" doesn't exist") |
| 203 | + endif() |
| 204 | + endforeach() |
| 205 | +endmacro() |
| 206 | + |
| 207 | +################################################################################ |
| 208 | +# Add compile options to source file |
| 209 | +# source_file_compile_options(<source_file> [compile_options...]) |
| 210 | +# Input: |
| 211 | +# source_file - Source file |
| 212 | +# compile_options - Options to add to COMPILE_FLAGS property |
| 213 | +################################################################################ |
| 214 | +function(source_file_compile_options SOURCE_FILE) |
| 215 | + if("${ARGC}" LESS_EQUAL "1") |
| 216 | + return() |
| 217 | + endif() |
| 218 | + |
| 219 | + get_source_file_property(COMPILE_OPTIONS "${SOURCE_FILE}" COMPILE_OPTIONS) |
| 220 | + |
| 221 | + if(COMPILE_OPTIONS) |
| 222 | + list(APPEND COMPILE_OPTIONS ${ARGN}) |
| 223 | + else() |
| 224 | + set(COMPILE_OPTIONS "${ARGN}") |
| 225 | + endif() |
| 226 | + |
| 227 | + set_source_files_properties("${SOURCE_FILE}" PROPERTIES COMPILE_OPTIONS "${COMPILE_OPTIONS}") |
| 228 | +endfunction() |
| 229 | + |
| 230 | +################################################################################ |
| 231 | +# Default properties of visual studio projects |
| 232 | +################################################################################ |
| 233 | +#set(DEFAULT_CXX_PROPS "${CMAKE_CURRENT_LIST_DIR}/DefaultCXX.cmake") |
| 234 | +#set(DEFAULT_Fortran_PROPS "${CMAKE_CURRENT_LIST_DIR}/DefaultFortran.cmake") |
0 commit comments