-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
554 lines (489 loc) · 15.5 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0063 NEW)
project(majak LANGUAGES C CXX)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
find_package(PythonInterp 3)
if (NOT PYTHONINTERP_FOUND)
find_package(PythonInterp 2.7)
endif()
find_package(Doxygen OPTIONAL_COMPONENTS dot)
find_program(NINJA_ASCIIDOC_EXECUTABLE asciidoc)
find_program(NINJA_XSLTPROC_EXECUTABLE xsltproc)
find_program(NINJA_DBLATEX_EXECUTABLE dblatex)
find_program(NINJA_RE2C_EXECUTABLE re2c)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(FeatureSummary)
include(GoogleTest)
include(RE2CTarget)
include(utils)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
ninja_find_filesystem_library()
message(STATUS "NINJA_FILESYSTEM_INCLUDE: ${NINJA_FILESYSTEM_INCLUDE}")
message(STATUS "NINJA_FILESYSTEM_NAMESPACE: ${NINJA_FILESYSTEM_NAMESPACE}")
message(STATUS "NINJA_FILESYSTEM_LIBRARY: ${NINJA_FILESYSTEM_LIBRARY}")
if (NINJA_RE2C_EXECUTABLE)
message(STATUS "RE2C: ${NINJA_RE2C_EXECUTABLE}")
add_executable(re2c IMPORTED)
else()
message(STATUS "RE2C: <built from third_party/re2c sources>")
add_subdirectory(third_party/re2c EXCLUDE_FROM_ALL)
set(NINJA_RE2C_EXECUTABLE re2c)
endif()
if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT MSVC)
add_definitions(-D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC)
set(NINJA_ENABLE_RTTI ON)
else()
option(
NINJA_ENABLE_RTTI
"Build ninja with RTTI enabled"
OFF
)
endif()
if (MSVC)
# required for abseil
add_definitions(-D_SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING)
endif()
if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND CMAKE_VERSION VERSION_GREATER "3.12")
set(ninja_shared_for_tests_default ON)
else()
set(ninja_shared_for_tests_default OFF)
endif()
option(
NINJA_FORCE_PSELECT
[=[
ppoll() is used by default where available,
but some platforms may need to use pselect instead'
]=]
OFF
)
option(
NINJA_BUILD_TESTS
"Build ninja tests and benchmarks."
ON
)
if (NINJA_FILESYSTEM_NAMESPACE STREQUAL "boost::filesystem")
set(NINJA_ENABLE_EXCEPTIONS ON CACHE INTERNAL FORCE "")
else()
option(
NINJA_ENABLE_EXCEPTIONS
"Build ninja with exceptions enabled."
OFF
)
endif()
option(
NINJA_ENABLE_RTTI
"Build ninja with RTTI enabled"
OFF
)
add_feature_info(
build_tests
"${NINJA_BUILD_TESTS}"
"build tests for ninja."
)
if (NINJA_BUILD_TESTS)
enable_testing()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(third_party/gtest EXCLUDE_FROM_ALL)
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/gtest/CMakeLists.txt")
message(
SEND_ERROR
[=[
Please run "git submodule update --init"
or set option NINJA_BUILD_TESTS to OFF.
]=]
)
endif()
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
endif()
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "" FORCE)
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(FLATBUFFERS_LIBCXX_WITH_CLANG OFF CACHE BOOL "" FORCE)
add_subdirectory(third_party/flatbuffers EXCLUDE_FROM_ALL)
add_subdirectory(third_party/abseil EXCLUDE_FROM_ALL)
if (MSVC)
fix_default_msvc_settings()
endif()
set(
ninja_sources
src/lib/build.cc
src/lib/build_log.cc
src/lib/clean.cc
src/lib/clparser.cc
src/lib/debug_flags.cc
src/lib/disk_interface.cc
src/lib/eval_env.cc
src/lib/graph.cc
src/lib/graphviz.cc
src/lib/line_printer.cc
src/lib/manifest_parser.cc
src/lib/message.cc
src/lib/metrics.cc
src/lib/ninja.cc
src/lib/state.cc
src/lib/string_piece_util.cc
src/lib/util.cc
src/lib/version.cc
)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/src/lib")
re2c_target(
NAME re2c-depfile_parser
INPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/depfile_parser.in.cc
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/lib/depfile_parser.cc
OPTIONS -b -i --no-generation-date
)
list(APPEND ninja_sources ${CMAKE_CURRENT_BINARY_DIR}/src/lib/depfile_parser.cc)
re2c_target(
NAME re2c-lexer
INPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/lexer.in.cc
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/lib/lexer.cc
OPTIONS -b -i --no-generation-date
)
list(APPEND ninja_sources ${CMAKE_CURRENT_BINARY_DIR}/src/lib/lexer.cc)
set_package_properties(RE2C PROPERTIES
TYPE OPTIONAL
DESCRIPTION "re2c is a free and open-source software lexer generator for C."
PURPOSE "Enables generation of manifest lexer and depfile parser."
)
if (WIN32)
list(APPEND
ninja_sources
src/lib/includes_normalize-win32.cc
src/lib/msvc_helper_main-win32.cc
src/lib/msvc_helper-win32.cc
src/lib/minidump-win32.cc
src/lib/subprocess-win32.cc
)
else()
list(APPEND
ninja_sources
src/lib/subprocess-posix.cc
)
endif()
check_cxx_source_compiles(
[=[
#include <unistd.h>
#include <getopt.h>
int main(int argc, char** argv) {
int i = getopt(argc, argv, "h");
int j = getopt_long(argc, argv, "h", nullptr, nullptr);
}
]=]
NINJA_HAVE_GETOPT
)
if (MSVC)
set(
ninja_compile_options
/nologo
/W4 # Highest warning level.
/WX # Warnings as errors.
/wd4530 /wd4100 /wd4706 /wd4244
/wd4512 /wd4800 /wd4702 /wd4819
# Disable warnings about constant conditional expressions.
/wd4127
# Disable warnings about passing "this" during initialization.
/wd4355
# Disable warnings about ignored typedef in DbgHelp.h
/wd4091
# Disable size_t -> int truncation warning.
# We never have strings or arrays larger than 2**31.
/wd4267
)
if (NINJA_ENABLE_RTTI)
list(APPEND ninja_compile_options /GR)
else()
list(APPEND ninja_compile_options /GR-)
endif()
else()
set(
ninja_compile_options
-Wall
-Wextra
-Wno-missing-field-initializers
-Wno-unused-parameter
-Wno-implicit-fallthrough
)
if (NOT NINJA_ENABLE_EXCEPTIONS)
list(APPEND ninja_compile_options -fno-exceptions)
endif()
if(NOT NINJA_ENABLE_RTTI)
list(APPEND ninja_compile_options -fno-rtti)
endif()
if (NOT NINJA_FORCE_PSELECT)
check_cxx_source_compiles(
[=[
#include <signal.h>
#include <poll.h>
int main() {
int i = ppoll(nullptr, 0, nullptr, nullptr);
}
]=]
NINJA_HAVE_PPOLL
)
if (NINJA_HAVE_PPOLL)
set(NINJA_USE_PPOLL TRUE)
endif()
else()
set(NINJA_USE_PPOLL FALSE)
endif()
add_feature_info(ppoll "${NINJA_USE_PPOLL}" "fast polling mechanism.")
check_cxx_compiler_flag(-fdiagnostics-color NINJA_COMPILER_SUPPORTS_COLOR)
if (NINJA_COMPILER_SUPPORTS_COLOR)
list(APPEND ninja_compile_options -fdiagnostics-color=always)
endif()
endif()
if (WIN32)
list(APPEND
ninja_compile_definitions
NOMINMAX
_CRT_SECURE_NO_WARNINGS
_HAS_EXCEPTIONS=0
)
endif()
if (PYTHONINTERP_FOUND AND NOT WIN32)
set(NINJA_PYTHON "${PYTHON_EXECUTABLE}")
set(NINJA_HAVE_BROWSE TRUE)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/src/lib/browse_py.h"
DEPENDS src/browse.py cmake/inline.cmake
COMMAND
"${CMAKE_COMMAND}"
-DINPUT=src/browse.py
-DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/src/lib/browse_py.h"
-DVARNAME=kBrowsePy
-P cmake/inline.cmake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "[inline] src/browse.py"
)
list(APPEND ninja_sources src/lib/browse.cc "${CMAKE_CURRENT_BINARY_DIR}/src/lib/browse_py.h")
endif()
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/src/lib/log_schema.h"
DEPENDS src/log.fbs cmake/inline.cmake
COMMAND
"${CMAKE_COMMAND}"
-DINPUT=src/log.fbs
-DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/src/lib/log_schema.h"
-DVARNAME=kBuildLogSchema
-P cmake/inline.cmake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "[inline] src/log.fbs"
)
list(APPEND ninja_sources "${CMAKE_CURRENT_BINARY_DIR}/src/lib/log_schema.h")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/ninja/log_generated.h"
DEPENDS src/log.fbs
COMMAND
flatc
--cpp
-o ${CMAKE_CURRENT_BINARY_DIR}/include/ninja
--gen-object-api
--scoped-enums
--gen-name-strings
--reflect-names
${CMAKE_CURRENT_SOURCE_DIR}/src/log.fbs
COMMENT "[flatc] src/log.fbs"
)
list(APPEND ninja_sources "${CMAKE_CURRENT_BINARY_DIR}/include/ninja/log_generated.h")
set_package_properties(PythonInterp PROPERTIES
TYPE OPTIONAL
DESCRIPTION "Python language interpreter."
PURPOSE "Enables manifest browser."
)
configure_file(include/ninja/ninja_config.in.h include/ninja/ninja_config.h)
add_library(ninja-getopt STATIC EXCLUDE_FROM_ALL src/getopt/getopt.c)
target_include_directories(ninja-getopt PUBLIC src/getopt)
add_library(libninja STATIC ${ninja_sources})
target_link_libraries(
libninja
PUBLIC
flatbuffers
absl::strings
"${NINJA_FILESYSTEM_LIBRARY}"
PRIVATE
str_format_internal
)
if (NOT NINJA_HAVE_GETOPT)
target_link_libraries(libninja PRIVATE ninja-getopt)
endif()
target_compile_definitions(libninja PRIVATE ${ninja_private_compile_definitions})
target_compile_options(libninja PUBLIC ${ninja_compile_options})
target_compile_definitions(libninja PUBLIC ${ninja_compile_definitions})
target_include_directories(
libninja
PUBLIC
include
"${CMAKE_CURRENT_BINARY_DIR}/include"
third_party/flatbuffers/include
PRIVATE
src/lib
"${CMAKE_CURRENT_BINARY_DIR}/src/lib"
)
add_executable(ninja src/main/ninja_main.cc)
target_link_libraries(ninja PRIVATE libninja)
if (NOT NINJA_HAVE_GETOPT)
target_link_libraries(ninja PRIVATE ninja-getopt)
endif()
add_executable(majak src/main/majak_main.cc)
target_link_libraries(majak PRIVATE libninja)
if (NOT NINJA_HAVE_GETOPT)
target_link_libraries(majak PRIVATE ninja-getopt)
endif()
if (NINJA_BUILD_TESTS)
set(
ninja_test_sources
src/tests/build_log_test.cc
src/tests/build_test.cc
src/tests/clean_test.cc
src/tests/clparser_test.cc
src/tests/depfile_parser_test.cc
src/tests/deps_log_test.cc
src/tests/disk_interface_test.cc
src/tests/graph_test.cc
src/tests/lexer_test.cc
src/tests/manifest_parser_test.cc
src/tests/message_test.cc
src/tests/state_test.cc
src/tests/string_piece_util_test.cc
src/tests/subprocess_test.cc
src/tests/test.cc
src/tests/util_test.cc
)
if (WIN32)
list(APPEND
ninja_test_sources
src/tests/includes_normalize_test.cc
src/tests/msvc_helper_test.cc
)
endif()
add_executable(ninja_test ${ninja_test_sources})
target_link_libraries(ninja_test ${ninja_filesystem_library} libninja gtest gtest_main)
target_include_directories(
ninja_test
PRIVATE
src/lib
)
if (COMMAND gtest_discover_tests)
gtest_discover_tests(ninja_test)
else()
add_test(NAME ninja_test COMMAND ninja_test)
endif()
set(
ninja_perftests
depfile_parser_perftest
hash_collision_bench
manifest_parser_perftest
clparser_perftest
)
set(
ninja_bench_perftests
build_log_perftest
canon_perftest
)
foreach(perftest_name IN LISTS ninja_perftests)
add_executable(${perftest_name} src/tests/${perftest_name}.cc)
target_link_libraries(${perftest_name} libninja)
target_include_directories(${perftest_name}
PRIVATE
src/lib
)
endforeach()
set_property(
SOURCE src/tests/manifest_parser_perftest.cc
PROPERTY
COMPILE_DEFINITIONS "CMAKE_CURRENT_SOURCE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\""
)
foreach(perftest_name IN LISTS ninja_bench_perftests)
add_executable(${perftest_name} src/tests/${perftest_name}.cc)
target_link_libraries(${perftest_name} libninja benchmark)
target_include_directories(
${perftest_name}
PRIVATE
src
"${CMAKE_CURRENT_BINARY_DIR}/src"
)
endforeach()
if (NOT NINJA_HAVE_GETOPT)
target_link_libraries(manifest_parser_perftest ninja-getopt)
endif()
if (Boost_FILESYSTEM_FOUND)
target_include_directories(canon_perftest PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(
canon_perftest
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
endif()
endif()
if (TARGET Doxygen::dot AND CMAKE_GENERATOR STREQUAL "Ninja")
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/graph.dot
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build.ninja
COMMAND
ninja -t graph all > ${CMAKE_CURRENT_BINARY_DIR}/graph.dot
COMMENT
"[graph] build.ninja"
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/graph.png
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/graph.dot
COMMAND
Doxygen::dot
-Tpng
-o${CMAKE_CURRENT_BINARY_DIR}/graph.png
${CMAKE_CURRENT_BINARY_DIR}/graph.dot
COMMENT
"[dot] graph.dot"
)
add_custom_target(graph DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/graph.png)
endif()
if (NINJA_ASCIIDOC_EXECUTABLE)
add_custom_command(
DEPENDS doc/manual.asciidoc
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
COMMAND
"${NINJA_ASCIIDOC_EXECUTABLE}"
-b docbook
-d book
-o "${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
${CMAKE_CURRENT_SOURCE_DIR}/doc/manual.asciidoc
COMMENT "[asciidoc] doc/manual.asciidoc"
)
if (NINJA_XSLTPROC_EXECUTABLE)
add_custom_command(
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
DEPENDS doc/style.css doc/docbook.xsl
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/manual.html"
COMMAND
"${NINJA_XSLTPROC_EXECUTABLE}"
--nonet
--output "${CMAKE_CURRENT_BINARY_DIR}/manual.html"
${CMAKE_CURRENT_SOURCE_DIR}/doc/docbook.xsl
"${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
COMMENT "[xsltproc] ${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
)
list(APPEND manuals ${CMAKE_CURRENT_BINARY_DIR}/manual.html)
endif()
if (NINJA_DBLATEX_EXECUTABLE)
add_custom_command(
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
DEPENDS doc/dblatex.xsl
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/manual.pdf"
COMMAND
"${NINJA_DBLATEX_EXECUTABLE}"
-q
-o "${CMAKE_CURRENT_BINARY_DIR}/manual.pdf"
-p ${CMAKE_CURRENT_SOURCE_DIR}/doc/dblatex.xsl
"${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
COMMENT "[dblatex] ${CMAKE_CURRENT_BINARY_DIR}/manual.xml"
)
list(APPEND manuals ${CMAKE_CURRENT_BINARY_DIR}/manual.pdf)
endif()
endif()
add_custom_target(manual DEPENDS ${manuals})
install(TARGETS ninja majak RUNTIME DESTINATION bin)
feature_summary(INCLUDE_QUIET_PACKAGES WHAT ALL)