Skip to content

libCYaml.a has wrong arch on M1 Mac #1167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
azarovalex opened this issue Sep 3, 2022 · 1 comment · Fixed by #1169
Closed

libCYaml.a has wrong arch on M1 Mac #1167

azarovalex opened this issue Sep 3, 2022 · 1 comment · Fixed by #1169

Comments

@azarovalex
Copy link
Contributor

As part of the toolchain build process on M1 mac, the build-toolchain script runs the next 2 commands:

build-script-helper.py build and build-script-helper.py install

Full commands

swift-driver/Utilities/build-script-helper.py build --package-path /Users/azarovalex/Developer/OpenSource/swift-project/swift-driver --build-path /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/swiftdriver-macosx-arm64 --configuration release --toolchain /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/intermediate-install/macosx-arm64/Library/Developer/Toolchains/swift-LOCAL-2022-09-03-a.xctoolchain/usr --ninja-bin /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/ninja-build/ninja --cmake-bin /opt/homebrew/bin/cmake --cross-compile-hosts macosx-x86_64

swift-driver/Utilities/build-script-helper.py install --package-path /Users/azarovalex/Developer/OpenSource/swift-project/swift-driver --build-path /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/swiftdriver-macosx-arm64 --configuration release --toolchain /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/intermediate-install/macosx-arm64/Library/Developer/Toolchains/swift-LOCAL-2022-09-03-a.xctoolchain/usr --ninja-bin /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/ninja-build/ninja --cmake-bin /opt/homebrew/bin/cmake --cross-compile-hosts macosx-x86_64 --prefix /Users/azarovalex/Developer/OpenSource/swift-project/build/buildbot_osx/intermediate-install/macosx-arm64/Library/Developer/Toolchains/swift-LOCAL-2022-09-03-a.xctoolchain/usr

Both these commands contain a --cross-compile-hosts macosx-x86_64 flag, so I expect artifacts for both arm64 and x86 to be built.
The arm one builds as expected, but x86 build crashes with an error:

Undefined symbols for architecture x86_64:
  "_yaml_document_end_event_initialize", referenced from:
      _$s4Yams7EmitterC9serialize4nodeyAA4NodeO_tKF in libYams.a(Emitter.swift.o)
  "_yaml_document_start_event_initialize", referenced from:
      _$s4Yams7EmitterC9serialize4nodeyAA4NodeO_tKF in libYams.a(Emitter.swift.o)

What's interesting is that all symbols start with _yams and running lipo -info for other dependencies return x86_64, only libCYaml.a return arm64.

Moreover, if I open dependencies/yams/build.ninja and manually replace all occurrences of -arch arm64 with -arch x86_64, everything builds as expected (until next cmake command overwrites everything).

Looks like there is an issue with CMakeLists somewhere, maybe even in Yams repo itself, I'll appreciate any help with this issue.

@azarovalex azarovalex changed the title libCYaml.a builds with wrong arch on M1 Mac libCYaml.a has wrong arch on M1 Mac Sep 3, 2022
@azarovalex
Copy link
Contributor Author

azarovalex commented Sep 5, 2022

So, the best fix I found so far is to force libCYaml.a to be built as a fat binary with both arm64 and x86_64 inside the Yams repo by adding
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
at the top of Sources/CYaml/CMakeLists.txt.

Without it generated build.ninja for CYaml sources will contain -arch arm64 flag and results in a arm64 binary when x86_64 is expected, even though we explicitly pass -DCMAKE_C_FLAGS=-target arm64-apple-macosx10.15 to cmake.🤔

This is a workaround unblockes me on building the swift toolchain on M1, but we need a better fix somewhere in build-script-helper.py, because current CMAKE_C_FLAGS it passes isn't enough to convince cmake to generate right flags for libCYaml.a. I am no expert in cmake, so idk how to do this.

@edymtt Can you take a look at this problem, it currently blocks toolchain build on M1 macs.

It is probably one line fix here, but I cannot add CMAKE_OSX_ARCHITECTURES since Yams has Swift sources as well and cmake doesn't allow using it.

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 a pull request may close this issue.

1 participant