-
Notifications
You must be signed in to change notification settings - Fork 5
/
LanguageStandards.cmake
86 lines (78 loc) · 2.71 KB
/
LanguageStandards.cmake
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
#
# Copyright (C) 2021 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
#
#
# Offers a simple function to set a number of targets to follow the company
# wide C/C++ standard version. One can bypass some of these standards through
# the following options:
#
# C_EXTENSIONS_ON: Turns on C_EXTENSIONS in the target properties if specified (see: https://cmake.org/cmake/help/latest/prop_tgt/C_EXTENSIONS.html)
#
# Single Value Options:
#
# C: C language standard to follow, current support values are 90, 99, and 11 (see: https://cmake.org/cmake/help/latest/prop_tgt/C_STANDARD.html)
# CXX: C++ language standard to follow, current supported values are 98, 11, 14, 17, and 20 (see: https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html)
#
# Usage: set a target to conform to company standard
#
# add_executable(target_name main.cpp)
# swift_set_language_standards(target_name)
#
# Usage: specify your own standards (C++17 and C98) on multiple targets
#
# add_library(library_target file1.c file2.cc)
# add_executable(executable_target main.cpp)
# target_link_libraries(executable_target PRIVATE library_target)
#
# swift_set_language_standards(executable_target library_target
# C 99
# CXX 17
# )
#
function(swift_set_language_standards)
set(argOption C_EXTENSIONS_ON)
set(argSingle "C" "CXX")
set(argMulti "")
cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
if(NOT x_C)
set(x_C 99)
endif()
if(NOT x_CXX)
set(x_CXX 14)
endif()
set(C_EXTENSIONS ON)
if(NOT x_C_EXTENSIONS_ON)
set(C_EXTENSIONS OFF)
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "IAR")
set_target_properties(${x_UNPARSED_ARGUMENTS}
PROPERTIES
C_STANDARD_REQUIRED ON
C_EXTENSIONS ${C_EXTENSIONS}
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
target_compile_options(${x_UNPARSED_ARGUMENTS} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:--strict>)
if(NOT ${C_EXTENSIONS})
target_compile_options(${x_UNPARSED_ARGUMENTS} PRIVATE $<$<COMPILE_LANGUAGE:C>:--strict>)
endif()
else()
set_target_properties(${x_UNPARSED_ARGUMENTS}
PROPERTIES
C_STANDARD ${x_C}
C_STANDARD_REQUIRED ON
C_EXTENSIONS ${C_EXTENSIONS}
CXX_STANDARD ${x_CXX}
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
endif()
endfunction()