Skip to content

Define _WINDOWS only when the target is Windows - #2291

Closed
YevheniiKotyrlo wants to merge 1 commit into
meta:static_hfrom
YevheniiKotyrlo:fix/win32-check-after-project
Closed

YevheniiKotyrlo wants to merge 1 commit into
meta:static_hfrom
YevheniiKotyrlo:fix/win32-check-after-project

Conversation

@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor

Summary

CMakeLists.txt tests WIN32 before project(), where it still holds what CMake set for the host (cmStateSnapshot::SetDefaultDefinitions); project() resets it for the target (CMakeSystemSpecificInitialize.cmake). So a build for Android from a Windows host defines _WINDOWS, and the sources that read it compile their Windows code: OSCompat.h includes io.h and ProcessStats.cpp includes windows.h, which the NDK does not have. React Native's source build of Hermes for Android fails this way on Windows.

This tests WIN32 after project(), so _WINDOWS is defined exactly when the target is Windows. A new build.yml job, build-android-from-windows, configures for Android on the Windows runner and builds hermesSupport and hermesInstrumentation, whose OSCompat.h and ProcessStats.cpp include Windows headers under _WINDOWS, so the test cannot move ahead of project() again unnoticed.

Fixes #2279

Test Plan

The new job's step, run on Windows 11 as the runner runs it, with NDK 30.0.16248370, the runner's ANDROID_NDK_LATEST_HOME, and CMake 4.1.2 where the runner has 3.31.6:

  • On static_h's tree at fbdd137 it fails:
    FAILED: lib/Support/CMakeFiles/hermesSupport_obj.dir/OSCompatCommon.cpp.o
    …/hermes/include/hermes/Support/OSCompat.h:16:10: fatal error: 'io.h' file not found
    FAILED: lib/Support/CMakeFiles/hermesSupport_obj.dir/StackOverflowGuard.cpp.o
    …/hermes/include/hermes/Support/OSCompat.h:16:10: fatal error: 'io.h' file not found
    FAILED: lib/VM/Instrumentation/CMakeFiles/hermesInstrumentation_obj.dir/ProcessStats.cpp.o
    …/hermes/lib/VM/Instrumentation/ProcessStats.cpp:17:10: fatal error: 'windows.h' file not found
    FAILED: lib/Support/CMakeFiles/hermesSupport_obj.dir/OSCompatWindows.cpp.o
    …/hermes/include/hermes/Support/OSCompat.h:16:10: fatal error: 'io.h' file not found
    ninja: build stopped: subcommand failed.
    
  • On this branch it builds all 34 objects.

Hermes's Android build for arm64-v8a on Windows 11, with JDK 17.0.20.1 and the Android SDK's CMake 4.1.2 and NDK 30.0.16248370, the newest installed, in place of the 3.31.6 and 28.2.13676358 that libs.versions.toml pins:

call "<Visual Studio 2026 Build Tools>\VC\Auxiliary\Build\vcvars64.bat"
set HERMES_WS_DIR=<checkout>
set CMAKE_VERSION=4.1.2
cd /d %HERMES_WS_DIR%\android
gradlew.bat --no-daemon --console=plain --project-cache-dir <a folder outside the checkout> :assembleRelease -PndkVersion=30.0.16248370 -Pandroid.injected.build.abi=arm64-v8a -Pkotlin.compiler.execution.strategy=in-process

--project-cache-dir gets these commits' build past #2286.

  • static_h at fbdd137: all 762 compile commands of arm64-v8a's build.ninja carry -D_WINDOWS, and the build fails:
    > Task :buildCMakeMinSizeRel[arm64-v8a][hermesvm] FAILED
    C/C++: <checkout>/include/hermes/Support/OSCompat.h:16:10: fatal error: 'io.h' file not found
    BUILD FAILED in 46m 15s
    
  • This branch: none of them does, and it builds libhermesvm.so, an ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, for Android 24, built by NDK r30 (16248370):
    > Task :assembleRelease
    BUILD SUCCESSFUL in 53m 30s
    

With the same change applied to hermes-v260318099.0.2, React Native 0.88.0's source build of Hermes for Android completes on Windows.

Configured for Windows with build.yml's Visual Studio flags (CMake 4.4.4) and for Linux (CMake 4.2.3 on Ubuntu 26.04), every target gets the same definitions before and after this change, _WINDOWS included on Windows; configured for Windows from Linux with mingw-w64, it now gets _WINDOWS as well.

The other build.yml and rn-build-hermes.yml jobs a pull request runs pass on this branch locally, the macOS ones with Xcode 27.2 where the workflows pin 26.6 and 16.4 and the Linux ones on Ubuntu 26.04; test-linux-test262 on ubuntu-24.04-arm, with and without the JIT, test-linux-armv7 and test-windows-test262 on windows-11-arm are unverified, since I have no arm64 Linux or Windows machine.

CMakeLists.txt tested WIN32 before project(), where it still holds what
CMake set for the host, so a build for Android from a Windows host
defined _WINDOWS, and the sources that read it compiled their Windows
code: ProcessStats.cpp includes windows.h and OSCompat.h io.h, which
the NDK does not have. Test it after project(), which resets WIN32 for
the target.

build.yml's new build-android-from-windows job configures for Android
on the Windows runner and builds hermesSupport and
hermesInstrumentation, whose OSCompat.h and ProcessStats.cpp include
Windows headers under _WINDOWS, so the test cannot move back ahead of
project() unnoticed.

Fixes meta#2279
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 5, 2026
@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as ready for review November 5, 2026 10:57
@meta-codesync

meta-codesync Bot commented Nov 5, 2026

Copy link
Copy Markdown

@lavenzg has imported this pull request. If you are a Meta employee, you can view this in D129958773.

@meta-codesync meta-codesync Bot closed this in c30dccf Nov 5, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 5, 2026
@meta-codesync

meta-codesync Bot commented Nov 5, 2026

Copy link
Copy Markdown

@lavenzg merged this pull request in c30dccf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An Android build from a Windows host defines _WINDOWS, because CMakeLists.txt tests WIN32 before project()

1 participant