build: support multi os/arch conf for mimalloc

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-10-28 16:06:41 +01:00
parent f01558251c
commit d1951b286c
2 changed files with 53 additions and 38 deletions

View File

@@ -4,6 +4,25 @@
ZIG := zig ZIG := zig
BC := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) BC := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
# OS and ARCH
kernel = $(shell uname -ms)
ifeq ($(kernel), Darwin arm64)
OS := macos
ARCH := aarch64
else ifeq ($(kernel), Linux aarch64)
OS := linux
ARCH := aarch64
else ifeq ($(kernel), Linux arm64)
OS := linux
ARCH := aarch64
else ifeq ($(kernel), Linux x86_64)
OS := linux
ARCH := x86_64
else
$(error "Unhandled kernel: $(kernel)")
endif
# Infos # Infos
# ----- # -----
.PHONY: help .PHONY: help
@@ -26,30 +45,11 @@ help:
.PHONY: build build-dev run run-release shell test bench download-zig wpt .PHONY: build build-dev run run-release shell test bench download-zig wpt
zig_version = $(shell grep 'recommended_zig_version = "' "vendor/zig-js-runtime/build.zig" | cut -d'"' -f2) zig_version = $(shell grep 'recommended_zig_version = "' "vendor/zig-js-runtime/build.zig" | cut -d'"' -f2)
kernel = $(shell uname -ms)
## Download the zig recommended version ## Download the zig recommended version
download-zig: download-zig:
ifeq ($(kernel), Darwin x86_64) $(eval url = "https://ziglang.org/builds/zig-$(OS)-$(ARCH)-$(zig_version).tar.xz")
$(eval target="macos") $(eval dest = "/tmp/zig-$(OS)-$(ARCH)-$(zig_version).tar.xz")
$(eval arch="x86_64")
else ifeq ($(kernel), Darwin arm64)
$(eval target="macos")
$(eval arch="aarch64")
else ifeq ($(kernel), Linux aarch64)
$(eval target="linux")
$(eval arch="aarch64")
else ifeq ($(kernel), Linux arm64)
$(eval target="linux")
$(eval arch="aarch64")
else ifeq ($(kernel), Linux x86_64)
$(eval target="linux")
$(eval arch="x86_64")
else
$(error "Unhandled kernel: $(kernel)")
endif
$(eval url = "https://ziglang.org/builds/zig-$(target)-$(arch)-$(zig_version).tar.xz")
$(eval dest = "/tmp/zig-$(target)-$(arch)-$(zig_version).tar.xz")
@printf "\e[36mDownload zig version $(zig_version)...\e[0m\n" @printf "\e[36mDownload zig version $(zig_version)...\e[0m\n"
@curl -o "$(dest)" -L "$(url)" || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;) @curl -o "$(dest)" -L "$(url)" || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
@printf "\e[33mDownloaded $(dest)\e[0m\n" @printf "\e[33mDownloaded $(dest)\e[0m\n"
@@ -188,24 +188,27 @@ install-zig-js-runtime:
make install make install
.PHONY: _build_mimalloc .PHONY: _build_mimalloc
_build_mimalloc: MIMALLOC := $(BC)vendor/mimalloc/out/$(OS)-$(ARCH)
@cd vendor/mimalloc && \ _build_mimalloc: clean-mimalloc
mkdir -p out/include && \ @mkdir -p $(MIMALLOC)/build && \
cp include/mimalloc.h out/include/ && \ cd $(MIMALLOC)/build && \
cd out && \ cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) ../../.. && \
cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) .. && \ make && \
make mkdir -p $(MIMALLOC)/lib
install-mimalloc-dev: _build_mimalloc install-mimalloc-dev: _build_mimalloc
install-mimalloc-dev: OPTS=-DCMAKE_BUILD_TYPE=Debug install-mimalloc-dev: OPTS=-DCMAKE_BUILD_TYPE=Debug
install-mimalloc-dev: install-mimalloc-dev:
@cd vendor/mimalloc/out && \ @cd $(MIMALLOC) && \
mv libmimalloc-debug.a libmimalloc.a mv build/libmimalloc-debug.a lib/libmimalloc.a
install-mimalloc: _build_mimalloc install-mimalloc: _build_mimalloc
install-mimalloc:
@cd $(MIMALLOC) && \
mv build/libmimalloc.a lib/libmimalloc.a
clean-mimalloc: clean-mimalloc:
@rm -fr vendor/mimalloc/lib/* @rm -Rf $(MIMALLOC)/build
## Init and update git submodule ## Init and update git submodule
install-submodule: install-submodule:

View File

@@ -146,15 +146,16 @@ fn common(
step: *std.Build.Step.Compile, step: *std.Build.Step.Compile,
options: jsruntime.Options, options: jsruntime.Options,
) !void { ) !void {
const target = step.root_module.resolved_target.?;
const jsruntimemod = try jsruntime_pkgs.module( const jsruntimemod = try jsruntime_pkgs.module(
b, b,
options, options,
step.root_module.optimize.?, step.root_module.optimize.?,
step.root_module.resolved_target.?, target,
); );
step.root_module.addImport("jsruntime", jsruntimemod); step.root_module.addImport("jsruntime", jsruntimemod);
const netsurf = moduleNetSurf(b); const netsurf = try moduleNetSurf(b, target);
netsurf.addImport("jsruntime", jsruntimemod); netsurf.addImport("jsruntime", jsruntimemod);
step.root_module.addImport("netsurf", netsurf); step.root_module.addImport("netsurf", netsurf);
@@ -164,16 +165,17 @@ fn common(
step.root_module.addImport("tls", tlsmod); step.root_module.addImport("tls", tlsmod);
} }
fn moduleNetSurf(b: *std.Build) *std.Build.Module { fn moduleNetSurf(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Module {
const mod = b.addModule("netsurf", .{ const mod = b.addModule("netsurf", .{
.root_source_file = b.path("src/netsurf/netsurf.zig"), .root_source_file = b.path("src/netsurf/netsurf.zig"),
.target = target,
}); });
// iconv // iconv
mod.addObjectFile(b.path("vendor/libiconv/lib/libiconv.a")); mod.addObjectFile(b.path("vendor/libiconv/lib/libiconv.a"));
mod.addIncludePath(b.path("vendor/libiconv/include")); mod.addIncludePath(b.path("vendor/libiconv/include"));
// mimalloc // mimalloc
mod.addImport("mimalloc", moduleMimalloc(b)); mod.addImport("mimalloc", (try moduleMimalloc(b, target)));
// netsurf libs // netsurf libs
const ns = "vendor/netsurf"; const ns = "vendor/netsurf";
@@ -193,13 +195,23 @@ fn moduleNetSurf(b: *std.Build) *std.Build.Module {
return mod; return mod;
} }
fn moduleMimalloc(b: *std.Build) *std.Build.Module { fn moduleMimalloc(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Module {
const mod = b.addModule("mimalloc", .{ const mod = b.addModule("mimalloc", .{
.root_source_file = b.path("src/mimalloc/mimalloc.zig"), .root_source_file = b.path("src/mimalloc/mimalloc.zig"),
.target = target,
}); });
mod.addObjectFile(b.path("vendor/mimalloc/out/libmimalloc.a")); const os = target.result.os.tag;
mod.addIncludePath(b.path("vendor/mimalloc/out/include")); const arch = target.result.cpu.arch;
const mimalloc = "vendor/mimalloc";
const lib_path = try std.fmt.allocPrint(
mod.owner.allocator,
mimalloc ++ "/out/{s}-{s}/lib/libmimalloc.a",
.{ @tagName(os), @tagName(arch) },
);
mod.addObjectFile(b.path(lib_path));
mod.addIncludePath(b.path(mimalloc ++ "/include"));
return mod; return mod;
} }