kabiroberai / node-swift

Create Node modules in Swift
MIT License
492 stars 16 forks source link

How to compile a .node file that supports both arm64 and x86 architectures simultaneously? #19

Closed Rex-xingjl closed 7 months ago

Rex-xingjl commented 7 months ago

I used the command swift build --arch x86_64 --arch arm64 in the subdirectory of the node-swift folder. At this point, in the build folder, I can see both the compiled files for arm64 and x86_64 architectures. However, when I go further into the example folder and execute the same command for compilation, it throws an error.


swift build --arch x86_64 --arch arm64                               
0%: Compute target dependency graph
Building targets in dependency order
Target dependency graph (26 targets)
0%: Gather provisioning inputs
0%: Create build description
0%: Discovering version info for clang
0%: Discovering version info for clang
0%: Discovering version info for swiftc
0%: Discovering version info for clang
0%: Discovering version info for ld
0%: Discovering version info for clang
0%: Discovering version info for clang
0%: Discovering version info for clang
0%: Discovering version info for clang
Build description signature: 559fb0e5e7f2b5972221aa3eb9142a81

Build description path: /Users/Rex/GitLab/node_swift/Example/.build/apple/Intermediates.noindex/XCBuildData/559fb0e5e7f2b5972221aa3eb9142a81.xcbuilddata

Unable to resolve build file: BuildFile<PACKAGE-PRODUCT:NodeAPI::BUILDPHASE_0::4> (The workspace has a reference to a missing target with GUID 'PACKAGE-TARGET:NodeAPIMacros@11')
Unable to resolve build file: BuildFile<PACKAGE-PRODUCT:NodeAPI::BUILDPHASE_0::4> (The workspace has a reference to a missing target with GUID 'PACKAGE-TARGET:NodeAPIMacros@11')
Build cancelled
kabiroberai commented 7 months ago

There’s a couple of different aspects here:

  1. SwiftPM uses Xcode for Universal builds, but the Xcode generator doesn’t appear to support macros yet.
  2. Even just compiling for x86_64 from arm64 doesn’t work as expected with SwiftPM when macros are involved. Fortunately a PR to fix this was just merged so hopefully this should be resolved in an upcoming release of Swift: https://github.com/apple/swift-package-manager/pull/7353

As a workaround, node-swift has the ability to directly build with xcodebuild instead of swift build. You can use that mode instead by setting "swift": { "builder": "xcode" } in your package.json before running npm run node-swift build.