简体   繁体   English

(交叉)为Raspberry PI编译Swift

[英](Cross-)Compiling Swift for Raspberry PI

Swift is now Open Source. Swift现在是开源的。 Did anyone tried compiling Swift for a Raspberry PI? 有人试过为Raspberry PI编译Swift吗? I started to do, but my 8 GB SD card seems to be too small for it ;) Is it possible to cross compile it from Ubuntu? 我开始做了,但我的8 GB SD卡似乎太小了;)是否可以从Ubuntu交叉编译?

A 8GB SD Card works ok, but you'll need to extend the root volume. 8GB SD卡工作正常,但您需要扩展根音量。 I have it working and the used space on /dev/root partition is around 3.1GB. 我有它工作, /dev/root分区上的已用空间大约为3.1GB。

The steps below are based on the blog post by Andrew Madsen with a little extra focus on the steps inside fdisk . 以下步骤基于Andrew Madsen的博客文章,稍微关注fdisk的步骤。

Get Ubuntu 获取Ubuntu

Download an image of Ubuntu 14.04 for Raspberry Pi 2 from finnie.org and copy it onto the SD card. finnie.org下载Ubuntu 14.04 for Raspberry Pi 2的图像并将其复制到SD卡上。 Boot the Raspberry Pi. 启动Raspberry Pi。

Change the partition 更改分区

Log into the Raspberry Pi and change the partition size. 登录Raspberry Pi并更改分区大小。 The default size for /dev/root is 1.7G with 1.1G available. /dev/root的默认大小为1.7G,可用1.1G。 That is not enough. 这还不够。

$ df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.7G  540M  1.1G  35% /
devtmpfs        458M  4.0K  458M   1% /dev
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none             93M  228K   93M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            462M     0  462M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/mmcblk0p1   64M   20M   45M  31% /boot/firmware

Run fdisk 运行fdisk

sudo fdisk /dev/mmcblk0

At the prompt enter p for 'print the partition table'. 在提示符下输入p ''打印分区表'。 There are two partitions 有两个分区

/dev/mmcblk0p1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          133120     3670015     1768448   83  Linux

When prompted, enter d (for delete), then 2 . 出现提示时,输入d (删除),然后输入2 Then, recreate the partition by entering n , then p , then 2 , then pressing enter at the next two prompts accepting the defaults. 然后,通过输入n ,然后是p ,然后输入2重新创建分区,然后在接下来的两个提示中按Enter键接受默认值。

Enter p again and see the second partition is now bigger, now all space on an 8GB card is used. 再次输入p并看到第二个分区现在更大,现在使用8GB卡上的所有空间。

           Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          133120    15523839     7695360   83  Linux

Enter w to write the changes to disk, then reboot 输入w将更改写入磁盘,然后重新启动

sudo reboot

Resize the partition 调整分区大小

After the reboot, resize the partition's file system by running 重新启动后,通过运行调整分区文件系统的大小

sudo resize2fs /dev/mmcblk0p2

Swap space 交换空间

Setup a swap file by doing 通过执行设置交换文件

sudo apt-get install dphys-swapfile

Install libicu-dev and clang-3.6 安装libicu-dev和clang-3.6

sudo apt-get install libicu-dev clang-3.6

Use update-alternatives to provide /usr/bin links for clang and clang++: 使用update-alternatives为clang和clang ++提供/ usr / bin链接:

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100 
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100

Then, add @iachievedit's repository key: 然后,添加@ iachievedit的存储库密钥:

wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -

Add the appropriate repository to sources.list: 将相应的存储库添加到sources.list:

echo "deb [arch=armhf] http://iachievedit-repos.s3.amazonaws.com/ trusty main" | sudo tee --append /etc/apt/sources.list

Run apt-get update: 运行apt-get update:

sudo apt-get update

Install Swift 安装Swift

sudo apt-get install swift-2.2

After the installation is complete, you're ready to compile Swift programs! 安装完成后,您就可以编译Swift程序了!

Write Swift 写Swift

Open your favorite text editor, write a program, and save it (eg to 'hello.swift'): 打开您喜欢的文本编辑器,编写程序并保存(例如'hello.swift'):

let device = "Raspberry Pi 2!" 
print("Hello from Swift on \(device)")

Compile it 编译它

swiftc hello.swift

and run it: 并运行它:

./hello

Hello from Swift on Raspberry Pi 2!

That's it! 而已! Swift running on Raspberry Pi Swift在Raspberry Pi上运行

The Swift Package Manager got custom toolchain support via PR-1098 end of April 2017. Swift Package Manager在2017年4月底通过PR-1098获得了定制工具链支持。

I wrote up detailed instructions on how to build a RaspberryPi toolchain over here: macOS -> RasPi Cross Compilation Toolchain and even the reverse ( build macOS binaries on a RaspberryPi ) for the fun of it. 我写了关于如何在这里构建RaspberryPi工具链的详细说明: macOS - > RasPi交叉编译工具链 ,甚至反过来( 在RaspberryPi上构建macOS二进制文件 )以获得它的乐趣。 The same would work for Intel-Linux to ARM-Linux w/ minimal modifications. 这同样适用于Intel-Linux到ARM-Linux的最小修改。 The SwiftPM repo contains an example script on how to do this for Intel-macOS to Intel-Ubuntu. SwiftPM repo包含一个示例脚本 ,介绍如何将Intel-macOS用于Intel-Ubuntu。

You can find a 2017-05-01 update on Swift-on-ARM over here: An Update on Swift 3.1.1 For Raspberry Pi Zero/1/2/3 . 你可以在这里找到关于Swift-on-ARM的2017-05-01更新: Swift 3.1.1更新为Raspberry Pi Zero / 1/2/3

As a small summary, so that this answer isn't just links ;-), ARM status 2017-05-16: 作为一个小小的总结,这个答案不只是链接;-),ARM状态2017-05-16:

  • you can compile Swift 3.1/3.1.1 on RaspberryPi Ubuntu 你可以在RaspberryPi Ubuntu上编译Swift 3.1 / 3.1.1
    • don't forget to setup swap, some minimal patches are required for 3.1.1. 不要忘记设置交换,3.1.1需要一些最小的补丁。 8GB disk may be a bit to little. 8GB磁盘可能有点小。
  • compilation on Raspbian doesn't seem to work yet (last known version is 3.0.2) Raspbian上的编译似乎还没有工作(最后的已知版本是3.0.2)
  • you can cross-compile Swift using a custom toolchain, which is reasonably easy to setup 您可以使用自定义工具链交叉编译Swift,这很容易设置
    • you need to grab a SwiftPM snapshot (Swift 4) for this (but the toolchain itself can be 3.1 or even 3.0.2 w/ minor changes) 你需要为此获取一个SwiftPM快照(Swift 4)(但工具链本身可以是3.1甚至3.0.2 w /次要更改)
  • you can also run (and compile) Swift via Docker , eg in HypriotOS. 你也可以通过Docker运行(并编译) Swift ,例如在HypriotOS中。
  • there is a Slack group for Swift ARM: swift-arm Swift ARM有一个Slack组: swift-arm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM