Skip to content

Commit

Permalink
!9 Upgrade ninja
Browse files Browse the repository at this point in the history
Merge pull request !9 from lijunru/ninja_upgrade
  • Loading branch information
openharmony_ci authored and gitee-org committed Feb 27, 2023
2 parents ff78c5d + 34e184c commit 2148959
Show file tree
Hide file tree
Showing 104 changed files with 3,900 additions and 1,197 deletions.
205 changes: 150 additions & 55 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
cmake_minimum_required(VERSION 3.15)

include(CheckSymbolExists)
include(CheckIPOSupported)

project(ninja)

# --- optional link-time optimization
if(CMAKE_BUILD_TYPE MATCHES "Release")
include(CheckIPOSupported)
check_ipo_supported(RESULT lto_supported OUTPUT error)

if(lto_supported)
message(STATUS "IPO / LTO enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(STATUS "IPO / LTO not supported: <${error}>")
endif()
check_ipo_supported(RESULT lto_supported OUTPUT error)

if(lto_supported)
message(STATUS "IPO / LTO enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
else()
message(STATUS "IPO / LTO not supported: <${error}>")
endif()

# --- compiler flags
if(MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
string(APPEND CMAKE_CXX_FLAGS " /W4 /GR- /Zc:__cplusplus")
string(REPLACE "/GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
add_compile_options(/W4 /wd4100 /wd4267 /wd4706 /wd4702 /wd4244 /GR- /Zc:__cplusplus)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
else()
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wno-deprecated flag_no_deprecated)
if(flag_no_deprecated)
string(APPEND CMAKE_CXX_FLAGS " -Wno-deprecated")
add_compile_options(-Wno-deprecated)
endif()
check_cxx_compiler_flag(-fdiagnostics-color flag_color_diag)
if(flag_color_diag)
string(APPEND CMAKE_CXX_FLAGS " -fdiagnostics-color")
add_compile_options(-fdiagnostics-color)
endif()
endif()

Expand All @@ -36,7 +39,7 @@ if(RE2C)
# the depfile parser and ninja lexers are generated using re2c.
function(re2c IN OUT)
add_custom_command(DEPENDS ${IN} OUTPUT ${OUT}
COMMAND ${RE2C} -b -i --no-generation-date -o ${OUT} ${IN}
COMMAND ${RE2C} -b -i --no-generation-date --no-version -o ${OUT} ${IN}
)
endfunction()
re2c(${PROJECT_SOURCE_DIR}/src/depfile_parser.in.cc ${PROJECT_BINARY_DIR}/depfile_parser.cc)
Expand All @@ -48,6 +51,43 @@ else()
endif()
target_include_directories(libninja-re2c PRIVATE src)

# --- Check for 'browse' mode support
function(check_platform_supports_browse_mode RESULT)
# Make sure the inline.sh script works on this platform.
# It uses the shell commands such as 'od', which may not be available.

execute_process(
COMMAND sh -c "echo 'TEST' | src/inline.sh var"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE inline_result
OUTPUT_QUIET
ERROR_QUIET
)
if(NOT inline_result EQUAL "0")
# The inline script failed, so browse mode is not supported.
set(${RESULT} "0" PARENT_SCOPE)
if(NOT WIN32)
message(WARNING "browse feature omitted due to inline script failure")
endif()
return()
endif()

# Now check availability of the unistd header
check_symbol_exists(fork "unistd.h" HAVE_FORK)
check_symbol_exists(pipe "unistd.h" HAVE_PIPE)
set(browse_supported 0)
if (HAVE_FORK AND HAVE_PIPE)
set(browse_supported 1)
endif ()
set(${RESULT} "${browse_supported}" PARENT_SCOPE)
if(NOT browse_supported)
message(WARNING "browse feature omitted due to missing `fork` and `pipe` functions")
endif()

endfunction()

check_platform_supports_browse_mode(platform_supports_ninja_browse)

# Core source files all build into ninja library.
add_library(libninja OBJECT
src/build_log.cc
Expand All @@ -63,11 +103,14 @@ add_library(libninja OBJECT
src/eval_env.cc
src/graph.cc
src/graphviz.cc
src/json.cc
src/line_printer.cc
src/manifest_parser.cc
src/metrics.cc
src/missing_deps.cc
src/parser.cc
src/state.cc
src/status.cc
src/string_piece_util.cc
src/util.cc
src/version.cc
Expand All @@ -79,62 +122,114 @@ if(WIN32)
src/msvc_helper-win32.cc
src/msvc_helper_main-win32.cc
src/getopt.c
src/minidump-win32.cc
)
if(MSVC)
target_sources(libninja PRIVATE src/minidump-win32.cc)
endif()
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)
endif()

# Needed for perfstat_cpu_total
if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
target_link_libraries(libninja PUBLIC "-lperfstat")
endif()
endif()

#Fixes GetActiveProcessorCount on MinGW
if(MINGW)
target_compile_definitions(libninja PRIVATE _WIN32_WINNT=0x0601 __USE_MINGW_ANSI_STDIO=1)
endif()

# On IBM i (identified as "OS400" for compatibility reasons) and AIX, this fixes missing
# PRId64 (and others) at compile time in C++ sources
if(CMAKE_SYSTEM_NAME STREQUAL "OS400" OR CMAKE_SYSTEM_NAME STREQUAL "AIX")
add_compile_definitions(__STDC_FORMAT_MACROS)
endif()

# Main executable is library plus main() function.
add_executable(ninja src/ninja.cc)
target_link_libraries(ninja PRIVATE libninja libninja-re2c)

# Tests all build into ninja_test executable.
add_executable(ninja_test
src/build_log_test.cc
src/build_test.cc
src/clean_test.cc
src/clparser_test.cc
src/depfile_parser_test.cc
src/deps_log_test.cc
src/disk_interface_test.cc
src/dyndep_parser_test.cc
src/edit_distance_test.cc
src/graph_test.cc
src/lexer_test.cc
src/manifest_parser_test.cc
src/ninja_test.cc
src/state_test.cc
src/string_piece_util_test.cc
src/subprocess_test.cc
src/test.cc
src/util_test.cc
)
if(WIN32)
target_sources(ninja_test PRIVATE src/includes_normalize_test.cc src/msvc_helper_test.cc)
target_sources(ninja PRIVATE windows/ninja.manifest)
endif()
target_link_libraries(ninja_test PRIVATE libninja libninja-re2c)

foreach(perftest
build_log_perftest
canon_perftest
clparser_perftest
depfile_parser_perftest
hash_collision_bench
manifest_parser_perftest
)
add_executable(${perftest} src/${perftest}.cc)
target_link_libraries(${perftest} PRIVATE libninja libninja-re2c)
endforeach()

enable_testing()
add_test(NinjaTest ninja_test)
# Adds browse mode into the ninja binary if it's supported by the host platform.
if(platform_supports_ninja_browse)
# Inlines src/browse.py into the browse_py.h header, so that it can be included
# by src/browse.cc
add_custom_command(
OUTPUT build/browse_py.h
MAIN_DEPENDENCY src/browse.py
DEPENDS src/inline.sh
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/build
COMMAND src/inline.sh kBrowsePy
< src/browse.py
> ${PROJECT_BINARY_DIR}/build/browse_py.h
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
VERBATIM
)

target_compile_definitions(ninja PRIVATE NINJA_HAVE_BROWSE)
target_sources(ninja PRIVATE src/browse.cc)
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"
)
endif()

include(CTest)
if(BUILD_TESTING)
# Tests all build into ninja_test executable.
add_executable(ninja_test
src/build_log_test.cc
src/build_test.cc
src/clean_test.cc
src/clparser_test.cc
src/depfile_parser_test.cc
src/deps_log_test.cc
src/disk_interface_test.cc
src/dyndep_parser_test.cc
src/edit_distance_test.cc
src/graph_test.cc
src/json_test.cc
src/lexer_test.cc
src/manifest_parser_test.cc
src/missing_deps_test.cc
src/ninja_test.cc
src/state_test.cc
src/string_piece_util_test.cc
src/subprocess_test.cc
src/test.cc
src/util_test.cc
)
if(WIN32)
target_sources(ninja_test PRIVATE src/includes_normalize_test.cc src/msvc_helper_test.cc)
endif()
target_link_libraries(ninja_test PRIVATE libninja libninja-re2c)

foreach(perftest
build_log_perftest
canon_perftest
clparser_perftest
depfile_parser_perftest
hash_collision_bench
manifest_parser_perftest
)
add_executable(${perftest} src/${perftest}.cc)
target_link_libraries(${perftest} PRIVATE libninja libninja-re2c)
endforeach()

if(CMAKE_SYSTEM_NAME STREQUAL "AIX" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
# These tests require more memory than will fit in the standard AIX shared stack/heap (256M)
target_link_options(hash_collision_bench PRIVATE "-Wl,-bmaxdata:0x80000000")
target_link_options(manifest_parser_perftest PRIVATE "-Wl,-bmaxdata:0x80000000")
endif()

add_test(NAME NinjaTest COMMAND ninja_test)
endif()

install(TARGETS ninja DESTINATION bin)
install(TARGETS ninja)
4 changes: 2 additions & 2 deletions README.OpenSource
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"Name": "ninja",
"License": "Apache License V2.0",
"License File": "COPYING",
"Version Number": "1.10.1",
"Owner": "wangweichao2@huawei.com",
"Version Number": "1.11.0",
"Owner": "liwentao20@huawei.com",
"Upstream URL": "https://ninja-build.org/",
"Description": "a small build system with a focus on speed"
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ to build Ninja with itself.
### CMake

```
cmake -Bbuild-cmake -H.
cmake -Bbuild-cmake
cmake --build build-cmake
```

Expand Down
16 changes: 11 additions & 5 deletions build_description.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#build ninja
# build ninja

#获取代码
## 获取代码
```
git clone https://gitee.com/openharmony/third_party_ninja.git
```

#通过python脚本构建生成二进制文件
## 通过python脚本构建生成二进制文件
```
cd third_party_ninja
./configure.py --bootstrap
```

#通过CMake编译生成二进制文件
## 通过CMake编译生成二进制文件
```
cd third_party_ninja
cmake -Bbuild-cmake -H.
cmake -Bbuild-cmake
cmake --build build-cmake
```
17 changes: 14 additions & 3 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def is_msvc(self):
return self._platform == 'msvc'

def msvc_needs_fs(self):
popen = subprocess.Popen(['cl', '/nologo', '/?'],
popen = subprocess.Popen(['cl', '/nologo', '/help'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = popen.communicate()
Expand Down Expand Up @@ -269,7 +269,7 @@ def _run_command(self, cmdline):
n.variable('configure_env', config_str + '$ ')
n.newline()

CXX = configure_env.get('CXX', 'g++')
CXX = configure_env.get('CXX', 'c++')
objext = '.o'
if platform.is_msvc():
CXX = 'cl'
Expand Down Expand Up @@ -479,7 +479,7 @@ def has_re2c():
return False
if has_re2c():
n.rule('re2c',
command='re2c -b -i --no-generation-date -o $out $in',
command='re2c -b -i --no-generation-date --no-version -o $out $in',
description='RE2C $out')
# Generate the .cc files in the source directory so we can check them in.
n.build(src('depfile_parser.cc'), 're2c', src('depfile_parser.in.cc'))
Expand Down Expand Up @@ -507,12 +507,15 @@ def has_re2c():
'eval_env',
'graph',
'graphviz',
'json',
'lexer',
'line_printer',
'manifest_parser',
'metrics',
'missing_deps',
'parser',
'state',
'status',
'string_piece_util',
'util',
'version']:
Expand Down Expand Up @@ -575,10 +578,13 @@ def has_re2c():
'disk_interface_test',
'edit_distance_test',
'graph_test',
'json_test',
'lexer_test',
'manifest_parser_test',
'missing_deps_test',
'ninja_test',
'state_test',
'status_test',
'string_piece_util_test',
'subprocess_test',
'test',
Expand All @@ -596,6 +602,11 @@ def has_re2c():

n.comment('Ancillary executables.')

if platform.is_aix() and '-maix64' not in ldflags:
# Both hash_collision_bench and manifest_parser_perftest require more
# memory than will fit in the standard 32-bit AIX shared stack/heap (256M)
libs.append('-Wl,-bmaxdata:0x80000000')

for name in ['build_log_perftest',
'canon_perftest',
'depfile_parser_perftest',
Expand Down
Loading

0 comments on commit 2148959

Please sign in to comment.