-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathMDSplusCheckAddFlags.cmake
More file actions
115 lines (86 loc) · 3.94 KB
/
MDSplusCheckAddFlags.cmake
File metadata and controls
115 lines (86 loc) · 3.94 KB
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
60
61
62
63
64
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
114
115
include_guard(GLOBAL)
include(CheckCompilerFlag)
#
# mdsplus_check_add_flags(
# [COMPILER <flags>]
# [LINKER <flags>]
# [LANGUAGES <langs>])
#
# Check if the COMPILER/LINKER flags are supported for the given LANGUAGES, and add them if they are.
# The compiler flags will be added to CMAKE_${LANG}_FLAGS for the supported languages, and the linker flags will be passed
# to add_link_options() if all languages pass their checks.
# LANGUAGES will default to "C;CXX;Fortran" if not specified.
#
function(mdsplus_check_add_flags)
# The ARGS is a prefix to all parsed argument variables
cmake_parse_arguments(
PARSE_ARGV 0 ARGS
# Booleans
""
# Single-Value
""
# Multi-Value
"COMPILER;LINKER;LANGUAGES"
)
if(NOT DEFINED ARGS_LANGUAGES)
set(ARGS_LANGUAGES C CXX Fortran)
endif()
set(_all_passed TRUE)
foreach(_lang IN ITEMS ${ARGS_LANGUAGES})
# This will be the name of a cache variable that will persist between configures, so it needs to
# be unique and consistent, computed from the requested flags
set(_variable_name "MDSPLUS_CHECK_ADD_FLAGS_${_lang}_${ARGS_COMPILER}_${ARGS_LINKER}")
string(REPLACE ";" "_" _variable_name "${_variable_name}")
string(REPLACE "-" "_" _variable_name "${_variable_name}")
string(REPLACE "/" "_" _variable_name "${_variable_name}")
string(REPLACE "=" "_" _variable_name "${_variable_name}")
if(NOT DEFINED ${_variable_name})
message(CHECK_START "Checking if flags are supported for ${_lang}")
if(DEFINED ARGS_COMPILER)
string(REPLACE ";" " " _compiler_flags_message "${ARGS_COMPILER}")
if(_lang STREQUAL "C")
set(_compiler_flags_message "CFLAGS=${_compiler_flags_message}")
elseif(_lang STREQUAL "CXX")
set(_compiler_flags_message "CXXFLAGS=${_compiler_flags_message}")
elseif(_lang STREQUAL "Fortran")
set(_compiler_flags_message "FCFLAGS=${_compiler_flags_message}")
endif()
message(STATUS " ${_compiler_flags_message}")
endif()
if(DEFINED ARGS_LINKER)
string(REPLACE ";" " " _linker_flags_message "${ARGS_LINKER}")
set(_linker_flags_message "LDFLAGS=${_linker_flags_message}")
message(STATUS " ${_linker_flags_message}")
endif()
# Remove the current flags so they don't interfere with the test
set(_saved_flags "${CMAKE_${_lang}_FLAGS}")
unset(CMAKE_${_lang}_FLAGS)
# Disable the default messaging to replace it with our own
set(CMAKE_REQUIRED_QUIET ON)
# In order to check more than one flag at a time, we use these and set <flag> to ""
set(CMAKE_REQUIRED_FLAGS ${ARGS_COMPILER})
set(CMAKE_REQUIRED_LINK_OPTIONS ${ARGS_LINKER})
check_compiler_flag(${_lang} "" ${_variable_name})
set(_result ${${_variable_name}})
unset(CMAKE_REQUIRED_FLAGS)
unset(CMAKE_REQUIRED_LINK_OPTIONS)
set(CMAKE_REQUIRED_QUIET OFF)
set(CMAKE_${_lang}_FLAGS "${_saved_flags}")
if(${_result})
message(CHECK_PASS "Success")
else()
message(CHECK_FAIL "Failed")
endif()
endif()
if(${_variable_name})
if(DEFINED ARGS_COMPILER)
set(CMAKE_${_lang}_FLAGS "${CMAKE_${_lang}_FLAGS} ${ARGS_COMPILER}" PARENT_SCOPE)
endif()
else()
set(_all_passed FALSE)
endif()
endforeach()
if(DEFINED ARGS_LINKER AND ${_all_passed})
add_link_options(${ARGS_LINKER})
endif()
endfunction()