Skip to content

build: keep the NDK's default compile flags - #2136

Merged
edusperoni merged 2 commits into
mainfrom
build/ndk-default-flags
Nov 25, 2026
Merged

edusperoni merged 2 commits into
mainfrom
build/ndk-default-flags

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Current behavior. test-app/runtime/CMakeLists.txt assigns CMAKE_CXX_FLAGS outright in both the release branch (set(CMAKE_CXX_FLAGS "${COMMON_CMAKE_ARGUMENTS} -O3 ...")) and the Debug branch. That throws away every flag the NDK toolchain had put there. AGP's compile_commands.json confirms that every runtime compile unit is built without:

  • -DANDROID, -fdata-sections, -funwind-tables
  • -Wformat -Werror=format-security
  • -fstack-protector-strong, -D_FORTIFY_SOURCE=2
  • on armeabi-v7a, -march=armv7-a -mthumb.

Because -mthumb was dropped too, the runtime's armeabi-v7a code has been A32 (ARM) all along, while the NDK default would be Thumb-2. V8's prebuilt v7 archive is Thumb-2.

-fno-data-sections (added in 2017 with no stated reason) and -mstackrealign (added for an x86 API 19 crash) are also passed for every ABI.

New behavior.

  • Both branches append to CMAKE_CXX_FLAGS (string(APPEND ...)), so the NDK defaults survive. -fno-data-sections is dropped.
  • The NDK's hardening defaults are intentionally switched back off: -fno-stack-protector -U_FORTIFY_SOURCE are appended after them. Their checks only cost code size (+65 KB .text measured on arm64) and time on the runtime's hot paths, so this keeps codegen as fast as today. None of the runtime's objects reference __stack_chk_fail or any __*_chk function (llvm-nm -u, all four ABIs). The runtime has no C or assembly sources, so the C++ flags cover everything.
  • We no longer pass -mstackrealign ourselves. It only works around an x86 stack-alignment bug below API 24, and the NDK toolchain adds it for exactly that case (x86 and minSdk < 24). x86 still gets it today through the NDK (checked in compile_commands.json). arm64-v8a, armeabi-v7a and x86_64 lose it.
  • Optimized links add -Wl,-O2, which turns on lld's string tail merging.
  • armeabi-v7a stays A32. -DANDROID_ARM_MODE=arm is passed to CMake (test-app/runtime/build.gradle), so the toolchain leaves out the -mthumb it would otherwise add. Every other NDK default still applies. NDK r29 does not add -marm for this mode; clang's armv7 default is A32. See ARM vs Thumb-2 below for why.
  • -Wformat -Werror=format-security are now on. The Debug variant (inspector and Node-API test addons included) and the optimized variant both build warning-free on all four ABIs, so no source changes were needed.

Mapping symbols confirm that the armeabi-v7a runtime objects are all A32: $a=4585, $t=0. With the Thumb default they were $t=4530, $a=0. arm64-v8a flags are unaffected by the ARM mode.

ARM vs Thumb-2 on armeabi-v7a

Taking the NDK's Thumb-2 default would shrink the stripped v7 library by 480 KB (−140 KB gzipped; the runtime's own .text drops by a third). On device, though, it made the JNI bridge consistently slower than the A32 code we ship today. Measured on a Samsung SM-G780G (Android 13), optimized release APKs installed as a 32-bit process (--abi armeabi-v7a), debug.ns.jnibench, 2 interleaved blocks × 2 runs per build. Each value is the median of the 4 per-run medians:

row main (A32) Thumb-2 Δ
static.noop() 175 ns 210 ns +19.5%
static.add(int,int) 294 ns 338 ns +14.7%
instance.getInt() 391 ns 425 ns +8.7%
instance.echo(String) 939 ns 1042 ns +11.0%
instance.getString() 603 ns 681 ns +13.0%
field.get int 305 ns 344 ns +12.9%
field.get Object 607 ns 681 ns +12.2%
geomean of 63 steady-state rows +8.6%

So the runtime's v7 code stays A32. Codegen mode is the same as today.

Confirmation run with this branch's final build (-DANDROID_ARM_MODE=arm). It used the same phone and harness, filtered to the 26 static.*/instance.*/field.* rows, and ran the three builds interleaved in 2 blocks, so each build got 2 runs. Values are run 1 / run 2. Δ is the median of the per-block deltas.

row main (A32) Thumb-2 this PR (A32) Thumb-2 vs main this PR vs main
static.noop() 151 / 151 ns 199 / 207 ns 150 / 153 ns +34.2% +0.2%
static.add(int,int) 292 / 292 ns 336 / 333 ns 290 / 288 ns +14.6% −1.1%
instance.getInt() 381 / 389 ns 415 / 414 ns 376 / 385 ns +7.7% −1.1%
instance.echo(String) 920 / 951 ns 1034 / 1015 ns 938 / 932 ns +9.5% −0.1%
instance.getString() 602 / 598 ns 669 / 667 ns 595 / 604 ns +11.3% −0.1%
instance.takeObject(Object) 1395 / 1398 ns 1503 / 1472 ns 1399 / 1384 ns +6.5% −0.4%
field.get int 358 / 364 ns 429 / 427 ns 377 / 378 ns +18.6% +4.6%
field.get Object 740 / 744 ns 847 / 891 ns 784 / 809 ns +17.2% +7.4%
geomean of 26 rows, per block +13.7% / +11.2% +0.9% / −0.9%

This branch's A32 build matches main: the geomean is within ±1% in both blocks. The field rows are mixed. field.get int and field.get Object are 5–7% slower in both blocks, while field.set int is 4% faster and field.get String is unchanged. With n=2, that is not resolved. Thumb-2 is slower on every row either way.

The phone switches between two clock states between sessions, so absolute values only compare within one table. For example, static.noop() on main is 175 ns in the first table and 151 ns here.

Size (optimized release, -Poptimized RelWithDebInfo, llvm-strip --strip-all, NDK r29)

ABI stripped .so gzip -9 .text runtime objects .text
arm64-v8a 24,511,312 → 24,445,776 (−64 KB, −0.3%) 8,768,754 → 8,707,304 (−60 KB) 20,730,668 → 20,697,660 (−32 KB) 1,634,188 → 1,626,704
armeabi-v7a (A32, this PR) 17,461,784 → 17,379,864 (−80 KB, −0.5%) 8,013,467 → 7,936,534 (−75 KB) 15,073,736 → 15,024,252 (−48 KB) 1,704,652 → 1,677,868
armeabi-v7a with the Thumb-2 default (rejected) 17,461,784 → 16,970,264 (−480 KB, −2.8%) 8,013,467 → 7,869,680 (−140 KB) 15,073,736 → 14,610,792 (−452 KB) 1,704,652 → 1,147,232 (−33%)
x86 26,616,336 → 26,546,704 (−68 KB, −0.3%) 10,045,327 → 9,966,153 (−77 KB) 21,903,375 → 21,865,823 (−37 KB) 1,914,380 → 1,914,380
x86_64 26,035,032 → 25,936,728 (−96 KB, −0.4%) 9,191,694 → 9,112,143 (−78 KB) 22,363,074 → 22,288,722 (−73 KB) 1,765,753 → 1,710,444

On x86_64, the stack-realigning prologues in the .so (and $-N, %rsp) drop from 3,760 to 1,420; the rest come from V8. x86 runtime code is unchanged because the NDK still adds -mstackrealign there at minSdk 21.

Performance on arm64

Same phone, same harness, arm64 process: neutral. The stable rows stay within ±1–3% of main. dlopen(libNativeScript.so, RTLD_NOW) of the stripped library, median of 20 interleaved fresh processes, is unchanged: arm64 58.24 → 58.28 ms. In a 32-bit process it was 61.16 → 61.13 ms, measured with the Thumb-2 variant of this branch.

Does your commit message include the wording below to reference a specific issue in this repo?

No.

Related Pull Requests

Does your pull request have unit tests?

No new tests: this is a build-flag change. It was verified as follows:

  • Full suite (./gradlew runtestsAndVerifyResults -Pabis=arm64-v8a) on an arm64 API 36 emulator: 1773 specs, 0 failures.
  • Optimized and Debug native builds of all four ABIs. Flag order was checked in compile_commands.json: the NDK defaults come first, followed by -fno-stack-protector -U_FORTIFY_SOURCE, then -O3.
  • The armeabi-v7a optimized release APK was installed and run as a 32-bit process on an arm64 phone (Android 13) for the benchmarks above.

Summary by CodeRabbit

  • Build Improvements
    • Updated Android runtime build settings to preserve NDK ABI flags and use ARM mode for armeabi-v7a.
    • Optimized release builds with updated compiler and linker options.

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).
With the NDK defaults no longer overwritten, armeabi-v7a picks up the
toolchain's -mthumb and the runtime's own code turns into Thumb-2. That
shrank the stripped v7 library by 480 KB (-140 KB gzipped, runtime code
-33%), but on a Samsung SM-G780G (API 33, optimized release, 32-bit
process) it made the JNI bridge consistently slower than the A32 build
we have shipped so far:

  static.noop()          175 -> 210 ns  (+19.5%)
  static.add(int,int)    +14.4%
  instance.getInt()      +10.2%
  instance.echo(String)  +10.9%
  field.get int          +12.8%
  geomean, 63 rows       +4-11%

Pass -DANDROID_ARM_MODE=arm so the toolchain leaves -mthumb out (clang's
armv7 default is A32) while every other NDK default still applies. V8's
prebuilt archive is unaffected and stays Thumb-2.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The runtime build configuration updates compiler and linker flags. It also passes ARM mode to CMake for armeabi-v7a runtime code.

Changes: Runtime build configuration

Layer / File(s) Summary
Compiler and linker flags
test-app/runtime/CMakeLists.txt
Common arguments no longer include -mstackrealign and add -fno-stack-protector -U_FORTIFY_SOURCE. Release and non-Debug flags append to existing CMAKE_CXX_FLAGS. Optimized builds add -Wl,-O2 to linker flags.
ARM runtime mode
test-app/runtime/build.gradle
The CMake arguments set ANDROID_ARM_MODE=arm for armeabi-v7a runtime code.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Suggested reviewers: triniwiz, vmutafov, vtrifonov

Merge Risk: Minimal · up to bf60a

This change keeps the NDK's default compile flags and builds armeabi-v7a runtime code in ARM mode. No concrete merge-blocking risk was found. Build behavior was checked by the author, not independently.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "build: keep the NDK's default compile flags" matches the main change. The CMakeLists.txt change appends flags to the NDK toolchain defaults instead of replacing them, and the summary confir…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "build: keep the NDK's default compile flags" matches the main change. The CMakeLists.txt change appends flags to the NDK toolchain defaults instead of replacing them, and the summary confirms this. The title is short, specific, and states the primary intent. It does not cover the -mstackrealign removal, the -Wl,-O2 link option, or the ARM mode change in build.gradle, but the task does not require full coverage.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9a514a7 and bf60aac.

Files selected for processing: 2

  • test-app/runtime/CMakeLists.txt
  • test-app/runtime/build.gradle

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edusperoni
edusperoni marked this pull request as ready for review November 25, 2026 17:15
@edusperoni
edusperoni merged commit 250338e into main Nov 25, 2026
8 checks passed
@edusperoni
edusperoni deleted the build/ndk-default-flags branch November 25, 2026 18:15
edusperoni added a commit that referenced this pull request Nov 25, 2026
)

### Description

Stacked on #2134 (Ada 4.1.1). This is the Android half of
NativeScript/ios#606.

As #2134 stands, the upgrade adds about 89 KB per ABI to
`libNativeScript.so`. This PR takes back most of that.

**1. Build ada at `-Os`.** ada was compiled at the runtime-wide `-O3`.
At `-Os`, ada's code is about 50 KB smaller per ABI. Native benchmark on
two phones (median of 3 rounds, ns per operation):

| | Galaxy S20 FE `-O3` | `-Os` | Galaxy S8 `-O3` | `-Os` |
|---|---|---|---|---|
| absolute URL parse | 192 | 214 (+11%) | 441 | 519 (+18%) |
| relative URL parse | 130 | 154 (+19%) | 336 | 388 (+15%) |
| IDN host parse | 1027 | 1097 (+7%) | 2203 | 2352 (+7%) |
| URLSearchParams | 609 | 599 (−2%) | 1408 | 1360 (−3%) |

The slowdown is tens of nanoseconds per `new URL()`. Only `ada.cpp`
changes; the rest of the runtime stays at `-O3`, and local Debug builds
are unaffected.

**2. Parse each URL once.**
- `new URL(input, base)` called `ada::can_parse(input, &base)`, which
parses both, then parsed both again. It now parses each once.
- `URL.canParse` uses `ada::parse` as well, so `ada::can_parse` and its
second parser instantiation are no longer linked.
- These fixes ride along:
- `canParse` stringifies its arguments instead of casting them to
`v8::String` unchecked, so `URL.canParse(urlObject)` no longer reads a
non-string as a string;
- `canParse` treats an undefined base as absent and no longer truncates
at an embedded NUL;
- `new URL()` without arguments throws a `TypeError` instead of reading
`undefined` as a string;
- a base object whose `toString()` throws now propagates the exception
instead of aborting in `ToLocalChecked()`.

**3. URLPattern capture groups and `hasRegExpGroups`.** Found while
adding specs: `testURLPattern.js` was never `require`d from
`mainpage.js`, so the Android URLPattern specs never ran. Once
registered, they exposed two bugs:
- `v8_regex_provider::regex_search` returned `RegExp.exec`'s whole match
as the first capture. Every named group read its predecessor's value, so
`:id` in `/books/:id` got `/books/123`. ada also received one more
result than it has group names. iOS already skips element 0.
- The `hasRegExpGroups` getter was registered as `hasRegexpGroups`.

Both now match iOS, and the specs now cover `test()`/`exec()` with
capture groups.

**Size.** arm64 `optimized` build. Numbers are loaded bytes from the
`lld` link map (code, rodata, data, unwind), because stripped `.so`
sizes are page-rounded.

| | ada | whole `libNativeScript.so` |
|---|---|---|
| main (ada 4.0.0) | 297.7 KB | 26,285,061 |
| #2134 (ada 4.1.1) | 391.8 KB | 26,374,175 |
| this PR | 307.2 KB | 26,290,495 (−83.7 KB vs #2134) |
| this PR + `-fdata-sections` from #2136 | 250.9 KB | 26,233,370 (−140.8
KB vs #2134, −51.7 KB vs main) |

**Not ported from iOS:**
- **Symbol visibility.** The iOS PR's biggest saving was hiding ada's
symbols. Android already builds with `-fvisibility=hidden` and a version
script. The equivalent loss here came from `-fno-data-sections`: it put
every ada vtable in one section, so unused code like the `ada::url`
parser stayed linked. #2136 removes that flag.
- **System ICU for IDNA.** The NDK's `libicu.so` exists only from API 31
and ships no `uidna.h`. The only system UTS #46 is Java's
`android.icu.text.IDNA` (API 24+, ICU 56 / Unicode 8 at the bottom of
the range), which would need JNI. Left out.
- **`std::regex`.** The Android runtime has no `std::regex` use;
URLPattern already runs on V8's RegExp.

### Does your commit message include the wording below to reference a
specific issue in this repo?

No.

### Related Pull Requests

- #2134, the base of this PR
- #2136, which removes `-fno-data-sections`; together they reach the
last size row above
- NativeScript/ios#606, the iOS counterpart

### Does your pull request have unit tests?

Yes. The full suite passes on an API 36 arm64 emulator: 1,792 specs, 0
failures. `testURLPattern.js` now runs, and new specs cover:
- relative URLs against string and `URL` bases;
- invalid bases;
- a throwing base `toString()`;
- `new URL()` without arguments;
- `URL.canParse` with string, `URL`-object and undefined arguments;
- IDN hosts (Unicode, uppercase `xn--`, ZWJ rejection);
- non-ASCII URLPattern group names;
- URLPattern `test()`/`exec()` with capture groups.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

- **Bug Fixes**
- Improved URL construction and validation, including relative URLs,
base handling, and internationalized hostnames.
- Updated `URL.canParse()` to validate inputs and optional bases
consistently.
- Corrected URLPattern capture-group results and the exposed
`hasRegExpGroups` property name.

- **Tests**
- Added coverage for URL parsing, validation, internationalized
hostnames, and URLPattern matching and captures.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant