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

前端之家收集整理的这篇文章主要介绍了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的输入优化脚本.有关更多信息,请参阅评论.

原文链接:https://www.f2er.com/swift/318812.html

猜你在找的Swift相关文章