diff --git a/BUILDING_FROM_SOURCE.md b/BUILDING_FROM_SOURCE.md index e7a9b99939..c43f5b8999 100644 --- a/BUILDING_FROM_SOURCE.md +++ b/BUILDING_FROM_SOURCE.md @@ -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. diff --git a/ci/install-ci-libraries.sh b/ci/install-ci-libraries.sh index 5a0bcb88a0..ac0b4da54c 100755 --- a/ci/install-ci-libraries.sh +++ b/ci/install-ci-libraries.sh @@ -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 diff --git a/compiler/builtins/bitcode/.gitignore b/compiler/builtins/bitcode/.gitignore index c7f8e4f525..2263f3259e 100644 --- a/compiler/builtins/bitcode/.gitignore +++ b/compiler/builtins/bitcode/.gitignore @@ -1 +1,4 @@ -lib.ll +main.ll +main.o +src/zig-cache +zig-cache diff --git a/compiler/builtins/bitcode/src/lib.c b/compiler/builtins/bitcode/src/lib.c deleted file mode 100644 index a97b60d8d5..0000000000 --- a/compiler/builtins/bitcode/src/lib.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - -// 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; -} diff --git a/compiler/builtins/bitcode/src/main.zig b/compiler/builtins/bitcode/src/main.zig new file mode 100644 index 0000000000..869ea4ea10 --- /dev/null +++ b/compiler/builtins/bitcode/src/main.zig @@ -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); +// } diff --git a/compiler/gen/build.rs b/compiler/gen/build.rs index bf76f2cfcc..3a023c1a10 100644 --- a/compiler/gen/build.rs +++ b/compiler/gen/build.rs @@ -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); } diff --git a/nix/zig.nix b/nix/zig.nix index 5d459c8f8c..03c506b0fa 100644 --- a/nix/zig.nix +++ b/nix/zig.nix @@ -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 ''; } diff --git a/nix/zls.nix b/nix/zls.nix new file mode 100644 index 0000000000..4a50135b38 --- /dev/null +++ b/nix/zls.nix @@ -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 + ''; +} diff --git a/shell.nix b/shell.nix index 0e32424426..b43d84e37d 100644 --- a/shell.nix +++ b/shell.nix @@ -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 {