我很难在Ubuntu 16.04上安装Swift 3.0和GCD。这在今天应该是可能的,对吧?
下面是一个Ansible任务,用于从Swift.org下载Swift 3,从GitHub克隆、构建和安装Swift corelibs-libdispatch。
即使libdispatch的安装完成时没有出现错误,它也不起作用。当我在Swift repl中尝试import Dispatch
时,它抱怨缺少功能"块"。检查Makefiles确认,至少向编译器提供了标志-fblocks
。
以下是Swift repl:的示例输出
vagrant@swift3:/tmp/swift-3.0-PREVIEW-3-ubuntu15.10/usr/bin$ ./swift
Welcome to Swift version 3.0 (swift-3.0-PREVIEW-3). Type :help for assistance.
1> 6 * 7
$R0: Int = 42
2> import Dispatch
error: module 'CDispatch' requires feature 'blocks'
error: could not build Objective-C module 'CDispatch'
2>
用于设置盒子的流浪文件:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.ssh.forward_agent = true
config.vm.box = "bento/ubuntu-16.04"
config.vm.define "swift3" do |dev|
dev.vm.hostname = "swift3.dev"
end
config.vm.network :private_network, ip: "10.0.0.10"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/main.yml"
end
end
安装Swift 3:的可靠任务
---
- name: Install Swift 3 requirements
apt: name={{ item }} state=installed
with_items:
- autoconf
- clang
- git
- libblocksruntime-dev
- libbsd-dev
- libcurl4-openssl-dev
- libdispatch-dev
- libkqueue-dev
- libpython2.7-dev
- libtool
- pkg-config
- name: download Swift 3
get_url: url=https://swift.org/builds/swift-3.0-preview-3/ubuntu1510/swift-3.0-PREVIEW-3/swift-3.0-PREVIEW-3-ubuntu15.10.tar.gz
dest=/tmp/swift.tgz mode=0440
- name: unarchive Swift 3
unarchive: dest=/tmp src=/tmp/swift.tgz copy=no creates=/tmp/swift-3.0-PREVIEW-3-ubuntu15.10
- name: clone Swift 3 libdispatch core library
git: repo=https://github.com/apple/swift-corelibs-libdispatch dest=/tmp/swift-corelibs-libdispatch
version=swift-3.0-preview-3-branch force=true
- name: generate Swift 3 libdispatch build files
command: "sh ./autogen.sh"
args:
chdir: /tmp/swift-corelibs-libdispatch
- name: configure Swift 3 libdispatch
command: "sh ./configure --with-blocks-runtime=/usr/lib/x86_64-linux-gnu --with-swift-toolchain=/tmp/swift-3.0-PREVIEW-3-ubuntu15.10/usr --prefix=/tmp/swift-3.0-PREVIEW-3-ubuntu15.10/usr"
args:
chdir: /tmp/swift-corelibs-libdispatch
- name: make Swift 3 libdispatch
command: "make"
args:
chdir: /tmp/swift-corelibs-libdispatch
- name: install Swift 3 libdispatch
command: "make install"
args:
chdir: /tmp/swift-corelibs-libdispatch
- name: grant permissions to use Swift 3
file: dest=/tmp/swift-3.0-PREVIEW-3-ubuntu15.10 mode=a+rX recurse=true
正如您所注意到的,在编译libdispatch时,-fblocks
链接器标志被适当地设置。这很好,因为现在您有了libdispatch的工作版本。
不幸的是,您所做的任何包含Dispatch
的也是也将需要-fblocks
链接器标志。
tl;dr的解决方案是在编译时简单地将-Xcc -fblocks
提供给swiftc
。
正如我所说,这是一个变通办法。长期的解决方案是"ClangImporter:在非Darwin平台上启用-fblocks"。尽管上述工作是从你所在的地方到你想去的地方的最短距离,但在这之前,一切都会结束。
我自己补充一下,我只是使用上面pull请求中的补丁来修补我的本地构建。YMMV。