question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swift host tools compiled with wrong cpu on Apple Silicon

See original GitHub issue

Host tools are being compiled for x86_64 cpu when compiling a target that uses them on an arm Mac, with an arm Bazel.

# BUILD
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")

genrule(
    name = "hello_swift",
    outs = ["hello.swift"],
    cmd = """echo 'print("Hello")' > $@""",
)

swift_binary(
    name = "hello_swift_bin",
    srcs = [":hello_swift"],
)

genrule(
    name = "a",
    outs = ["a.out"],
    cmd = """$(execpath :hello_swift_bin) > $@""",
    tools = [":hello_swift_bin"],
)
$ bazel build a
...
SUBCOMMAND: # //:hello [action 'Compiling Swift module hello [for host]', configuration: 130cff6c5881621671f254bf93eadfab04237d1f78aac0f4e2233c4b12d50eee, execution platform: @local_config_platform//:host]
(cd /private/var/tmp/_bazel_user/c65ad10e7591af2b858f41477e1f1a20/execroot/build_bazel_rules_swift && \
  exec env - \
    APPLE_SDK_PLATFORM=MacOSX \
    APPLE_SDK_VERSION_OVERRIDE=11.0 \
    XCODE_VERSION_OVERRIDE=12.2.0.12B45b \
  bazel-out/host/bin/tools/worker/worker swiftc @bazel-out/host/bin/hello.swiftmodule-0.params)
SUBCOMMAND: # //:hello [action 'Linking hello [for host]', configuration: 130cff6c5881621671f254bf93eadfab04237d1f78aac0f4e2233c4b12d50eee, execution platform: @local_config_platform//:host]
(cd /private/var/tmp/_bazel_user/c65ad10e7591af2b858f41477e1f1a20/execroot/build_bazel_rules_swift && \
  exec env - \
    APPLE_SDK_PLATFORM=MacOSX \
    APPLE_SDK_VERSION_OVERRIDE=11.0 \
    PATH=/bin:/usr/bin \
    XCODE_VERSION_OVERRIDE=12.2.0.12B45b \
  external/local_config_cc/cc_wrapper.sh -lc++ -fobjc-link-runtime -Wl,-S -o bazel-out/host/bin/hello bazel-out/host/bin/hello_objs/hello.swift.o -headerpad_max_install_names -Wl,-rpath,/usr/lib/swift -Wl,-rpath,__BAZEL_XCODE_DEVELOPER_DIR__/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -F__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/MacOSX.platform/Developer/Library/Frameworks -L__BAZEL_XCODE_DEVELOPER_DIR__/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -ObjC -Wl,-objc_abi_version,2 -L/usr/lib/swift -no-canonical-prefixes -target arm64-apple-macosx '-mmacosx-version-min=11.0')
ERROR: /Users/user/forks/rules_swift/BUILD:26:13: Linking hello [for host] failed: (Exit 1): cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh -lc++ -fobjc-link-runtime -Wl,-S -o bazel-out/host/bin/hello bazel-out/host/bin/hello_objs/hello.swift.o -headerpad_max_install_names -Wl,-rpath,/usr/lib/swift ... (remaining 10 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh -lc++ -fobjc-link-runtime -Wl,-S -o bazel-out/host/bin/hello bazel-out/host/bin/hello_objs/hello.swift.o -headerpad_max_install_names -Wl,-rpath,/usr/lib/swift ... (remaining 10 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
ld: warning: ignoring file bazel-out/host/bin/hello_objs/hello.swift.o, building for macOS-arm64 but attempting to link with file built for unknown-x86_64
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Target //:a failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 52.627s, Critical Path: 15.98s
INFO: 214 processes: 7 internal, 206 darwin-sandbox, 1 worker.
FAILED: Build did NOT complete successfully
$ cat bazel-out/host/bin/hello_swift_bin.swiftmodule-0.params
-target
x86_64-apple-macosx11.0
-sdk
__BAZEL_XCODE_SDKROOT__
-F__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/MacOSX.platform/Developer/Library/Frameworks
-I__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/MacOSX.platform/Developer/usr/lib
-emit-object
-output-file-map
bazel-out/host/bin/hello_swift_bin.output_file_map.json
-emit-module-path
bazel-out/host/bin/hello_swift_bin.swiftmodule
-emit-objc-header-path
bazel-out/host/bin/hello_swift_bin-Swift.h
-DNDEBUG
-O
-Xwrapped-swift=-ephemeral-module-cache
-Xcc
-iquote.
-Xcc
-iquotebazel-out/host/bin
-Xfrontend
-color-diagnostics
-enable-batch-mode
-module-name
hello_swift_bin
-Xcc
-Os
-Xcc
-DNDEBUG=1
-Xcc
-Wno-unused-variable
-Xcc
-Winit-self
-Xcc
-Wno-extra
bazel-out/host/bin/hello.swift

It works fine if we build the tool directly with --macos_cpus=arm64.

Tools written in C/C++ work fine.

I’m using a custom build for darwin arm64 at https://github.com/thii/bazel/tree/4.0.0-darwin_arm64 (based on 4.0.0rc2), so there’s a possibility that it’s a problem with my build.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
allevatocommented, Dec 9, 2020

@susinmotion Transferred!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't build to simulator on Apple … | Apple Developer Forums
Using Xcode 12.2, the specific error is "building for iOS Simulator, but linking in object file built for iOS, file '.
Read more >
Could not find module for target 'x86_64-apple-ios-simulator'
I have build a custom framework and everything was working fine until the update of XCode 11. The new XCode 11 released and...
Read more >
Xcode 14 Release Notes | Apple Developer Documentation
Workaround: Quit and relaunch Xcode. CGFLOAT_EPSILON is no longer always type Float on watchOS, and it may cause compile issues. (88698530). Workaround: ...
Read more >
Xcode 13 Release Notes | Apple Developer Documentation
Improved performance and significantly reduced the CPU usage by the XCBBuildService process, while it's processing requests from the source languages service.
Read more >
Programmatically detect Apple Silicon (ie ARM CPU)
On an Apple Silicon based mac, this will return arm64 whereas on an Intel one it'll be x86_64. Be aware however that if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found