Skip to content
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

chore: add example of SWIFT_PACKAGE defines #1261

Draft
wants to merge 1 commit into
base: luis/fix-swift-pacakge-defines
Choose a base branch
from

Conversation

luispadron
Copy link
Collaborator

@luispadron luispadron commented Sep 26, 2024

Showcases an issue with SWIFT_PACKAGE defines when consuming a Swift Package like: https://github.com/luispadron/swift-package-defines-example

The package above builds with swift build but fails when building through bazel with:

bazel build @swiftpkg_swift_package_defines_example//:FooSwift.rspm

ERROR: /private/var/tmp/_bazel_luispadron/5af19db7d5e52089e6b3b651a88eb3e7/external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/BUILD.bazel:34:14: Compiling Swift module @@rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example//:FooSwift.rspm failed: (Exit 1): worker failed: error executing SwiftCompile command (from target @@rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example//:FooSwift.rspm) 
  (cd /private/var/tmp/_bazel_luispadron/5af19db7d5e52089e6b3b651a88eb3e7/execroot/_main && \
  exec env - \
    APPLE_SDK_PLATFORM=MacOSX \
    APPLE_SDK_VERSION_OVERRIDE=15.0 \
    PATH=/bin:/usr/bin:/usr/local/bin \
    XCODE_VERSION_OVERRIDE=16.0.0.16A242d \
  bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/external/rules_swift~/tools/worker/worker swiftc @bazel-out/darwin_arm64-fastbuild/bin/external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/FooSwift.swiftmodule-0.params)
# Configuration: c0e955b657088cfeaf65914df4e2f29ac5886d3e2dcf08bafe9d3d979080c98e
# Execution platform: @@platforms//host:host
external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/Sources/FooSwift/Foo.swift:5:24: error: cannot find 'foo' in scope
3 | public func fooSwift() {
4 |     print("FooSwift")
5 |     print("ObjC foo: \(foo())")
  |                        `- error: cannot find 'foo' in scope
6 | }
7 | 

#1260 allows the same build to pass

@cgrindel
Copy link
Owner

I dumped the subcommands while compiling this example. The following is the compilation command for Foo.m:

external/apple_support~~apple_cc_configure_extension~local_config_apple_cc/wrapped_clang \
-target \
x86_64-apple-macosx13.0 \
'-D_FORTIFY_SOURCE=1' \
-fstack-protector \
-fcolor-diagnostics \
-Wall \
-Wthread-safety \
-Wself-assign \
-fno-omit-frame-pointer \
-O0 \
-DDEBUG \
'-fdebug-prefix-map=__BAZEL_EXECUTION_ROOT__=.' \
'-fdebug-prefix-map=__BAZEL_XCODE_DEVELOPER_DIR__=/PLACEHOLDER_DEVELOPER_DIR' \
'-Werror=incompatible-sysroot' \
-Wshorten-64-to-32 \
-Wbool-conversion \
-Wconstant-conversion \
-Wduplicate-method-match \
-Wempty-body \
-Wenum-conversion \
-Wint-conversion \
-Wunreachable-code \
-Wmismatched-return-types \
-Wundeclared-selector \
-Wuninitialized \
-Wunused-function \
-Wunused-variable \
-iquote \
external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example \
-iquote \
bazel-out/darwin_x86_64-fastbuild/bin/external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example \
-Iexternal/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/Sources/Foo/include \
-Ibazel-out/darwin_x86_64-fastbuild/bin/external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/Sources/Foo/include \
-MD \
-MF \
bazel-out/darwin_x86_64-fastbuild/bin/external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/_objs/Foo.rspm/arc/Foo.d \
-DOS_MACOSX \
-fno-autolink \
-isysroot \
__BAZEL_XCODE_SDKROOT__ \
-F__BAZEL_XCODE_SDKROOT__/System/Library/Frameworks \
-F__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/MacOSX.platform/Developer/Library/Frameworks \
-fobjc-arc \
-fblocks \
-fobjc-arc \
-fPIC \
'-fmodule-name=Foo' \
'-DSWIFT_PACKAGE=1' \
-fmodules \
'-fmodules-cache-path=bazel-out/darwin_x86_64-fastbuild/bin/_objc_module_cache' \
-O0 \
'-DDEBUG=1' \
-c \
external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/Sources/Foo/Foo.m \
-o \
bazel-out/darwin_x86_64-fastbuild/bin/external/rules_swift_package_manager~~swift_deps~swiftpkg_swift_package_defines_example/_objs/Foo.rspm/arc/Foo.o)

The SWIFT_PACKAGE macro is being specified.

@cgrindel
Copy link
Owner

I confirmed that Foo.o has the _foo symbol.

$ nm _objs/Foo.rspm/arc/Foo.o
0000000000000000 T _foo

I think the issue is related to how Swift processes the header files. Swift independently reads the header files via the include directories. Hence, the need to pass the macro to clang via Swift.

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 this pull request may close these issues.

2 participants