diff --git a/Makefile b/Makefile index b0ae69015..77daf2a1a 100644 --- a/Makefile +++ b/Makefile @@ -9,24 +9,29 @@ F= # OS and ARCH kernel = $(shell uname -ms) ifeq ($(kernel), Darwin arm64) - OS := macos - ARCH := aarch64 + OS ?= macos + ARCH ?= aarch64 else ifeq ($(kernel), Darwin x86_64) - OS := macos - ARCH := x86_64 + OS ?= macos + ARCH ?= x86_64 else ifeq ($(kernel), Linux aarch64) - OS := linux - ARCH := aarch64 + OS ?= linux + ARCH ?= aarch64 else ifeq ($(kernel), Linux arm64) - OS := linux - ARCH := aarch64 + OS ?= linux + ARCH ?= aarch64 else ifeq ($(kernel), Linux x86_64) - OS := linux - ARCH := x86_64 + OS ?= linux + ARCH ?= x86_64 else $(error "Unhandled kernel: $(kernel)") endif +MAKE ?= make +CMAKE ?= cmake +AUTOCONF_FLAGS ?= +CFLAGS ?= +LDFLAGS ?= # Infos # ----- @@ -149,38 +154,40 @@ _install-netsurf: clean-netsurf 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" && \ + export OPTLDFLAGS="$(LDFLAGS) -L$(ICONV)/lib" && \ + export OPTCFLAGS="$(CFLAGS) $(OPTCFLAGS) -I$(ICONV)/include" && \ printf "\e[33mInstalling libwapcaplet...\e[0m\n" && \ cd vendor/netsurf/libwapcaplet && \ - BUILDDIR=$(BC_NS)/build/libwapcaplet make install && \ + BUILDDIR=$(BC_NS)/build/libwapcaplet $(MAKE) install && \ cd ../libparserutils && \ printf "\e[33mInstalling libparserutils...\e[0m\n" && \ - BUILDDIR=$(BC_NS)/build/libparserutils make install && \ + BUILDDIR=$(BC_NS)/build/libparserutils $(MAKE) install && \ cd ../libhubbub && \ printf "\e[33mInstalling libhubbub...\e[0m\n" && \ - BUILDDIR=$(BC_NS)/build/libhubbub make install && \ + BUILDDIR=$(BC_NS)/build/libhubbub $(MAKE) install && \ rm src/treebuilder/autogenerated-element-type.c && \ cd ../libdom && \ printf "\e[33mInstalling libdom...\e[0m\n" && \ - BUILDDIR=$(BC_NS)/build/libdom make install && \ - printf "\e[33mRunning libdom example...\e[0m\n" && \ - cd examples && \ - $(ZIG) cc \ - -I$(ICONV)/include \ - -I$(BC_NS)/include \ - -L$(ICONV)/lib \ - -L$(BC_NS)/lib \ - -liconv \ - -ldom \ - -lhubbub \ - -lparserutils \ - -lwapcaplet \ - -o a.out \ - dom-structure-dump.c \ - $(ICONV)/lib/libiconv.a && \ - ./a.out > /dev/null && \ - rm a.out && \ + BUILDDIR=$(BC_NS)/build/libdom $(MAKE) install && \ + if [ -z "$${SKIP_EXAMPLES}" ]; then \ + printf "\e[33mRunning libdom example...\e[0m\n" && \ + cd examples && \ + $(ZIG) cc \ + -I$(ICONV)/include \ + -I$(BC_NS)/include \ + -L$(ICONV)/lib \ + -L$(BC_NS)/lib \ + -liconv \ + -ldom \ + -lhubbub \ + -lparserutils \ + -lwapcaplet \ + -o a.out \ + dom-structure-dump.c \ + $(ICONV)/lib/libiconv.a && \ + ./a.out > /dev/null && \ + rm a.out; \ + fi && \ printf "\e[36mDone NetSurf $(OS)\e[0m\n" clean-netsurf: @@ -204,7 +211,7 @@ endif build-libiconv: clean-libiconv @cd vendor/libiconv/libiconv-1.17 && \ - ./configure --prefix=$(ICONV) --enable-static && \ + ./configure --prefix=$(ICONV) --enable-static $(AUTOCONF_FLAGS) && \ make && make install install-libiconv: download-libiconv build-libiconv @@ -224,7 +231,7 @@ 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) ../../.. && \ + $(CMAKE) -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) ../../.. && \ make && \ mkdir -p $(MIMALLOC)/lib diff --git a/build.zig b/build.zig index 1c9385818..b6597379c 100644 --- a/build.zig +++ b/build.zig @@ -149,6 +149,25 @@ pub fn build(b: *Build) !void { const build_step = b.step("build-v8", "Build v8"); build_step.dependOn(&build_v8.step); } + + { + // static lib + // ------- + const static_lib_module = b.addModule("lightpanda", .{ + .root_source_file = b.path("src/lib.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + .link_libcpp = true, + }); + try addDependencies(b, static_lib_module, opts); + + const lib = b.addLibrary(.{ .name = "lightpanda", .root_module = static_lib_module, .use_llvm = true, .linkage = .static }); + lib.bundle_compiler_rt = true; + const install_artifact = b.addInstallArtifact(lib, .{}); + const build_step = b.step("static-lib", "Build static lib"); + build_step.dependOn(&install_artifact.step); + } } fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !void { @@ -176,6 +195,7 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo const os = switch (target.result.os.tag) { .linux => "linux", .macos => "macos", + .ios => "ios", else => return error.UnsupportedPlatform, }; var lib_path = try std.fmt.allocPrint( @@ -199,6 +219,12 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo mod.addSystemFrameworkPath(.{ .cwd_relative = "/System/Library/Frameworks" }); mod.linkFramework("CoreFoundation", .{}); }, + .ios => { + const sdk_path = try std.process.getEnvVarOwned(mod.owner.allocator, "SDK"); + const framework_path = try std.fmt.allocPrint(mod.owner.allocator, "{s}/System/Library/Frameworks", .{sdk_path}); + mod.addSystemFrameworkPath(.{ .cwd_relative = framework_path }); + mod.linkFramework("CoreFoundation", .{}); + }, else => {}, } } @@ -390,6 +416,13 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo mod.linkFramework("CoreFoundation", .{}); mod.linkFramework("SystemConfiguration", .{}); }, + .ios => { + const sdk_path = try std.process.getEnvVarOwned(mod.owner.allocator, "SDK"); + const framework_path = try std.fmt.allocPrint(mod.owner.allocator, "{s}/System/Library/Frameworks", .{sdk_path}); + mod.addSystemFrameworkPath(.{ .cwd_relative = framework_path }); + mod.linkFramework("CoreFoundation", .{}); + mod.linkFramework("SystemConfiguration", .{}); + }, else => {}, } } @@ -397,19 +430,33 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo fn moduleNetSurf(b: *Build, mod: *Build.Module) !void { const target = mod.resolved_target.?; - const os = target.result.os.tag; - const arch = target.result.cpu.arch; + const os = switch (target.result.os.tag) { + .linux => "linux", + .macos => "macos", + .ios => switch (target.result.abi) { + .simulator => "iphonesimulator", + else => return error.UnsupportedPlatform, + }, + else => return error.UnsupportedPlatform, + }; + const arch = switch (target.result.os.tag) { + .ios => switch (target.result.cpu.arch) { + .aarch64 => "arm64", + else => @tagName(target.result.cpu.arch), + }, + else => @tagName(target.result.cpu.arch), + }; // iconv const libiconv_lib_path = try std.fmt.allocPrint( b.allocator, "vendor/libiconv/out/{s}-{s}/lib/libiconv.a", - .{ @tagName(os), @tagName(arch) }, + .{ os, arch }, ); const libiconv_include_path = try std.fmt.allocPrint( b.allocator, "vendor/libiconv/out/{s}-{s}/lib/libiconv.a", - .{ @tagName(os), @tagName(arch) }, + .{ os, arch }, ); mod.addObjectFile(b.path(libiconv_lib_path)); mod.addIncludePath(b.path(libiconv_include_path)); @@ -420,7 +467,7 @@ fn moduleNetSurf(b: *Build, mod: *Build.Module) !void { const lib_path = try std.fmt.allocPrint( b.allocator, mimalloc ++ "/out/{s}-{s}/lib/libmimalloc.a", - .{ @tagName(os), @tagName(arch) }, + .{ os, arch }, ); mod.addObjectFile(b.path(lib_path)); mod.addIncludePath(b.path(mimalloc ++ "/include")); @@ -431,7 +478,7 @@ fn moduleNetSurf(b: *Build, mod: *Build.Module) !void { const ns_include_path = try std.fmt.allocPrint( b.allocator, ns ++ "/out/{s}-{s}/include", - .{ @tagName(os), @tagName(arch) }, + .{ os, arch }, ); mod.addIncludePath(b.path(ns_include_path)); @@ -445,7 +492,7 @@ fn moduleNetSurf(b: *Build, mod: *Build.Module) !void { const ns_lib_path = try std.fmt.allocPrint( b.allocator, ns ++ "/out/{s}-{s}/lib/" ++ lib ++ ".a", - .{ @tagName(os), @tagName(arch) }, + .{ os, arch }, ); mod.addObjectFile(b.path(ns_lib_path)); mod.addIncludePath(b.path(ns ++ "/" ++ lib ++ "/src")); @@ -494,7 +541,12 @@ fn buildMbedtls(b: *Build, m: *Build.Module) !void { mbedtls.addIncludePath(b.path(root ++ "include")); mbedtls.addIncludePath(b.path(root ++ "library")); - mbedtls.addCSourceFiles(.{ .flags = &.{}, .files = &.{ + const flags: []const []const u8 = if (m.resolved_target.?.result.os.tag == .ios) + &.{"-Wno-nullability-completeness"} + else + &.{}; + + mbedtls.addCSourceFiles(.{ .flags = flags, .files = &.{ root ++ "library/aes.c", root ++ "library/aesni.c", root ++ "library/aesce.c", @@ -648,6 +700,12 @@ fn buildNghttp2(b: *Build, m: *Build.Module) !void { } fn buildCurl(b: *Build, m: *Build.Module) !void { + if (m.resolved_target.?.result.os.tag == .ios) { + const sdk_path = try std.process.getEnvVarOwned(b.allocator, "SDK"); + const include_path = try std.fmt.allocPrint(b.allocator, "{s}/usr/include", .{sdk_path}); + m.addIncludePath(.{ .cwd_relative = include_path }); + } + const curl = b.addLibrary(.{ .name = "curl", .root_module = m, diff --git a/build.zig.zon b/build.zig.zon index ff74c5619..340d4e94f 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -8,6 +8,6 @@ .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/7177ee1ae267a44751a0e7e012e257177699a375.tar.gz", .hash = "v8-0.0.0-xddH63TCAwC1D1hEiOtbEnLBbtz9ZPHrdiGWLcBcYQB7", }, - // .v8 = .{ .path = "../zig-v8-fork" } + // .v8 = .{ .path = "../zig-v8-fork" }, }, } diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 000000000..8c89251d5 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/app.zig b/src/app.zig index ee690e731..b6cb12df8 100644 --- a/src/app.zig +++ b/src/app.zig @@ -92,11 +92,24 @@ pub const App = struct { } }; +fn getAppDir(allocator: Allocator) ![]const u8 { + if (@import("builtin").os.tag == .ios) { + // std.fs.getAppDataDir is not available on iOS, so we inline the same macOS implementation here. + const home_dir = std.posix.getenv("HOME") orelse { + return error.AppDataDirUnavailable; + }; + return std.fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", "lightpanda" }); + } else { + return try std.fs.getAppDataDir(allocator, "lightpanda"); + } +} + fn getAndMakeAppDir(allocator: Allocator) ?[]const u8 { if (@import("builtin").is_test) { return allocator.dupe(u8, "/tmp") catch unreachable; } - const app_dir_path = std.fs.getAppDataDir(allocator, "lightpanda") catch |err| { + + const app_dir_path = getAppDir(allocator) catch |err| { log.warn(.app, "get data dir", .{ .err = err }); return null; }; diff --git a/src/server.zig b/src/server.zig index f4f169d11..0f7badb14 100644 --- a/src/server.zig +++ b/src/server.zig @@ -77,8 +77,17 @@ pub const Server = struct { self.listener = listener; try posix.setsockopt(listener, posix.SOL.SOCKET, posix.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1))); - if (@hasDecl(posix.TCP, "NODELAY")) { - try posix.setsockopt(listener, posix.IPPROTO.TCP, posix.TCP.NODELAY, &std.mem.toBytes(@as(c_int, 1))); + switch (builtin.os.tag) { + .ios => { + // TCP.NODELAY is not defined for iOS in posix module + const TCP_NODELAY = 0x01; + try posix.setsockopt(listener, posix.IPPROTO.TCP, TCP_NODELAY, &std.mem.toBytes(@as(c_int, 1))); + }, + else => { + if (@hasDecl(posix.TCP, "NODELAY")) { + try posix.setsockopt(listener, posix.IPPROTO.TCP, posix.TCP.NODELAY, &std.mem.toBytes(@as(c_int, 1))); + } + }, } try posix.bind(listener, &address.any, address.getOsSockLen());