-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
407 lines (371 loc) · 16.5 KB
/
Copy pathCMakeLists.txt
File metadata and controls
407 lines (371 loc) · 16.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# SPDX-License-Identifier: GPL-3.0-or-later
cmake_minimum_required(VERSION 3.16)
project(copal
VERSION 0.5.0
DESCRIPTION "Lightweight cross-platform GUI library in C"
HOMEPAGE_URL "https://atomskz.github.io/copal"
LANGUAGES C)
# Detect whether we are the top-level project (for add_subdirectory use).
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(COPAL_IS_TOP_LEVEL ON)
else()
set(COPAL_IS_TOP_LEVEL OFF)
endif()
# ---------------------------------------------------------------------------
# Options
# ---------------------------------------------------------------------------
# Examples, tests and install rules default to OFF when copal is embedded via
# add_subdirectory - the host project can still opt in explicitly.
option(COPAL_BUILD_SHARED "Build copal as a shared library" OFF)
option(COPAL_BUILD_EXAMPLES "Build examples" ${COPAL_IS_TOP_LEVEL})
option(COPAL_BUILD_TESTS "Build tests" ${COPAL_IS_TOP_LEVEL})
option(COPAL_ENABLE_SDL "Enable SDL2 platform backend" OFF) # Stage 6
option(COPAL_ENABLE_OPENGL "Enable OpenGL renderer backend" OFF) # Stage 6
option(COPAL_FETCH_SDL2 "Download and build SDL2 via FetchContent" OFF)
option(COPAL_ENABLE_SANITIZERS "Enable ASan/UBSan" OFF)
option(COPAL_ENABLE_COVERAGE "Instrument for gcov/llvm-cov coverage" OFF)
option(COPAL_ENABLE_INSTALL "Enable install rules" ${COPAL_IS_TOP_LEVEL})
option(COPAL_BUILD_BENCHMARKS "Build the headless benchmark harness" OFF)
option(COPAL_HOSTED "Build the hosted (OS libc) support paths; OFF = freestanding core (inject allocator/mutex, load fonts from memory)" ON)
# Single-config generators build with no optimisation at all when
# CMAKE_BUILD_TYPE is empty; default to Release for a top-level build.
if(COPAL_IS_TOP_LEVEL AND NOT CMAKE_CONFIGURATION_TYPES
AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
Debug Release RelWithDebInfo MinSizeRel)
message(STATUS "copal: CMAKE_BUILD_TYPE not set; defaulting to Release")
endif()
include(GNUInstallDirs)
include(cmake/CompilerWarnings.cmake)
# ---------------------------------------------------------------------------
# Version consistency
# ---------------------------------------------------------------------------
# version.h is the source of truth (the ABI handshake compiles it into user
# code); the CMake project version feeds SOVERSION and the package files, so
# the two must never drift apart.
file(STRINGS include/copal/version.h _copal_ver_lines
REGEX "^#define COPAL_VERSION_(MAJOR|MINOR|PATCH) ")
string(REGEX MATCH "COPAL_VERSION_MAJOR ([0-9]+)" _ "${_copal_ver_lines}")
set(_copal_h_ver "${CMAKE_MATCH_1}")
string(REGEX MATCH "COPAL_VERSION_MINOR ([0-9]+)" _ "${_copal_ver_lines}")
string(APPEND _copal_h_ver ".${CMAKE_MATCH_1}")
string(REGEX MATCH "COPAL_VERSION_PATCH ([0-9]+)" _ "${_copal_ver_lines}")
string(APPEND _copal_h_ver ".${CMAKE_MATCH_1}")
if(NOT _copal_h_ver STREQUAL PROJECT_VERSION)
message(FATAL_ERROR
"Version mismatch: include/copal/version.h declares ${_copal_h_ver} "
"but project(copal VERSION ...) declares ${PROJECT_VERSION}; "
"update both together.")
endif()
unset(_copal_ver_lines)
unset(_copal_h_ver)
# ---------------------------------------------------------------------------
# Library target
# ---------------------------------------------------------------------------
if(COPAL_BUILD_SHARED)
set(COPAL_LIB_TYPE SHARED)
else()
set(COPAL_LIB_TYPE STATIC)
endif()
# Core sources: foundation + widget/layout/event/theme/text. The SDL2 platform
# and OpenGL renderer backends are added when their options are enabled (they
# require SDL2/OpenGL); the mock backends live in the separate copal_mocks
# test-support library below. The linear-framebuffer platform
# (src/platform/fb) is software-only and freestanding-safe, so it is always
# built (the freestanding/UEFI reference backend).
set(COPAL_SOURCES
src/core/foundation/version.c
src/core/foundation/error.c
src/core/foundation/abi.c
src/core/foundation/allocator.c
src/core/foundation/cstr.c
src/core/foundation/fmath.c
src/core/foundation/format.c
src/core/foundation/utf8.c
src/core/foundation/mutex.c
src/text/font.c
src/text/stb_impl.c
src/theme/theme.c
src/render/paint_context.c
src/render/image.c
src/render/soft/renderer_soft.c
src/platform/fb/platform_fb.c
src/widget/widget.c
src/layout/vbox.c
src/layout/hbox.c
src/layout/scrollview.c
src/widgets/imageview.c
src/widgets/label.c
src/widgets/button.c
src/widgets/checkbox.c
src/widgets/radiobutton.c
src/widgets/slider.c
src/widgets/menu.c
src/widgets/menubar.c
src/widgets/list.c
src/widgets/progressbar.c
src/widgets/messagebox.c
src/widgets/panel.c
src/widgets/spacer.c
src/widgets/radiogroup.c
src/widgets/combobox.c
src/widgets/textbox.c
src/widgets/tooltip.c
src/app/application.c
src/app/window.c
src/app/timer.c
src/app/animation.c)
add_library(copal ${COPAL_LIB_TYPE} ${COPAL_SOURCES})
add_library(copal::copal ALIAS copal)
target_compile_features(copal PUBLIC c_std_11)
target_include_directories(copal
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/third_party
${CMAKE_CURRENT_SOURCE_DIR}/third_party/stb)
# stb_truetype is third-party. GCC/Clang: build its implementation TU with
# warnings off. MSVC: stb_impl.c silences warnings with a pragma instead — a
# per-file /w would override the target's /W4 and trigger warning D9025.
if(NOT MSVC)
set_source_files_properties(src/text/stb_impl.c PROPERTIES
COMPILE_FLAGS "-w")
endif()
# copal carries no libm dependency: the software renderer and stb_truetype route
# their math through the freestanding cl_* helpers (src/core/foundation/fmath.c).
# -fno-math-errno lets GCC/Clang lower the sqrt/abs builtins to hardware ops with
# no errno-setting libm call, so nothing pulls in -lm.
if(NOT MSVC)
target_compile_options(copal PRIVATE -fno-math-errno)
endif()
# The hosted build's default task-queue mutex uses pthreads; PUBLIC so headless
# consumers that post from their own threads inherit the flags. The freestanding
# build has no built-in mutex (the embedder injects one), so it needs no threads.
if(COPAL_HOSTED)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(copal PUBLIC Threads::Threads)
endif()
# Native backend: SDL2 platform. The SDL platform (including the OpenGL-free
# software window) needs only SDL2; the OpenGL renderer is added on top when
# COPAL_ENABLE_OPENGL is also set. So COPAL_ENABLE_SDL alone yields a working
# software-rendering build with no GL/driver dependency.
if(COPAL_ENABLE_SDL)
# SDL2 comes from either a system/vcpkg package (find_package) or, when
# COPAL_FETCH_SDL2 is set, is downloaded and built as a subproject. The
# latter needs no local SDL2 install and is the easy path on Windows.
if(COPAL_FETCH_SDL2)
include(FetchContent)
# Pinned by commit, not by tag: tags are movable, so a tag pin is
# neither reproducible nor tamper-evident. The hash below is what
# release-2.30.9 pointed at when it was vendored.
FetchContent_Declare(SDL2
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG c98c4fbff6d8f3016a3ce6685bf8f43433c3efcc # release-2.30.9
GIT_SHALLOW TRUE)
set(SDL2_DISABLE_INSTALL ON CACHE BOOL "" FORCE)
set(SDL_TEST OFF CACHE BOOL "" FORCE)
set(SDL_SHARED ON CACHE BOOL "" FORCE)
set(SDL_STATIC OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(SDL2)
else()
find_package(SDL2 REQUIRED)
endif()
target_sources(copal PRIVATE src/platform/sdl/platform_sdl.c)
target_compile_definitions(copal PRIVATE CL_ENABLE_SDL)
target_link_libraries(copal PRIVATE SDL2::SDL2)
if(COPAL_ENABLE_OPENGL)
find_package(OpenGL REQUIRED)
target_sources(copal PRIVATE
src/render/gl/gl_loader.c
src/render/gl/renderer_gl.c)
target_compile_definitions(copal PRIVATE CL_ENABLE_OPENGL)
target_link_libraries(copal PRIVATE OpenGL::GL)
endif()
elseif(COPAL_ENABLE_OPENGL)
message(WARNING "COPAL_ENABLE_OPENGL requires COPAL_ENABLE_SDL; "
"OpenGL renderer disabled.")
endif()
if(COPAL_FETCH_SDL2 AND NOT COPAL_ENABLE_SDL)
message(WARNING "COPAL_FETCH_SDL2 is ON but COPAL_ENABLE_SDL is OFF; "
"SDL2 will not be downloaded. Set COPAL_ENABLE_SDL=ON "
"to build the SDL2 backend.")
endif()
target_compile_definitions(copal PRIVATE COPAL_BUILDING)
# CL_HOSTED gates the paths that need a hosted C runtime: the default malloc
# allocator, the file/system font loaders, and the stderr log fallback. OFF
# builds the freestanding core - the embedder injects the allocator and mutex
# and loads fonts from memory (see docs, freestanding/UEFI).
if(COPAL_HOSTED)
# PUBLIC so white-box tests and the mock library, which include internal
# headers gated on CL_HOSTED (e.g. the built-in mutex), see the same value.
target_compile_definitions(copal PUBLIC CL_HOSTED)
else()
message(STATUS "copal: COPAL_HOSTED=OFF - freestanding core "
"(inject allocator/mutex; load fonts from memory)")
endif()
# _CRT_SECURE_NO_WARNINGS (MSVC C4996) is applied by copal_set_warnings() below.
if(COPAL_BUILD_SHARED)
target_compile_definitions(copal PUBLIC COPAL_SHARED)
set_target_properties(copal PROPERTIES
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)
endif()
# Pre-1.0 the ABI is not frozen: each 0.x minor gets its own SONAME so a
# consumer linked against one 0.x is never silently loaded against another
# (matches the "rebuild for every 0.x" policy). Switch to major-only at 1.0.
if(PROJECT_VERSION_MAJOR EQUAL 0)
set(COPAL_SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
else()
set(COPAL_SOVERSION ${PROJECT_VERSION_MAJOR})
endif()
set_target_properties(copal PROPERTIES
OUTPUT_NAME copal
EXPORT_NAME copal
VERSION ${PROJECT_VERSION}
SOVERSION ${COPAL_SOVERSION})
copal_set_warnings(copal)
if(COPAL_ENABLE_SANITIZERS)
copal_enable_sanitizers(copal)
endif()
if(COPAL_ENABLE_COVERAGE)
copal_enable_coverage(copal)
endif()
# White-box tests reach internal (non-exported) symbols, so they require the
# static library: with COPAL_BUILD_SHARED they are disabled, and only the
# black-box example smoke tests remain in ctest.
set(COPAL_WHITEBOX_TESTS ${COPAL_BUILD_TESTS})
if(COPAL_BUILD_SHARED AND COPAL_BUILD_TESTS)
message(STATUS "copal: COPAL_BUILD_SHARED=ON - the white-box test suite "
"needs the static library and is disabled; the example "
"smoke tests still run.")
set(COPAL_WHITEBOX_TESTS OFF)
endif()
# Benchmarks are white-box too (internal render/layout entry points), so they
# also need the static library.
if(COPAL_BUILD_SHARED AND COPAL_BUILD_BENCHMARKS)
message(STATUS "copal: COPAL_BUILD_SHARED=ON - benchmarks need the static "
"library and are disabled.")
set(COPAL_BUILD_BENCHMARKS OFF)
endif()
# ---------------------------------------------------------------------------
# Mock backends (test support)
# ---------------------------------------------------------------------------
# A headless platform/renderer pair for white-box tests and benchmarks. A
# separate static library so the mocks never end up in the installed artifact.
if(COPAL_WHITEBOX_TESTS OR COPAL_BUILD_BENCHMARKS)
add_library(copal_mocks STATIC
src/render/mock/renderer_mock.c
src/platform/mock/platform_mock.c)
add_library(copal::mocks ALIAS copal_mocks)
target_link_libraries(copal_mocks PUBLIC copal::copal)
target_include_directories(copal_mocks
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
copal_set_warnings(copal_mocks)
if(COPAL_ENABLE_SANITIZERS)
copal_enable_sanitizers(copal_mocks)
endif()
if(COPAL_ENABLE_COVERAGE)
copal_enable_coverage(copal_mocks)
endif()
endif()
# ---------------------------------------------------------------------------
# Examples / tests
# ---------------------------------------------------------------------------
# enable_testing() must precede the examples: they register headless smoke
# runs of themselves as tests.
if(COPAL_BUILD_TESTS AND COPAL_IS_TOP_LEVEL)
enable_testing()
endif()
if(COPAL_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
if(COPAL_WHITEBOX_TESTS AND COPAL_IS_TOP_LEVEL)
add_subdirectory(tests)
endif()
if(COPAL_BUILD_BENCHMARKS AND COPAL_IS_TOP_LEVEL)
add_subdirectory(benchmarks)
endif()
# ---------------------------------------------------------------------------
# Install + package config
# ---------------------------------------------------------------------------
# A FetchContent SDL2 is a target built in this tree, not an installable
# package, so it cannot be part of copal's export set. Disable install/export
# in that configuration (COPAL_FETCH_SDL2 is a build-it-locally convenience).
# Only when SDL is actually vendored: FETCH_SDL2 without ENABLE_SDL fetches
# nothing (warned about above) and must not cost the install rules too.
if(COPAL_ENABLE_INSTALL AND COPAL_FETCH_SDL2 AND COPAL_ENABLE_SDL)
message(STATUS "copal: COPAL_FETCH_SDL2 is ON; disabling install/export "
"(the vendored SDL2 cannot be part of the package).")
set(COPAL_ENABLE_INSTALL OFF)
endif()
if(COPAL_ENABLE_INSTALL)
install(TARGETS copal
EXPORT copalTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(DIRECTORY include/copal
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT copalTargets
FILE copalTargets.cmake
NAMESPACE copal::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/copal)
# Export the EFFECTIVE backend/dependency state, not the raw options: the
# GL renderer (and its OpenGL link) exists only when SDL is also enabled,
# and Threads is linked only on the hosted build.
if(COPAL_ENABLE_SDL AND COPAL_ENABLE_OPENGL)
set(COPAL_EXPORT_OPENGL ON)
else()
set(COPAL_EXPORT_OPENGL OFF)
endif()
include(CMakePackageConfigHelpers)
configure_package_config_file(
cmake/copalConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/copalConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/copal)
# Pre-1.0, 0.x minors are not API/ABI compatible, so find_package(copal 0.2)
# must not accept an installed 0.3. Use SameMinorVersion while the major is
# 0; switch to SameMajorVersion at 1.0.
if(PROJECT_VERSION_MAJOR EQUAL 0)
set(COPAL_PKG_COMPAT SameMinorVersion)
else()
set(COPAL_PKG_COMPAT SameMajorVersion)
endif()
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/copalConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY ${COPAL_PKG_COMPAT})
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/copalConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/copalConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/copal)
# pkg-config metadata for non-CMake consumers. The private entries matter
# for static linking: copal is a static library by default, so consumers
# must also pull its own dependencies.
set(COPAL_PC_REQUIRES_PRIVATE "")
if(COPAL_ENABLE_SDL)
set(COPAL_PC_REQUIRES_PRIVATE "sdl2")
endif()
set(COPAL_PC_LIBS_PRIVATE "")
if(NOT WIN32 AND COPAL_HOSTED)
set(COPAL_PC_LIBS_PRIVATE "-pthread")
endif()
if(COPAL_ENABLE_SDL AND COPAL_ENABLE_OPENGL)
string(STRIP "${COPAL_PC_LIBS_PRIVATE} -lGL" COPAL_PC_LIBS_PRIVATE)
endif()
# A shared build needs COPAL_SHARED defined so CL_API decorates symbols with
# dllimport on Windows; CMake consumers inherit it, pkg-config ones need it
# in Cflags. The leading space keeps the line well-formed when empty.
set(COPAL_PC_CFLAGS "")
if(COPAL_BUILD_SHARED)
set(COPAL_PC_CFLAGS " -DCOPAL_SHARED")
endif()
configure_file(cmake/copal.pc.in ${CMAKE_CURRENT_BINARY_DIR}/copal.pc
@ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/copal.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()