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/ diff --git a/Dockerfile b/Dockerfile index ef3b3401..7d55d6d1 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 diff --git a/Makefile b/Makefile index 0ca915b9..ba2f29ea 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" @@ -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 -ICONV := $(BC)vendor/libiconv +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" && \ @@ -169,14 +168,22 @@ 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 - +endif + +install-libiconv: download-libiconv clean-libiconv @cd vendor/libiconv/libiconv-1.17 && \ - ./configure --prefix=$(BC)vendor/libiconv --enable-static && \ + ./configure --prefix=$(ICONV) --enable-static && \ make && make install + +clean-libiconv: +ifneq ("$(wildcard vendor/libiconv/libiconv-1.17/Makefile)","") + @cd vendor/libiconv/libiconv-1.17 && \ + make clean endif install-zig-js-runtime-dev: @@ -188,24 +195,28 @@ 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..86ad4ef9 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,20 +165,40 @@ 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, }); + + 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", moduleMimalloc(b)); + mod.addImport("mimalloc", (try moduleMimalloc(b, target))); // 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", @@ -186,20 +207,35 @@ fn moduleNetSurf(b: *std.Build) *std.Build.Module { "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")); } 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; }