rust-lang / cmake-rs

Rust build dependency for running cmake
https://docs.rs/cmake
Apache License 2.0
301 stars 121 forks source link

Trying to cross compile zeromq-src for aarch64-apple-ios #173

Closed berendsliedrecht closed 1 year ago

berendsliedrecht commented 1 year ago

hi

I am currently trying to compile zeromq-src (0.1.10+4.3.2) and it seems to error that cmake can not find the iOS sdk.

This part of the stack trace caught my eye:

CMake Error at /opt/homebrew/Cellar/cmake/3.25.1/share/cmake/Modules/Platform/iOS-Initialize.cmake:4 (message):
    / is not an iOS SDK

After doing some digging in cmake-rs it seems to be related to this line here https://github.com/rust-lang/cmake-rs/blob/07cbf8fd36168b8a5dc9e107f6e659130dbcdd30/src/lib.rs.

This sets the CMAKE_OSX_SYSROOT to / and afterwards this code executes within cmake:

...

# Transform CMAKE_OSX_SYSROOT to absolute path
set(_CMAKE_OSX_SYSROOT_PATH "")
if(CMAKE_OSX_SYSROOT)      <----------- EXISTS
  if("x${CMAKE_OSX_SYSROOT}" MATCHES "/")        <---------- MATCHES TRUE
    # This is a path to the SDK.  Make sure it exists.
    if(NOT IS_DIRECTORY "${CMAKE_OSX_SYSROOT}")          <-------------- IS_DIRECTORY TRUE
      message(WARNING "Ignoring CMAKE_OSX_SYSROOT value:\n ${CMAKE_OSX_SYSROOT}\n"
        "because the directory does not exist.")
      set(CMAKE_OSX_SYSROOT "")
    endif()
    set(_CMAKE_OSX_SYSROOT_PATH "${CMAKE_OSX_SYSROOT}") <-------- SETS _CMAKE_OSX_SYSROOT_PATH to /
  else()
    _apple_resolve_sdk_path(${CMAKE_OSX_SYSROOT} _sdk_path)
    if(IS_DIRECTORY "${_sdk_path}")
      set(_CMAKE_OSX_SYSROOT_PATH "${_sdk_path}")
      # For non-Xcode generators use the path.
      if(NOT "${CMAKE_GENERATOR}" MATCHES "Xcode")
        set(CMAKE_OSX_SYSROOT "${_CMAKE_OSX_SYSROOT_PATH}")
      endif()
    endif()
  endif()
endif()

After this code, this executes:

include(Platform/Darwin-Initialize)

if(NOT _CMAKE_OSX_SYSROOT_PATH MATCHES "/iPhone(OS|Simulator)")   <--- _CMAKE_OSX_SYSROOT_PATH = / and therefore does not match the regex
  message(FATAL_ERROR "${CMAKE_OSX_SYSROOT} is not an iOS SDK")
endif()

set(IOS 1)

set(_CMAKE_FEATURE_DETECTION_TARGET_TYPE STATIC_LIBRARY)

I do not know almost anything about cmake, but this does seem very weird and I am unsure how I can resolve this.

Reproduce (on MacOS)

git clone https://github.com/hyperledger/indy-vdr # this uses zmq 0.9.0 vendored, which uses zeromq-src
cd indy-vdr
rustup target add aarch64-apple-ios
cargo build --release --package=indy-vdr --target=aarch64-apple-ios

Happy to provide more information about my system where required.

Stacktrace

error: failed to run custom build command for `zmq-sys v0.11.0`

Caused by:
  process didn't exit successfully: `/Users/beri/Developer/tmp/indy-vdr/target/release/build/zmq-sys-e224b96d1c794902/build-script-main` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=build/main.rs
  cargo:rerun-if-env-changed=PROFILE
  CMAKE_TOOLCHAIN_FILE_x86_64-apple-ios = None
  CMAKE_TOOLCHAIN_FILE_x86_64_apple_ios = None
  TARGET_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_x86_64-apple-ios = None
  CMAKE_GENERATOR_x86_64_apple_ios = None
  TARGET_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None
  running: "xcrun" "--show-sdk-path" "--sdk" "iphonesimulator"
  exit status: 0
  running: "xcrun" "--show-sdk-path" "--sdk" "iphonesimulator"
  exit status: 0
  CMAKE_PREFIX_PATH_x86_64-apple-ios = None
  CMAKE_PREFIX_PATH_x86_64_apple_ios = None
  TARGET_CMAKE_PREFIX_PATH = None
  CMAKE_PREFIX_PATH = None
  CMAKE_x86_64-apple-ios = None
  CMAKE_x86_64_apple_ios = None
  TARGET_CMAKE = None
  CMAKE = None
  running: "cmake" "/Users/beri/.cargo/registry/src/github.com-1ecc6299db9ec823/zeromq-src-0.1.10+4.3.2/vendor" "-DCMAKE_OSX_SYSROOT=/" "-DCMAKE_OSX_DEPLOYMENT_TARGET=" "-DCMAKE_INSTALL_LIBDIR=lib" "-DCMAKE_C_STANDARD=99" "-DZMQ_BUILD_TESTS=OFF" "-DENABLE_DRAFTS=OFF" "-DENABLE_CURVE=ON" "-DCMAKE_BUILD_TYPE=Release" "-DWITH_PERF_TOOL=OFF" "-DBUILD_SHARED=OFF" "-DBUILD_STATIC=ON" "-DWITH_LIBSODIUM=OFF" "-DCMAKE_SYSTEM_NAME=iOS" "-DCMAKE_SYSTEM_PROCESSOR=x86_64" "-DCMAKE_INSTALL_PREFIX=/Users/beri/Developer/tmp/indy-vdr/target/x86_64-apple-ios/release/build/zmq-sys-828525e3e50a962a/out" "-DCMAKE_C_FLAGS= -fPIC --target=x86_64-apple-ios -m64 -mios-simulator-version-min=7.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.0.sdk -fembed-bitcode" "-DCMAKE_C_COMPILER=/usr/bin/clang" "-DCMAKE_CXX_FLAGS= -fPIC --target=x86_64-apple-ios -m64 -mios-simulator-version-min=7.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.0.sdk -fembed-bitcode" "-DCMAKE_CXX_COMPILER=/usr/bin/clang++" "-DCMAKE_ASM_FLAGS= -fPIC --target=x86_64-apple-ios -m64 -mios-simulator-version-min=7.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.0.sdk -fembed-bitcode" "-DCMAKE_ASM_COMPILER=/usr/bin/clang"
  -- Configuring incomplete, errors occurred!
  See also "/Users/beri/Developer/tmp/indy-vdr/target/x86_64-apple-ios/release/build/zmq-sys-828525e3e50a962a/out/build/CMakeFiles/CMakeOutput.log".

  --- stderr
  CMake Error at /opt/homebrew/Cellar/cmake/3.25.1/share/cmake/Modules/Platform/iOS-Initialize.cmake:4 (message):
    / is not an iOS SDK
  Call Stack (most recent call first):
    /opt/homebrew/Cellar/cmake/3.25.1/share/cmake/Modules/CMakeSystemSpecificInitialize.cmake:21 (include)
    CMakeLists.txt:2 (project)

  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 1

  build script failed, must exit now', /Users/beri/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.49/src/lib.rs:1104:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

EDIT

It seems that the release of 0.1.49 caused this issue, likely from this PR: https://github.com/rust-lang/cmake-rs/pull/160

luser commented 1 year ago

I also hit this trying to compile a different crate for iOS.

This sets the CMAKE_OSX_SYSROOT to / and afterwards this code executes within cmake: This is the CMake code you listed, for reference: https://gitlab.kitware.com/cmake/cmake/-/blob/a50b2785129ec6c2eec2e4ac31f59f4592ec4df0/Modules/Platform/Darwin-Initialize.cmake#L298

After this code, this executes: https://gitlab.kitware.com/cmake/cmake/-/blob/a50b2785129ec6c2eec2e4ac31f59f4592ec4df0/Modules/Platform/iOS-Initialize.cmake#L3

It seems that the release of 0.1.49 caused this issue, likely from this PR: https://github.com/rust-lang/cmake-rs/pull/160

That doesn't look to me like it would have caused this, but #110 added the code you linked to set CMAKE_OSX_SYSROOT=/. I'm not sure why that was added, honestly, it doesn't seem correct.

luser commented 1 year ago

It seems that the release of 0.1.49 caused this issue

You're correct on this count, however. I was able to get past the cmake portion of my build using version 0.1.48 of this crate.

luser commented 1 year ago

Aha, it looks like #158 caused this issue! I assume that setting CMAKE_SYSTEM_NAME = iOS makes cmake use the code in iOS-Initialize.cmake linked in my previous comment. Prior to that change it would not have.

luser commented 1 year ago

That PR I created adds an iOS cross-compile test, which reproduces the failure here. I'm going to try simply removing that block of code that sets CMAKE_OSX_SYSROOT for iOS. Cmake now has built-in support for cross-compiling to iOS. That was added in CMake 3.14, the release of which predates #110, but perhaps that version was not in widespread use at the time?

luser commented 1 year ago

@blu3beri thanks for filing this issue with so much detail! It was helpful in figuring out a fix.