-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
123 lines (108 loc) · 4.36 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
cmake_minimum_required(VERSION 3.12)
project(ev)
# add custom cmake files
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/third_party/cmake-modules")
# Build options
option(EV_ENABLE_COVERAGE "Enable code coverage. GCC/Clang only" OFF)
option(EV_DEV "Enable develop mode." OFF)
option(EV_LINENO "Enable line control when generate code" OFF)
option(EV_ASAN "Enable address sanitizer" OFF)
###############################################################################
# Support functions
###############################################################################
function(ev_setup_target_wall name)
if (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
target_compile_options(${name} PRIVATE /W4 /WX)
else ()
target_compile_options(${name} PRIVATE -Wall -Wextra -Werror)
endif ()
endfunction()
###############################################################################
# Dev Mode
###############################################################################
if (EV_DEV)
add_library(${PROJECT_NAME}_raw
"src/ev.c"
)
target_include_directories(${PROJECT_NAME}_raw
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
ev_setup_target_wall(${PROJECT_NAME}_raw)
add_subdirectory(tool/amalgamate)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ev.h
COMMAND $<TARGET_FILE:amalgamate>
--in=${CMAKE_CURRENT_SOURCE_DIR}/include/ev.h
--out=${CMAKE_CURRENT_SOURCE_DIR}/ev.h
--commit=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE
--commit=${CMAKE_CURRENT_SOURCE_DIR}/CHANGELOG.md
--lineno=${EV_LINENO}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE
${CMAKE_CURRENT_SOURCE_DIR}/CHANGELOG.md
${PROJECT_NAME}_raw
VERBATIM
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ev.c
COMMAND $<TARGET_FILE:amalgamate>
--in=${CMAKE_CURRENT_SOURCE_DIR}/src/ev.c
--out=${CMAKE_CURRENT_SOURCE_DIR}/ev.c
--commit=${CMAKE_CURRENT_SOURCE_DIR}/LICENSE
--lineno=${EV_LINENO}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE
${CMAKE_CURRENT_SOURCE_DIR}/ev.h
${PROJECT_NAME}_raw
VERBATIM
)
endif()
###############################################################################
# Build libev
###############################################################################
add_library(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/ev.c)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
set_property(TARGET ${PROJECT_NAME}
PROPERTY POSITION_INDEPENDENT_CODE ON
)
# add library dependencies
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
if(WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE Ws2_32 Mswsock)
else()
target_link_libraries(${PROJECT_NAME} PRIVATE rt)
endif()
# add warning check
ev_setup_target_wall(${PROJECT_NAME})
if (EV_ENABLE_COVERAGE AND CMAKE_C_COMPILER_ID MATCHES "(GNU|Clang)")
include(CodeCoverage)
target_compile_options(${PROJECT_NAME} PRIVATE -fprofile-arcs -ftest-coverage)
set(EV_HAVE_COVERAGE true)
endif ()
if (EV_ASAN)
target_compile_options(${PROJECT_NAME} PRIVATE -fsanitize=address)
target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address)
endif()
###############################################################################
# Build examples
###############################################################################
add_subdirectory(example)
###############################################################################
# Build test
###############################################################################
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
endif()
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
include(third_party/cutest.cmake)
include(test/CMakeLists.txt)
endif()