swift:如何从命令行生成arm7或arm64

时间:2017-03-08 16:45:49

标签: swift command-line

使用swiftc的Xcode 8.3 beta版本,如何生成Arm7或Arm64二进制文件?

我已经尝试了明显的参数-target-cpu arm64,它给了我一个链接器消息<unknown>:0: warning: argument unused during compilation: '-mcpu=arm64' ld: library not found for -lobjc,它正在努力构建一个x64目标。

实际命令:

swiftc -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/ -L /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/ -swift-version 3 -target-cpu arm64 somefile.swift

1 个答案:

答案 0 :(得分:10)

您可以使用sdk和target选项执行此操作。这是一个例子:

/Projects/Test $ cat main.swift 
print("Hello world!");

编译x86_64

/Projects/Test $ swiftc main.swift 
/Projects/Test $ lipo -info main
Non-fat file: main is architecture: x86_64
/Projects/Test $ ./main
Hello world!

编译armv7

/Projects/Test $ swiftc main.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.2.sdk -target armv7-apple-ios8.1
/Projects/Test $ lipo -info main
Non-fat file: main is architecture: armv7

编译arm64

/Projects/Test $ swiftc main.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.2.sdk -target arm64-apple-ios8.1
/Projects/Test $ lipo -info main
Non-fat file: main is architecture: arm64

我写了一个脚本build.sh,以便更容易使用:

#!/bin/sh
TARGET_MAC_VERSION=10.11
TARGET_IOS_VERSION=8.1

if [ "$#" -ne 2 ]; then
    echo "Usage $0: <armv7/arm64/x86_64> <file>"
    exit
fi

if [ "$1" != 'armv7' ] && [ "$1" != 'arm64' ] && [ "$1" != 'x86_64' ]; then
  echo "Usage $0: <armv7/arm64/x86_64>"
  exit
fi

if [ "$1" == 'x86_64' ]; then
  SDK=macosx
  TARGET="x86_64-macosx$TARGET_MAC_VERSION"
else
  SDK=iphoneos
  TARGET="$1-apple-ios$TARGET_IOS_VERSION"
fi
echo "xcrun -sdk  $SDK swiftc $2 -target $TARGET"
xcrun -sdk  $SDK swiftc $2 -target $TARGET

输出

  $ ./build.sh armv7 main.swift 
xcrun -sdk  iphoneos swiftc main.swift -target armv7-apple-ios8.1
  $ lipo -info main
Non-fat file: main is architecture: armv7
  $ ./build.sh arm64 main.swift 
xcrun -sdk  iphoneos swiftc main.swift -target arm64-apple-ios8.1
  $ lipo -info main
Non-fat file: main is architecture: arm64
  $ ./build.sh x86_64 main.swift 
xcrun -sdk  macosx swiftc main.swift -target x86_64-macosx10.11
  $ lipo -info main
Non-fat file: main is architecture: x86_64

编辑注意:根据@jens的输入优化脚本。有关更多信息,请参阅注释。