-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
513 lines (448 loc) · 14.2 KB
/
CMakeLists.txt
File metadata and controls
513 lines (448 loc) · 14.2 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# CMakeLists.txt for Lua C++ project
# Modern CMake build system for C++23 Lua implementation
cmake_minimum_required(VERSION 3.20)
project(Lua
VERSION 5.5.0
DESCRIPTION "Lua programming language - C++ implementation"
LANGUAGES C CXX
)
# C++23 standard
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Build options
option(LUA_BUILD_TESTS "Build with test mode enabled" ON)
option(LUA_ENABLE_ASSERTIONS "Enable runtime assertions (for debugging)" ON)
option(LUA_ENABLE_ASAN "Enable AddressSanitizer" OFF)
option(LUA_ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer" OFF)
option(LUA_ENABLE_COVERAGE "Enable code coverage reporting (gcov/lcov)" OFF)
option(LUA_ENABLE_LTO "Enable Link Time Optimization" OFF)
option(LUA_BUILD_SHARED "Build shared library in addition to static" OFF)
# Platform detection
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
endif()
# Compiler warning flags (common to GCC and Clang)
set(WARNING_FLAGS
-Werror
-Wfatal-errors
-Wextra
-Wshadow
-Wundef
-Wwrite-strings
-Wredundant-decls
-Wdisabled-optimization
-Wdouble-promotion
-Wmissing-declarations
-Wconversion
-Wstrict-overflow=2
)
# GCC-specific warnings
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND WARNING_FLAGS
-Wlogical-op
-Wno-aggressive-loop-optimizations
-Wno-invalid-offsetof
)
endif()
# Clang-specific warnings
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
list(APPEND WARNING_FLAGS
-Wno-invalid-offsetof
)
endif()
# Optimization and platform flags
set(OPTIMIZE_FLAGS
-O3
-fno-stack-protector
-fno-common
)
# Test mode definitions
if(LUA_BUILD_TESTS)
add_compile_definitions(
LUA_USER_H="ltests.h"
)
# If assertions are disabled, tell ltests.h not to enable them
if(NOT LUA_ENABLE_ASSERTIONS)
add_compile_definitions(LUA_TESTS_NO_ASSERT)
endif()
endif()
# Assertion control (independent of test mode, for non-test builds)
if(LUA_ENABLE_ASSERTIONS AND NOT LUA_BUILD_TESTS)
add_compile_definitions(LUAI_ASSERT)
endif()
# Platform definitions
if(LINUX)
add_compile_definitions(LUA_USE_LINUX)
endif()
# Sanitizer options
if(LUA_ENABLE_ASAN)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
endif()
if(LUA_ENABLE_UBSAN)
add_compile_options(-fsanitize=undefined)
add_link_options(-fsanitize=undefined)
endif()
# Coverage options
if(LUA_ENABLE_COVERAGE)
message(STATUS "Code coverage enabled")
add_compile_options(--coverage -fprofile-arcs -ftest-coverage)
add_link_options(--coverage)
# Disable optimizations for better coverage accuracy
if(CMAKE_BUILD_TYPE STREQUAL "Release")
message(WARNING "Coverage works best with Debug or RelWithDebInfo build type")
endif()
endif()
# Link Time Optimization
if(LUA_ENABLE_LTO)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
# LTO can be aggressive about strict aliasing - Lua uses type punning extensively
# so we need to disable strict aliasing optimization to ensure correctness
add_compile_options(-fno-strict-aliasing)
add_link_options(-fno-strict-aliasing)
# Use fat LTO objects to avoid some LTO bugs
add_compile_options(-ffat-lto-objects)
endif()
# Source files organized by subdirectory
set(LUA_CORE_SOURCES
src/core/lapi.cpp
src/core/ldo.cpp
src/core/lstack.cpp
src/core/lstate.cpp
src/core/ltm.cpp
src/core/ldebug.cpp
)
set(LUA_COMPILER_SOURCES
src/compiler/lcode.cpp
src/compiler/llex.cpp
src/compiler/parser.cpp
src/compiler/funcstate.cpp
src/compiler/parselabels.cpp
src/compiler/parseutils.cpp
src/compiler/lopcodes.cpp
)
set(LUA_VM_SOURCES
src/vm/lvm.cpp
src/vm/lvm_comparison.cpp
src/vm/lvm_conversion.cpp
src/vm/lvm_loops.cpp
src/vm/lvirtualmachine.cpp
)
set(LUA_OBJECT_SOURCES
src/objects/lobject.cpp
src/objects/ltable.cpp
src/objects/lstring.cpp
src/objects/lfunc.cpp
src/objects/lctype.cpp
)
set(LUA_MEMORY_SOURCES
src/memory/lgc.cpp
src/memory/lmem.cpp
src/memory/gc/gc_core.cpp
src/memory/gc/gc_marking.cpp
src/memory/gc/gc_sweeping.cpp
src/memory/gc/gc_finalizer.cpp
src/memory/gc/gc_weak.cpp
src/memory/gc/gc_collector.cpp
)
set(LUA_SERIALIZATION_SOURCES
src/serialization/lundump.cpp
src/serialization/ldump.cpp
src/serialization/lzio.cpp
)
set(LUA_AUX_SOURCES
src/auxiliary/lauxlib.cpp
src/auxiliary/linit.cpp
)
set(LUA_LIB_SOURCES
src/libraries/lbaselib.cpp
src/libraries/ldblib.cpp
src/libraries/liolib.cpp
src/libraries/lmathlib.cpp
src/libraries/loslib.cpp
src/libraries/ltablib.cpp
src/libraries/lstrlib.cpp
src/libraries/lutf8lib.cpp
src/libraries/loadlib.cpp
src/libraries/lcorolib.cpp
)
# Test source (only in test mode)
if(LUA_BUILD_TESTS)
set(LUA_TEST_SOURCES src/testing/ltests.cpp)
else()
set(LUA_TEST_SOURCES "")
endif()
# Public headers
set(LUA_PUBLIC_HEADERS
include/lua.h
include/luaconf.h
include/lualib.h
)
# All library sources
set(LUA_ALL_SOURCES
${LUA_CORE_SOURCES}
${LUA_COMPILER_SOURCES}
${LUA_VM_SOURCES}
${LUA_OBJECT_SOURCES}
${LUA_MEMORY_SOURCES}
${LUA_SERIALIZATION_SOURCES}
${LUA_AUX_SOURCES}
${LUA_LIB_SOURCES}
${LUA_TEST_SOURCES}
)
# Static library target
add_library(liblua_static STATIC
${LUA_ALL_SOURCES}
)
# Set output name to liblua.a (remove double lib prefix)
set_target_properties(liblua_static PROPERTIES
OUTPUT_NAME lua
PUBLIC_HEADER "${LUA_PUBLIC_HEADERS}"
)
target_compile_options(liblua_static PRIVATE
-Wall
${WARNING_FLAGS}
${OPTIMIZE_FLAGS}
)
target_link_libraries(liblua_static PUBLIC
m # Math library
dl # Dynamic linking library (for loadlib)
)
# Export symbols for dynamic loading
target_link_options(liblua_static PUBLIC
-Wl,-E
)
target_include_directories(liblua_static
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary>
$<INSTALL_INTERFACE:include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/core
${CMAKE_CURRENT_SOURCE_DIR}/src/compiler
${CMAKE_CURRENT_SOURCE_DIR}/src/vm
${CMAKE_CURRENT_SOURCE_DIR}/src/objects
${CMAKE_CURRENT_SOURCE_DIR}/src/memory
${CMAKE_CURRENT_SOURCE_DIR}/src/memory/gc
${CMAKE_CURRENT_SOURCE_DIR}/src/serialization
${CMAKE_CURRENT_SOURCE_DIR}/src/libraries
${CMAKE_CURRENT_SOURCE_DIR}/src/testing
)
# Optional shared library
if(LUA_BUILD_SHARED)
add_library(liblua_shared SHARED
${LUA_ALL_SOURCES}
)
set_target_properties(liblua_shared PROPERTIES
OUTPUT_NAME lua
VERSION ${PROJECT_VERSION}
SOVERSION 5.5
PUBLIC_HEADER "${LUA_PUBLIC_HEADERS}"
)
target_compile_options(liblua_shared PRIVATE
-Wall
${WARNING_FLAGS}
${OPTIMIZE_FLAGS}
)
target_link_libraries(liblua_shared PUBLIC m dl)
target_link_options(liblua_shared PUBLIC -Wl,-E)
target_include_directories(liblua_shared
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary>
$<INSTALL_INTERFACE:include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/core
${CMAKE_CURRENT_SOURCE_DIR}/src/compiler
${CMAKE_CURRENT_SOURCE_DIR}/src/vm
${CMAKE_CURRENT_SOURCE_DIR}/src/objects
${CMAKE_CURRENT_SOURCE_DIR}/src/memory
${CMAKE_CURRENT_SOURCE_DIR}/src/memory/gc
${CMAKE_CURRENT_SOURCE_DIR}/src/serialization
${CMAKE_CURRENT_SOURCE_DIR}/src/libraries
${CMAKE_CURRENT_SOURCE_DIR}/src/testing
)
endif()
# Lua interpreter executable
add_executable(lua
src/interpreter/lua.cpp
)
target_link_libraries(lua PRIVATE
liblua_static
)
target_compile_options(lua PRIVATE
-Wall
${WARNING_FLAGS}
${OPTIMIZE_FLAGS}
)
target_include_directories(lua PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/testing
${CMAKE_CURRENT_SOURCE_DIR}/src/memory
${CMAKE_CURRENT_SOURCE_DIR}/src/memory/gc
${CMAKE_CURRENT_SOURCE_DIR}/src/core
${CMAKE_CURRENT_SOURCE_DIR}/src/objects
${CMAKE_CURRENT_SOURCE_DIR}/src/compiler
${CMAKE_CURRENT_SOURCE_DIR}/src/vm
${CMAKE_CURRENT_SOURCE_DIR}/src/serialization
)
# LuaAllocator test executable (optional, for testing the allocator)
# Note: Only build with GCC due to C++23 libstdc++ compatibility issues with Clang 15
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_executable(test_luaallocator
test_luaallocator.cpp
)
target_link_libraries(test_luaallocator PRIVATE
liblua_static
)
target_compile_options(test_luaallocator PRIVATE
-Wall
${WARNING_FLAGS}
${OPTIMIZE_FLAGS}
)
target_include_directories(test_luaallocator PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/testing
${CMAKE_CURRENT_SOURCE_DIR}/src/memory
${CMAKE_CURRENT_SOURCE_DIR}/src/memory/gc
${CMAKE_CURRENT_SOURCE_DIR}/src/core
${CMAKE_CURRENT_SOURCE_DIR}/src/objects
${CMAKE_CURRENT_SOURCE_DIR}/src/compiler
${CMAKE_CURRENT_SOURCE_DIR}/src/vm
${CMAKE_CURRENT_SOURCE_DIR}/src/serialization
)
endif()
# Install targets
include(GNUInstallDirs)
install(TARGETS liblua_static lua
EXPORT LuaTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
if(LUA_BUILD_SHARED)
install(TARGETS liblua_shared
EXPORT LuaTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
# Export targets for use by other CMake projects
install(EXPORT LuaTargets
FILE LuaTargets.cmake
NAMESPACE Lua::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Lua
)
# Create package config file
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/LuaConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/LuaConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Lua
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/LuaConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/LuaConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/LuaConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Lua
)
# Testing with CTest
if(LUA_BUILD_TESTS)
enable_testing()
# Build test library modules
set(TEST_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/testes/libs)
set(TEST_LIB_OUTPUT_DIR ${TEST_LIB_DIR})
# Common compile options for test libraries
set(TEST_LIB_COMPILE_OPTIONS -Wall -O2 -fPIC -ULUA_USER_H)
# lib1.so
add_library(lib1 MODULE ${TEST_LIB_DIR}/lib1.c)
set_target_properties(lib1 PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY ${TEST_LIB_OUTPUT_DIR}
)
target_include_directories(lib1 PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary
)
target_compile_options(lib1 PRIVATE ${TEST_LIB_COMPILE_OPTIONS})
set_property(TARGET lib1 PROPERTY COMPILE_DEFINITIONS "")
# lib11.so
add_library(lib11 MODULE ${TEST_LIB_DIR}/lib11.c)
set_target_properties(lib11 PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY ${TEST_LIB_OUTPUT_DIR}
)
target_include_directories(lib11 PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary
)
target_compile_options(lib11 PRIVATE ${TEST_LIB_COMPILE_OPTIONS})
set_property(TARGET lib11 PROPERTY COMPILE_DEFINITIONS "")
# lib2.so
add_library(lib2 MODULE ${TEST_LIB_DIR}/lib2.c)
set_target_properties(lib2 PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY ${TEST_LIB_OUTPUT_DIR}
)
target_include_directories(lib2 PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary
)
target_compile_options(lib2 PRIVATE ${TEST_LIB_COMPILE_OPTIONS})
set_property(TARGET lib2 PROPERTY COMPILE_DEFINITIONS "")
# lib21.so
add_library(lib21 MODULE ${TEST_LIB_DIR}/lib21.c)
set_target_properties(lib21 PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY ${TEST_LIB_OUTPUT_DIR}
)
target_include_directories(lib21 PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary
)
target_compile_options(lib21 PRIVATE ${TEST_LIB_COMPILE_OPTIONS})
set_property(TARGET lib21 PROPERTY COMPILE_DEFINITIONS "")
# lib2-v2.so (built from lib22.c)
add_library(lib2-v2 MODULE ${TEST_LIB_DIR}/lib22.c)
set_target_properties(lib2-v2 PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY ${TEST_LIB_OUTPUT_DIR}
)
target_include_directories(lib2-v2 PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src/auxiliary
)
target_compile_options(lib2-v2 PRIVATE ${TEST_LIB_COMPILE_OPTIONS})
set_property(TARGET lib2-v2 PROPERTY COMPILE_DEFINITIONS "")
# Create a custom target to build all test libraries
add_custom_target(test_libs ALL
DEPENDS lib1 lib11 lib2 lib21 lib2-v2
)
add_test(
NAME LuaTestSuite
COMMAND lua all.lua
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testes
)
set_tests_properties(LuaTestSuite PROPERTIES
TIMEOUT 120
PASS_REGULAR_EXPRESSION "final OK"
)
endif()
# Print configuration summary
message(STATUS "")
message(STATUS "Lua ${PROJECT_VERSION} Configuration Summary:")
message(STATUS " C++ Standard: C++${CMAKE_CXX_STANDARD}")
message(STATUS " Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS " Test Mode: ${LUA_BUILD_TESTS}")
message(STATUS " Assertions: ${LUA_ENABLE_ASSERTIONS}")
message(STATUS " Build Shared Library: ${LUA_BUILD_SHARED}")
message(STATUS " AddressSanitizer: ${LUA_ENABLE_ASAN}")
message(STATUS " UBSanitizer: ${LUA_ENABLE_UBSAN}")
message(STATUS " LTO: ${LUA_ENABLE_LTO}")
message(STATUS " Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "")