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

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

我已经尝试了这个明显的参数-target-cpu arm64,它给了我一个链接器消息< unkNown>:0:warning:参数在编译期间未使用:’-mcpu = arm64′
ld:库找不到-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
您可以使用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的输入优化脚本.有关更多信息,请参阅评论.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...