Skip to content

Commit 4595c58

Browse files
committed
build(runtime): stop overwriting the NDK's default compile flags
CMakeLists.txt assigned CMAKE_CXX_FLAGS outright in both the release and Debug branches, discarding everything the NDK toolchain had put there: -DANDROID, -fdata-sections, -funwind-tables, -Wformat -Werror=format-security, the hardening defaults and, on armeabi-v7a, -march=armv7-a -mthumb. The runtime's v7 code was therefore built as A32 while V8's v7 archive is Thumb-2. Append to CMAKE_CXX_FLAGS instead so the toolchain defaults survive, and drop -fno-data-sections, which had no stated reason. The NDK's -fstack-protector-strong and -D_FORTIFY_SOURCE=2 are switched back off right after the defaults, so codegen stays as fast as before. -mstackrealign is no longer passed by us for any ABI: it only works around an x86 stack-alignment bug below API 24, and the NDK toolchain adds it itself for exactly that case. Optimized links also get -Wl,-O2 (string tail merging).
1 parent 9a514a7 commit 4595c58

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

‎test-app/runtime/CMakeLists.txt‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ endif (CCACHE_FOUND AND (USE_CCACHE))
1919
MESSAGE(STATUS "## ANDROID_NDK_ROOT: " ${ANDROID_NDK_ROOT})
2020

2121

22-
set(COMMON_CMAKE_ARGUMENTS "-std=c++20 -Werror -Wno-unused-result -mstackrealign -fexceptions -fno-builtin-stpcpy -fno-rtti -DV8_31BIT_SMIS_ON_64BIT_ARCH -DV8_31BIT_SMIS_ON_64BIT_ARCH -DV8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -DV8_EMBEDDED_BUILTINS -Wno-vla-extension -Wno-deprecated -Wno-vla-cxx-extension")
22+
# These are appended to CMAKE_CXX_FLAGS, never assigned over it: the NDK toolchain
23+
# puts its ABI flags there, among them -march=armv7-a -mthumb and, for x86 below
24+
# API 24, -mstackrealign.
25+
set(COMMON_CMAKE_ARGUMENTS "-std=c++20 -Werror -Wno-unused-result -fexceptions -fno-builtin-stpcpy -fno-rtti -DV8_31BIT_SMIS_ON_64BIT_ARCH -DV8_31BIT_SMIS_ON_64BIT_ARCH -DV8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -DV8_EMBEDDED_BUILTINS -Wno-vla-extension -Wno-deprecated -Wno-vla-cxx-extension")
2326

2427

2528
if("${ANDROID_ABI}" MATCHES "arm64-v8a$" OR "${ANDROID_ABI}" MATCHES "x86_64$")
2629
# Enable pointer compression on 64 bit platforms
2730
set(COMMON_CMAKE_ARGUMENTS "${COMMON_CMAKE_ARGUMENTS} -DV8_COMPRESS_POINTERS")
2831
endif()
2932

33+
# Opts out of the NDK's -fstack-protector-strong and -D_FORTIFY_SOURCE=2 defaults:
34+
# their checks cost code size and time on the runtime's hot paths. Must follow
35+
# the NDK flags, so the last -U/-f wins.
36+
string(APPEND COMMON_CMAKE_ARGUMENTS " -fno-stack-protector -U_FORTIFY_SOURCE")
37+
3038

3139
# AOSP has switched to using LLD by default and the NDK will use it by default in the next release.
3240
# BFD and Gold will be removed once LLD has been through a release cycle with no major unresolved issues (estimated r21)
@@ -118,14 +126,14 @@ add_custom_command(
118126
# nativescript-regular.aar and selected for apps that set useV8Symbols, so it
119127
# must carry the release flags. Only a local Debug build keeps plain -g.
120128
if (OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD OR NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
121-
set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 -fvisibility=hidden -ffunction-sections -fno-data-sections")
129+
string(APPEND CMAKE_CXX_FLAGS " ${COMMON_CMAKE_ARGUMENTS} -O3 -fvisibility=hidden -ffunction-sections")
122130
# CMake appends CMAKE_CXX_FLAGS_<CONFIG> after CMAKE_CXX_FLAGS and clang honours the
123131
# last -O, so the -O3 above only survives in configs that carry no -O of their own
124132
# (Debug). AGP builds the release variant as RelWithDebInfo, whose default -O2 would
125133
# otherwise win.
126134
string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -O3")
127135
else ()
128-
set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -g")
136+
string(APPEND CMAKE_CXX_FLAGS " ${COMMON_CMAKE_ARGUMENTS} -g")
129137
endif ()
130138

131139
if (NOT OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD)
@@ -268,6 +276,9 @@ set(NATIVES_BLOB_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src/main/libs/${ANDRO
268276
# interposable so a plugin and the runtime never end up with two copies of one variable.
269277
set(NS_SYMBOLIC_FUNCTIONS "-Wl,-Bsymbolic-functions")
270278

279+
# Lets lld merge string tails across SHF_MERGE sections.
280+
set(NS_RELEASE_LINK_OPTIMIZATION "-Wl,-O2")
281+
271282
if (OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD)
272283
set_target_properties(
273284
NativeScript
@@ -276,7 +287,7 @@ if (OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD)
276287
# --icf=all folds identical functions even when their addresses are taken;
277288
# --icf=safe folds nothing here, as V8's objects carry no address-significance
278289
# tables.
279-
PROPERTIES LINK_FLAGS "-Wl,--allow-multiple-definition -Wl,--gc-sections -Wl,--icf=all -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exported-symbols.map ${NS_SYMBOLIC_FUNCTIONS}"
290+
PROPERTIES LINK_FLAGS "-Wl,--allow-multiple-definition -Wl,--gc-sections -Wl,--icf=all -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exported-symbols.map ${NS_SYMBOLIC_FUNCTIONS} ${NS_RELEASE_LINK_OPTIMIZATION}"
280291
INTERFACE_INCLUDE_DIRECTORIES NATIVES_BLOB_INCLUDE_DIRECTORIES
281292
)
282293
else ()

0 commit comments

Comments
 (0)