-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
169 lines (150 loc) · 6.91 KB
/
CMakeLists.txt
File metadata and controls
169 lines (150 loc) · 6.91 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
cmake_minimum_required(VERSION 3.10)
# CMake fix for crosscompile (MinGW-w64)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
project(tinycsocket)
include(CheckSymbolExists)
include(CheckIncludeFile)
# Add warning levels for gcc and msvc
if(MSVC)
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(
REGEX REPLACE
"/W[0-4]"
"/W4"
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS}"
)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
if(CMAKE_C_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_C_FLAGS
"${CMAKE_C_FLAGS} -Wno-unused-parameter -Wall -Wextra -Wpedantic -Walloca -Wcast-align -Wcast-qual -Wdouble-promotion -Wduplicated-cond -Wenum-conversion -Wfloat-equal -Wformat-overflow=2 -Wformat-signedness -Wformat=2 -Wframe-larger-than=2048 -Wlogical-op -Wmissing-braces -Wmultichar -Wpointer-arith -Wrestrict -Wshadow -Wjump-misses-init -Wsuggest-attribute=format -Wsuggest-attribute=malloc -Wuninitialized -Wvla -Wwrite-strings -Wsign-conversion -std=gnu99"
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wall -Wextra -Wpedantic -Walloca -Wcast-align -Wcast-qual -Wcomma-subscript -Wctor-dtor-privacy -Wdeprecated-copy-dtor -Wdouble-promotion -Wduplicated-cond -Wenum-conversion -Wextra-semi -Wfloat-equal -Wformat-overflow=2 -Wformat-signedness -Wformat=2 -Wframe-larger-than=2048 -Wlogical-op -Wmismatched-tags -Wmissing-braces -Wmultichar -Wnoexcept -Wnon-virtual-dtor -Woverloaded-virtual -Wpointer-arith -Wrange-loop-construct -Wrestrict -Wshadow -Wstrict-null-sentinel -Wsuggest-attribute=format -Wsuggest-attribute=malloc -Wuninitialized -Wvla -Wvolatile -Wwrite-strings -Wsign-conversion")
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
else()
message("WARNING: Unknown compiler, warning flags were not set")
endif()
option(TCS_ENABLE_TESTS "Enable tests" OFF)
option(TCS_ENABLE_EXAMPLES "Enable examples" OFF)
option(TCS_WARNINGS_AS_ERRORS "Enable treat warnings as errors" OFF)
option(TCS_GENERATE_COVERAGE "Enable for test coverage generation" OFF)
set(TINYCSOCKET_SRC
"src/tinycsocket_internal.h"
"src/tinydatastructures.h"
"src/tinycsocket_common.c"
"src/tinycsocket_win32.c"
"src/tinycsocket_posix.c"
)
# Version generation
add_custom_target(
generate_version
COMMAND ${CMAKE_COMMAND} -DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GenerateVersion.cmake
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_target_properties(generate_version PROPERTIES FOLDER tinycsocket)
# Header generation (concatenates source files into single-header via cmake)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/include/tinycsocket.h
COMMAND ${CMAKE_COMMAND} -DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GenerateHeader.cmake
DEPENDS ${TINYCSOCKET_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/tinycsocket.h.in
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_custom_target(
generate_header
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/include/tinycsocket.h
DEPENDS generate_version
)
set_target_properties(generate_header PROPERTIES FOLDER tinycsocket)
# Header interface
add_library(tinycsocket_header INTERFACE)
add_dependencies(tinycsocket_header generate_header)
target_include_directories(
tinycsocket_header
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include
)
if(WIN32)
target_link_libraries(tinycsocket_header INTERFACE wsock32 ws2_32 iphlpapi)
elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
target_link_libraries(tinycsocket_header INTERFACE socket nsl)
endif()
# Tinycsocket static library
add_library(tinycsocket STATIC ${TINYCSOCKET_SRC})
add_library(tinycsockets ALIAS tinycsocket)
target_include_directories(tinycsocket PUBLIC include PRIVATE src)
target_link_libraries(tinycsocket PRIVATE tinycsocket_header)
# illumos/Solaris needs __EXTENSIONS__ for full POSIX visibility (like _GNU_SOURCE on glibc)
if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
target_compile_definitions(tinycsocket PRIVATE __EXTENSIONS__ _XOPEN_SOURCE=500)
endif()
set_target_properties(tinycsocket PROPERTIES FOLDER tinycsocket)
if(TCS_WARNINGS_AS_ERRORS)
if(MSVC)
target_compile_options(tinycsocket PRIVATE /W4 /WX)
else()
target_compile_options(tinycsocket PRIVATE -Werror)
endif()
endif()
# Extra targets for development
if(TCS_ENABLE_TESTS)
enable_testing()
add_library(tinycsocket_wrapped STATIC ${TINYCSOCKET_SRC} "src/dbg_wrap.h")
target_include_directories(tinycsocket_wrapped PUBLIC include src)
target_link_libraries(tinycsocket_wrapped INTERFACE tinycsocket_header)
if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
target_compile_definitions(tinycsocket_wrapped PRIVATE __EXTENSIONS__ _XOPEN_SOURCE=500)
endif()
target_compile_options(tinycsocket_wrapped PUBLIC "-DDO_WRAP")
endif()
if(TCS_GENERATE_COVERAGE)
if(NOT TCS_ENABLE_TESTS)
message(SEND_ERROR "TCS_GENERATE_COVERAGE requires TCS_ENABLE_TESTS to be enabled.")
elseif(MSVC)
message(SEND_ERROR "MSVC with code coverage is not supported.")
else()
target_compile_options(tinycsocket_wrapped PRIVATE --coverage)
target_link_libraries(tinycsocket_wrapped PRIVATE --coverage)
endif()
endif()
if(TCS_ENABLE_TESTS)
add_subdirectory(tests)
endif()
if(TCS_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()
# Documentation
if(NOT CYGWIN) # FindDoxygen crashes on Cygwin due to path translation issues
find_package(Doxygen QUIET)
endif()
if(DOXYGEN_FOUND)
# Define output directory for Doxygen documentation
set(DOXYGEN_OUTPUT_DIR "${CMAKE_BINARY_DIR}/docs")
# Add custom target to build documentation
find_program(UV_EXECUTABLE uv)
add_custom_target(
docs
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile
COMMAND ${UV_EXECUTABLE} run --with sphinx==6.2.1 --with pypandoc-binary==1.11
python3 ${CMAKE_CURRENT_SOURCE_DIR}/docs/reference_generator.py
COMMAND ${UV_EXECUTABLE} run --with sphinx==6.2.1 --with pypandoc-binary==1.11
sphinx-build -M html ${CMAKE_CURRENT_SOURCE_DIR}/docs ${CMAKE_CURRENT_SOURCE_DIR}/docs/_build
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/docs
COMMENT "Generating documentation with Doxygen, reference generator and Sphinx"
VERBATIM
)
# Set the target folder for organization in IDEs
set_target_properties(docs PROPERTIES FOLDER tinycsocket/docs)
message(STATUS "Doxygen found: documentation target 'docs' is available")
else()
message(STATUS "Doxygen not found: documentation target will not be available")
endif()