Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: how to generate arm7 or arm64 from the command line

Using the Xcode 8.3 beta incarnation of swiftc, how do you generate an Arm7 or Arm64 binary?

I've tried the obvious argument -target-cpu arm64 which gives me a linker message <unknown>:0: warning: argument unused during compilation: '-mcpu=arm64' ld: library not found for -lobjc and it plows ahead trying to build an x64 target.

Actual command:

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
like image 457
plinth Avatar asked Mar 08 '17 16:03

plinth


1 Answers

you can use the sdk and target option to do this. Here is an example:

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

Compiling for 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!

Compiling for 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

Compiling for 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

I wrote a script build.sh so that its easier to use:

#!/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

Output

  $ ./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

Edit Note: Optimized the script based on the input from @jens. See the comments for more information.

like image 122
manishg Avatar answered Oct 23 '22 01:10

manishg