Debian Codegraph @

debian/external_deps/fetk/cmake/FETKBuildFunctions.cmake

raw
1 # FETKBuildFunctions.cmake
2 #
3 # Author: N.S. Oblath
4 # Created: Apr 6, 2021
5
6
7 # Determine if this is a standalone build or not
8 set(FETK_STANDALONE FALSE CACHE INTERNAL "Flag for whether or not this is a stand-alone build")
9 if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
10 set(FETK_STANDALONE TRUE)
11
12
13
14
15 endif()
16
17
18 ################################################################################
19 # set_basic_vars_and_paths (macro)
20 #
21 # Use this macro just after the project() command to set common variables and paths
22 #
23 # No arguments
24 ################################################################################
25 macro(set_basic_vars_and_paths)
26
27 message(STATUS "FETK set_basic_vars_and_paths: ${FETK_STANDALONE}, ${PROJECT_NAME}")
28 # Conditions for this being used:
29 # 1. FETK_STANDALONE means FETK or one of its subprojects is being built in standalone mode
30 # 2. If FETK is a subproject; in this case FETK_STANDALONE will be false and the project name will be FETK
31 if( FETK_STANDALONE OR (NOT FETK_STANDALONE AND ${PROJECT_NAME} STREQUAL "FETK") )
32
33 ################################################################################
34 # Set paths
35 ################################################################################
36
37 message(STATUS "Setting project paths")
38
39 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
40 set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "Install prefix" FORCE)
41 endif()
42 message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
43
44 set(CMAKE_INSTALL_DATAROOTDIR "share/fetk")
45 include(GNUInstallDirs)
46
47 ################################################################################
48 # Other options and settings
49 ################################################################################
50
51 add_compile_options(-fPIC)
52
53 option(FETK_STATIC_BUILD "Flag to indicate whether a static build should be used (if false, will be dynamic)" ON)
54
55 if(FETK_STATIC_BUILD)
56 add_definitions(-DFETK_STATIC_BUILD)
57 set(BLA_STATIC ON)
58 set(BUILD_SHARED_LIBS OFF)
59 set(CMAKE_LINK_SEARCH_START_STATIC ON)
60 set(CMAKE_LINK_SEARCH_END_STATIC ON)
61 if(WIN32)
62 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
63 list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .lib .a)
64 else()
65 list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a)
66 endif()
67 else()
68 remove_definitions(-DFETK_STATIC_BUILD)
69 set(BLA_STATIC OFF)
70 set(BUILD_SHARED_LIBS ON)
71 set(CMAKE_LINK_SEARCH_START_STATIC OFF)
72 set(CMAKE_LINK_SEARCH_END_STATIC OFF)
73 if(WIN32)
74 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL$<$<CONFIG:Debug>:Debug>")
75 list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .dll)
76 elseif(APPLE)
77 list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .dylib .so)
78 else()
79 list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .so)
80 endif()
81 endif()
82
83 message(STATUS "FETK static build: ${FETK_STATIC_BUILD}")
84 message(STATUS "Building shared libs: ${BUILD_SHARED_LIBS}")
85 message(STATUS "Find-library suffixes: ${CMAKE_FIND_LIBRARY_SUFFIXES}")
86
87
88 ################################################################################
89 # Enable ansi pedantic compiling
90 ################################################################################
91
92 option(ENABLE_PEDANTIC "Enable the pedantic ansi compilation" OFF)
93
94 if(ENABLE_PEDANTIC)
95 add_compile_options("-Wall -pedantic -ansi")
96 message(STATUS "Pedantic compilation enabled")
97 endif()
98
99 ################################################################################
100 # Handle conditional debug building #
101 ################################################################################
102
103 if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
104 set(DEBUG 1)
105 set(HAVE_DEBUG 1)
106 message(STATUS "Debugging compilation enabled")
107 add_definitions(-DHAVE_DEBUG)
108 endif()
109
110 else()
111
112 ################################################################################
113 # Set package variables
114 ################################################################################
115
116 set(FETK_VERSION ${FETK_VERSION})
117 set(PACKAGE_NAME ${PROJECT_NAME})
118 set(PACKAGE_TARNAME ${PROJECT_NAME})
119 set(PACKAGE_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}")
120 set(PACKAGE_STRING "${PROJECT_NAME} ${PACKAGE_VERSION}")
121 set(PACKAGE_BUGREPORT "[email protected]")
122
123 endif()
124
125 set(CMAKE_INSTALL_INCLUDEDIR "include/${PROJECT_NAME}")
126
127 endmacro()
128
129
130 ################################################################################
131 # header_and_type_checks (macro)
132 #
133 # Perform standard checks for headers and types
134 ################################################################################
135 macro(header_and_type_checks)
136
137 ################################################################################
138 # Check for types and required headers #
139 ################################################################################
140
141 include(CheckTypeSize)
142
143 set(CMAKE_EXTRA_INCLUDE_FILES stddef.h sys/stat.h sys/types.h)
144 CHECK_TYPE_SIZE("mode_t" MODE_T)
145 CHECK_TYPE_SIZE("pid_t" PID_T)
146 CHECK_TYPE_SIZE("size_t" SIZE_T)
147 set(CMAKE_EXTRA_INCLUDE_FILES)
148
149 if(NOT HAVE_MODE_T)
150 set(mode_t "int")
151 endif()
152
153 if(NOT HAVE_PID_T)
154 set(pid_t "int")
155 endif()
156
157 if(NOT HAVE_SIZE_T)
158 set(size_t "unsigned int")
159 endif()
160
161
162 include(CheckIncludeFiles)
163
164 CHECK_INCLUDE_FILES("stdlib.h;stdarg.h;string.h;float.h" STDC_HEADERS)
165 if(NOT STDC_HEADERS)
166 message(FATAL_ERROR "Missing standard C headers")
167 endif()
168
169
170 if(NOT WIN32)
171 CHECK_INCLUDE_FILES("unistd.h" HAVE_UNISTD_H)
172 if(NOT HAVE_UNISTD_H)
173 message(FATAL_ERROR "Missing required unistd.h header")
174 endif()
175
176 CHECK_INCLUDE_FILES("sys/socket.h" HAVE_SYS_SOCKET_H)
177 if(NOT HAVE_SYS_SOCKET_H)
178 message(FATAL_ERROR "Missing required sys/socket.h header")
179 endif()
180
181 CHECK_INCLUDE_FILES("sys/time.h" HAVE_SYS_TIME_H)
182 if(NOT HAVE_SYS_TIME_H)
183 message(FATAL_ERROR "Missing required sys/time.h header")
184 endif()
185
186 CHECK_INCLUDE_FILES("sys/un.h" HAVE_SYS_UN_H)
187 if(NOT HAVE_SYS_UN_H)
188 message(FATAL_ERROR "Missing required sys/un.h header")
189 endif()
190
191 CHECK_INCLUDE_FILES("sys/wait.h" HAVE_SYS_WAIT_H)
192 if(NOT HAVE_SYS_WAIT_H)
193 message(FATAL_ERROR "Missing required sys/wait.h header")
194 endif()
195
196 CHECK_INCLUDE_FILES("netinet/in.h" HAVE_NETINET_IN_H)
197 if(NOT HAVE_NETINET_IN_H)
198 message(FATAL_ERROR "Missing required netinet/in.h header")
199 endif()
200
201 CHECK_INCLUDE_FILES("arpa/inet.h" HAVE_ARPA_INET_H)
202 if(NOT HAVE_ARPA_INET_H)
203 message(FATAL_ERROR "Missing required arpa/inet.h header")
204 endif()
205
206 CHECK_INCLUDE_FILES("netdb.h" HAVE_NETDB_H)
207 if(NOT HAVE_UNISTD_H)
208 message(FATAL_ERROR "Missing required netdb.h header")
209 endif()
210 list(APPEND MALOC_REQUIRED_HEADERS "netdb.h")
211 endif()
212
213 CHECK_INCLUDE_FILES("sys/stat.h" HAVE_SYS_STAT_H)
214 if(NOT HAVE_SYS_STAT_H)
215 message(FATAL_ERROR "Missing required sys/stat.h header")
216 endif()
217
218 CHECK_INCLUDE_FILES("sys/types.h" HAVE_SYS_TYPES_H)
219 if(NOT HAVE_SYS_TYPES_H)
220 message(FATAL_ERROR "Missing required sys/types.h header")
221 endif()
222
223 CHECK_INCLUDE_FILES("fcntl.h" HAVE_FCNTL_H)
224 if(NOT HAVE_FCNTL_H)
225 message(FATAL_ERROR "Missing required rcntl.h header")
226 endif()
227
228 CHECK_INCLUDE_FILES("rpc/rpc.h" HAVE_RPC_RPC_H)
229 if(NOT HAVE_RPC_RPC_H)
230 message(WARNING "Missing/invalid rpc/rpc.h header. XDR support disabled")
231 set(HAVE_XDR NO)
232 else()
233 list(APPEND MALOC_REQUIRED_HEADERS "rpc/rpc.h")
234 set(HAVE_XDR YES)
235 endif()
236
237
238 ################################################################################
239 # Check for a few required functions and symbols #
240 ################################################################################
241
242 include(CheckFunctionExists)
243
244 CHECK_FUNCTION_EXISTS(getcwd HAVE_GETCWD)
245
246 if(NOT HAVE_GETCWD)
247 message(WARNING "The getcwd function was not found")
248 endif()
249
250
251 include(CheckSymbolExists)
252
253 CHECK_SYMBOL_EXISTS(O_NONBLOCK "fcntl.h" HAVE_O_NONBLOCK)
254
255 if(NOT HAVE_O_NONBLOCK)
256 message(WARNING "The O_NONBLOCK symbol was not found")
257 endif()
258
259 endmacro()
260
261
262 ################################################################################
263 # find_or_build (function)
264 #
265 # Finds a library or builds it if it isn't found
266 ################################################################################
267 function(find_or_build)
268
269 set(options OPTIONAL FAST)
270 set(oneValueArgs DESTINATION RENAME)
271 set(multiValueArgs TARGETS CONFIGURATIONS)
272
273 cmake_parse_arguments(PARSE_ARGV 0 FOB options oneValueArgs multiValueArgs )
274
275
276 endfunction()
277
278
279 ################################################################################
280 # setup_for_build (macro)
281 #
282 # Prepares the variables that will be used to build the library
283 ################################################################################
284 macro(setup_for_build)
285 set(LIB_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR})
286 set(ALLOWABLE_TYPES "SOURCES;EXTERNAL_HEADERS;INTERNAL_HEADERS")
287 set(SOURCES)
288 set(EXTERNAL_HEADERS)
289 set(INTERNAL_HEADERS)
290 endmacro()
291
292
293 ################################################################################
294 # add_build_items (macro)
295 #
296 # Adds items (sources, internal headers, and external headers) for the build
297 #
298 # Arguments:
299 # TYPE: options are
300 # - SOURCES
301 # - INTERNAL_HEADERS
302 # - EXTERNAL HEADERS
303 # ITEMS: space-separated list of files (relative or absolute paths)
304 ################################################################################
305 macro(add_build_items)
306
307 set(oneValueArgs TYPE)
308 set(multiValueArgs ITEMS)
309 #cmake_parse_arguments(ABI "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
310 cmake_parse_arguments(ABI "" "TYPE" "ITEMS" ${ARGN})
311
312 message(STATUS "Type: ${ABI_TYPE}")
313 message(STATUS "Items: ${ABI_ITEMS}")
314
315 # Make sure the TYPE argument is valid
316 if(NOT "${ABI_TYPE}" IN_LIST ALLOWABLE_TYPES)
317 message(
318 FATAL_ERROR
319 "Passed an invalid type to add_build_items macro. "
320 "Must be one of ${ALLOWABLE_TYPES}. "
321 "Was ${ABI_TYPE}."
322 )
323 endif()
324
325 # Add each item successively
326 foreach(ITEM ${ABI_ITEMS})
327 get_filename_component(ITEM_PATH ${ITEM} ABSOLUTE)
328
329 # Add the item to the appropriately typed list
330 list(APPEND ${ABI_TYPE} "${ITEM_PATH}")
331 endforeach()
332
333 # If this folder is not the library-build directory, push the updated item list to the parent scope
334 if(NOT LIB_BUILD_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
335 set(${ABI_TYPE} ${${ABI_TYPE}} PARENT_SCOPE)
336 endif()
337
338 endmacro()