-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
131 lines (107 loc) · 3.79 KB
/
CMakeLists.txt
File metadata and controls
131 lines (107 loc) · 3.79 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
cmake_minimum_required(VERSION 3.21)
file(READ VERSION VERSION)
if(VERSION STREQUAL "")
message(FATAL_ERROR "Failed to detect version")
else()
message("Build version ${VERSION}")
endif()
project(
code-format
VERSION ${VERSION}
DESCRIPTION "This is a tool to automate cosmetic fixes in C and C++ code"
LANGUAGES CXX)
include(ExternalProject)
option(USE_SANITIZERS_FOR_DEBUG "Use Sanitizers for Debug build" ON)
option(OPTION_EXPORT_COMPILE_DEFS_AND_INCLUDE_DIRS
"Export compile definitions and include directories" OFF)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message("Using C++${CMAKE_CXX_STANDARD} standard")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
if(UNIX)
set(CMAKE_SKIP_RPATH OFF)
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../lib")
endif()
foreach(
var_name
CMAKE_C_COMPILER
CMAKE_C_FLAGS
CMAKE_CXX_COMPILER
CMAKE_CXX_STANDARD
CMAKE_CXX_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_BUILD_TYPE)
if(DEFINED ${var_name})
list(APPEND EXT_PROJECT_CMAKE_ARGS "-D${var_name}=${${var_name}}")
endif()
endforeach()
get_property(IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
set(CONFIG_SUBDIR $<$<BOOL:${IS_MULTI_CONFIG}>:$<CONFIG>>)
# ##############################################################################
# Prepare `uxs` library
set(UXS_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/uxs/${CONFIG_SUBDIR})
set(UXS_INCLUDE_DIR ${UXS_INSTALL_DIR}/include)
set(UXS_LIBRARY_NAME $<IF:$<BOOL:${WIN32}>,uxs.lib,libuxs.so>)
set(UXS_LIBRARY ${UXS_INSTALL_DIR}/lib/${UXS_LIBRARY_NAME})
ExternalProject_Add(
uxs
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/uxs
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/uxs
BUILD_BYPRODUCTS ${UXS_LIBRARY}
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${UXS_INSTALL_DIR}
CMAKE_ARGS -DUSE_ZLIB=Off #
-DUSE_LIBZIP=Off #
-DUSE_SANITIZERS_FOR_DEBUG=${USE_SANITIZERS_FOR_DEBUG} #
${EXT_PROJECT_CMAKE_ARGS})
if(WIN32)
install(
DIRECTORY ${UXS_INSTALL_DIR}/bin
DESTINATION .
COMPONENT binary)
elseif(UNIX)
install(
DIRECTORY ${UXS_INSTALL_DIR}/lib
DESTINATION .
COMPONENT library)
endif()
# ##############################################################################
# Compilation flags
if(MSVC)
add_compile_options(/Zc:__cplusplus /utf-8)
else()
add_compile_options(-Wall -Wextra -pedantic)
add_compile_options(-Wno-missing-field-initializers)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options($<$<CONFIG:Debug>:-fstandalone-debug>)
endif()
endif()
if(USE_SANITIZERS_FOR_DEBUG)
message("Using sanitizers for Debug build")
if(MSVC)
add_compile_options($<$<CONFIG:Debug>:/fsanitize=address>)
else()
add_compile_options($<$<CONFIG:Debug>:-fsanitize=address,undefined,leak>)
add_link_options($<$<CONFIG:Debug>:-fsanitize=address,undefined,leak>)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_link_options($<$<CONFIG:Debug>:-shared-libsan>)
endif()
endif()
endif()
# ##############################################################################
# Add `code-format` build target
file(GLOB_RECURSE sources src/*.h;src/*.cpp)
add_executable(code-format .clang-format ${sources})
add_dependencies(code-format uxs)
target_compile_definitions(code-format PRIVATE VERSION=${VERSION})
target_include_directories(code-format PRIVATE ${UXS_INCLUDE_DIR})
target_link_libraries(code-format PRIVATE ${UXS_LIBRARY})
install(TARGETS code-format RUNTIME DESTINATION bin COMPONENT binary)
# ##############################################################################
# Auxiliary
include("uxs/cmake/uxs/macros.cmake")
add_compile_defs_and_include_dirs_targets(code-format
${CMAKE_CURRENT_SOURCE_DIR})