about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/compilers/mrustc/bootstrap.nix
blob: 35e7daaf21031056ab23c4bc5eb6af9d16cdd1a8 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{ lib, stdenv
, fetchurl
, mrustc
, mrustc-minicargo
, rust
, llvm_7
, llvmPackages_7
, libffi
, cmake
, python3
, zlib
, libxml2
, openssl
, pkg-config
, curl
, which
, time
}:

let
  rustcVersion = "1.29.0";
  rustcSrc = fetchurl {
    url = "https://static.rust-lang.org/dist/rustc-${rustcVersion}-src.tar.gz";
    sha256 = "1sb15znckj8pc8q3g7cq03pijnida6cg64yqmgiayxkzskzk9sx4";
  };
  rustcDir = "rustc-${rustcVersion}-src";
in

stdenv.mkDerivation rec {
  pname = "mrustc-bootstrap";
  version = "${mrustc.version}_${rustcVersion}";

  inherit (mrustc) src;
  postUnpack = "tar -xf ${rustcSrc} -C source/";

  # the rust build system complains that nix alters the checksums
  dontFixLibtool = true;

  patches = [
    ./patches/0001-use-shared-llvm.patch
    ./patches/0002-dont-build-llvm.patch
    ./patches/0003-echo-newlines.patch
    ./patches/0004-increase-parallelism.patch
  ];

  postPatch = ''
    echo "applying patch ./rustc-${rustcVersion}-src.patch"
    patch -p0 -d ${rustcDir}/ < rustc-${rustcVersion}-src.patch

    for p in ${lib.concatStringsSep " " llvmPackages_7.compiler-rt.patches}; do
      echo "applying patch $p"
      patch -p1 -d ${rustcDir}/src/libcompiler_builtins/compiler-rt < $p
    done
  '';

  # rustc unfortunately needs cmake to compile llvm-rt but doesn't
  # use it for the normal build. This disables cmake in Nix.
  dontUseCmakeConfigure = true;

  strictDeps = true;
  nativeBuildInputs = [
    cmake
    mrustc
    mrustc-minicargo
    pkg-config
    python3
    time
    which
  ];
  buildInputs = [
    # for rustc
    llvm_7 libffi zlib libxml2
    # for cargo
    openssl curl
  ];

  makeFlags = [
    # Use shared mrustc/minicargo/llvm instead of rebuilding them
    "MRUSTC=${mrustc}/bin/mrustc"
    "MINICARGO=${mrustc-minicargo}/bin/minicargo"
    "LLVM_CONFIG=${llvm_7}/bin/llvm-config"
    "RUSTC_TARGET=${rust.toRustTarget stdenv.targetPlatform}"
  ];

  buildPhase = ''
    runHook preBuild

    local flagsArray=(
      PARLEVEL=$NIX_BUILD_CORES
      ${toString makeFlags}
    )

    echo minicargo.mk: libs
    make -f minicargo.mk "''${flagsArray[@]}" LIBS

    echo minicargo.mk: deps
    mkdir -p output/cargo-build
    # minicargo has concurrency issues when running these; let's build them
    # without parallelism
    for crate in regex regex-0.2.11 curl-sys
    do
      echo "building $crate"
      minicargo ${rustcDir}/src/vendor/$crate \
        --vendor-dir ${rustcDir}/src/vendor \
        --output-dir output/cargo-build -L output/
    done

    echo minicargo.mk: rustc
    make -f minicargo.mk "''${flagsArray[@]}" output/rustc

    echo minicargo.mk: cargo
    make -f minicargo.mk "''${flagsArray[@]}" output/cargo

    echo run_rustc
    make -C run_rustc "''${flagsArray[@]}"

    unset flagsArray

    runHook postBuild
  '';

  doCheck = true;
  checkPhase = ''
    runHook preCheck
    run_rustc/output/prefix/bin/hello_world | grep "hello, world"
    runHook postCheck
  '';

  installPhase = ''
    runHook preInstall
    mkdir -p $out/bin/ $out/lib/
    cp run_rustc/output/prefix/bin/cargo $out/bin/cargo
    cp run_rustc/output/prefix/bin/rustc_binary $out/bin/rustc

    cp -r run_rustc/output/prefix/lib/* $out/lib/
    cp $out/lib/rustlib/${rust.toRustTarget stdenv.targetPlatform}/lib/*.so $out/lib/
    runHook postInstall
  '';

  meta = with lib; {
    inherit (src.meta) homepage;
    description = "A minimal build of Rust";
    longDescription = ''
      A minimal build of Rust, built from source using mrustc.
      This is useful for bootstrapping the main Rust compiler without
      an initial binary toolchain download.
    '';
    maintainers = with maintainers; [ progval r-burns ];
    license = with licenses; [ mit asl20 ];
    platforms = [ "x86_64-linux" ];
  };
}