mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
154 lines
4.3 KiB
CMake
154 lines
4.3 KiB
CMake
include(CMakeParseArguments)
|
|
|
|
function(precondition var)
|
|
cmake_parse_arguments(
|
|
PRECONDITION # prefix
|
|
"NEGATE" # options
|
|
"MESSAGE" # single-value args
|
|
"" # multi-value args
|
|
${ARGN})
|
|
|
|
if (PRECONDITION_NEGATE)
|
|
if (${var})
|
|
if (PRECONDITION_MESSAGE)
|
|
message(FATAL_ERROR "Error! ${PRECONDITION_MESSAGE}")
|
|
else()
|
|
message(FATAL_ERROR "Error! Variable ${var} is true or not empty. The value of ${var} is ${${var}}.")
|
|
endif()
|
|
endif()
|
|
else()
|
|
if (NOT ${var})
|
|
if (PRECONDITION_MESSAGE)
|
|
message(FATAL_ERROR "Error! ${PRECONDITION_MESSAGE}")
|
|
else()
|
|
message(FATAL_ERROR "Error! Variable ${var} is false, empty or not set.")
|
|
endif()
|
|
endif()
|
|
endif()
|
|
endfunction()
|
|
|
|
# Assert is 'NOT ${LHS} ${OP} ${RHS}' is true.
|
|
function(precondition_binary_op OP LHS RHS)
|
|
cmake_parse_arguments(
|
|
PRECONDITIONBINOP # prefix
|
|
"NEGATE" # options
|
|
"MESSAGE" # single-value args
|
|
"" # multi-value args
|
|
${ARGN})
|
|
|
|
if (PRECONDITIONBINOP_NEGATE)
|
|
if (${LHS} ${OP} ${RHS})
|
|
if (PRECONDITIONBINOP_MESSAGE)
|
|
message(FATAL_ERROR "Error! ${PRECONDITIONBINOP_MESSAGE}")
|
|
else()
|
|
message(FATAL_ERROR "Error! ${LHS} ${OP} ${RHS} is true!")
|
|
endif()
|
|
endif()
|
|
else()
|
|
if (NOT ${LHS} ${OP} ${RHS})
|
|
if (PRECONDITIONBINOP_MESSAGE)
|
|
message(FATAL_ERROR "Error! ${PRECONDITIONBINOP_MESSAGE}")
|
|
else()
|
|
message(FATAL_ERROR "Error! ${LHS} ${OP} ${RHS} is false!")
|
|
endif()
|
|
endif()
|
|
endif()
|
|
endfunction()
|
|
|
|
# Translate a yes/no variable to the presence of a given string in a
|
|
# variable.
|
|
#
|
|
# Usage:
|
|
# translate_flag(is_set flag_name var_name)
|
|
#
|
|
# If is_set is true, sets ${var_name} to ${flag_name}. Otherwise,
|
|
# unsets ${var_name}.
|
|
function(translate_flag is_set flag_name var_name)
|
|
if(${is_set})
|
|
set("${var_name}" "${flag_name}" PARENT_SCOPE)
|
|
else()
|
|
set("${var_name}" "" PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
macro(translate_flags prefix options)
|
|
foreach(var ${options})
|
|
translate_flag("${${prefix}_${var}}" "${var}" "${prefix}_${var}_keyword")
|
|
endforeach()
|
|
endmacro()
|
|
|
|
# Set ${outvar} to ${${invar}}, asserting if ${invar} is not set.
|
|
function(precondition_translate_flag invar outvar)
|
|
precondition(${invar})
|
|
set(${outvar} "${${invar}}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
function(is_build_type_optimized build_type result_var_name)
|
|
if("${build_type}" STREQUAL "Debug")
|
|
set("${result_var_name}" FALSE PARENT_SCOPE)
|
|
elseif("${build_type}" STREQUAL "RelWithDebInfo" OR
|
|
"${build_type}" STREQUAL "Release" OR
|
|
"${build_type}" STREQUAL "MinSizeRel")
|
|
set("${result_var_name}" TRUE PARENT_SCOPE)
|
|
else()
|
|
message(FATAL_ERROR "Unknown build type: ${build_type}")
|
|
endif()
|
|
endfunction()
|
|
|
|
function(is_build_type_with_debuginfo build_type result_var_name)
|
|
if("${build_type}" STREQUAL "Debug" OR
|
|
"${build_type}" STREQUAL "RelWithDebInfo")
|
|
set("${result_var_name}" TRUE PARENT_SCOPE)
|
|
elseif("${build_type}" STREQUAL "Release" OR
|
|
"${build_type}" STREQUAL "MinSizeRel")
|
|
set("${result_var_name}" FALSE PARENT_SCOPE)
|
|
else()
|
|
message(FATAL_ERROR "Unknown build type: ${build_type}")
|
|
endif()
|
|
endfunction()
|
|
|
|
# Set variable to value if value is not null or false. Otherwise set variable to
|
|
# default_value.
|
|
function(set_with_default variable value)
|
|
cmake_parse_argument(
|
|
SWD
|
|
""
|
|
"DEFAULT"
|
|
"" ${ARGN})
|
|
precondition(SWD_DEFAULT
|
|
MESSAGE "Must specify a default argument")
|
|
if (value)
|
|
set(${variable} ${value} PARENT_SCOPE)
|
|
else()
|
|
set(${variable} ${SWD_DEFAULT} PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
function(swift_create_post_build_symlink target)
|
|
set(options IS_DIRECTORY)
|
|
set(oneValueArgs SOURCE DESTINATION WORKING_DIRECTORY COMMENT)
|
|
cmake_parse_arguments(CS
|
|
"${options}"
|
|
"${oneValueArgs}"
|
|
""
|
|
${ARGN})
|
|
|
|
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
|
|
if(CS_IS_DIRECTORY)
|
|
set(cmake_symlink_option "copy_directory")
|
|
else()
|
|
set(cmake_symlink_option "copy_if_different")
|
|
endif()
|
|
else()
|
|
set(cmake_symlink_option "create_symlink")
|
|
endif()
|
|
|
|
add_custom_command(TARGET "${target}" POST_BUILD
|
|
COMMAND
|
|
"${CMAKE_COMMAND}" "-E" "${cmake_symlink_option}"
|
|
"${CS_SOURCE}"
|
|
"${CS_DESTINATION}"
|
|
WORKING_DIRECTORY "${CS_WORKING_DIRECTORY}"
|
|
COMMENT "${CS_COMMENT}")
|
|
endfunction()
|