rustgn

11 commits
Updated 2026-05-09 17:23:41
scripts/build
scripts/build/mod.nu
const ROOT = (path self | path dirname | path dirname | path dirname)

const OUT = ($ROOT | path join ".out")
const TOOLS = ($ROOT | path join ".tools")
const ZED = ($ROOT | path join ".zed")

module out {
    export const DEBUG = ($OUT | path join "debug")
    export const LSP = ($OUT | path join "lsp")
    export const RELEASE = ($OUT | path join "release")
    export const RUST_ANALYZER = ($OUT | path join "rust-analyzer")
}

module tools {
    export const GN = ($TOOLS | path join "gn")
    export const CLANGD = ($TOOLS | path join "clangd")
    export const RUST_ANALYZER = ($TOOLS | path join "rust-analyzer")
    export const NINJA = ($TOOLS | path join "ninja")
    export const RUSTC = ($TOOLS | path join "rustc")
}

# Setup the build environment
export def "build setup" [] {
    print $"Setting up build environment in ($ROOT)..."
    print "  out: $OUT"
    print "  tools: $TOOLS"
    print "  zed: $ZED"
    mkdir $OUT
    mkdir $TOOLS
    mkdir $ZED

    print $"Exporting tools in ($TOOLS)..."
    for tool in ["nu", "gn", "ninja", "gcc", "rustc", "clangd", "rust-analyzer"] {
        ls (which $tool | get path | path dirname | first) |
        where type == file or type == symlink |
        where { |it| $it.name | path exists } |
        each { |it|
            let src = $it.name
            let dst = ($TOOLS | path join ($src | path basename))
            ^ln -sf $src $dst
        }
    }

    print $"Updating Zed settings in ($ZED)..."

    open ($ZED | path join "settings.json") |
        upsert lsp.clangd.binary.path (use tools CLANGD; $CLANGD) |
        upsert lsp.clangd.binary.arguments [$"--compile-commands-dir=(use out LSP; $LSP)"] |
        upsert lsp.rust-analyzer.binary.path (use tools RUST_ANALYZER; $RUST_ANALYZER) |
        upsert lsp.rust-analyzer.initialization_options {
            linkedProjects: [ (use out RUST_ANALYZER; $RUST_ANALYZER | path join "rust-project.json") ]
            check: {
                overrideCommand: [
                    (use tools NINJA; $NINJA),
                    $"-C(use out LSP; $LSP)"
                ]
            }
        } |
    save -f ($ZED | path join "settings.json")

    build reload
}

# Reload the build environment (ie. recreating LSP configuration)
export def "build reload" [] {
    print "Generating build configuration..."
    ^(use tools GN; $GN) gen (use out DEBUG; $DEBUG)
    ^(use tools GN; $GN) gen (use out RELEASE; $RELEASE) --args="build_mode=\"release\""
    ^(use tools GN; $GN) gen (use out LSP; $LSP) --args="rust_analyzer=true" --export-compile-commands --export-rust-project

    print "Exporting 'rust-project.json' metadata..."

    mkdir (use out RUST_ANALYZER; $RUST_ANALYZER)
    open (use out LSP; $LSP | path join "rust-project.json") |
        upsert sysroot (use tools RUSTC; ^$RUSTC --print sysroot) |
        upsert sysroot_src ($env.RUSTC_SRC)/library |
        upsert sysroot_project {
            sysroot: (use tools RUSTC; ^$RUSTC --print sysroot),
            sysroot_src: ($env.RUSTC_SRC)/library,
            crates: [
                (mkcrate "core" [[], []]),
                (mkcrate "alloc" [[core], [0]]),
                (mkcrate "std" [[core, alloc], [0, 1]]),
                (mkcrate "proc_macro" [[std], [2]]),
                (mkcrate "test" [[core, std], [0, 2]])
            ]
        } |
    save -f (use out RUST_ANALYZER; $RUST_ANALYZER | path join "rust-project.json")
}

def mkcrate [name: string, deps: list<list>] {
    {
        display_name: $name,
        root_module: ($env.RUSTC_SRC)/library/($name)/src/lib.rs,
        edition: "2024",
        deps: ($deps.0 | enumerate | each { |it| { crate: ($deps.1 | get $it.index), name: $it.item } })
    }
}

# Run the debug build
export def "build debug" [] {
    ^(use tools NINJA; $NINJA) -C (use out DEBUG; $DEBUG)
}

# Run the release build
export def "build release" [] {
    ^(use tools NINJA; $NINJA) -C (use out RELEASE; $RELEASE)
}

# Run the build check
export def "build check" [] {
    ^(use tools NINJA; $NINJA) -C (use out LSP; $LSP)
}