From 5f95edf690b26c6b9172456261643c7eb2612987 Mon Sep 17 00:00:00 2001 From: chloe caruso Date: Mon, 27 May 2024 13:34:48 -0700 Subject: [PATCH] add build script --- README.md | 5 ++++- build | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ build.cmd | 29 +++++++++++++++++++++++++++++ build.zig | 12 +++++++++++- 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100755 build create mode 100644 build.cmd diff --git a/README.md b/README.md index 0f0fd74ed9af3ab73dd4b7d085668e4fd7ffc320..b8d160e953194d154e353630050120bd37feebed 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ This language is a mix of features from JavaScript, Rust, and Zig. Some notable - **Zig** - Execute any code at compile time, everything is an expression. - **JavaScript** - Garbage collected values with a versitile type system. +## Developing + +To compile a development build, one can use the provided `./build` or `.\build.cmd` scripts to download Zig and build the compiler. These scripts are effective aliases of `zig build`. A release build can be built by passing the `--release` flag to any of these commands. + ## Standard Library Currently, there is no standard library. One will be introduced later. @@ -79,4 +83,3 @@ var number: float = 0; // `number` is `float` ``` ## Functions - diff --git a/build b/build new file mode 100755 index 0000000000000000000000000000000000000000..3c2e1a79d588ca1c4ba3e0d7a5a6d723e87f1ab7 --- /dev/null +++ b/build @@ -0,0 +1,53 @@ +#!/bin/sh +# This script will build the compiler on any POSIX system. +# It automatically installs a Zig compiler to ./zig-cache +# if needed. The output will be in ./zig-out/bin. +# +# `curl` and `tar` is required in order to download and +# extract Zig, respectively. The rest of the dependencies +# are downloaded compiled using the Zig build system. +cd -- "$(dirname -- "$0")" + +ZIG_VERSION=$(grep .minimum_zig_version build.zig.zon \ + | cut -d '"' -f 2) + +ZIG="zig-cache/zig/$ZIG_VERSION/zig" +if [ ! -f "$ZIG" ]; then + OS=$(uname -s | tr '[:upper:]' '[:lower:]') + if [ "$OS" = "darwin" ]; then + OS="macos" + fi + ARCH=$(uname -m) + if [ "$ARCH" = "arm64" ]; then + ARCH="aarch64" + fi + + URL="https://ziglang.org/builds/zig-$OS-$ARCH-$ZIG_VERSION.tar.xz" + + echo "Installing Zig v$ZIG_VERSION via $URL" + + CURL=$(command -v curl 2>/dev/null) + TAR=$(command -v curl 2>/dev/null) + + if [ -z "$CURL" ]; then + echo "This script requires 'curl' to be installed." + fi + if [ -z "$TAR" ]; then + echo "This script requires 'tar' to be installed." + fi + + if [ -z "$CURL" ] || [ -z "$TAR" ]; then + exit 1 + fi + + mkdir -p zig-cache/zig/$ZIG_VERSION + curl -fsSL --retry 3 --retry-delay 3 $URL \ + | tar -xJ --strip-components=1 -C zig-cache/zig/$ZIG_VERSION + if [ "$($ZIG version)" != "$ZIG_VERSION" ]; then + echo "Zig failed to install." + rm -rf zig-cache/zig/$ZIG_VERSION + exit 1 + fi +fi + +$ZIG build $@ diff --git a/build.cmd b/build.cmd new file mode 100644 index 0000000000000000000000000000000000000000..07879cf6f0c99cdada497c0caa5fc6fb491beafb --- /dev/null +++ b/build.cmd @@ -0,0 +1,29 @@ +@ECHO OFF +@REM This script will build the compiler on any Windows system +@REM It automatically installs a Zig compiler to ./zig-cache +@REM The output will be in ./zig-out/bin + +SET ROOT=%~dp0 +FOR /F "tokens=3 delims=, " %%i IN ( + 'FINDSTR /R "\.minimum_zig_version *= *""".+""",\n" ^ + "%ROOT%build.zig.zon"' +) DO SET ZIG_VERSION=%%i +SET ZIG_VERSION=%ZIG_VERSION:~1,-1% +IF NOT EXIST "%ROOT%zig-cache\zig\%ZIG_VERSION%\zig.exe" GOTO :find_powershell +:build +"%ROOT%zig-cache\zig\%ZIG_VERSION%\zig.exe" build %* +EXIT /b %ERRORLEVEL% +:find_powershell +FOR %%i IN (pwsh powershell) DO WHERE %%i /Q && SET PS=%%i && GOTO :download +ECHO Powershell not found. Please install it and try again. +EXIT /b 1 +:download +SET ZIG_ARCH=x86_64 +SET URI=https://ziglang.org/builds/zig-windows-%ZIG_ARCH%-%ZIG_VERSION%.zip +SET X=$null = New-Item -ItemType Directory -Force -Path zig-cache\zig +SET X=%X%;Invoke-RestMethod -Uri %URI% -OutFile zig-cache/zig-%ZIG_VERSION%.zip +SET X=%X%;Expand-Archive zig-cache/zig-%ZIG_VERSION%.zip -DestinationPath zig-cache/zig +SET X=%X%;Remove-Item zig-cache/zig-%ZIG_VERSION%.zip +%PS% -NoProfile -Command "%X%" +MOVE zig-cache\zig\zig-windows-%ZIG_ARCH%-%ZIG_VERSION% zig-cache\zig\%ZIG_VERSION% >NUL +GOTO :build diff --git a/build.zig b/build.zig index 5bc5f92e3880af4528727e2f504297444d4d227a..a66998bce30b70508240713d6052815013e3e2c3 100644 --- a/build.zig +++ b/build.zig @@ -7,7 +7,17 @@ pub fn build(b: *std.Build) void { b.reference_trace = 20; const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); + const optimize_fallback = b.standardOptimizeOption(.{}); + const optimize: std.builtin.OptimizeMode = switch (b.release_mode) { + .off => optimize_fallback, + .safe => .ReleaseSafe, + .fast => .ReleaseFast, + .small => .ReleaseSmall, + .any => brk: { + b.release_mode = .safe; + break :brk .ReleaseSafe; + }, + }; const tracing = b.option(enum { none, -- 2.54.0