Skip to content

Commit 3af34d1

Browse files
Merge pull request #291 from lightpanda-io/multi_build
Multi build
2 parents eed7b71 + 3ecfa6a commit 3af34d1

File tree

4 files changed

+105
-59
lines changed

4 files changed

+105
-59
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
zig-cache
22
/.zig-cache/
33
zig-out
4-
/vendor/netsurf/build/
5-
/vendor/netsurf/lib/
6-
/vendor/netsurf/include/
4+
/vendor/netsurf/out
75
/vendor/libiconv/

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ RUN cd vendor/zig-js-runtime && \
5555
git submodule init && \
5656
git submodule update --recursive
5757

58-
RUN make install-netsurf && \
58+
RUN make install-libiconv && \
59+
make install-netsurf && \
5960
make install-mimalloc
6061

6162
# download and install v8

Makefile

Lines changed: 55 additions & 44 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"
@@ -100,25 +100,27 @@ test:
100100
.PHONY: install-dev install
101101

102102
## Install and build dependencies for release
103-
install: install-submodule install-zig-js-runtime install-netsurf install-mimalloc
103+
install: install-submodule install-zig-js-runtime install-libiconv install-netsurf install-mimalloc
104104

105105
## Install and build dependencies for dev
106-
install-dev: install-submodule install-zig-js-runtime-dev install-netsurf-dev install-mimalloc-dev
106+
install-dev: install-submodule install-zig-js-runtime-dev install-libiconv install-netsurf-dev install-mimalloc-dev
107107

108108
install-netsurf-dev: _install-netsurf
109109
install-netsurf-dev: OPTCFLAGS := -O0 -g -DNDEBUG
110110

111111
install-netsurf: _install-netsurf
112112
install-netsurf: OPTCFLAGS := -DNDEBUG
113113

114-
BC_NS := $(BC)vendor/netsurf
115-
ICONV := $(BC)vendor/libiconv
114+
BC_NS := $(BC)vendor/netsurf/out/$(OS)-$(ARCH)
115+
ICONV := $(BC)vendor/libiconv/out/$(OS)-$(ARCH)
116116
# TODO: add Linux iconv path (I guess it depends on the distro)
117117
# TODO: this way of linking libiconv is not ideal. We should have a more generic way
118118
# and stick to a specif version. Maybe build from source. Anyway not now.
119-
_install-netsurf: install-libiconv
119+
_install-netsurf: clean-netsurf
120120
@printf "\e[36mInstalling NetSurf...\e[0m\n" && \
121-
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;) && \
121+
ls $(ICONV)/lib/libiconv.a 1> /dev/null || (printf "\e[33mERROR: you need to execute 'make install-libiconv'\e[0m\n"; exit 1;) && \
122+
mkdir -p $(BC_NS) && \
123+
cp -R vendor/netsurf/share $(BC_NS) && \
122124
export PREFIX=$(BC_NS) && \
123125
export OPTLDFLAGS="-L$(ICONV)/lib" && \
124126
export OPTCFLAGS="$(OPTCFLAGS) -I$(ICONV)/include" && \
@@ -156,10 +158,7 @@ _install-netsurf: install-libiconv
156158

157159
clean-netsurf:
158160
@printf "\e[36mCleaning NetSurf build...\e[0m\n" && \
159-
cd vendor/netsurf && \
160-
rm -R build && \
161-
rm -R lib && \
162-
rm -R include
161+
rm -Rf $(BC_NS)
163162

164163
test-netsurf:
165164
@printf "\e[36mTesting NetSurf...\e[0m\n" && \
@@ -169,14 +168,22 @@ test-netsurf:
169168
cd vendor/netsurf/libdom && \
170169
BUILDDIR=$(BC_NS)/build/libdom make test
171170

172-
install-libiconv:
173-
ifeq ("$(wildcard vendor/libiconv/lib/libiconv.a)","")
171+
download-libiconv:
172+
ifeq ("$(wildcard vendor/libiconv/libiconv-1.17)","")
174173
@mkdir -p vendor/libiconv
175174
@cd vendor/libiconv && \
176175
curl https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz | tar -xvzf -
176+
endif
177+
178+
install-libiconv: download-libiconv clean-libiconv
177179
@cd vendor/libiconv/libiconv-1.17 && \
178-
./configure --prefix=$(BC)vendor/libiconv --enable-static && \
180+
./configure --prefix=$(ICONV) --enable-static && \
179181
make && make install
182+
183+
clean-libiconv:
184+
ifneq ("$(wildcard vendor/libiconv/libiconv-1.17/Makefile)","")
185+
@cd vendor/libiconv/libiconv-1.17 && \
186+
make clean
180187
endif
181188

182189
install-zig-js-runtime-dev:
@@ -188,24 +195,28 @@ install-zig-js-runtime:
188195
make install
189196

190197
.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
198+
199+
MIMALLOC := $(BC)vendor/mimalloc/out/$(OS)-$(ARCH)
200+
_build_mimalloc: clean-mimalloc
201+
@mkdir -p $(MIMALLOC)/build && \
202+
cd $(MIMALLOC)/build && \
203+
cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) ../../.. && \
204+
make && \
205+
mkdir -p $(MIMALLOC)/lib
198206

199207
install-mimalloc-dev: _build_mimalloc
200208
install-mimalloc-dev: OPTS=-DCMAKE_BUILD_TYPE=Debug
201209
install-mimalloc-dev:
202-
@cd vendor/mimalloc/out && \
203-
mv libmimalloc-debug.a libmimalloc.a
210+
@cd $(MIMALLOC) && \
211+
mv build/libmimalloc-debug.a lib/libmimalloc.a
204212

205213
install-mimalloc: _build_mimalloc
214+
install-mimalloc:
215+
@cd $(MIMALLOC) && \
216+
mv build/libmimalloc.a lib/libmimalloc.a
206217

207218
clean-mimalloc:
208-
@rm -fr vendor/mimalloc/lib/*
219+
@rm -Rf $(MIMALLOC)/build
209220

210221
## Init and update git submodule
211222
install-submodule:

build.zig

Lines changed: 47 additions & 11 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,20 +165,40 @@ 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
});
173+
174+
const os = target.result.os.tag;
175+
const arch = target.result.cpu.arch;
176+
171177
// iconv
172-
mod.addObjectFile(b.path("vendor/libiconv/lib/libiconv.a"));
173-
mod.addIncludePath(b.path("vendor/libiconv/include"));
178+
const libiconv_lib_path = try std.fmt.allocPrint(
179+
mod.owner.allocator,
180+
"vendor/libiconv/out/{s}-{s}/lib/libiconv.a",
181+
.{ @tagName(os), @tagName(arch) },
182+
);
183+
const libiconv_include_path = try std.fmt.allocPrint(
184+
mod.owner.allocator,
185+
"vendor/libiconv/out/{s}-{s}/lib/libiconv.a",
186+
.{ @tagName(os), @tagName(arch) },
187+
);
188+
mod.addObjectFile(b.path(libiconv_lib_path));
189+
mod.addIncludePath(b.path(libiconv_include_path));
174190

175191
// mimalloc
176-
mod.addImport("mimalloc", moduleMimalloc(b));
192+
mod.addImport("mimalloc", (try moduleMimalloc(b, target)));
177193

178194
// netsurf libs
179195
const ns = "vendor/netsurf";
180-
mod.addIncludePath(b.path(ns ++ "/include"));
196+
const ns_include_path = try std.fmt.allocPrint(
197+
mod.owner.allocator,
198+
ns ++ "/out/{s}-{s}/include",
199+
.{ @tagName(os), @tagName(arch) },
200+
);
201+
mod.addIncludePath(b.path(ns_include_path));
181202

182203
const libs: [4][]const u8 = .{
183204
"libdom",
@@ -186,20 +207,35 @@ fn moduleNetSurf(b: *std.Build) *std.Build.Module {
186207
"libwapcaplet",
187208
};
188209
inline for (libs) |lib| {
189-
mod.addObjectFile(b.path(ns ++ "/lib/" ++ lib ++ ".a"));
210+
const ns_lib_path = try std.fmt.allocPrint(
211+
mod.owner.allocator,
212+
ns ++ "/out/{s}-{s}/lib/" ++ lib ++ ".a",
213+
.{ @tagName(os), @tagName(arch) },
214+
);
215+
mod.addObjectFile(b.path(ns_lib_path));
190216
mod.addIncludePath(b.path(ns ++ "/" ++ lib ++ "/src"));
191217
}
192218

193219
return mod;
194220
}
195221

196-
fn moduleMimalloc(b: *std.Build) *std.Build.Module {
222+
fn moduleMimalloc(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Module {
197223
const mod = b.addModule("mimalloc", .{
198224
.root_source_file = b.path("src/mimalloc/mimalloc.zig"),
225+
.target = target,
199226
});
200227

201-
mod.addObjectFile(b.path("vendor/mimalloc/out/libmimalloc.a"));
202-
mod.addIncludePath(b.path("vendor/mimalloc/out/include"));
228+
const os = target.result.os.tag;
229+
const arch = target.result.cpu.arch;
230+
231+
const mimalloc = "vendor/mimalloc";
232+
const lib_path = try std.fmt.allocPrint(
233+
mod.owner.allocator,
234+
mimalloc ++ "/out/{s}-{s}/lib/libmimalloc.a",
235+
.{ @tagName(os), @tagName(arch) },
236+
);
237+
mod.addObjectFile(b.path(lib_path));
238+
mod.addIncludePath(b.path(mimalloc ++ "/include"));
203239

204240
return mod;
205241
}

0 commit comments

Comments
 (0)