huh/justfile
2026-02-18 18:25:17 -07:00

143 lines
4.3 KiB
Makefile

set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
set shell := ["bash", "-cu"]
# configuration
project := "p2601"
out_dir := "out"
thirdparty := "thirdparty"
# platform-specific
host_os := if os() == "macos" { "macos" } else { os() }
host_arch := arch()
exe := if os() == "windows" { ".exe" } else { "" }
# zig stuff
zig_version := "0.15.2"
zig_slug := "zig-" + host_arch + "-" + host_os + "-" + zig_version
zig_dir := thirdparty / zig_slug
zig := zig_dir / ("zig" + exe)
zig_ext := if os() == "windows" { "zip" } else { "tar.xz" }
zig_url := "https://ziglang.org/download/" + zig_version + "/" + zig_slug + "." + zig_ext
# blocks runtime
blocks_dir := thirdparty / "blocksruntime"
blocks_repo := "https://github.com/mackyle/blocksruntime.git"
# use the real blocks runtime if we're on mac
blocks_include := if os() == "macos" { "" } else { "-I" + blocks_dir / "BlocksRuntime" + " -I" + blocks_dir }
blocks_src := if os() == "macos" { "" } else { blocks_dir / "BlocksRuntime" / "runtime.c" + " " + blocks_dir / "BlocksRuntime" / "data.c" }
# cflags
macflags := if os() == "macos" { "-x objective-c -lobjc -framework Foundation -framework Cocoa -framework CoreFoundation -framework CoreGraphics -framework QuartzCore -framework Metal -framework MetalKit"} else { "" }
csrc := "src/main.c" + " " + blocks_src
cinc := "-Isrc -Ithirdparty " + blocks_include
cflags := "-std=c23 -fblocks -g " + macflags + " " + cinc
# how the hotdog is made
# ----------------------
# platform-specific vendoring
[unix]
[private]
vendor-zig:
#!/usr/bin/env bash
set -euo pipefail
[[ -x "{{zig}}" ]] && exit 0
echo ":: Downloading Zig {{zig_version}} ({{host_os}}/{{host_arch}})…"
echo {{zig_url}}
mkdir -p "{{thirdparty}}"
curl -sSfL "{{zig_url}}" | tar -xJ -C "{{thirdparty}}"
[windows]
[private]
vendor-zig:
#!powershell.exe
if (Test-Path "{{zig}}") { exit 0 }
Write-Host ":: Downloading Zig {{zig_version}} ({{host_os}}/{{host_arch}})..."
New-Item -ItemType Directory -Force -Path "{{thirdparty}}" | Out-Null
$tmp = Join-Path $env:TEMP "zig-vendor.zip"
Invoke-WebRequest -Uri "{{zig_url}}" -OutFile $tmp -UseBasicParsing
Expand-Archive -Path $tmp -DestinationPath "{{thirdparty}}" -Force
Remove-Item $tmp
[unix]
[private]
vendor-blocks:
#!/usr/bin/env bash
set -euo pipefail
[[ -d "{{blocks_dir}}" ]] && exit 0
echo ":: Cloning libBlocksRuntime…"
git clone --depth 1 "{{blocks_repo}}" "{{blocks_dir}}"
[windows]
[private]
vendor-blocks:
#!powershell.exe
if (Test-Path "{{blocks_dir}}") { exit 0 }
Write-Host ":: Cloning libBlocksRuntime..."
git clone --depth 1 "{{blocks_repo}}" "{{blocks_dir}}"
[doc("Pull in unversioned thirdparty dependencies")]
vendor: vendor-zig vendor-blocks
# building/running
[unix]
[private]
_mkout:
@mkdir -p "{{out_dir}}"
[windows]
[private]
_mkout:
@New-Item -ItemType Directory -Force -Path "{{out_dir}}" | Out-Null
[doc("Compile natively (debug)")]
[default]
build: vendor _mkout
{{zig}} cc {{cflags}} {{csrc}} {{blocks_include}} -o {{out_dir}}/{{project}}{{exe}}
[doc("Compile natively with optimizations")]
build-release: vendor _mkout
{{zig}} cc {{cflags}} -Ofast -Wno-everything -ffast-math -DNDEBUG {{csrc}} {{blocks_include}} -o {{out_dir}}/{{project}}{{exe}}
[doc("Compile for wasm32-wasi")]
build-wasm: vendor _mkout
{{zig}} cc {{cflags}} -target wasm32-wasi {{csrc}} {{blocks_include}} -o {{out_dir}}/{{project}}.wasm
[doc("Cross-compile for a Zig target triple (e.g. aarch64-linux-gnu)")]
build-cross target: vendor _mkout
{{zig}} cc {{cflags}} -target {{target}} {{csrc}} {{blocks_include}} -o {{out_dir}}/{{project}}-{{target}}
[doc("Build and run natively")]
run *args: build
@./{{out_dir}}/{{project}}{{exe}} {{args}}
[doc("Build for wasm32-wasi and run via Wasmer")]
run-wasm *args: build-wasm
@wasmer run {{out_dir}}/{{project}}.wasm -- {{args}}
# extras
[doc("List Zig cross-compilation targets")]
list-targets: vendor-zig
{{zig}} targets
[doc("List available just commands")]
list:
@just --list
[doc("Remove build artifacts")]
[unix]
[confirm]
@clean:
rm -rf {{out_dir}}
[doc("Remove build artifacts")]
[windows]
[confirm]
clean:
if (Test-Path "{{out_dir}}") { Remove-Item -Recurse -Force "{{out_dir}}" }