在 Ubuntu 中编译安装苹果iOS工具链(ios-toolchain) | Konica 的自留地

在 Ubuntu 中编译安装苹果iOS工具链(ios-toolchain)

闪电, 罢工, 螺栓, 电力, 能源, 风暴, 云, 雷暴, 闪存, 气候, 天气, 引人注目, 隐喻

前言

在 Linux 机器中编译安装 iOS 工具链,是不是感觉很奇妙?
实际上,苹果公司的项目并非都是闭源的,苹果公司就开源了某些 iOS 的 api.

https://opensource.apple.com/tarballs/clang
https://opensource.apple.com/tarballs/tapi

所以我们可以在非 macOS 机器上编译安装 iOS 工具链
类似于 Android NDK 的 make standalone toolchain

本文以 Ubuntu 系统为例,其他操作系统未测试

需要做的准备

  1. Ubuntu系统
  2. 准备 iOS-SDK,我已打包,可直接下载使用。
    打包方法为:下载最新版本的 Xcode,将
    Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.0.sdk(可能不是 10.0)提取出,使用 tar+gz 打包即可。
  3. Github cctools-port

检查 clang 版本

安装依赖前先运行clang -v查看 clang 的版本
如果版本号高于或等于4.0,或者系统没有安装clang,请跳到下一步骤
如果版本号低于4.0,要先把clang卸载

1
sudo apt-get autoremove clang

安装依赖

请使用 Ubuntu 的官方软件源,使用第三方软件源可能会出现版本太低、找不到软件包等问题
软件源配置在 /etc/apt/sources.list, 内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#deb cdrom:[Ubuntu 16.04.2 LTS _Xenial Xerus_ - Release amd64 (20170215.2)]/ xenial main restricted
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial universe
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

使用 apt-get 安装依赖
安装期间可能会有一些操作需要确认,全都 yes 就行了

1
2
sudo apt-get update
sudo apt-get install git gcc cmake libssl-dev libtool autoconf automake clang-4.0

如果报错找不到 clang-4.0,说明你该换源了

运行clang-4.0 -v以确认 clang-4.0 安装成功
运行which clang-4.0以确认 clang-4.0 安装位置,如果不是/usr/bin,下面命令要修改

制作软链接

1
2
sudo ln -s /usr/bin/clang-4.0 /usr/bin/clang
sudo ln -s /usr/bin/clang++-4.0 /usr/bin/clang++

拷贝 cctools-port 到本地

1
2
3
mkdir ~/git
cd ~/git
git clone https://github.com/tpoechtrager/cctools-port

制作 iOS 工具链 (ios-toolchain)

确定 iOS-SDK 的本地位置

我将 iOS-SDK 放入家目录
~/iPhoneOS10.0.sdk.tar.gz

制作工具链

build.sh 使用方法

./build.sh /path/to/sdk.tar.*

参见具体使用方法

制作 iOS armv7 工具链

1
2
cd cctools-port
IPHONEOS_DEPLOYMENT_TARGET=5.0 usage_examples/ios_toolchain/build.sh ~/iPhoneOS10.0.sdk.tar.gz armv7

制作工具链成功后会提示*** all done ***

将生成的工具链移到 /usr/local/ 目录并更名为 ios-ndk-armv7

1
sudo mv usage_examples/ios_toolchain/target /usr/local/ios-ndk-armv7

将库文件拷贝一份,放进公共库 /usr/lib

1
sudo cp /usr/local/ios-ndk-armv7/lib/libtapi.so /usr/lib

最后将工具链的 bin 目录加入PATH,方便调用

1
export PATH=$PATH:/usr/local/ios-ndk-armv7/bin

制作 iOS arm64 工具链

1
2
cd cctools-port
IPHONEOS_DEPLOYMENT_TARGET=5.0 usage_examples/ios_toolchain/build.sh ~/iPhoneOS10.0.sdk.tar.gz arm64

制作工具链成功后会提示*** all done ***

将生成的工具链移到 /usr/local/ 目录并更名为 ios-ndk-arm64

1
sudo mv usage_examples/ios_toolchain/target /usr/local/ios-ndk-arm64

使用 rename 命令重命名前缀以与 armv7 区分开来
把 arm- 前缀改为 aarch64- 前缀

1
2
3
rename 's/arm-/aarch64-/' /usr/local/ios-ndk-arm64/bin/*
sudo rm /usr/local/ios-ndk-arm64/aarch64-apple-darwin11-clang++
sudo ln -s /usr/local/ios-ndk-arm64/aarch64-apple-darwin11-clang /usr/local/ios-ndk-arm64/aarch64-apple-darwin11-clang++

将库文件拷贝一份,放进公共库 /usr/lib

1
sudo cp /usr/local/ios-ndk-armv7/lib/libtapi.so /usr/lib

最后将工具链的 bin 目录加入PATH,方便调用

1
export PATH=$PATH:/usr/local/ios-ndk-arm64/bin

(可选)合并 armv7 和 arm64 工具链

除了 bin 目录,其他的目录内容貌似都是一样的,把两个 bin 目录合在一起就行了

示例:编译 iOS C语言程序

hello.c 代码:

1
2
3
4
5
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
}

编译程序

1
arm-apple-darwin11-clang hello.c -o hello

编译成功,完整过程:

1
2
3
4
5
6
7
8
apple@ubuntu:~/hello$ vim hello.c
apple@ubuntu:~/hello$ arm-apple-darwin11-clang hello.c -o hello
apple@ubuntu:~/hello$ ls
hello hello.c
apple@ubuntu:~/hello$ ./hello
bash: ./hello: cannot execute binary file: Exec format error
apple@ubuntu:~/hello$ file hello
hello: Mach-O arm_v7 executable