forked from DaanDeMeyer/reproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
480 lines (400 loc) · 13.8 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
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
# Every variable is namespaced with REPROC and everything except for the library
# itself is turned off by default to make reproc easy to use as a CMake
# subproject with add_subdirectory.
cmake_minimum_required(VERSION 3.1)
# Use updated policies on newer versions of CMake
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.12)
endif()
project(reproc VERSION 1.0.0 LANGUAGES C)
# Tip: use cmake-gui when contributing to reproc to turn on everything without
# having to type out all the options by hand (cmake .. && cmake-gui .).
option(REPROC_BUILD_TESTS "Build tests" OFF)
option(REPROC_BUILD_EXAMPLES "Build examples. Enable REPROC_BUILD_CXX_WRAPPER \
to build C++ examples." OFF)
option(REPROC_BUILD_CXX_WRAPPER "Build C++ wrapper" OFF)
option(REPROC_BUILD_DOCS "Build API documentation with Doxygen" OFF)
option(REPROC_CI "Add -Werror or equivalent to compiler and clang-tidy" OFF)
option(REPROC_INSTALL "Generate install target" OFF)
option(REPROC_CLANG_TIDY "Run clang-tidy as part of the build (requires \
clang-tidy to be in the PATH)" OFF)
option(REPROC_CLANG_FORMAT "Find clang-format and add target to run it on all \
source files" OFF)
option(REPROC_SANITIZERS "Compile with sanitizers" OFF)
# CMake adds /W3 to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS by default on Windows so
# when we add /W4 via target_compile_options cl.exe complains that both /W3 and
# /W4 are passed. We can avoid these warnings by replacing /W3 with /W4 in
# CMAKE_CXX_FLAGS but this is intrusive when reproc is used via add_subdirectory
# since it would add /W4 for every target. To solve this, we add this option so
# /W4 can be enabled by developers and in CI without forcing it on for users.
option(REPROC_ENABLE_W4 "Enable /W4 on Windows (modifies CMAKE_C_FLAGS and \
CMAKE_CXX_FLAGS)" OFF)
option(REPROC_COLORED_OUTPUT "Force colored output from GCC and Clang")
# C++
if(REPROC_BUILD_CXX_WRAPPER OR REPROC_BUILD_TESTS)
enable_language(CXX)
endif()
### clang-tidy ###
if(REPROC_CLANG_TIDY)
# CMake added clang-tidy support in CMake 3.6.
cmake_minimum_required(VERSION 3.6)
find_program(REPROC_CLANG_TIDY_PROGRAM clang-tidy)
if(REPROC_CLANG_TIDY_PROGRAM)
# Treat clang-tidy warnings as errors when on CI.
if (REPROC_CI)
set(REPROC_CLANG_TIDY_PROGRAM
${REPROC_CLANG_TIDY_PROGRAM} -warnings-as-errors=*)
endif()
else()
message(WARNING "clang-tidy not found")
endif()
endif()
### Common development flags ###
# Target used to propagate common flags to all targets defined by reproc
add_library(reproc-dev-flags INTERFACE)
if(MSVC)
# See REPROC_ENABLE_W4 option declaration for why we can't just add /W4 as a
# compile option.
if(REPROC_ENABLE_W4)
string(REGEX REPLACE /W[0-4] "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4" )
string(REGEX REPLACE /W[0-4] "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4" )
endif()
include(CheckCCompilerFlag)
check_c_compiler_flag(/permissive- REPROC_HAS_PERMISSIVE)
target_compile_options(reproc-dev-flags INTERFACE
# Silence MSVC compiler version output.
/nologo
$<$<BOOL:${REPROC_CI}>:/WX>
$<$<BOOL:${REPROC_HAS_PERMISSIVE}>:/permissive->
# MSVC reports non-constant initializers as a nonstandard extension but
# they've been standardized in C99 and we're targeting C99 so this warning
# is not valid for us.
$<$<C_COMPILER_ID:MSVC>:/wd4204>
)
target_link_libraries(reproc-dev-flags INTERFACE
# Silence MSVC linker version output.
-nologo
# Disable incremental linking to silence warnings when rebuilding reproc
# after executing ninja -t clean.
-INCREMENTAL:NO
)
else()
target_compile_options(reproc-dev-flags INTERFACE
-Wall
-Wextra
-pedantic-errors
-Wshadow
-Wconversion
$<$<BOOL:${REPROC_CI}>:-Werror>
)
if(REPROC_SANITIZERS)
target_compile_options(reproc-dev-flags INTERFACE
-fsanitize=address,undefined
)
target_link_libraries(reproc-dev-flags INTERFACE
-fsanitize=address,undefined
# GCC sanitizers only work when using the gold linker.
$<$<C_COMPILER_ID:GNU>:-fuse-ld=gold>
)
endif()
endif()
if(REPROC_COLORED_OUTPUT)
target_compile_options(reproc-dev-flags INTERFACE
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
$<$<CXX_COMPILER_ID:Clang>:-fcolor-diagnostics>
)
endif()
### Helpers ###
set(REPROC_C_STANDARD 99)
set(REPROC_CXX_STANDARD 11)
function(reproc_set_common TARGET LANGUAGE OUTPUT_DIRECTORY)
string(REPLACE "reproc-" "" WITHOUT_PREFIX ${TARGET})
set_target_properties(${TARGET} PROPERTIES
${LANGUAGE}_STANDARD ${REPROC_${LANGUAGE}_STANDARD}
${LANGUAGE}_STANDARD_REQUIRED ON
${LANGUAGE}_EXTENSIONS OFF
OUTPUT_NAME ${WITHOUT_PREFIX}
RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY}
)
target_link_libraries(${TARGET} PRIVATE
# Avoid error when generating install target (we only need the flags when
# building anyway).
$<BUILD_INTERFACE:reproc-dev-flags>
)
if(REPROC_CLANG_TIDY AND REPROC_CLANG_TIDY_PROGRAM)
set_target_properties(${TARGET} PROPERTIES
# REPROC_CLANG_TIDY is a list so we surround it with quotes to pass it as
# a single argument.
${LANGUAGE}_CLANG_TIDY "${REPROC_CLANG_TIDY_PROGRAM}"
)
endif()
endfunction()
### Library ###
add_library(reproc "")
add_library(reproc::reproc ALIAS reproc)
reproc_set_common(reproc C lib)
# Put library files in tests and examples directories on CI so the executables
# in those directories finds reproc.dll without us having to modify the PATH.
if(REPROC_CI AND BUILD_SHARED_LIBS AND WIN32)
add_custom_command(
TARGET reproc
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:reproc> tests
)
add_custom_command(
TARGET reproc
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:reproc> examples
)
endif()
if(NOT MSVC)
target_compile_options(reproc PRIVATE
# More or less dllexport/dllimport for GCC and Clang.
-fvisibility=hidden
)
endif()
set_target_properties(reproc PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
# # Silence MSVC Library Manager version output.
# if(MSVC)
# set_property(TARGET reproc APPEND PROPERTY STATIC_LIBRARY_FLAGS /nologo)
# endif()
# Used to add visibility modifiers (see include/c/export.h and
# include/cpp/export.hpp).
target_compile_definitions(reproc
PUBLIC $<$<BOOL:${BUILD_SHARED_LIBS}>:REPROC_SHARED>
PRIVATE $<BUILD_INTERFACE:REPROC_BUILDING>
)
# Only use the headers from the repository when building. When installing we
# want to use the install location of the headers (e.g. /usr/include) as the
# include directory which is specified later in this file (Install section).
target_include_directories(reproc PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/c>
)
target_include_directories(reproc PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/c/common>
)
target_sources(reproc PRIVATE src/c/common/reproc/error.c)
if(WIN32)
target_include_directories(reproc PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/c/windows>
)
target_sources(reproc PRIVATE
src/c/windows/reproc/error.c
src/c/windows/reproc/handle.c
src/c/windows/reproc/pipe.c
src/c/windows/reproc/process.c
src/c/windows/reproc/reproc.c
src/c/windows/reproc/string_utils.c
)
elseif(UNIX)
target_include_directories(reproc PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/c/posix>
)
target_sources(reproc PRIVATE
src/c/posix/reproc/error.c
src/c/posix/reproc/fork.c
src/c/posix/reproc/pipe.c
src/c/posix/reproc/reproc.c
src/c/posix/reproc/process.c
)
endif()
if(MSVC)
# Check if STARTUPINFOEX and related functions are available.
include(CheckSymbolExists)
check_symbol_exists(InitializeProcThreadAttributeList windows.h
REPROC_ATTRIBUTE_LIST_FOUND)
target_compile_definitions(reproc PRIVATE
_CRT_SECURE_NO_WARNINGS
# Remove unneeded stuff from windows.h.
WIN32_LEAN_AND_MEAN
VC_EXTRALEAN
NOMINMAX
$<$<BOOL:${REPROC_ATTRIBUTE_LIST_FOUND}>:HAS_ATTRIBUTE_LIST>
)
elseif(UNIX)
# Check if pipe2 is available.
include(CheckSymbolExists)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(pipe2 unistd.h REPROC_PIPE2_FOUND)
list(REMOVE_AT CMAKE_REQUIRED_DEFINITIONS -1)
target_compile_definitions(reproc PRIVATE
_GNU_SOURCE
$<$<BOOL:${REPROC_PIPE2_FOUND}>:HAS_PIPE2>
)
endif()
### C++ wrapper ###
if(REPROC_BUILD_CXX_WRAPPER)
reproc_set_common(reproc CXX lib)
target_include_directories(reproc PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/cpp>
)
target_sources(reproc PRIVATE
src/cpp/reproc.cpp
src/cpp/error.cpp
src/cpp/parser.cpp
)
endif()
### Tests ###
if(REPROC_BUILD_TESTS)
add_executable(reproc-tests "")
reproc_set_common(reproc-tests CXX tests)
target_link_libraries(reproc-tests PRIVATE reproc)
target_include_directories(reproc-tests SYSTEM PRIVATE external/doctest)
target_sources(reproc-tests PRIVATE
tests/impl.cpp
tests/read-write.cpp
tests/stop.cpp
tests/working-directory.cpp
)
### Helper programs ###
function(reproc_add_test_helper TARGET)
add_executable(reproc-${TARGET} tests/resources/${TARGET}.cpp)
reproc_set_common(reproc-${TARGET} CXX tests/resources)
string(TOUPPER ${TARGET} TARGET_UPPER_CASE)
# Pass paths as compile time parameters so tests can be run from any
# directory without hardcoding the path in the tests which differs for each
# user.
target_compile_definitions(reproc-tests PRIVATE
${TARGET_UPPER_CASE}_PATH="$<TARGET_FILE:reproc-${TARGET}>"
${TARGET_UPPER_CASE}_DIR="$<TARGET_FILE_DIR:reproc-${TARGET}>"
)
add_dependencies(reproc-tests reproc-${TARGET})
endfunction()
reproc_add_test_helper(stdout)
reproc_add_test_helper(stderr)
reproc_add_test_helper(infinite)
reproc_add_test_helper(noop)
add_custom_target(
reproc-run-tests
COMMAND $<TARGET_FILE:reproc-tests> --force-colors=true
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
add_dependencies(reproc-run-tests reproc-tests)
endif()
### Examples ###
if(REPROC_BUILD_EXAMPLES)
set(REPROC_C_EXT c)
set(REPROC_CXX_EXT cpp)
function(reproc_add_example TARGET LANGUAGE)
add_executable(reproc-${TARGET} examples/${TARGET}.${REPROC_${LANGUAGE}_EXT})
reproc_set_common(reproc-${TARGET} ${LANGUAGE} examples)
target_link_libraries(reproc-${TARGET} PRIVATE reproc)
if(REPROC_SANITIZERS)
set_target_properties(reproc-${TARGET} PROPERTIES
# Hack to avoid UBSAN undefined reference errors.
LINKER_LANGUAGE CXX
)
endif()
endfunction()
reproc_add_example(git-status C)
if(REPROC_BUILD_CXX_WRAPPER)
reproc_add_example(cmake-help CXX)
reproc_add_example(forward CXX)
endif()
endif()
### Install ###
# Adapted from https://codingnest.com/basic-cmake-part-2/.
if(REPROC_INSTALL)
include(GNUInstallDirs)
set(REPROC_INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/reproc)
install(
TARGETS reproc
EXPORT reproc-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
target_include_directories(reproc PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
install(
EXPORT reproc-targets
FILE reproc-targets.cmake
NAMESPACE reproc::
DESTINATION ${REPROC_INSTALL_CONFIGDIR}
)
install(
DIRECTORY include/c/reproc
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
if(REPROC_BUILD_CXX_WRAPPER)
install(
DIRECTORY include/cpp/reproc
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${PROJECT_BINARY_DIR}/reproc-config-version.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/tools/cmake/reproc-config.cmake.in
${PROJECT_BINARY_DIR}/reproc-config.cmake
INSTALL_DESTINATION ${REPROC_INSTALL_CONFIGDIR}}
)
install(
FILES
${PROJECT_BINARY_DIR}/reproc-config.cmake
${PROJECT_BINARY_DIR}/reproc-config-version.cmake
DESTINATION ${REPROC_INSTALL_CONFIGDIR}
)
endif()
### clang-format ###
if(REPROC_CLANG_FORMAT)
find_program(REPROC_CLANG_FORMAT_PROGRAM clang-format)
if(REPROC_CLANG_FORMAT_PROGRAM)
# CMake discourages globbing for specifying source files but since we
# already manually specify our sources there's no harm in using globbing to
# gather all source files so we can pass them to clang-format.
file(GLOB_RECURSE REPROC_SOURCES
include/*.[h,c,hpp,cpp]
src/*.[h,c,hpp,cpp]
tests/*.[h,c,hpp,cpp]
examples/*.[h,c,hpp,cpp]
)
add_custom_target(
reproc-format
COMMAND ${REPROC_CLANG_FORMAT_PROGRAM} -i ${REPROC_SOURCES}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
else()
message(WARNING "clang-format not found")
endif()
endif()
### Doxygen ###
if(REPROC_BUILD_DOCS)
find_package(Doxygen 1.8.3 REQUIRED)
if(DOXYGEN_FOUND)
configure_file(
${PROJECT_SOURCE_DIR}/tools/doxygen/doxyfile.in
${PROJECT_BINARY_DIR}/doxyfile
@ONLY
)
if(NOT EXISTS ${PROJECT_BINARY_DIR}/cppreference-doxygen-web.tag.xml)
# Download tag file to automatically generate links to cppreference from
# doxygen.
file(
DOWNLOAD
http://upload.cppreference.com/mwiki/images/f/f8/cppreference-doxygen-web.tag.xml
${PROJECT_BINARY_DIR}/cppreference-doxygen-web.tag.xml
)
endif()
add_custom_target(
reproc-docs
COMMAND ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/doxyfile
)
else()
message(WARNING "doxygen not found")
endif()
endif()