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

pli*_*nth 10 command-line swift

使用Xcode 8.3 beta版本swiftc,如何生成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
Run Code Online (Sandbox Code Playgroud)

man*_*shg 10

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

/Projects/Test $ cat main.swift 
print("Hello world!");
Run Code Online (Sandbox Code Playgroud)

为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!
Run Code Online (Sandbox Code Playgroud)

编译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
Run Code Online (Sandbox Code Playgroud)

编译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
Run Code Online (Sandbox Code Playgroud)

我写了一个脚本,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
Run Code Online (Sandbox Code Playgroud)

产量

  $ ./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
Run Code Online (Sandbox Code Playgroud)

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