Skip to content

Commit

Permalink
feat(libfetchers): add zig build
Browse files Browse the repository at this point in the history
  • Loading branch information
RossComputerGuy committed Feb 22, 2025
1 parent 81c3117 commit 5f7b9b4
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 73 deletions.
157 changes: 157 additions & 0 deletions src/libfetchers/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const linkage = b.option(std.builtin.LinkMode, "linkage", "The link mode of binaries");
const fsys_libutil = b.systemIntegrationOption("nix-util", .{});
const fsys_libstore = b.systemIntegrationOption("nix-store", .{});
const use_meson_libs = std.mem.eql(u8, b.graph.env_map.get("USE_MESON_LIBS") orelse "0", "1");

const libfetchers = std.Build.Step.Compile.create(b, .{
.name = "nixfetchers",
.kind = .lib,
.linkage = linkage,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
}),
});

libfetchers.root_module.addCMacro("SYSTEM", b.fmt("\"{s}-{s}\"", .{
@tagName(target.result.cpu.arch),
@tagName(target.result.os.tag),
}));

libfetchers.addIncludePath(b.path("."));

libfetchers.addCSourceFiles(.{
.files = &.{
"attrs.cc",
"cache.cc",
"fetch-settings.cc",
"fetch-to-store.cc",
"fetchers.cc",
"filtering-source-accessor.cc",
"git-lfs-fetch.cc",
"git-utils.cc",
"git.cc",
"github.cc",
"indirect.cc",
"mercurial.cc",
"path.cc",
"registry.cc",
"store-path-accessor.cc",
"tarball.cc",
},
.flags = &.{
"--std=c++2a",
b.fmt("-I{s}", .{std.mem.trimRight(u8, b.run(&.{ "pkg-config", "--variable=includedir", "boost" }), "\n")}),
},
});

libfetchers.linkSystemLibrary("nlohmann_json");
libfetchers.linkSystemLibrary("libgit2");

if (fsys_libutil) {
if (use_meson_libs) {
libfetchers.linkSystemLibrary("nixutil");

for (b.search_prefixes.items) |prefix| {
const path = b.pathJoin(&.{ prefix, "include", "nix" });

var dir = std.fs.cwd().openDir(path, .{}) catch continue;
defer dir.close();

libfetchers.root_module.addSystemIncludePath(.{ .cwd_relative = path });
}

libfetchers.linkSystemLibrary("libarchive");
libfetchers.linkSystemLibrary("boost");
libfetchers.linkSystemLibrary("nlohmann_json");
} else {
libfetchers.linkSystemLibrary("nix-util");
}
} else {
const libutil = b.dependency("nix-util", .{
.target = target,
.optimize = optimize,
.linkage = linkage orelse .static,
});

libfetchers.linkLibrary(libutil.artifact("nixutil"));

for (libutil.artifact("nixutil").root_module.include_dirs.items) |hdr| {
libfetchers.root_module.include_dirs.append(b.allocator, hdr) catch @panic("OOM");
}

libfetchers.linkSystemLibrary("libarchive");
libfetchers.linkSystemLibrary("boost");
libfetchers.linkSystemLibrary("nlohmann_json");
}

if (fsys_libstore) {
if (use_meson_libs) {
libfetchers.linkSystemLibrary("nixstore");

for (b.search_prefixes.items) |prefix| {
const path = b.pathJoin(&.{ prefix, "include", "nix" });

var dir = std.fs.cwd().openDir(path, .{}) catch continue;
defer dir.close();

libfetchers.root_module.addSystemIncludePath(.{ .cwd_relative = path });
}
} else {
libfetchers.linkSystemLibrary("nix-store");
}
} else {
const libstore = b.dependency("nix-store", .{
.target = target,
.optimize = optimize,
.linkage = linkage orelse .static,
});

libfetchers.linkLibrary(libstore.artifact("nixstore"));

for (libstore.artifact("nixstore").root_module.include_dirs.items) |hdr| {
libfetchers.root_module.include_dirs.append(b.allocator, hdr) catch @panic("OOM");
}
}

inline for (&.{
"attrs.hh",
"cache.hh",
"fetch-settings.hh",
"fetch-to-store.hh",
"fetchers.hh",
"filtering-source-accessor.hh",
"git-lfs-fetch.hh",
"git-utils.hh",
"registry.hh",
"store-path-accessor.hh",
"tarball.hh",
}) |hdr| {
libfetchers.installHeader(b.path(hdr), "nix/" ++ hdr);
}

b.installArtifact(libfetchers);

b.getInstallStep().dependOn(&b.addInstallFileWithDir(b.addWriteFile("nix-fetchers.pc", b.fmt(
\\prefix={s}
\\libdir={s}
\\includedir={s}
\\
\\Name: Nix
\\Description: Nix Package Manager
\\Version: 0.1.0
\\Cflags: -I${{includedir}}/nix -std=c++2a
\\Libs: -L${{libdir}} -lnixfetchers
, .{
b.getInstallPath(.prefix, ""),
b.getInstallPath(.lib, ""),
b.getInstallPath(.header, ""),
})).getDirectory().path(b, "nix-fetchers.pc"), .lib, "pkgconfig/nix-fetchers.pc").step);
}
13 changes: 13 additions & 0 deletions src/libfetchers/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.{
.name = "nix-fetchers",
.version = "0.1.0",
.paths = .{"."},
.dependencies = .{
.@"nix-util" = .{
.path = "../libutil",
},
.@"nix-store" = .{
.path = "../libstore",
},
},
}
117 changes: 46 additions & 71 deletions src/libfetchers/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
project('nix-fetchers', 'cpp',
project('nix-fetchers',
version : files('.zix-version'),
default_options : [
'cpp_std=c++2a',
# TODO(Qyriad): increase the warning level
'warning_level=1',
'errorlogs=true', # Please print logs for tests that fail
Expand All @@ -10,82 +9,58 @@ project('nix-fetchers', 'cpp',
license : 'LGPL-2.1-or-later',
)

cxx = meson.get_compiler('cpp')
fs = import('fs')

subdir('nix-meson-build-support/deps-lists')
subdir('nix-meson-build-support/zig')

configuration_data()
zig_prefix = meson.current_build_dir() / 'zig-out'

deps_private_maybe_subproject = [
]
deps_public_maybe_subproject = [
dependency('nix-util'),
dependency('nix-store'),
]
subdir('nix-meson-build-support/subprojects')
deps = []

nlohmann_json = dependency('nlohmann_json', version : '>= 3.9')
deps_public += nlohmann_json
libutil = dependency('nix-util', required: false)
if libutil.found()
deps += libutil
zig_build_args += [
'-fsys=nix-util',
'--search-prefix', libutil.get_variable('prefix'),
'--search-prefix', fs.parent(libutil.get_variable('includedir')),
'--search-prefix', fs.parent(libutil.get_variable('libdir')),
]
endif

libgit2 = dependency('libgit2')
deps_private += libgit2
libstore = dependency('nix-store', required: false)
if libstore.found()
deps += libstore
zig_build_args += [
'-fsys=nix-store',
'--search-prefix', libstore.get_variable('prefix'),
'--search-prefix', fs.parent(libstore.get_variable('includedir')),
'--search-prefix', fs.parent(libstore.get_variable('libdir')),
]
endif

add_project_arguments(
# TODO(Qyriad): Yes this is how the autoconf+Make system did it.
# It would be nice for our headers to be idempotent instead.
'-include', 'config-util.hh',
'-include', 'config-store.hh',
# '-include', 'config-fetchers.h',
language : 'cpp',
zig_target = custom_target('zig build',
output: ['zig-out'],
command: [zig, 'build', zig_build_args, '--prefix', zig_prefix, '--build-file', meson.project_source_root() / 'build.zig'],
env: {
'USE_MESON_LIBS': '1',
},
)

subdir('nix-meson-build-support/common')
includedir = zig_prefix / 'include' / 'nix'
compile_args = ['-I' + includedir]

sources = files(
'attrs.cc',
'cache.cc',
'fetch-settings.cc',
'fetch-to-store.cc',
'fetchers.cc',
'filtering-source-accessor.cc',
'git-lfs-fetch.cc',
'git-utils.cc',
'git.cc',
'github.cc',
'indirect.cc',
'mercurial.cc',
'path.cc',
'registry.cc',
'store-path-accessor.cc',
'tarball.cc',
)

include_dirs = [include_directories('.')]

headers = files(
'attrs.hh',
'cache.hh',
'fetch-settings.hh',
'fetch-to-store.hh',
'fetchers.hh',
'filtering-source-accessor.hh',
'git-lfs-fetch.hh',
'git-utils.hh',
'registry.hh',
'store-path-accessor.hh',
'tarball.hh',
)

this_library = library(
'nixfetchers',
sources,
dependencies : deps_public + deps_private + deps_other,
prelink : true, # For C++ static initializers
install : true,
)

install_headers(headers, subdir : 'nix', preserve_path : true)

libraries_private = []
install_subdir(zig_prefix / 'lib', install_dir: get_option('libdir'), strip_directory: true)
install_subdir(zig_prefix / 'include', install_dir: get_option('includedir'), strip_directory: true)

subdir('nix-meson-build-support/export')
meson.override_dependency('nix-fetchers', declare_dependency(
compile_args: compile_args,
link_args: ['-L' + zig_prefix / 'lib', '-lnixfetchers'],
sources: [zig_target],
dependencies: deps,
variables: {
'prefix': zig_prefix,
'includedir': zig_prefix / 'include',
'libdir': zig_prefix / 'lib'
}
))
18 changes: 16 additions & 2 deletions src/libfetchers/package.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
lib,
mkMesonLibrary,
mkZigLibrary,

nix-util,
nix-store,
Expand All @@ -17,12 +17,14 @@ let
inherit (lib) fileset;
in

mkMesonLibrary (finalAttrs: {
mkZigLibrary (finalAttrs: {
pname = "zix-fetchers";
inherit version nixVersion;

workDir = ./.;
fileset = fileset.unions [
../libutil
../libstore
../../nix-meson-build-support
./nix-meson-build-support
../../.version
Expand All @@ -33,6 +35,8 @@ mkMesonLibrary (finalAttrs: {
./meson.options
(fileset.fileFilter (file: file.hasExt "cc") ./.)
(fileset.fileFilter (file: file.hasExt "hh") ./.)
(fileset.fileFilter (file: file.hasExt "zig") ./.)
(fileset.fileFilter (file: file.hasExt "zon") ./.)
];

buildInputs = [
Expand All @@ -45,6 +49,16 @@ mkMesonLibrary (finalAttrs: {
nlohmann_json
];

zigBuildFlags = [
"-fsys=nix-util"
"-fsys=nix-store"
];

postInstall = ''
substituteInPlace $out/lib/pkgconfig/nix-fetchers.pc \
--replace-fail "includedir=$out" "includedir=$dev"
'';

meta = {
platforms = lib.platforms.unix ++ lib.platforms.windows;
};
Expand Down

0 comments on commit 5f7b9b4

Please sign in to comment.