-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuild.lua
More file actions
182 lines (157 loc) · 5.01 KB
/
Copy pathbuild.lua
File metadata and controls
182 lines (157 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
-- Copyright 2025 The zb Authors
-- SPDX-License-Identifier: MIT
-- This is the zb build script that builds zb. :)
local stdlib <const> = fetchArchive {
url = "https://github.com/256lights/zb-stdlib/releases/download/v0.2.0/zb-stdlib-v0.2.0.tar.gz";
hash = "sha256:9991a0f854c4302b83b50e731e25e2b3120e23a807794b87c29a2c7e6520469b";
}
local go <const> = import(stdlib.."/packages/go/go.lua")
local seeds <const> = import(stdlib.."/bootstrap/seeds.lua")
local stdenv <const> = import(stdlib.."/stdenv/stdenv.lua")
local strings <const> = import(stdlib.."/strings.lua")
local tables <const> = import(stdlib.."/tables.lua")
local module <const> = {}
local getters <const> = {}
local version <const> = "0.2.0-beta6"
module.gomod = path {
path = ".";
name = "zb-source-go.mod";
filter = function(name)
return name == "go.mod" or name == "go.sum"
end;
}
---@param name string
---@param prefix string
---@return boolean
local function allowSubtree(name, prefix)
return name == prefix or name:sub(1, #prefix + 1) == prefix.."/"
end
function getters.src()
return path {
path = ".";
name = "zb-source";
filter = function(name)
local base = strings.baseNameOf(name)
return (allowSubtree(name, "bytebuffer") or
allowSubtree(name, "cmd") or
allowSubtree(name, "internal") or
allowSubtree(name, "sets") or
allowSubtree(name, "zbstore") or
allowSubtree(name, "launchd") or
allowSubtree(name, "systemd") or
name == "LICENSE" or
name == "go.mod" or
name == "go.sum") and
name ~= "cmd/zb/zb" and
name ~= "cmd/zb/zb.exe" and
name ~= "internal/ui/build" and
base ~= ".vscode" and
base ~= "node_modules" and
base ~= ".git" and
base ~= ".env" and
not base:find("%.js%.map$") and
not base:find("%.css%.map$")
end;
}
end
local zbTarget = {}
---@param args {
---makeDerivation: (fun(args: table<string, any>): derivation),
---makeDerivationNoCC: (fun(args: table<string, any>): derivation)?,
---go: any,
---targetSystem: string?,
---}
---@return derivation
function module.new(args)
local t = tables.clone(args)
t.version = version
t.gomod = module.gomod
return setmetatable(t, zbTarget)
end
function zbTarget:__index(key)
if key == "src" then
return module.src
else
return nil
end
end
function zbTarget:__outputs(buildSystem)
local goEnv = go.envForSystem(self.targetSystem or buildSystem)
local goToolchain = outputs(self.go, buildSystem)[""]
local modules = (self.makeDerivationNoCC or self.makeDerivation) {
pname = "zb-go-modules";
src = module.gomod;
buildSystem = buildSystem;
-- GOOS/GOARCH not needed for downloading.
-- Omitting it allows all targets to reuse the same derivation.
PATH = strings.makeBinPath {
goToolchain,
};
__network = true;
preBuild = [[export GOCACHE="$ZB_BUILD_TOP/cache"; export GOMODCACHE="$(pwd)/mod"]];
buildPhase = [[go mod download]];
installPhase = [[cp --reflink=auto -R "$GOMODCACHE" "$out"]];
}
local busybox
local seedsForSystem = seeds[self.targetSystem or buildSystem]
if seedsForSystem then
busybox = seedsForSystem.busybox
end
return self.makeDerivation {
pname = "zb";
version = version;
src = module.src;
buildSystem = buildSystem;
GOOS = goEnv.GOOS;
GOARCH = goEnv.GOARCH;
GOMODCACHE = modules;
PATH = strings.makeBinPath {
goToolchain,
};
busybox = busybox;
preBuild = [[export GOCACHE="$ZB_BUILD_TOP/cache"]];
-- See https://pkg.go.dev/cloud.google.com/go/storage#hdr-gRPC_API
-- for info about -tags=disable_grpc_modules.
buildPhase = [[go build -trimpath -ldflags="-s -w -X main.zbVersion=$version" -tags=disable_grpc_modules zb.256lights.llc/pkg/cmd/zb]];
installPhase = [=[
mkdir -p "$out/bin"
name="zb$(go env GOEXE)"
cp --reflink=auto "$name" "$out/bin/$name"
if [[ "$GOOS" = linux ]]; then
mkdir -p "$out/lib/systemd/system"
cp systemd/zb-serve.socket "$out/lib/systemd/system/zb-serve.socket"
sed \
-e "s:@zb@:$out/bin/zb:g" \
-e "s:@sh@:$busybox/bin/sh:g" \
systemd/zb-serve.service.in > "$out/lib/systemd/system/zb-serve.service"
elif [[ "$GOOS" = darwin ]]; then
mkdir -p "$out/Library/LaunchDaemons"
sed \
-e "s:@zb@:$out/bin/zb:g" \
launchd/dev.zb-build.serve.plist.in > "$out/Library/LaunchDaemons/dev.zb-build.serve.plist"
fi
]=];
}
end
local supportedTargetSystems <const> = {
"x86_64-unknown-linux",
"x86_64-pc-windows",
"aarch64-apple-macos",
}
local mygo = setmetatable({}, {
__outputs = function(_, system)
return go[system]["1.26"]
end;
})
module.zb = module.new {
makeDerivation = stdenv.makeDerivationNoCC;
go = mygo;
}
for _, targetSystem in ipairs(supportedTargetSystems) do
module["zb-"..targetSystem] = module.new {
makeDerivation = stdenv.makeDerivationNoCC;
go = mygo;
targetSystem = targetSystem;
}
end
return setmetatable(module, { __index = tables.lazyModule(getters) })