From d1951b286c66b7240fc0e6693e198cfe747e265c Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Mon, 28 Oct 2024 16:06:41 +0100 Subject: [PATCH 1/6] build: support multi os/arch conf for mimalloc Signed-off-by: Francis Bouvier --- Makefile | 65 +++++++++++++++++++++++++++++-------------------------- build.zig | 26 ++++++++++++++++------ 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 0ca915b9..a88cff4e 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,25 @@ ZIG := zig 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 # ----- .PHONY: help @@ -26,30 +45,11 @@ help: .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) -kernel = $(shell uname -ms) ## Download the zig recommended version download-zig: -ifeq ($(kernel), Darwin x86_64) - $(eval target="macos") - $(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") + $(eval url = "https://ziglang.org/builds/zig-$(OS)-$(ARCH)-$(zig_version).tar.xz") + $(eval dest = "/tmp/zig-$(OS)-$(ARCH)-$(zig_version).tar.xz") @printf "\e[36mDownload zig version $(zig_version)...\e[0m\n" @curl -o "$(dest)" -L "$(url)" || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;) @printf "\e[33mDownloaded $(dest)\e[0m\n" @@ -188,24 +188,27 @@ install-zig-js-runtime: make install .PHONY: _build_mimalloc -_build_mimalloc: - @cd vendor/mimalloc && \ - mkdir -p out/include && \ - cp include/mimalloc.h out/include/ && \ - cd out && \ - cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) .. && \ - make +MIMALLOC := $(BC)vendor/mimalloc/out/$(OS)-$(ARCH) +_build_mimalloc: clean-mimalloc + @mkdir -p $(MIMALLOC)/build && \ + cd $(MIMALLOC)/build && \ + cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) ../../.. && \ + make && \ + mkdir -p $(MIMALLOC)/lib install-mimalloc-dev: _build_mimalloc install-mimalloc-dev: OPTS=-DCMAKE_BUILD_TYPE=Debug install-mimalloc-dev: - @cd vendor/mimalloc/out && \ - mv libmimalloc-debug.a libmimalloc.a + @cd $(MIMALLOC) && \ + mv build/libmimalloc-debug.a lib/libmimalloc.a install-mimalloc: _build_mimalloc +install-mimalloc: + @cd $(MIMALLOC) && \ + mv build/libmimalloc.a lib/libmimalloc.a clean-mimalloc: - @rm -fr vendor/mimalloc/lib/* + @rm -Rf $(MIMALLOC)/build ## Init and update git submodule install-submodule: diff --git a/build.zig b/build.zig index 364c24cf..9b356412 100644 --- a/build.zig +++ b/build.zig @@ -146,15 +146,16 @@ fn common( step: *std.Build.Step.Compile, options: jsruntime.Options, ) !void { + const target = step.root_module.resolved_target.?; const jsruntimemod = try jsruntime_pkgs.module( b, options, step.root_module.optimize.?, - step.root_module.resolved_target.?, + target, ); step.root_module.addImport("jsruntime", jsruntimemod); - const netsurf = moduleNetSurf(b); + const netsurf = try moduleNetSurf(b, target); netsurf.addImport("jsruntime", jsruntimemod); step.root_module.addImport("netsurf", netsurf); @@ -164,16 +165,17 @@ fn common( 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", .{ .root_source_file = b.path("src/netsurf/netsurf.zig"), + .target = target, }); // iconv mod.addObjectFile(b.path("vendor/libiconv/lib/libiconv.a")); mod.addIncludePath(b.path("vendor/libiconv/include")); // mimalloc - mod.addImport("mimalloc", moduleMimalloc(b)); + mod.addImport("mimalloc", (try moduleMimalloc(b, target))); // netsurf libs const ns = "vendor/netsurf"; @@ -193,13 +195,23 @@ fn moduleNetSurf(b: *std.Build) *std.Build.Module { 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", .{ .root_source_file = b.path("src/mimalloc/mimalloc.zig"), + .target = target, }); - mod.addObjectFile(b.path("vendor/mimalloc/out/libmimalloc.a")); - mod.addIncludePath(b.path("vendor/mimalloc/out/include")); + const os = target.result.os.tag; + 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; } From be5d7022cc810dcc31f2ef14b0b1a02632f4c386 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Mon, 28 Oct 2024 21:08:46 +0100 Subject: [PATCH 2/6] build: support multi os/arch conf for libiconv Signed-off-by: Francis Bouvier --- Makefile | 19 +++++++++++++------ build.zig | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index a88cff4e..f84c6e83 100644 --- a/Makefile +++ b/Makefile @@ -112,7 +112,7 @@ install-netsurf: _install-netsurf install-netsurf: OPTCFLAGS := -DNDEBUG BC_NS := $(BC)vendor/netsurf -ICONV := $(BC)vendor/libiconv +ICONV := $(BC)vendor/libiconv/out/$(OS)-$(ARCH) # TODO: add Linux iconv path (I guess it depends on the distro) # TODO: this way of linking libiconv is not ideal. We should have a more generic way # and stick to a specif version. Maybe build from source. Anyway not now. @@ -169,16 +169,23 @@ test-netsurf: cd vendor/netsurf/libdom && \ BUILDDIR=$(BC_NS)/build/libdom make test -install-libiconv: -ifeq ("$(wildcard vendor/libiconv/lib/libiconv.a)","") +download-libiconv: +ifeq ("$(wildcard vendor/libiconv/libiconv-1.17)","") @mkdir -p vendor/libiconv @cd vendor/libiconv && \ curl https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz | tar -xvzf - - @cd vendor/libiconv/libiconv-1.17 && \ - ./configure --prefix=$(BC)vendor/libiconv --enable-static && \ - make && make install endif +install-libiconv: download-libiconv clean-libiconv + @cd vendor/libiconv/libiconv-1.17 && \ + ./configure --prefix=$(ICONV) --enable-static && \ + make && make install + +clean-libiconv: + @cd vendor/libiconv/libiconv-1.17 && \ + make clean > /dev/null && cd .. && \ + rm -Rf lib && rm -Rf share && rm -Rf bin && rm -Rf include + install-zig-js-runtime-dev: @cd vendor/zig-js-runtime && \ make install-dev diff --git a/build.zig b/build.zig index 9b356412..7b55b773 100644 --- a/build.zig +++ b/build.zig @@ -170,9 +170,23 @@ fn moduleNetSurf(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Mo .root_source_file = b.path("src/netsurf/netsurf.zig"), .target = target, }); + + const os = target.result.os.tag; + const arch = target.result.cpu.arch; + // iconv - mod.addObjectFile(b.path("vendor/libiconv/lib/libiconv.a")); - mod.addIncludePath(b.path("vendor/libiconv/include")); + const libiconv_lib_path = try std.fmt.allocPrint( + mod.owner.allocator, + "vendor/libiconv/out/{s}-{s}/lib/libiconv.a", + .{ @tagName(os), @tagName(arch) }, + ); + const libiconv_include_path = try std.fmt.allocPrint( + mod.owner.allocator, + "vendor/libiconv/out/{s}-{s}/lib/libiconv.a", + .{ @tagName(os), @tagName(arch) }, + ); + mod.addObjectFile(b.path(libiconv_lib_path)); + mod.addIncludePath(b.path(libiconv_include_path)); // mimalloc mod.addImport("mimalloc", (try moduleMimalloc(b, target))); From b9e2be2052a0780d227c5a8454017de9f7713958 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Tue, 29 Oct 2024 20:06:29 +0100 Subject: [PATCH 3/6] build: support multi os/arch conf for netsurf Signed-off-by: Francis Bouvier --- Makefile | 18 +++++++++--------- build.zig | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index f84c6e83..8531cc52 100644 --- a/Makefile +++ b/Makefile @@ -100,10 +100,10 @@ test: .PHONY: install-dev install ## Install and build dependencies for release -install: install-submodule install-zig-js-runtime install-netsurf install-mimalloc +install: install-submodule install-zig-js-runtime install-libiconv install-netsurf install-mimalloc ## Install and build dependencies for dev -install-dev: install-submodule install-zig-js-runtime-dev install-netsurf-dev install-mimalloc-dev +install-dev: install-submodule install-zig-js-runtime-dev install-libiconv install-netsurf-dev install-mimalloc-dev install-netsurf-dev: _install-netsurf install-netsurf-dev: OPTCFLAGS := -O0 -g -DNDEBUG @@ -111,14 +111,16 @@ install-netsurf-dev: OPTCFLAGS := -O0 -g -DNDEBUG install-netsurf: _install-netsurf install-netsurf: OPTCFLAGS := -DNDEBUG -BC_NS := $(BC)vendor/netsurf +BC_NS := $(BC)vendor/netsurf/out/$(OS)-$(ARCH) ICONV := $(BC)vendor/libiconv/out/$(OS)-$(ARCH) # TODO: add Linux iconv path (I guess it depends on the distro) # TODO: this way of linking libiconv is not ideal. We should have a more generic way # and stick to a specif version. Maybe build from source. Anyway not now. -_install-netsurf: install-libiconv +_install-netsurf: clean-netsurf @printf "\e[36mInstalling NetSurf...\e[0m\n" && \ - ls $(ICONV) 1> /dev/null || (printf "\e[33mERROR: you need to install libiconv in your system (on MacOS on with Homebrew)\e[0m\n"; exit 1;) && \ + ls $(ICONV)/lib/libiconv.a 1> /dev/null || (printf "\e[33mERROR: you need to execute 'make install-libiconv'\e[0m\n"; exit 1;) && \ + mkdir -p $(BC_NS) && \ + cp -R vendor/netsurf/share $(BC_NS) && \ export PREFIX=$(BC_NS) && \ export OPTLDFLAGS="-L$(ICONV)/lib" && \ export OPTCFLAGS="$(OPTCFLAGS) -I$(ICONV)/include" && \ @@ -156,10 +158,7 @@ _install-netsurf: install-libiconv clean-netsurf: @printf "\e[36mCleaning NetSurf build...\e[0m\n" && \ - cd vendor/netsurf && \ - rm -R build && \ - rm -R lib && \ - rm -R include + rm -Rf $(BC_NS) test-netsurf: @printf "\e[36mTesting NetSurf...\e[0m\n" && \ @@ -195,6 +194,7 @@ install-zig-js-runtime: make install .PHONY: _build_mimalloc + MIMALLOC := $(BC)vendor/mimalloc/out/$(OS)-$(ARCH) _build_mimalloc: clean-mimalloc @mkdir -p $(MIMALLOC)/build && \ diff --git a/build.zig b/build.zig index 7b55b773..86ad4ef9 100644 --- a/build.zig +++ b/build.zig @@ -193,7 +193,12 @@ fn moduleNetSurf(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Mo // netsurf libs const ns = "vendor/netsurf"; - mod.addIncludePath(b.path(ns ++ "/include")); + const ns_include_path = try std.fmt.allocPrint( + mod.owner.allocator, + ns ++ "/out/{s}-{s}/include", + .{ @tagName(os), @tagName(arch) }, + ); + mod.addIncludePath(b.path(ns_include_path)); const libs: [4][]const u8 = .{ "libdom", @@ -202,7 +207,12 @@ fn moduleNetSurf(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Mo "libwapcaplet", }; inline for (libs) |lib| { - mod.addObjectFile(b.path(ns ++ "/lib/" ++ lib ++ ".a")); + const ns_lib_path = try std.fmt.allocPrint( + mod.owner.allocator, + ns ++ "/out/{s}-{s}/lib/" ++ lib ++ ".a", + .{ @tagName(os), @tagName(arch) }, + ); + mod.addObjectFile(b.path(ns_lib_path)); mod.addIncludePath(b.path(ns ++ "/" ++ lib ++ "/src")); } From 2a2486cbe07f7da2546b72eb9ad1082f65eacfe6 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Wed, 30 Oct 2024 13:41:34 +0100 Subject: [PATCH 4/6] build: fix clean-libiconv Signed-off-by: Francis Bouvier --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 8531cc52..ba2f29ea 100644 --- a/Makefile +++ b/Makefile @@ -181,9 +181,10 @@ install-libiconv: download-libiconv clean-libiconv make && make install clean-libiconv: +ifneq ("$(wildcard vendor/libiconv/libiconv-1.17/Makefile)","") @cd vendor/libiconv/libiconv-1.17 && \ - make clean > /dev/null && cd .. && \ - rm -Rf lib && rm -Rf share && rm -Rf bin && rm -Rf include + make clean +endif install-zig-js-runtime-dev: @cd vendor/zig-js-runtime && \ From b9bae3f66da159290c85aaf1a4941011ba8714e3 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Wed, 30 Oct 2024 13:41:48 +0100 Subject: [PATCH 5/6] build: update gitignore Signed-off-by: Francis Bouvier --- .gitignore | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 91d299d9..49ae9a0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ zig-cache /.zig-cache/ zig-out -/vendor/netsurf/build/ -/vendor/netsurf/lib/ -/vendor/netsurf/include/ +/vendor/netsurf/out /vendor/libiconv/ From 3ecfa6aca8d4e368038831f629596461d39da3da Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Wed, 6 Nov 2024 18:10:08 +0100 Subject: [PATCH 6/6] Dockerfile: add install-libiconv Signed-off-by: Francis Bouvier --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index dc70dbe2..3bbc9349 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,7 +55,8 @@ RUN cd vendor/zig-js-runtime && \ git submodule init && \ git submodule update --recursive -RUN make install-netsurf && \ +RUN make install-libiconv && \ + make install-netsurf && \ make install-mimalloc # download and install v8