Skip to content

Commit d1951b2

Browse files
build: support multi os/arch conf for mimalloc
Signed-off-by: Francis Bouvier <[email protected]>
1 parent f015582 commit d1951b2

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed

Makefile

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
ZIG := zig
55
BC := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
66

7+
# OS and ARCH
8+
kernel = $(shell uname -ms)
9+
ifeq ($(kernel), Darwin arm64)
10+
OS := macos
11+
ARCH := aarch64
12+
else ifeq ($(kernel), Linux aarch64)
13+
OS := linux
14+
ARCH := aarch64
15+
else ifeq ($(kernel), Linux arm64)
16+
OS := linux
17+
ARCH := aarch64
18+
else ifeq ($(kernel), Linux x86_64)
19+
OS := linux
20+
ARCH := x86_64
21+
else
22+
$(error "Unhandled kernel: $(kernel)")
23+
endif
24+
25+
726
# Infos
827
# -----
928
.PHONY: help
@@ -26,30 +45,11 @@ help:
2645
.PHONY: build build-dev run run-release shell test bench download-zig wpt
2746

2847
zig_version = $(shell grep 'recommended_zig_version = "' "vendor/zig-js-runtime/build.zig" | cut -d'"' -f2)
29-
kernel = $(shell uname -ms)
3048

3149
## Download the zig recommended version
3250
download-zig:
33-
ifeq ($(kernel), Darwin x86_64)
34-
$(eval target="macos")
35-
$(eval arch="x86_64")
36-
else ifeq ($(kernel), Darwin arm64)
37-
$(eval target="macos")
38-
$(eval arch="aarch64")
39-
else ifeq ($(kernel), Linux aarch64)
40-
$(eval target="linux")
41-
$(eval arch="aarch64")
42-
else ifeq ($(kernel), Linux arm64)
43-
$(eval target="linux")
44-
$(eval arch="aarch64")
45-
else ifeq ($(kernel), Linux x86_64)
46-
$(eval target="linux")
47-
$(eval arch="x86_64")
48-
else
49-
$(error "Unhandled kernel: $(kernel)")
50-
endif
51-
$(eval url = "https://ziglang.org/builds/zig-$(target)-$(arch)-$(zig_version).tar.xz")
52-
$(eval dest = "/tmp/zig-$(target)-$(arch)-$(zig_version).tar.xz")
51+
$(eval url = "https://ziglang.org/builds/zig-$(OS)-$(ARCH)-$(zig_version).tar.xz")
52+
$(eval dest = "/tmp/zig-$(OS)-$(ARCH)-$(zig_version).tar.xz")
5353
@printf "\e[36mDownload zig version $(zig_version)...\e[0m\n"
5454
@curl -o "$(dest)" -L "$(url)" || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
5555
@printf "\e[33mDownloaded $(dest)\e[0m\n"
@@ -188,24 +188,27 @@ install-zig-js-runtime:
188188
make install
189189

190190
.PHONY: _build_mimalloc
191-
_build_mimalloc:
192-
@cd vendor/mimalloc && \
193-
mkdir -p out/include && \
194-
cp include/mimalloc.h out/include/ && \
195-
cd out && \
196-
cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) .. && \
197-
make
191+
MIMALLOC := $(BC)vendor/mimalloc/out/$(OS)-$(ARCH)
192+
_build_mimalloc: clean-mimalloc
193+
@mkdir -p $(MIMALLOC)/build && \
194+
cd $(MIMALLOC)/build && \
195+
cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) ../../.. && \
196+
make && \
197+
mkdir -p $(MIMALLOC)/lib
198198

199199
install-mimalloc-dev: _build_mimalloc
200200
install-mimalloc-dev: OPTS=-DCMAKE_BUILD_TYPE=Debug
201201
install-mimalloc-dev:
202-
@cd vendor/mimalloc/out && \
203-
mv libmimalloc-debug.a libmimalloc.a
202+
@cd $(MIMALLOC) && \
203+
mv build/libmimalloc-debug.a lib/libmimalloc.a
204204

205205
install-mimalloc: _build_mimalloc
206+
install-mimalloc:
207+
@cd $(MIMALLOC) && \
208+
mv build/libmimalloc.a lib/libmimalloc.a
206209

207210
clean-mimalloc:
208-
@rm -fr vendor/mimalloc/lib/*
211+
@rm -Rf $(MIMALLOC)/build
209212

210213
## Init and update git submodule
211214
install-submodule:

build.zig

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,16 @@ fn common(
146146
step: *std.Build.Step.Compile,
147147
options: jsruntime.Options,
148148
) !void {
149+
const target = step.root_module.resolved_target.?;
149150
const jsruntimemod = try jsruntime_pkgs.module(
150151
b,
151152
options,
152153
step.root_module.optimize.?,
153-
step.root_module.resolved_target.?,
154+
target,
154155
);
155156
step.root_module.addImport("jsruntime", jsruntimemod);
156157

157-
const netsurf = moduleNetSurf(b);
158+
const netsurf = try moduleNetSurf(b, target);
158159
netsurf.addImport("jsruntime", jsruntimemod);
159160
step.root_module.addImport("netsurf", netsurf);
160161

@@ -164,16 +165,17 @@ fn common(
164165
step.root_module.addImport("tls", tlsmod);
165166
}
166167

167-
fn moduleNetSurf(b: *std.Build) *std.Build.Module {
168+
fn moduleNetSurf(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Module {
168169
const mod = b.addModule("netsurf", .{
169170
.root_source_file = b.path("src/netsurf/netsurf.zig"),
171+
.target = target,
170172
});
171173
// iconv
172174
mod.addObjectFile(b.path("vendor/libiconv/lib/libiconv.a"));
173175
mod.addIncludePath(b.path("vendor/libiconv/include"));
174176

175177
// mimalloc
176-
mod.addImport("mimalloc", moduleMimalloc(b));
178+
mod.addImport("mimalloc", (try moduleMimalloc(b, target)));
177179

178180
// netsurf libs
179181
const ns = "vendor/netsurf";
@@ -193,13 +195,23 @@ fn moduleNetSurf(b: *std.Build) *std.Build.Module {
193195
return mod;
194196
}
195197

196-
fn moduleMimalloc(b: *std.Build) *std.Build.Module {
198+
fn moduleMimalloc(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Module {
197199
const mod = b.addModule("mimalloc", .{
198200
.root_source_file = b.path("src/mimalloc/mimalloc.zig"),
201+
.target = target,
199202
});
200203

201-
mod.addObjectFile(b.path("vendor/mimalloc/out/libmimalloc.a"));
202-
mod.addIncludePath(b.path("vendor/mimalloc/out/include"));
204+
const os = target.result.os.tag;
205+
const arch = target.result.cpu.arch;
206+
207+
const mimalloc = "vendor/mimalloc";
208+
const lib_path = try std.fmt.allocPrint(
209+
mod.owner.allocator,
210+
mimalloc ++ "/out/{s}-{s}/lib/libmimalloc.a",
211+
.{ @tagName(os), @tagName(arch) },
212+
);
213+
mod.addObjectFile(b.path(lib_path));
214+
mod.addIncludePath(b.path(mimalloc ++ "/include"));
203215

204216
return mod;
205217
}

0 commit comments

Comments
 (0)