mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 07:41:12 +00:00
Use zig to build bitcode
This commit is contained in:
parent
3d8f34b553
commit
6278a19c35
9 changed files with 89 additions and 48 deletions
|
@ -7,15 +7,17 @@ To build the compiler, you need these installed:
|
|||
|
||||
* `libunwind` (macOS should already have this one installed)
|
||||
* `libc++-dev`
|
||||
* [`zig`](https://ziglang.org/download/)
|
||||
* a particular version of LLVM
|
||||
|
||||
To run the test suite (via `cargo test`), you additionally need to install:
|
||||
|
||||
* [`valgrind`](https://www.valgrind.org/) (needs special treatment to [install on macOS](https://stackoverflow.com/a/61359781)]
|
||||
|
||||
Some systems may already have `libc++-dev` on them, but if not, you may need to install it. (On Ubuntu, this can be done with `sudo apt-get install libc++-dev`.) macOS systems
|
||||
should already have `libunwind`, but other systems will need to install it
|
||||
(e.g. with `sudo apt-get install libunwind-dev`).
|
||||
Some systems may already have `libc++-dev` on them, but if not, you may need to install it. (On Ubuntu, this can be done with `sudo apt-get install libc++-dev`.)
|
||||
MacOS systems should already have `libunwind`, but other systems will need to install it (e.g. with `sudo apt-get install libunwind-dev`).
|
||||
|
||||
To install Zig on macOS, use `brew install zig`. To install on Ubuntu, checkout [zig's docs](https://github.com/dryzig/zig-debian/blob/master/README.md).
|
||||
|
||||
To see which version of LLVM you need, take a look at `Cargo.toml`, in particular the `branch` section of the `inkwell` dependency. It should have something like `llvmX-Y` where X and Y are the major and minor revisions of LLVM you need.
|
||||
|
||||
|
|
|
@ -60,3 +60,11 @@ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
|
|||
add-apt-repository "${REPO_NAME}"
|
||||
apt-get update
|
||||
apt-get install -y clang-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION libc++abi-dev libunwind-dev valgrind
|
||||
|
||||
# install zig
|
||||
echo 'deb https://dl.bintray.com/dryzig/zig-ubuntu bionic main' | tee -a /etc/apt/sources.list
|
||||
apt update
|
||||
apt install zig
|
||||
|
||||
# symlink llvm tools
|
||||
ln -s /usr/bin/llvm-as-10 /usr/local/bin/llvm-as
|
||||
|
|
5
compiler/builtins/bitcode/.gitignore
vendored
5
compiler/builtins/bitcode/.gitignore
vendored
|
@ -1 +1,4 @@
|
|||
lib.ll
|
||||
main.ll
|
||||
main.o
|
||||
src/zig-cache
|
||||
zig-cache
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// float -> f32
|
||||
// double -> f64
|
||||
// int -> i16
|
||||
// long int -> i32
|
||||
// long long -> i64
|
||||
|
||||
bool is_finite_(double num) { return isfinite(num); }
|
||||
|
||||
double atan_(double num) { return atan(num); }
|
||||
|
||||
long long pow_int_(long long base, long long exp) {
|
||||
int acc = 1;
|
||||
|
||||
while (exp > 1) {
|
||||
if ((exp & 1) == 1) {
|
||||
acc *= base;
|
||||
}
|
||||
exp /= 2;
|
||||
base *= base;
|
||||
}
|
||||
|
||||
// Deal with the final bit of the exponent separately, since
|
||||
// squaring the base afterwards is not necessary and may cause a
|
||||
// needless overflow.
|
||||
if (exp == 1) {
|
||||
acc *= base;
|
||||
}
|
||||
|
||||
return acc;
|
||||
}
|
18
compiler/builtins/bitcode/src/main.zig
Normal file
18
compiler/builtins/bitcode/src/main.zig
Normal file
|
@ -0,0 +1,18 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
fn is_finite_(f: f64) bool {
|
||||
return std.math.isFinite(f);
|
||||
}
|
||||
|
||||
fn atan(f: f64) f64 {
|
||||
return std.math.atan(f);
|
||||
}
|
||||
|
||||
fn pow_int_(base: i64, exp: i64) i64 {
|
||||
return std.math.pow(i64, base, exp);
|
||||
}
|
||||
|
||||
// test "basic add functionality" {
|
||||
// testing.expect(add(3, 7) == 10);
|
||||
// }
|
|
@ -4,20 +4,27 @@ use std::path::Path;
|
|||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let src_path = fs::canonicalize("./../builtins/bitcode/src/lib.c")
|
||||
let src_path = fs::canonicalize("./../builtins/bitcode/src/main.zig")
|
||||
.expect("Failed to resolve bitcode source");
|
||||
let src = src_path.to_str().expect("Invalid src path");
|
||||
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
let dest_path = Path::new(&out_dir).join("builtins.bc");
|
||||
let dest = dest_path.to_str().expect("Invalid dest path");
|
||||
let dest_ll_path = Path::new(&out_dir).join("builtins.ll");
|
||||
let dest_ll = dest_ll_path.to_str().expect("Invalid dest ir path");
|
||||
let dest_bc_path = Path::new(&out_dir).join("builtins.bc");
|
||||
let dest_bc = dest_bc_path.to_str().expect("Invalid dest bc path");
|
||||
|
||||
Command::new("clang")
|
||||
.args(&["-emit-llvm", "-o", dest, "-c", src])
|
||||
Command::new("zig")
|
||||
.args(&["build-obj", src, "-femit-llvm-ir=", dest_ll])
|
||||
.status()
|
||||
.unwrap();
|
||||
|
||||
Command::new("llvm-as")
|
||||
.args(&[dest_ll, "-o", dest_bc])
|
||||
.status()
|
||||
.unwrap();
|
||||
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed={}", src);
|
||||
println!("cargo:rustc-env=BUILTINS_BC={}", dest);
|
||||
println!("cargo:rustc-env=BUILTINS_BC={}", dest_bc);
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
if isMacOS
|
||||
then
|
||||
# As of 2020-10-25, building Zig from source on MacOS fails
|
||||
# so we just download the binary from their release page
|
||||
let
|
||||
version = "0.6.0";
|
||||
archiveName = "zig-macos-x86_64-${version}+91a1c20e7";
|
||||
in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
pname = "zig-${version}";
|
||||
pname = "zig";
|
||||
version = version;
|
||||
buildInputs = [ pkgs.gzip ];
|
||||
src = pkgs.fetchurl {
|
||||
|
@ -19,7 +21,9 @@ if isMacOS
|
|||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
tar -xf $src
|
||||
cp ${archiveName}/zig $out/bin/zig
|
||||
cp ${archiveName}/zig $out/zig
|
||||
cp -r ${archiveName}/lib $out/lib
|
||||
ln -s "$out/zig" "$out/bin/zig"
|
||||
chmod +x $out/bin/zig
|
||||
'';
|
||||
}
|
||||
|
|
29
nix/zls.nix
Normal file
29
nix/zls.nix
Normal file
|
@ -0,0 +1,29 @@
|
|||
{ pkgs, zig }:
|
||||
|
||||
# As of 2020-10-25, building zls is not available on Nix
|
||||
# For some reason, this hangs on `zig build`. I'll try
|
||||
# to figure it our later :(
|
||||
|
||||
let
|
||||
rev = "e8c20351d85da8eb4bf22480045b994007284d69";
|
||||
in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
pname = "zig-language-server";
|
||||
version = rev;
|
||||
src = pkgs.fetchgit {
|
||||
inherit rev;
|
||||
fetchSubmodules = true;
|
||||
url = "https://github.com/zigtools/zls.git";
|
||||
sha256 = "06g8gml1g0fmvcfysy93bd1hb64vjd2v12x3kgxz58kmk5z0168y";
|
||||
};
|
||||
phases = [ "buildPhase" "installPhase" ];
|
||||
buildInputs = [ zig ];
|
||||
buildPhase = ''
|
||||
zig build
|
||||
'';
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp ./zig-cache/bin/zls $out/bin/zls
|
||||
chmod +x $out/bin/zls
|
||||
'';
|
||||
}
|
|
@ -9,10 +9,11 @@ with {
|
|||
ref = "refs/heads/nixpkgs-unstable";
|
||||
rev = "502845c3e31ef3de0e424f3fcb09217df2ce6df6";
|
||||
}) { };
|
||||
|
||||
isMacOS = builtins.currentSystem == "x86_64-darwin";
|
||||
};
|
||||
|
||||
let
|
||||
isMacOS = builtins.currentSystem == "x86_64-darwin";
|
||||
darwin-frameworks =
|
||||
if isMacOS then
|
||||
with pkgs.darwin.apple_sdk.frameworks; [
|
||||
|
@ -28,14 +29,15 @@ let
|
|||
[ ];
|
||||
llvm = pkgs.llvm_10;
|
||||
lld = pkgs.lld_10; # this should match llvm's version
|
||||
zig = import ./nix/zig.nix { inherit pkgs isMacOS; };
|
||||
inputs =
|
||||
[
|
||||
# build libraries
|
||||
pkgs.rustup
|
||||
pkgs.cargo
|
||||
llvm
|
||||
pkgs.valgrind
|
||||
(import ./nix/zig.nix { inherit pkgs isMacOS; })
|
||||
zig
|
||||
llvm
|
||||
# llb deps
|
||||
pkgs.libffi
|
||||
pkgs.libxml2
|
||||
|
@ -44,6 +46,7 @@ let
|
|||
lld
|
||||
# dev tools
|
||||
pkgs.rust-analyzer
|
||||
# (import ./nix/zls.nix { inherit pkgs zig; })
|
||||
pkgs.ccls
|
||||
];
|
||||
in pkgs.mkShell {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue