-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
170 lines (135 loc) · 5.14 KB
/
CMakeLists.txt
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
#################
# Configuration #
#################
# CMake
cmake_minimum_required(VERSION 3.24)
# Project
set(ULIB_VERSION_MAJOR 0)
set(ULIB_VERSION_MINOR 2)
set(ULIB_VERSION_PATCH 6)
set(ULIB_VERSION "${ULIB_VERSION_MAJOR}.${ULIB_VERSION_MINOR}.${ULIB_VERSION_PATCH}")
set(ULIB_AUTHOR "Ivano Bilenchi")
set(ULIB_AUTHOR_URL "https://ivanobilenchi.com")
set(ULIB_COPYRIGHT_YEAR "2018-2024")
set(ULIB_GIT_URL "https://github.com/IvanoBilenchi/ulib")
project(uLib
VERSION "${ULIB_VERSION}"
DESCRIPTION "A modern, generic C library."
HOMEPAGE_URL "${ULIB_GIT_URL}"
LANGUAGES C)
# Options
option(ULIB_TINY "Use small types" OFF)
option(ULIB_HUGE "Use large types" OFF)
option(ULIB_LTO "Enable link-time optimization, if available" ON)
option(ULIB_CLANG_TIDY "Enable clang-tidy, if available" OFF)
option(ULIB_LEAKS "Enable debugging of memory leaks (keep OFF in production builds)" OFF)
option(ULIB_SANITIZERS "Enable sanitizers (keep OFF in production builds)" OFF)
set(ULIB_LIBRARY_TYPE "STATIC" CACHE STRING "Type of library to build.")
set(ULIB_USER_HEADERS "" CACHE STRING "User-specified header files")
set(ULIB_USER_SOURCES "" CACHE STRING "User-specified source files")
set(ULIB_MALLOC "" CACHE STRING "ulib_malloc override")
set(ULIB_CALLOC "" CACHE STRING "ulib_calloc override")
set(ULIB_REALLOC "" CACHE STRING "ulib_realloc override")
set(ULIB_STACKALLOC "" CACHE STRING "ulib_stackalloc override")
set(ULIB_FREE "" CACHE STRING "ulib_free override")
# Directories
set(ULIB_PROJECT_DIR "${CMAKE_CURRENT_LIST_DIR}")
set(ULIB_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(ULIB_PUBLIC_HEADERS_DIR "${ULIB_PROJECT_DIR}/include")
set(ULIB_SRC_DIR "${ULIB_PROJECT_DIR}/src")
set(ULIB_PRIVATE_HEADERS_DIR "${ULIB_SRC_DIR}")
set(ULIB_DOCS_DIR "${ULIB_PROJECT_DIR}/docs")
set(ULIB_TEST_DIR "${ULIB_PROJECT_DIR}/test")
set(ULIB_HEADERS_OUT_DIR "${ULIB_OUTPUT_DIR}/include")
# Target settings
set(ULIB_COMPILE_FEATURES c_std_11)
if(ULIB_CLANG_TIDY)
set(ULIB_CLANG_TIDY_COMMAND "clang-tidy")
if(CMAKE_COMPILE_WARNING_AS_ERROR)
list(APPEND ULIB_CLANG_TIDY_COMMAND "-warnings-as-errors=*")
endif()
endif()
if(ULIB_LEAKS)
set(ULIB_MALLOC p_utest_leak_malloc)
set(ULIB_CALLOC p_utest_leak_calloc)
set(ULIB_REALLOC p_utest_leak_realloc)
set(ULIB_FREE p_utest_leak_free)
list(APPEND ULIB_PRIVATE_DEFINES ULIB_LEAKS)
list(APPEND ULIB_USER_HEADERS "${ULIB_PUBLIC_HEADERS_DIR}/utest.h")
endif()
if(NOT ULIB_MALLOC STREQUAL "")
list(APPEND ULIB_PUBLIC_DEFINES ulib_malloc=${ULIB_MALLOC})
endif()
if(NOT ULIB_CALLOC STREQUAL "")
list(APPEND ULIB_PUBLIC_DEFINES ulib_calloc=${ULIB_CALLOC})
endif()
if(NOT ULIB_REALLOC STREQUAL "")
list(APPEND ULIB_PUBLIC_DEFINES ulib_realloc=${ULIB_REALLOC})
endif()
if(NOT ULIB_STACKALLOC STREQUAL "")
list(APPEND ULIB_PUBLIC_DEFINES ulib_stackalloc=${ULIB_STACKALLOC})
endif()
if(NOT ULIB_FREE STREQUAL "")
list(APPEND ULIB_PUBLIC_DEFINES ulib_free=${ULIB_FREE})
endif()
list(APPEND ULIB_PRIVATE_DEFINES
ULIB_BUILDING
ULIB_VERSION_MAJOR=${ULIB_VERSION_MAJOR}
ULIB_VERSION_MINOR=${ULIB_VERSION_MINOR}
ULIB_VERSION_PATCH=${ULIB_VERSION_PATCH})
if(MSVC)
set(ULIB_WARNING_OPTIONS /W4 /wd4996)
else()
set(ULIB_WARNING_OPTIONS -Wall -Wextra)
if(ULIB_SANITIZERS)
set(ULIB_SANITIZER_OPTIONS "-fsanitize=address,undefined")
endif()
endif()
if(ULIB_LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT ULIB_LTO_ENABLED)
endif()
if(ULIB_TINY)
list(APPEND ULIB_PUBLIC_DEFINES ULIB_TINY)
elseif(ULIB_HUGE)
list(APPEND ULIB_PUBLIC_DEFINES ULIB_HUGE)
endif()
# Header files
file(GLOB ULIB_PUBLIC_HEADERS CONFIGURE_DEPENDS "${ULIB_PUBLIC_HEADERS_DIR}/*.h")
# Sources
file(GLOB ULIB_SOURCES CONFIGURE_DEPENDS "${ULIB_SRC_DIR}/*.c")
list(APPEND ULIB_SOURCES ${ULIB_USER_SOURCES})
###########
# Targets #
###########
add_library(ulib ${ULIB_LIBRARY_TYPE} ${ULIB_SOURCES})
set_target_properties(ulib PROPERTIES
PREFIX ""
PUBLIC_HEADER "${ULIB_PUBLIC_HEADERS}"
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)
target_compile_features(ulib PRIVATE ${ULIB_COMPILE_FEATURES})
target_compile_options(ulib
PUBLIC ${ULIB_SANITIZER_OPTIONS}
PRIVATE ${ULIB_WARNING_OPTIONS})
target_link_options(ulib PUBLIC ${ULIB_SANITIZER_OPTIONS})
target_compile_definitions(ulib
PUBLIC ${ULIB_PUBLIC_DEFINES}
PRIVATE ${ULIB_PRIVATE_DEFINES})
target_include_directories(ulib
PUBLIC "${ULIB_PUBLIC_HEADERS_DIR}"
PRIVATE "${ULIB_PRIVATE_HEADERS_DIR}")
target_precompile_headers(ulib PUBLIC ${ULIB_USER_HEADERS})
if(ULIB_LIBRARY_TYPE STREQUAL "SHARED")
target_compile_definitions(ulib PUBLIC ULIB_SHARED)
endif()
if(ULIB_LTO_ENABLED)
set_property(TARGET ulib PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
endif()
if(ULIB_CLANG_TIDY_COMMAND)
set_property(TARGET ulib PROPERTY C_CLANG_TIDY "${ULIB_CLANG_TIDY_COMMAND}")
endif()
install(TARGETS ulib)
# Subprojects
add_subdirectory("${ULIB_TEST_DIR}")
add_subdirectory("${ULIB_DOCS_DIR}")