Debian Codegraph @

cmake/BuildUtils.cmake

raw
1 # Distributed under the original FontForge BSD 3-clause license
2
3 #[=======================================================================[.rst:
4 BuiltUtils
5 ----------
6
7 ``build_option`` standardises the way in which to specify a build option.
8 Supports any type that the ``set()`` function supports. Options are
9 stored in the cache.
10
11 For ``BOOL`` options, passing in a 5th parameter (after the description)
12 will lead to that parameter being evaluated. If it evaluates to false,
13 the build option is forced to ``OFF``.
14
15 The ``AUTO`` type is for tri-state Boolean options. The ``value`` parameter
16 is ignored; it will always be initialised to ``AUTO``.
17
18 The ``ENUM`` type is for options that may be one of a number of defined
19 values. All arguments after the description are treated as allowed values
20 of the enumeration.
21
22 All other types are passed directly to ``set()``.
23
24 ``print_build_options`` lists all build options, along with their
25 current status.
26
27 ``set_default_build_type sets the default build type if none is
28 explicitly specified.
29
30 ``set_default_rpath`` sets the default RPATH to be used on platforms
31 that support it. Can only be called after GNUInstallDirs has been
32 included.
33
34 ``enable_sanitizer`` checks if the specified sanitizer is supported,
35 and adds the required compiler flags to enable it if so. If unsupported,
36 it will error out.
37
38 #]=======================================================================]
39
40 function(build_option variable type value description)
41 if(${type} STREQUAL BOOL)
42 if(${ARGC} EQUAL 4)
43 option("${variable}" "${description}" "${value}")
44 elseif(${ARGC} EQUAL 5)
45 if(${ARGV4})
46 option("${variable}" "${description}" "${value}")
47 else()
48 set("${variable}" OFF CACHE BOOL "${description}" FORCE)
49 endif()
50 else()
51 message(FATAL_ERROR "Invalid number of arguments for dependent option")
52 endif()
53 elseif(${type} STREQUAL AUTO)
54 set("${variable}" AUTO CACHE STRING "${description}")
55 set_property(CACHE "${variable}" PROPERTY STRINGS AUTO ON OFF)
56 elseif(${type} STREQUAL ENUM)
57 if(${ARGC} LESS 5)
58 message(FATAL_ERROR "Must pass at least one enum type")
59 endif()
60 set("${variable}" "${value}" CACHE STRING "${description} Valid values: ${ARGN}")
61 set_property(CACHE "${variable}" PROPERTY STRINGS ${ARGN})
62 if(NOT "${${variable}}" IN_LIST ARGN)
63 message(FATAL_ERROR "'${${variable}}' is not a valid value for ${variable}, expect one of ${ARGN}")
64 endif()
65 else()
66 set("${variable}" "${value}" CACHE "${type}" "${description}")
67 endif()
68
69 if(NOT "${variable}" IN_LIST BUILD_OPTIONS)
70 set(BUILD_OPTIONS "${BUILD_OPTIONS};${variable}" CACHE INTERNAL "List of build options")
71 endif()
72 endfunction()
73
74 function(print_build_options)
75 message(STATUS "Build options: ")
76 foreach(opt ${BUILD_OPTIONS})
77 if("${${opt}}" STREQUAL "AUTO" AND DEFINED ${opt}_RESULT)
78 if(${opt}_RESULT)
79 message(STATUS " ${opt} = ${${opt}} => ON")
80 else()
81 message(STATUS " ${opt} = ${${opt}} => OFF")
82 endif()
83 else()
84 message(STATUS " ${opt} = ${${opt}}")
85 endif()
86 endforeach()
87 endfunction()
88
89 function(set_default_build_type default_build_type)
90 # https://blog.kitware.com/cmake-and-the-default-build-type
91 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
92 message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
93 set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
94 STRING "Choose the type of build." FORCE)
95 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
96 "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
97 endif()
98 if(NOT "CMAKE_BUILD_TYPE" IN_LIST BUILD_OPTIONS)
99 set(BUILD_OPTIONS "${BUILD_OPTIONS};CMAKE_BUILD_TYPE" CACHE INTERNAL "List of build options")
100 endif()
101 endfunction()
102
103 function(set_default_rpath)
104 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" _present)
105 if(_present LESS 0)
106 list(FIND CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}" _present)
107 if(_present LESS 0)
108 list(APPEND CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
109 endif()
110 endif()
111 if(APPLE)
112 list(APPEND CMAKE_INSTALL_RPATH "@loader_path/../lib")
113 list(APPEND CMAKE_INSTALL_RPATH "@loader_path/../..") # For fontforge.so
114 endif()
115 set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_RPATH} PARENT_SCOPE)
116 endfunction()
117
118 function(enable_sanitizer type)
119 if("${type}" STREQUAL "none")
120 return()
121 elseif(NOT CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
122 message(FATAL_ERROR "Require a GCC-like compiler to enable sanitizers.")
123 endif()
124
125 include(CheckCCompilerFlag)
126 include(CMakePushCheckState)
127
128 cmake_push_check_state(RESET)
129 set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=${type}")
130 check_c_compiler_flag("-fsanitize=${type} -fno-omit-frame-pointer" "_sanitizer_${type}")
131 cmake_pop_check_state()
132
133 if(NOT _sanitizer_${type})
134 message(FATAL_ERROR "Sanitizer flags not supported: -fsanitize=${type} -fno-omit-frame-pointer")
135 endif()
136
137 # kind of dirty but what can you do
138 add_compile_options("-fsanitize=${type}" "-fno-omit-frame-pointer")
139 link_libraries("-fsanitize=${type}")
140 endfunction()
141
142 function(set_font_family type value)
143 if("${type}" STREQUAL "ui")
144 if("${value}" STREQUAL "default")
145 if(APPLE)
146 set(FONTFORGE_UI_FAMILIES "system-ui,Deja Vu Sans,Lucida Grande,sans" PARENT_SCOPE)
147 elseif(WIN32 OR CYGWIN)
148 set(FONTFORGE_UI_FAMILIES "windows-cjk-workaround,system-ui,Deja Vu Sans,Calibri" PARENT_SCOPE)
149 else()
150 set(FONTFORGE_UI_FAMILIES "system-ui,Deja Vu Sans" PARENT_SCOPE)
151 endif()
152 else()
153 set(FONTFORGE_UI_FAMILIES "${value}" PARENT_SCOPE)
154 endif()
155 elseif("${type}" STREQUAL "label")
156 if("${value}" STREQUAL "default")
157 if(APPLE)
158 set(FONTFORGE_LABEL_FAMILIES "Deja Vu Sans,Lucida Grande,unifont,unifont upper" PARENT_SCOPE)
159 elseif(WIN32 OR CYGWIN)
160 set(FONTFORGE_LABEL_FAMILIES "Deja Vu Sans,Calibri,unifont,unifont upper" PARENT_SCOPE)
161 else()
162 set(FONTFORGE_LABEL_FAMILIES "Deja Vu Sans,unifont,unifont upper" PARENT_SCOPE)
163 endif()
164 else()
165 set(FONTFORGE_LABEL_FAMILIES "${value}" PARENT_SCOPE)
166 endif()
167 elseif("${type}" STREQUAL "serif")
168 if("${value}" STREQUAL "default")
169 if(APPLE)
170 set(FONTFORGE_SERIF_FAMILIES "serif" PARENT_SCOPE)
171 elseif(WIN32 OR CYGWIN)
172 set(FONTFORGE_SERIF_FAMILIES "serif" PARENT_SCOPE)
173 else()
174 set(FONTFORGE_SERIF_FAMILIES "serif" PARENT_SCOPE)
175 endif()
176 else()
177 set(FONTFORGE_SERIF_FAMILIES "${value}" PARENT_SCOPE)
178 endif()
179 elseif("${type}" STREQUAL "mono")
180 if("${value}" STREQUAL "default")
181 if(APPLE)
182 set(FONTFORGE_MONO_FAMILIES "monospace,unifont" PARENT_SCOPE)
183 elseif(WIN32 OR CYGWIN)
184 set(FONTFORGE_MONO_FAMILIES "monospace,unifont" PARENT_SCOPE)
185 else()
186 set(FONTFORGE_MONO_FAMILIES "monospace,unifont" PARENT_SCOPE)
187 endif()
188 else()
189 set(FONTFORGE_MONO_FAMILIES "${value}" PARENT_SCOPE)
190 endif()
191 endif()
192 endfunction()