Skip to content

Cross-compilation failure for Darwin targets: test discovery on Linux host #1525

@johnathan79717

Description

@johnathan79717

Description

Both darwin-arm64 and darwin-amd64 preset builds fail during the CMake build process when GoogleTest attempts to run test discovery on Darwin/macOS executables from a Linux build host. The cross-compiled binaries cannot be executed on Linux, causing the build to fail.

Steps to Reproduce

  1. Configure the darwin-arm64 preset:
    cd barretenberg/cpp
    cmake --preset darwin-arm64
  2. Build the project:
    cmake --build build-darwin-arm64 -j$(nproc)

Similarly for darwin-amd64:

  1. Configure the darwin-amd64 preset:
    cd barretenberg/cpp
    cmake --preset darwin-amd64
  2. Build the project:
    cmake --build build-darwin-amd64 -j$(nproc)

Error Messages

darwin-arm64:

/mnt/user-data/jonathan/aztec-packages-2/barretenberg/cpp/build-darwin-arm64/bin/crypto_blake3s_full_tests: 4: Syntax error: ")" unexpected
CMake Error at /usr/share/cmake-3.28/Modules/GoogleTestAddTests.cmake:112 (message):
  Error running test executable.

darwin-amd64:

/mnt/user-data/jonathan/aztec-packages-2/barretenberg/cpp/build-darwin-amd64/bin/crypto_blake2s_tests: 1: ����h��!H__PAGEZEROx__TEXT__text__TEXT�,���,�__stubs__TEXT�: not found
/mnt/user-data/jonathan/aztec-packages-2/barretenberg/cpp/build-darwin-amd64/bin/crypto_blake2s_tests: 7: Syntax error: ")" unexpected
CMake Error at /usr/share/cmake-3.28/Modules/GoogleTestAddTests.cmake:112 (message):
  Error running test executable.

Root Cause

The build system is using GoogleTest's automatic test discovery feature (gtest_discover_tests) which attempts to execute the compiled test binaries to discover test cases. When cross-compiling for Darwin/macOS on a Linux host, these Mach-O executables cannot be run on Linux, resulting in syntax errors when the shell tries to interpret them as scripts.

The error messages show:

  • Binary format signatures being interpreted as shell commands (e.g., __PAGEZERO, __TEXT)
  • Shell syntax errors when encountering binary data

Suggested Fix

Several possible solutions:

  1. Disable test discovery for cross-compilation: Use gtest_add_tests instead of gtest_discover_tests when cross-compiling
  2. Conditional test discovery: Only enable automatic test discovery when not cross-compiling:
    if(CMAKE_CROSSCOMPILING)
      gtest_add_tests(TARGET ${test_target})
    else()
      gtest_discover_tests(${test_target})
    endif()
  3. Use BUILD_TESTING flag: Allow users to disable test building entirely during cross-compilation:
    option(BUILD_TESTING "Build tests" ON)
    if(BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
      # Add test targets
    endif()
  4. Defer test discovery: Use DISCOVERY_MODE PRE_TEST to delay discovery until test execution time (when presumably on the target platform)

Environment

  • Build presets: darwin-arm64, darwin-amd64
  • Host system: Linux
  • Target system: Darwin/macOS
  • Toolchain: OSXCross with Apple SDK
  • CMake version: 3.28
  • Affected component: GoogleTest test discovery

Impact

This prevents cross-compilation of the project for macOS/Darwin targets from Linux hosts, which is critical for:

  • CI/CD pipelines that build for multiple platforms
  • Linux-based build servers producing macOS binaries
  • Developer workflows on Linux targeting macOS

Workaround

As a temporary workaround, users can:

  1. Build without tests by modifying CMakeLists.txt to skip test targets
  2. Build on native macOS hardware instead of cross-compiling
  3. Use a macOS VM or container for building

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions