首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NixOS上构建Swift项目?

如何在NixOS上构建Swift项目?
EN

Stack Overflow用户
提问于 2019-10-10 21:31:20
回答 1查看 615关注 0票数 3

我试图在NixOS上构建一个简单的Swift项目。以下是复制的步骤:

代码语言:javascript
复制
git clone https://github.com/PerfectlySoft/PerfectTemplate.git
cd PerfectTemplate
touch shell.nix

添加具有以下内容的shell.nix

代码语言:javascript
复制
with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "swift-env";

  buildInputs = [
    openssl
    stdenv
    binutils
    cmake
    pkgconfig
    curl
    glibc
    icu
    libblocksruntime
    libbsd
    libedit
    libuuid
    libxml2
    ncurses
    sqlite
    swig
  ];

}

运行nix-shell --pure shell.nix并从那里运行swift build,给出如下信息:

代码语言:javascript
复制
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-target’
gcc: error: x86_64-unknown-linux: No such file or directory
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fblocks’
gcc: error: unrecognized command line option ‘-target’
gcc: error: unrecognized command line option ‘-fmodule-name=PerfectCZlib’
gcc: error: unrecognized command line option ‘-fmodules’; did you mean ‘-fmudflap’?
gcc: error: unrecognized command line option ‘-fmodules-cache-path=/home/dmi3y/Dev/Swift/PerfectTemplate/.build/x86_64-unknown-linux/debug/ModuleCache’
gcc: error: unrecognized command line option ‘-fblocks’

我希望能在这方面提供任何帮助,因为我对NixOS和NixOS非常陌生。

编辑

我将我的shell.nix改为:

代码语言:javascript
复制
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "swift-env";

  buildInputs = with pkgs; [
    openssl
    clang
    swift
  ];

  shellHook = ''
    CC=clang
  '';
}

但是,当我运行nix-shellswift build时,我现在得到的是:

代码语言:javascript
复制
[1/25] Compiling PerfectCZlib zutil.c
[2/25] Compiling PerfectCZlib uncompr.c
[3/25] Compiling PerfectCZlib inftrees.c
[4/25] Compiling PerfectCZlib inflate.c
[5/25] Compiling PerfectCZlib trees.c
[6/25] Compiling PerfectCZlib inffast.c
[7/25] Compiling PerfectCZlib infback.c
[8/25] Compiling PerfectCZlib gzwrite.c
/nix/store/iz6k7x3l14ykr56crz3dizas1rrkcyzr-clang-wrapper-7.1.0/resource-root/include/module.modulemap:24:8: error: redefinition of module '_Builtin_intrinsics'

#  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
   ^
1 warning and 3 errors generated.
1 warning and 3 errors generated.

完整的日志可以找到这里

这现在看起来像是缺少的库依赖项或版本冲突。

为了记录在案,下面是关于我的设置的其他信息,这些信息可能会提供更多的信息:

代码语言:javascript
复制
$ nix-channel --list                                                    
home-manager https://github.com/rycee/home-manager/archive/master.tar.gz
nixos https://nixos.org/channels/nixos-19.09  

$ nix-shell --run 'swift -version'                                      
Swift version 5.0.2 (swift-5.0.2-RELEASE)
Target: x86_64-unknown-linux-gnub

在搜索完web之后,我发现了Swift编译器和这个具体评论的Jira问题。我决定将clang从依赖项中排除在外,并且只包含swift,因为这种组合似乎相互冲突:

代码语言:javascript
复制
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "swift-env";

  buildInputs = with pkgs; [
    openssl
    swift
  ];

  shellHook = ''
    CC=clang
  '';
}

这起作用了!我终于能够建造这个项目了!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 23:38:05

由于使用了--pure选项,所以不会将以前环境中的任何内容导入shell。这包括swift二进制本身。因此,首先我要将swift添加到buildInputs中。

您得到的错误表明,gcc甚至不理解正在传递的命令行选项。我对Swift一无所知,但在看到这一点之后,我认为它需要一个不同的编译器。当然,在快速搜索之后,它看起来像是基于llvm的。因此,还需要将clang添加到构建输入中。此外,您还需要使用mkShell而不是mkDerivation来设置合适的环境。

前者是后者的包装器,专为设置nix-shell而设计,并确保正确处理环境。您需要传入shellHook属性来使用clang envvar覆盖默认的c编译器到CCshellHook是在shell开头运行任意命令的一种方式。

最后,nix-shell命令默认情况下搜索shell.nix,因此不必将其作为参数传递。如果shell.nix不存在,那么它将尝试default.nix。如果两者都不存在,则必须显式指定nix表达式的位置。

TLDR

把它们放在一起:

代码语言:javascript
复制
# a more idiomatic way to import nixpkgs, overridable from the cmdline
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "swift-env";

  buildInputs = with pkgs; [
    # your other inputs
    clang
    swift
  ];

  shellHook = ''
    CC=clang
  '';
}

为了确保我的逻辑是正确的,我下载了回购程序,并让它运行。所有东西都成功地编译和运行。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58331375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档