summaryrefslogtreecommitdiffstats
path: root/CMakeLists.txt
diff options
context:
space:
mode:
authorJan Niklas Hasse <jhasse@bixense.com>2024-04-11 16:44:05 (GMT)
committerJan Niklas Hasse <jhasse@bixense.com>2024-04-11 16:44:05 (GMT)
commit65d0dfcbbea6b8ca7d8a3a0f673ecb522379e43c (patch)
tree7b30fc5f0a477efd075ab0f8eead78aa7554869b /CMakeLists.txt
parent448ae1ccacd17025457ace965d78a45a113c70c6 (diff)
parent1dcebc6399dc76a9bdf643ad9722d7f2d7fee51c (diff)
downloadNinja-1.12.0.zip
Ninja-1.12.0.tar.gz
Ninja-1.12.0.tar.bz2
v1.12.0v1.12.0
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r--CMakeLists.txt110
1 files changed, 98 insertions, 12 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 70fc5e9..90e3418 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,7 +3,10 @@ cmake_minimum_required(VERSION 3.15)
include(CheckSymbolExists)
include(CheckIPOSupported)
-project(ninja)
+option(NINJA_BUILD_BINARY "Build ninja binary" ON)
+option(NINJA_FORCE_PSELECT "Use pselect() even on platforms that provide ppoll()" OFF)
+
+project(ninja CXX)
# --- optional link-time optimization
check_ipo_supported(RESULT lto_supported OUTPUT error)
@@ -19,6 +22,8 @@ endif()
if(MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
+ # Note that these settings are separately specified in configure.py, and
+ # these lists should be kept in sync.
add_compile_options(/W4 /wd4100 /wd4267 /wd4706 /wd4702 /wd4244 /GR- /Zc:__cplusplus)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
else()
@@ -31,11 +36,34 @@ else()
if(flag_color_diag)
add_compile_options(-fdiagnostics-color)
endif()
+
+ if(NOT NINJA_FORCE_PSELECT)
+ # Check whether ppoll() is usable on the target platform.
+ # Set -DUSE_PPOLL=1 if this is the case.
+ #
+ # NOTE: Use check_cxx_symbol_exists() instead of check_symbol_exists()
+ # because on Linux, <poll.h> only exposes the symbol when _GNU_SOURCE
+ # is defined.
+ #
+ # Both g++ and clang++ define the symbol by default, because the C++
+ # standard library headers require it, but *not* gcc and clang, which
+ # are used by check_symbol_exists().
+ include(CheckCXXSymbolExists)
+ check_cxx_symbol_exists(ppoll poll.h HAVE_PPOLL)
+ if(HAVE_PPOLL)
+ add_compile_definitions(USE_PPOLL=1)
+ endif()
+ endif()
endif()
# --- optional re2c
+set(RE2C_MAJOR_VERSION 0)
find_program(RE2C re2c)
if(RE2C)
+ execute_process(COMMAND "${RE2C}" --vernum OUTPUT_VARIABLE RE2C_RAW_VERSION)
+ math(EXPR RE2C_MAJOR_VERSION "${RE2C_RAW_VERSION} / 10000")
+endif()
+if(${RE2C_MAJOR_VERSION} GREATER 1)
# the depfile parser and ninja lexers are generated using re2c.
function(re2c IN OUT)
add_custom_command(DEPENDS ${IN} OUTPUT ${OUT}
@@ -46,7 +74,7 @@ if(RE2C)
re2c(${PROJECT_SOURCE_DIR}/src/lexer.in.cc ${PROJECT_BINARY_DIR}/lexer.cc)
add_library(libninja-re2c OBJECT ${PROJECT_BINARY_DIR}/depfile_parser.cc ${PROJECT_BINARY_DIR}/lexer.cc)
else()
- message(WARNING "re2c was not found; changes to src/*.in.cc will not affect your build.")
+ message(WARNING "re2c 2 or later was not found; changes to src/*.in.cc will not affect your build.")
add_library(libninja-re2c OBJECT src/depfile_parser.cc src/lexer.cc)
endif()
target_include_directories(libninja-re2c PRIVATE src)
@@ -86,6 +114,8 @@ function(check_platform_supports_browse_mode RESULT)
endfunction()
+set(NINJA_PYTHON "python" CACHE STRING "Python interpreter to use for the browse tool")
+
check_platform_supports_browse_mode(platform_supports_ninja_browse)
# Core source files all build into ninja library.
@@ -124,10 +154,18 @@ if(WIN32)
src/getopt.c
src/minidump-win32.cc
)
+ # Build getopt.c, which can be compiled as either C or C++, as C++
+ # so that build environments which lack a C compiler, but have a C++
+ # compiler may build ninja.
+ set_source_files_properties(src/getopt.c PROPERTIES LANGUAGE CXX)
else()
target_sources(libninja PRIVATE src/subprocess-posix.cc)
if(CMAKE_SYSTEM_NAME STREQUAL "OS400" OR CMAKE_SYSTEM_NAME STREQUAL "AIX")
target_sources(libninja PRIVATE src/getopt.c)
+ # Build getopt.c, which can be compiled as either C or C++, as C++
+ # so that build environments which lack a C compiler, but have a C++
+ # compiler may build ninja.
+ set_source_files_properties(src/getopt.c PROPERTIES LANGUAGE CXX)
endif()
# Needed for perfstat_cpu_total
@@ -136,6 +174,8 @@ else()
endif()
endif()
+target_compile_features(libninja PUBLIC cxx_std_11)
+
#Fixes GetActiveProcessorCount on MinGW
if(MINGW)
target_compile_definitions(libninja PRIVATE _WIN32_WINNT=0x0601 __USE_MINGW_ANSI_STDIO=1)
@@ -148,11 +188,13 @@ if(CMAKE_SYSTEM_NAME STREQUAL "OS400" OR CMAKE_SYSTEM_NAME STREQUAL "AIX")
endif()
# Main executable is library plus main() function.
-add_executable(ninja src/ninja.cc)
-target_link_libraries(ninja PRIVATE libninja libninja-re2c)
+if(NINJA_BUILD_BINARY)
+ add_executable(ninja src/ninja.cc)
+ target_link_libraries(ninja PRIVATE libninja libninja-re2c)
-if(WIN32)
- target_sources(ninja PRIVATE windows/ninja.manifest)
+ if(WIN32)
+ target_sources(ninja PRIVATE windows/ninja.manifest)
+ endif()
endif()
# Adds browse mode into the ninja binary if it's supported by the host platform.
@@ -171,18 +213,58 @@ if(platform_supports_ninja_browse)
VERBATIM
)
- target_compile_definitions(ninja PRIVATE NINJA_HAVE_BROWSE)
- target_sources(ninja PRIVATE src/browse.cc)
+ if(NINJA_BUILD_BINARY)
+ target_compile_definitions(ninja PRIVATE NINJA_HAVE_BROWSE)
+ target_sources(ninja PRIVATE src/browse.cc)
+ endif()
set_source_files_properties(src/browse.cc
PROPERTIES
OBJECT_DEPENDS "${PROJECT_BINARY_DIR}/build/browse_py.h"
INCLUDE_DIRECTORIES "${PROJECT_BINARY_DIR}"
- COMPILE_DEFINITIONS NINJA_PYTHON="python"
+ COMPILE_DEFINITIONS NINJA_PYTHON="${NINJA_PYTHON}"
)
endif()
include(CTest)
if(BUILD_TESTING)
+ find_package(GTest)
+ if(NOT GTest_FOUND)
+ include(FetchContent)
+ FetchContent_Declare(
+ googletest
+ URL https://github.com/google/googletest/archive/release-1.10.0.tar.gz
+ URL_HASH SHA1=9c89be7df9c5e8cb0bc20b3c4b39bf7e82686770
+ )
+ FetchContent_MakeAvailable(googletest)
+
+ # Before googletest-1.11.0, the CMake files provided by the source archive
+ # did not define the GTest::gtest target, only the gtest one, so define
+ # an alias when needed to ensure the rest of this file works with all
+ # GoogleTest releases.
+ #
+ # Note that surprisingly, this is not needed when using GTEST_ROOT to
+ # point to a local installation, because this one contains CMake-generated
+ # files that contain the right target definition, and which will be
+ # picked up by the find_package(GTest) file above.
+ #
+ # This comment and the four lines below can be removed once Ninja only
+ # depends on release-1.11.0 or above.
+ if (NOT TARGET GTest::gtest)
+ message(STATUS "Defining GTest::gtest alias to work-around bug in older release.")
+ add_library(GTest::gtest ALIAS gtest)
+
+ # NOTE: gtest uninit some variables, gcc >= 1.11.3 may cause error on compile.
+ # Remove this comment and six lines below, once ninja deps gtest-1.11.0 or above.
+ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "1.11.3")
+ check_cxx_compiler_flag(-Wmaybe-uninitialized flag_maybe_uninit)
+ if (flag_maybe_uninit)
+ target_compile_options(gtest PRIVATE -Wno-maybe-uninitialized)
+ endif()
+ endif()
+
+ endif()
+ endif()
+
# Tests all build into ninja_test executable.
add_executable(ninja_test
src/build_log_test.cc
@@ -207,9 +289,11 @@ if(BUILD_TESTING)
src/util_test.cc
)
if(WIN32)
- target_sources(ninja_test PRIVATE src/includes_normalize_test.cc src/msvc_helper_test.cc)
+ target_sources(ninja_test PRIVATE src/includes_normalize_test.cc src/msvc_helper_test.cc
+ windows/ninja.manifest)
endif()
- target_link_libraries(ninja_test PRIVATE libninja libninja-re2c)
+ find_package(Threads REQUIRED)
+ target_link_libraries(ninja_test PRIVATE libninja libninja-re2c GTest::gtest Threads::Threads)
foreach(perftest
build_log_perftest
@@ -232,4 +316,6 @@ if(BUILD_TESTING)
add_test(NAME NinjaTest COMMAND ninja_test)
endif()
-install(TARGETS ninja)
+if(NINJA_BUILD_BINARY)
+ install(TARGETS ninja)
+endif()