From 2cfc919e3f33266ed287a24271589bbb8f435e1b Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 17 Jun 2021 16:13:02 +0000 Subject: [PATCH 1/3] zig: backport 3 upstream commits These patches are required in order to integrate zig's crosscompilation features with xbps when using the zig build system. All 3 of these commits have landed in the 0.8.x branch upstream and will be included in the upcoming 0.8.1 release. --- ...01-zig-build-add-libc-general-option.patch | 65 +++++ .../0002-stage2-add-sysroot-link-option.patch | 245 ++++++++++++++++++ ...std-build-add-sysroot-general-option.patch | 61 +++++ srcpkgs/zig/template | 2 +- 4 files changed, 372 insertions(+), 1 deletion(-) create mode 100644 srcpkgs/zig/patches/0001-zig-build-add-libc-general-option.patch create mode 100644 srcpkgs/zig/patches/0002-stage2-add-sysroot-link-option.patch create mode 100644 srcpkgs/zig/patches/0003-std-build-add-sysroot-general-option.patch diff --git a/srcpkgs/zig/patches/0001-zig-build-add-libc-general-option.patch b/srcpkgs/zig/patches/0001-zig-build-add-libc-general-option.patch new file mode 100644 index 000000000000..6c6d894c8e41 --- /dev/null +++ b/srcpkgs/zig/patches/0001-zig-build-add-libc-general-option.patch @@ -0,0 +1,65 @@ +From 3e4f3a1924150e22e0ca87e906681ebadcf8433e Mon Sep 17 00:00:00 2001 +From: Isaac Freund +Date: Sun, 13 Jun 2021 04:49:54 +0000 +Subject: [PATCH 1/3] zig build: add --libc general option + +This new option sets a default libc paths file to be used for all +LibExeObjSteps. Setting LibExeObjStep.libc_file overrides this default. + +This is required to allow users to cross compile projects linking system +libraries without needing to patch the build.zig. +--- + lib/std/build.zig | 4 ++++ + lib/std/special/build_runner.zig | 7 +++++++ + 2 files changed, 11 insertions(+) + +diff --git a/lib/std/build.zig b/lib/std/build.zig +index 7628a18c5..e433ec386 100644 +--- a/lib/std/build.zig ++++ b/lib/std/build.zig +@@ -58,6 +58,7 @@ pub const Builder = struct { + h_dir: []const u8, + install_path: []const u8, + search_prefixes: ArrayList([]const u8), ++ libc_file: ?[]const u8 = null, + installed_files: ArrayList(InstalledFile), + build_root: []const u8, + cache_root: []const u8, +@@ -2377,6 +2378,9 @@ pub const LibExeObjStep = struct { + if (self.libc_file) |libc_file| { + try zig_args.append("--libc"); + try zig_args.append(builder.pathFromRoot(libc_file)); ++ } else if (builder.libc_file) |libc_file| { ++ try zig_args.append("--libc"); ++ try zig_args.append(libc_file); + } + + switch (self.build_mode) { +diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig +index c6185ef09..2a0ca86d7 100644 +--- a/lib/std/special/build_runner.zig ++++ b/lib/std/special/build_runner.zig +@@ -93,6 +93,12 @@ pub fn main() !void { + return usageAndErr(builder, false, stderr_stream); + }; + builder.addSearchPrefix(search_prefix); ++ } else if (mem.eql(u8, arg, "--libc")) { ++ const libc_file = nextArg(args, &arg_idx) orelse { ++ warn("Expected argument after --libc\n\n", .{}); ++ return usageAndErr(builder, false, stderr_stream); ++ }; ++ builder.libc_file = libc_file; + } else if (mem.eql(u8, arg, "--color")) { + const next_arg = nextArg(args, &arg_idx) orelse { + warn("expected [auto|on|off] after --color", .{}); +@@ -190,6 +196,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void + \\ --verbose Print commands before executing them + \\ -p, --prefix [path] Override default install prefix + \\ --search-prefix [path] Add a path to look for binaries, libraries, headers ++ \\ --libc [file] Provide a file which specifies libc paths + \\ --color [auto|off|on] Enable or disable colored error messages + \\ + \\Project-Specific Options: +-- +2.32.0 + diff --git a/srcpkgs/zig/patches/0002-stage2-add-sysroot-link-option.patch b/srcpkgs/zig/patches/0002-stage2-add-sysroot-link-option.patch new file mode 100644 index 000000000000..947a4aae1df0 --- /dev/null +++ b/srcpkgs/zig/patches/0002-stage2-add-sysroot-link-option.patch @@ -0,0 +1,245 @@ +From 16228e87d6d7d8929305083e8c76168603780f5f Mon Sep 17 00:00:00 2001 +From: Isaac Freund +Date: Mon, 21 Jun 2021 22:45:43 +0200 +Subject: [PATCH 2/3] stage2: add --sysroot link option + +This feature is necessary for cross-compiling code that dynamically +links system libraries, at least with the current feature set of lld. +--- + src/Compilation.zig | 42 +++++++++++++++++++++++++----------------- + src/link.zig | 4 ++-- + src/link/Elf.zig | 5 +++++ + src/link/MachO.zig | 8 ++++---- + src/main.zig | 11 ++++++++++- + 5 files changed, 46 insertions(+), 24 deletions(-) + +diff --git a/src/Compilation.zig b/src/Compilation.zig +index 977c9f4b8..5f5c1c5dd 100644 +--- a/src/Compilation.zig ++++ b/src/Compilation.zig +@@ -612,6 +612,7 @@ pub const InitOptions = struct { + output_mode: std.builtin.OutputMode, + thread_pool: *ThreadPool, + dynamic_linker: ?[]const u8 = null, ++ sysroot: ?[]const u8 = null, + /// `null` means to not emit a binary file. + emit_bin: ?EmitLoc, + /// `null` means to not emit a C header file. +@@ -868,25 +869,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { + break :blk false; + }; + +- const DarwinOptions = struct { +- syslibroot: ?[]const u8 = null, +- system_linker_hack: bool = false, ++ const darwin_can_use_system_linker_and_sdk = ++ // comptime conditions ++ ((build_options.have_llvm and comptime std.Target.current.isDarwin()) and ++ // runtime conditions ++ (use_lld and std.builtin.os.tag == .macos and options.target.isDarwin())); ++ ++ const darwin_system_linker_hack = blk: { ++ if (darwin_can_use_system_linker_and_sdk) { ++ break :blk std.os.getenv("ZIG_SYSTEM_LINKER_HACK") != null; ++ } else { ++ break :blk false; ++ } + }; + +- const darwin_options: DarwinOptions = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: { +- const opts: DarwinOptions = if (use_lld and std.builtin.os.tag == .macos and options.target.isDarwin()) inner: { ++ const sysroot = blk: { ++ if (options.sysroot) |sysroot| { ++ break :blk sysroot; ++ } else if (darwin_can_use_system_linker_and_sdk) { + // TODO Revisit this targeting versions lower than macOS 11 when LLVM 12 is out. + // See https://github.com/ziglang/zig/issues/6996 + const at_least_big_sur = options.target.os.getVersionRange().semver.min.major >= 11; +- const syslibroot = if (at_least_big_sur) try std.zig.system.getSDKPath(arena) else null; +- const system_linker_hack = std.os.getenv("ZIG_SYSTEM_LINKER_HACK") != null; +- break :inner .{ +- .syslibroot = syslibroot, +- .system_linker_hack = system_linker_hack, +- }; +- } else .{}; +- break :outer opts; +- } else .{}; ++ break :blk if (at_least_big_sur) try std.zig.system.getSDKPath(arena) else null; ++ } else { ++ break :blk null; ++ } ++ }; + + const lto = blk: { + if (options.want_lto) |explicit| { +@@ -897,7 +905,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { + break :blk false; + } else if (options.c_source_files.len == 0) { + break :blk false; +- } else if (darwin_options.system_linker_hack) { ++ } else if (darwin_system_linker_hack) { + break :blk false; + } else switch (options.output_mode) { + .Lib, .Obj => break :blk false, +@@ -1273,13 +1281,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { + .module = module, + .target = options.target, + .dynamic_linker = options.dynamic_linker, ++ .sysroot = sysroot, + .output_mode = options.output_mode, + .link_mode = link_mode, + .object_format = ofmt, + .optimize_mode = options.optimize_mode, + .use_lld = use_lld, + .use_llvm = use_llvm, +- .system_linker_hack = darwin_options.system_linker_hack, ++ .system_linker_hack = darwin_system_linker_hack, + .link_libc = link_libc, + .link_libcpp = link_libcpp, + .link_libunwind = link_libunwind, +@@ -1287,7 +1296,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { + .frameworks = options.frameworks, + .framework_dirs = options.framework_dirs, + .system_libs = system_libs, +- .syslibroot = darwin_options.syslibroot, + .lib_dirs = options.lib_dirs, + .rpath_list = options.rpath_list, + .strip = strip, +diff --git a/src/link.zig b/src/link.zig +index fcb263f03..2d2c1ebea 100644 +--- a/src/link.zig ++++ b/src/link.zig +@@ -37,6 +37,8 @@ pub const Options = struct { + /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`. + module: ?*Module, + dynamic_linker: ?[]const u8, ++ /// The root path for the dynamic linker and system libraries (as well as frameworks on Darwin) ++ sysroot: ?[]const u8, + /// Used for calculating how much space to reserve for symbols in case the binary file + /// does not already have a symbol table. + symbol_count_hint: u64 = 32, +@@ -103,8 +105,6 @@ pub const Options = struct { + llvm_cpu_features: ?[*:0]const u8, + /// Extra args passed directly to LLD. Ignored when not linking with LLD. + extra_lld_args: []const []const u8, +- /// Darwin-only. Set the root path to the system libraries and frameworks. +- syslibroot: ?[]const u8, + + objects: []const []const u8, + framework_dirs: []const []const u8, +diff --git a/src/link/Elf.zig b/src/link/Elf.zig +index 5bf20fdd2..981211151 100644 +--- a/src/link/Elf.zig ++++ b/src/link/Elf.zig +@@ -1354,6 +1354,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { + man.hash.add(allow_shlib_undefined); + man.hash.add(self.base.options.bind_global_refs_locally); + man.hash.add(self.base.options.tsan); ++ man.hash.addOptionalBytes(self.base.options.sysroot); + + // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. + _ = try man.hit(); +@@ -1423,6 +1424,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { + + try argv.append("-error-limit=0"); + ++ if (self.base.options.sysroot) |sysroot| { ++ try argv.append(try std.fmt.allocPrint(arena, "--sysroot={s}", .{sysroot})); ++ } ++ + if (self.base.options.lto) { + switch (self.base.options.optimize_mode) { + .Debug => {}, +diff --git a/src/link/MachO.zig b/src/link/MachO.zig +index 24dfa2c32..1e11a3258 100644 +--- a/src/link/MachO.zig ++++ b/src/link/MachO.zig +@@ -590,7 +590,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { + man.hash.add(allow_shlib_undefined); + man.hash.add(self.base.options.bind_global_refs_locally); + man.hash.add(self.base.options.system_linker_hack); +- man.hash.addOptionalBytes(self.base.options.syslibroot); ++ man.hash.addOptionalBytes(self.base.options.sysroot); + + // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. + _ = try man.hit(); +@@ -720,7 +720,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { + for (self.base.options.lib_dirs) |path| { + if (fs.path.isAbsolute(path)) { + var candidates = std.ArrayList([]const u8).init(arena); +- if (self.base.options.syslibroot) |syslibroot| { ++ if (self.base.options.sysroot) |syslibroot| { + const full_path = try fs.path.join(arena, &[_][]const u8{ syslibroot, path }); + try candidates.append(full_path); + } +@@ -813,7 +813,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { + try argv.append("zig"); + try argv.append("ld"); + +- if (self.base.options.syslibroot) |syslibroot| { ++ if (self.base.options.sysroot) |syslibroot| { + try argv.append("-syslibroot"); + try argv.append(syslibroot); + } +@@ -959,7 +959,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { + } + } + +- if (self.base.options.syslibroot) |dir| { ++ if (self.base.options.sysroot) |dir| { + try argv.append("-syslibroot"); + try argv.append(dir); + } +diff --git a/src/main.zig b/src/main.zig +index f5bda4379..b136a05bd 100644 +--- a/src/main.zig ++++ b/src/main.zig +@@ -377,6 +377,7 @@ const usage_build_generic = + \\ -T[script], --script [script] Use a custom linker script + \\ --version-script [path] Provide a version .map file + \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so) ++ \\ --sysroot [path] Set the system root directory (usually /) + \\ --version [ver] Dynamic library semver + \\ -fsoname[=name] (Linux) Override the default SONAME value + \\ -fno-soname (Linux) Disable emitting a SONAME +@@ -602,6 +603,7 @@ fn buildOutputType( + var link_eh_frame_hdr = false; + var link_emit_relocs = false; + var each_lib_rpath: ?bool = null; ++ var sysroot: ?[]const u8 = null; + var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC"); + var machine_code_model: std.builtin.CodeModel = .default; + var runtime_args_start: ?usize = null; +@@ -859,6 +861,10 @@ fn buildOutputType( + if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); + i += 1; + target_dynamic_linker = args[i]; ++ } else if (mem.eql(u8, arg, "--sysroot")) { ++ if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); ++ i += 1; ++ sysroot = args[i]; + } else if (mem.eql(u8, arg, "--libc")) { + if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); + i += 1; +@@ -1621,7 +1627,9 @@ fn buildOutputType( + want_native_include_dirs = true; + } + +- if (cross_target.isNativeOs() and (system_libs.items.len != 0 or want_native_include_dirs)) { ++ if (sysroot == null and cross_target.isNativeOs() and ++ (system_libs.items.len != 0 or want_native_include_dirs)) ++ { + const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| { + fatal("unable to detect native system paths: {s}", .{@errorName(err)}); + }; +@@ -1898,6 +1906,7 @@ fn buildOutputType( + .is_native_os = cross_target.isNativeOs(), + .is_native_abi = cross_target.isNativeAbi(), + .dynamic_linker = target_info.dynamic_linker.get(), ++ .sysroot = sysroot, + .output_mode = output_mode, + .root_pkg = root_pkg, + .emit_bin = emit_bin_loc, +-- +2.32.0 + diff --git a/srcpkgs/zig/patches/0003-std-build-add-sysroot-general-option.patch b/srcpkgs/zig/patches/0003-std-build-add-sysroot-general-option.patch new file mode 100644 index 000000000000..fbb579d44b06 --- /dev/null +++ b/srcpkgs/zig/patches/0003-std-build-add-sysroot-general-option.patch @@ -0,0 +1,61 @@ +From e75dfbcb3f1892f67fa9a86157bc503fad300772 Mon Sep 17 00:00:00 2001 +From: Isaac Freund +Date: Fri, 25 Jun 2021 14:04:38 +0200 +Subject: [PATCH 3/3] std/build: add --sysroot general option + +--- + lib/std/build.zig | 5 +++++ + lib/std/special/build_runner.zig | 7 +++++++ + 2 files changed, 12 insertions(+) + +diff --git a/lib/std/build.zig b/lib/std/build.zig +index e433ec386..9b7ebb1f4 100644 +--- a/lib/std/build.zig ++++ b/lib/std/build.zig +@@ -57,6 +57,7 @@ pub const Builder = struct { + exe_dir: []const u8, + h_dir: []const u8, + install_path: []const u8, ++ sysroot: ?[]const u8 = null, + search_prefixes: ArrayList([]const u8), + libc_file: ?[]const u8 = null, + installed_files: ArrayList(InstalledFile), +@@ -2601,6 +2602,10 @@ pub const LibExeObjStep = struct { + } + } + ++ if (builder.sysroot) |sysroot| { ++ try zig_args.appendSlice(&[_][]const u8{ "--sysroot", sysroot }); ++ } ++ + for (builder.search_prefixes.items) |search_prefix| { + try zig_args.append("-L"); + try zig_args.append(try fs.path.join(builder.allocator, &[_][]const u8{ +diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig +index 2a0ca86d7..d1154add0 100644 +--- a/lib/std/special/build_runner.zig ++++ b/lib/std/special/build_runner.zig +@@ -87,6 +87,12 @@ pub fn main() !void { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; ++ } else if (mem.eql(u8, arg, "--sysroot")) { ++ const sysroot = nextArg(args, &arg_idx) orelse { ++ warn("Expected argument after --sysroot\n\n", .{}); ++ return usageAndErr(builder, false, stderr_stream); ++ }; ++ builder.sysroot = sysroot; + } else if (mem.eql(u8, arg, "--search-prefix")) { + const search_prefix = nextArg(args, &arg_idx) orelse { + warn("Expected argument after --search-prefix\n\n", .{}); +@@ -195,6 +201,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void + \\ -h, --help Print this help and exit + \\ --verbose Print commands before executing them + \\ -p, --prefix [path] Override default install prefix ++ \\ --sysroot [path] Set the system root directory (usually /) + \\ --search-prefix [path] Add a path to look for binaries, libraries, headers + \\ --libc [file] Provide a file which specifies libc paths + \\ --color [auto|off|on] Enable or disable colored error messages +-- +2.32.0 + diff --git a/srcpkgs/zig/template b/srcpkgs/zig/template index baefd41af01e..f9a4c61839aa 100644 --- a/srcpkgs/zig/template +++ b/srcpkgs/zig/template @@ -1,7 +1,7 @@ # Template file for 'zig' pkgname=zig version=0.8.0 -revision=1 +revision=2 archs="x86_64* aarch64*" build_style=cmake make_cmd=make From 79c42debada0cf99491fc9531c9525a9a0ade7ca Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 17 Jun 2021 16:20:30 +0000 Subject: [PATCH 2/3] build-styles: add zig-build We call this "zig-build" instead of just "zig" as this build-style relies on usage of the zig build system. In the future, other build systems such as meson may support zig code as well. Furthermore, the zig build system may be used to build C/C++ code as well, not just zig. --- common/build-style/zig-build.sh | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 common/build-style/zig-build.sh diff --git a/common/build-style/zig-build.sh b/common/build-style/zig-build.sh new file mode 100644 index 000000000000..69dea4ca555a --- /dev/null +++ b/common/build-style/zig-build.sh @@ -0,0 +1,49 @@ +do_build() { + local zig_abi + local zig_target + local zig_cpu + + case $XBPS_TARGET_LIBC in + glibc) zig_abi="gnu";; + musl) zig_abi="musl";; + *) broken="Unknown target libc";; + esac + + case $XBPS_TARGET_MACHINE in + aarch64*|i686*|x86_64*) + zig_target="${XBPS_TARGET_MACHINE%-musl}-linux-${zig_abi}" zig_cpu="baseline";; + armv6l*) zig_target="arm-linux-${zig_abi}" zig_cpu="generic+v6";; + armv7l*) zig_target="arm-linux-${zig_abi}" zig_cpu="generic+v7a";; + ppc64le*) zig_target="powerpc64le-linux-${zig_abi}" zig_cpu="baseline";; + ppc64*) zig_target="powerpc64-linux-${zig_abi}" zig_cpu="baseline";; + ppcle*) zig_target="powerpcle-linux-${zig_abi}" zig_cpu="baseline";; + ppc*) zig_target="powerpc-linux-${zig_abi}" zig_cpu="baseline";; + *) broken="TODO: support more target machines for the zig build style";; + esac + + # Inform zig of the required libc include paths. + cat > xbps_zig_libc.txt <<-EOF + include_dir=${XBPS_CROSS_BASE}/usr/include + sys_include_dir=${XBPS_CROSS_BASE}/usr/include + crt_dir=${XBPS_CROSS_BASE}/usr/lib + msvc_lib_dir= + kernel32_lib_dir= + gcc_dir= + EOF + + # The Zig build system only has a single install step, there is no + # way to build artifacts for a given prefix and then install those artifacts + # to that prefix at some later time. Therefore, we build and install to the zig-out + # directory and later copy the artifacts to the destdir in do_install(). + # We use zig-out to avoid path conflicts as it is the default install + # prefix used by the zig build system. + DESTDIR="zig-out" zig build \ + --sysroot "${XBPS_CROSS_BASE}" \ + --libc xbps_zig_libc.txt \ + -Dtarget=$zig_target -Dcpu=$zig_cpu \ + -Drelease-safe --prefix /usr install +} + +do_install() { + cp -r zig-out/* "${DESTDIR}" +} From c0897fe6957c1682fbfaf2aa71dbdbe34e72bd0d Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 17 Jun 2021 01:40:15 +0000 Subject: [PATCH 3/3] New package: rundird-0.1.1 --- srcpkgs/rundird/files/rundird/run | 2 ++ srcpkgs/rundird/template | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 srcpkgs/rundird/files/rundird/run create mode 100644 srcpkgs/rundird/template diff --git a/srcpkgs/rundird/files/rundird/run b/srcpkgs/rundird/files/rundird/run new file mode 100644 index 000000000000..df06fdb0fc88 --- /dev/null +++ b/srcpkgs/rundird/files/rundird/run @@ -0,0 +1,2 @@ +#!/bin/sh +exec /usr/bin/rundird diff --git a/srcpkgs/rundird/template b/srcpkgs/rundird/template new file mode 100644 index 000000000000..c2c855164783 --- /dev/null +++ b/srcpkgs/rundird/template @@ -0,0 +1,17 @@ +# Template file for 'rundird' +pkgname=rundird +version=0.1.1 +revision=1 +build_style=zig-build +hostmakedepends="zig" +makedepends="pam-devel" +short_desc="Simple daemon + PAM module providing an XDG_RUNTIME_DIR" +maintainer="Isaac Freund " +license="GPL-3.0-or-later" +homepage="https://github.com/ifreund/rundird" +distfiles="${homepage}/archive/v${version}.tar.gz" +checksum=d3a848fba0016841385aab57298164ba8f30377799c217b6b6abf83ce1d2ca91 + +post_install() { + vsv rundird +}