diff --git a/.envrc b/.envrc index bfe33bb..7dd7e81 100644 --- a/.envrc +++ b/.envrc @@ -1,4 +1,5 @@ -: ${XDG_CACHE_HOME:=$HOME/.cache} +#!/usr/bin/env bash +: "${XDG_CACHE_HOME:=$HOME/.cache}" hash_source() { echo -n "$PWD" diff --git a/build.zig b/build.zig index ede29e9..49e9842 100644 --- a/build.zig +++ b/build.zig @@ -22,14 +22,25 @@ pub fn build(b: *std.Build) void { .unit = b.step("unit", "Run pgzx unit tests"), }; - // pgzx_pgsys module: C bindings to Postgres - const pgzx_pgsys = blk: { - const module = b.addModule("pgzx_pgsys", .{ - .root_source_file = b.path("./src/pgzx/c.zig"), + // c_translated module: C bindings to Postgres + const c_translated = blk: { + const translate_c = b.addTranslateC(.{ + .root_source_file = b.path("./src/pgzx/c/include/headers.h"), .target = target, .optimize = optimize, }); + translate_c.addIncludePath(b.path("./src/pgzx/c/include/")); + + translate_c.addIncludePath(.{ + .cwd_relative = pgbuild.getIncludeServerDir(), + }); + translate_c.addIncludePath(.{ + .cwd_relative = pgbuild.getIncludeDir(), + }); + + const module = translate_c.createModule(); + // Internal C headers module.addIncludePath(b.path("./src/pgzx/c/include/")); @@ -56,38 +67,44 @@ pub fn build(b: *std.Build) void { }); module.linkSystemLibrary("pq", .{}); + b.modules.put(b.dupe("c_translated"), module) catch @panic("OOM"); + break :blk module; }; // codegen // The codegen produces Zig files that are imported as modules by pgzx. const node_tags_src = blk: { - const tool = b.addExecutable(.{ - .name = "gennodetags", + const tool_module = b.createModule(.{ .root_source_file = b.path("./tools/gennodetags/main.zig"), .target = b.graph.host, .link_libc = true, }); - tool.root_module.addIncludePath(.{ .cwd_relative = pgbuild.getIncludeServerDir() }); - tool.root_module.addIncludePath(.{ .cwd_relative = pgbuild.getIncludeDir() }); + tool_module.addIncludePath(.{ .cwd_relative = pgbuild.getIncludeServerDir() }); + tool_module.addIncludePath(.{ .cwd_relative = pgbuild.getIncludeDir() }); + + const tool = b.addExecutable(.{ + .name = "gennodetags", + .root_module = tool_module, + }); const tool_step = b.addRunArtifact(tool); break :blk tool_step.addOutputFileArg("nodetags.zig"); }; // pgzx: main project module. - // This module re-exports pgzx_pgsys, other generated modules, and utility functions. + // This module re-exports c_translated, other generated modules, and utility functions. const pgzx = blk: { const module = b.addModule("pgzx", .{ .root_source_file = b.path("./src/pgzx.zig"), .target = target, .optimize = optimize, }); - module.addImport("pgzx_pgsys", pgzx_pgsys); + module.addImport("c_translated", c_translated); module.addAnonymousImport("gen_node_tags", .{ .root_source_file = node_tags_src, .imports = &.{ - .{ .name = "pgzx_pgsys", .module = pgzx_pgsys }, + .{ .name = "c_translated", .module = c_translated }, }, }); @@ -145,12 +162,12 @@ pub fn build(b: *std.Build) void { tests.lib.root_module.addIncludePath(b.path("./src/pgzx/c/include/")); - tests.lib.root_module.addImport("pgzx_pgsys", pgzx_pgsys); + tests.lib.root_module.addImport("c_translated", c_translated); tests.lib.root_module.addImport("pgzx", pgzx); tests.lib.root_module.addAnonymousImport("gen_node_tags", .{ .root_source_file = node_tags_src, .imports = &.{ - .{ .name = "pgzx_pgsys", .module = pgzx_pgsys }, + .{ .name = "c_translated", .module = c_translated }, }, }); diff --git a/dev/bin/pginit b/dev/bin/pginit index cef7ec2..34063dc 100755 --- a/dev/bin/pginit +++ b/dev/bin/pginit @@ -88,8 +88,13 @@ log_file=$log_dir/server.log # create the database mkdir -p "$cluster_dir" -mkdir -p "$data_dir" mkdir -p "$log_dir" + +if [ -d "$data_dir" ]; then + rm -rf "$data_dir" +fi +mkdir -p "$data_dir" + "$PG_BIN"/pg_ctl initdb -D "$data_dir" -o "--encoding=UTF8" # create database user diff --git a/dev/bin/pglocal b/dev/bin/pglocal index 30f5a7c..03f8911 100755 --- a/dev/bin/pglocal +++ b/dev/bin/pglocal @@ -70,7 +70,8 @@ cp_direct_files() { # create symlinks find "$src_dir" -maxdepth 1 -type l | while read -r link; do - ln -s "$(realpath "$link")" "$dest_dir/$(basename "$link")" || return 1 + target="$dest_dir/$(basename "$link")" + [ -e "$target" ] || ln -s "$(realpath "$link")" "$target" || return 1 done } @@ -93,6 +94,13 @@ cp_recursive() { find "$src_dir" -type f -printf '%P\n' | while read -r file; do cp -r "$src_dir/$file" "$dest_dir/$file" || return 1 done + + # copy symlinked files by dereferencing them + find "$src_dir" -type l -printf '%P\n' | while read -r link; do + # Skip directory symlinks - they should be handled separately + [ -d "$src_dir/$link" ] && continue + cp -L "$src_dir/$link" "$dest_dir/$link" || return 1 + done } mkdir -p "$new_install_dir" @@ -130,6 +138,7 @@ fi ) | sort -u | while read -r dir; do cp_direct_files "$new_install_dir/lib" "$dir" done +cp_recursive "$new_install_dir/lib/bitcode" "$($PG_CONFIG --pkglibdir)/bitcode" cp_recursive "$new_install_dir/lib/pkgconfig" "$($PG_CONFIG --pkglibdir)/pkgconfig" cp_recursive "$new_install_dir/lib/postgresql" "$($PG_CONFIG --pkglibdir)/postgresql" diff --git a/devshell.nix b/devshell.nix index c1efcaa..9483c85 100644 --- a/devshell.nix +++ b/devshell.nix @@ -32,6 +32,14 @@ EOF ''; }; + + postgresql_unified = pkgs.symlinkJoin { + name = "postgresql-unified"; + paths = [ + pkgs.postgresql_16_jit + pkgs.postgresql_16_jit.pg_config + ]; + }; # On darwin we expect command line tools to be installed. # It is possible to install clang/gcc as nix package, but linking # can be quite a pain. @@ -48,7 +56,7 @@ in { pkgs.shellcheck pkgs.shfmt - pkgs.postgresql_16_jit + postgresql_unified pkgs.openssl pkgs.gss pkgs.krb5 diff --git a/docs/index.html b/docs/index.html index b10024a..e60a3f9 100644 --- a/docs/index.html +++ b/docs/index.html @@ -6,6 +6,9 @@ Zig Documentation