Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge all 3 libraries in one #6

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ jobs:
- zig-version: "master"
os: ubuntu-latest
build-options: ""
#- zig-version: "master"
# os: macos-latest # Apple Silicon (M1)
# build-options: "-Ddisable-ssl"
- zig-version: "master"
os: macos-latest
build-options: "-Ddisable-ssl -Ddisable-zstd"
os: macos-13 # Intel macOS
build-options: "-Ddisable-ssl"

runs-on: ${{ matrix.os }}

Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Provides a package to be used by the zig package manager for C programs.

| Architecture \ OS | Linux | MacOS |
|:------------------|:------|-------------------|
| x86_64 | ✅ | ? |
| arm 64 | | ☑️ `-Ddisable-ssl` |
| x86_64 | ✅ | ☑️ `-Ddisable-ssl` |
| arm 64 | __?__ | ☑️ `-Ddisable-ssl` |

Optional dependencies used by default:
- openssl
Expand All @@ -25,11 +25,7 @@ Then, in your `build.zig`:
```zig
const postgres = b.dependency("libpq", { .target = target, .optimize = optimize });
const libpq = postgres.artifact("pq");
const libpgcommon = postgres.artifact("pgcommon");
const libpgport = postgres.artifact("pgport");

// wherever needed:
exe.linkLibrary(libpq);
exe.linkLibrary(libpgcommon);
exe.linkLibrary(libpgport);
```
81 changes: 33 additions & 48 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,61 +37,53 @@ pub fn build(b: *std.Build) !void {
default_paths,
);

const libpq = b.addStaticLibrary(.{ .name = "pq", .target = target, .optimize = optimize });
const libport = b.addStaticLibrary(.{ .name = "pgport", .target = target, .optimize = optimize });
const common = b.addStaticLibrary(.{ .name = "pgcommon", .target = target, .optimize = optimize });
const lib = b.addStaticLibrary(.{ .name = "pq", .target = target, .optimize = optimize });

libpq.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path(libpq_path),
.files = &libpq_sources,
.flags = &CFLAGS,
});
libport.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path("src/port"),
.files = &libport_sources,
.flags = &CFLAGS,
});
common.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path("src/common"),
.files = &common_sources,
.flags = &CFLAGS,
});

const config_headers = [_]*std.Build.Step.ConfigHeader{ config_ext, pg_config, config_os };
const libs = [_]*std.Build.Step.Compile{ libpq, libport, common };

for (libs) |lib| {
lib.addIncludePath(upstream.path("src/include"));
lib.addIncludePath(b.path("include"));
lib.addConfigHeader(config_path);
lib.root_module.addCMacro("_GNU_SOURCE", "1");
lib.root_module.addCMacro("FRONTEND", "1");
lib.linkLibC();
b.installArtifact(lib);
}

lib.addIncludePath(upstream.path("src/include"));
lib.addIncludePath(b.path("include"));
lib.addConfigHeader(config_path);
lib.root_module.addCMacro("_GNU_SOURCE", "1");
lib.root_module.addCMacro("FRONTEND", "1");
lib.linkLibC();
b.installArtifact(lib);

for (config_headers) |header| {
for (libs) |lib| {
lib.addConfigHeader(header);
}
common.installConfigHeader(header);
lib.addConfigHeader(header);
lib.installConfigHeader(header);
}

if (!disable_ssl) {
if (b.lazyDependency("openssl", .{ .target = target, .optimize = optimize })) |openssl_dep| {
const openssl = openssl_dep.artifact("openssl");
for (libs) |lib| {
lib.linkLibrary(openssl);
}
lib.linkLibrary(openssl);
}
libpq.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path(libpq_path),
.files = &.{
"fe-secure-common.c",
"fe-secure-openssl.c",
},
.flags = &CFLAGS,
});
common.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path("src/common"),
.files = &.{
"cryptohash_openssl.c",
Expand All @@ -101,7 +93,7 @@ pub fn build(b: *std.Build) !void {
.flags = &CFLAGS,
});
} else {
common.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path("src/common"),
.files = &.{
"cryptohash.c",
Expand Down Expand Up @@ -132,21 +124,15 @@ pub fn build(b: *std.Build) !void {

if (!disable_zlib) {
if (b.lazyDependency("zlib", .{ .target = target, .optimize = optimize })) |zlib_dep| {
const zlib = zlib_dep.artifact("z");
for (libs) |lib| {
lib.linkLibrary(zlib);
}
lib.linkLibrary(zlib_dep.artifact("z"));
}
}
const use_z: ?u8 = if (disable_zlib) null else 1;
pg_config.addValues(.{ .HAVE_LIBZ = use_z });

if (!disable_zstd) {
if (b.lazyDependency("zstd", .{ .target = target, .optimize = optimize })) |zstd_dep| {
const zstd = zstd_dep.artifact("zstd");
for (libs) |lib| {
lib.linkLibrary(zstd);
}
lib.linkLibrary(zstd_dep.artifact("zstd"));
}
}
const use_zstd: ?u8 = if (disable_zstd) null else 1;
Expand All @@ -157,7 +143,7 @@ pub fn build(b: *std.Build) !void {

const have_strlcat: bool = target.result.os.tag == .macos; // or linux with glibc >= 2.38, how can I test that ?
if (!have_strlcat) {
libport.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = upstream.path("src/port"),
.files = &.{
"strlcat.c",
Expand Down Expand Up @@ -201,22 +187,22 @@ pub fn build(b: *std.Build) !void {
.HAVE_MEMSET_S = 1,
.HAVE_SYS_UCRED_H = 1,
});
libport.addCSourceFile(.{
lib.addCSourceFile(.{
.file = upstream.path("src/port/explicit_bzero.c"),
.flags = &CFLAGS,
});
} else return error.ConfigUnknown;

// Export public headers
libpq.installHeadersDirectory(
lib.installHeadersDirectory(
upstream.path(libpq_path),
"",
.{ .include_extensions = &.{
"libpq-fe.h",
"libpq-events.h",
} },
);
libpq.installHeadersDirectory(
lib.installHeadersDirectory(
upstream.path(libpq_path),
"postgresql/internal",
.{ .include_extensions = &.{
Expand All @@ -225,13 +211,13 @@ pub fn build(b: *std.Build) !void {
"pqexpbuffer.h",
} },
);
libpq.installHeader(upstream.path("src/include/libpq/libpq-fs.h"), "libpq/libpq-fs.h");
libpq.installHeader(upstream.path("src/include/libpq/pqcomm.h"), "postgresql/internal/libpq/pqcomm.h");
common.installHeader(upstream.path("src/include/pg_config_manual.h"), "pg_config_manual.h");
common.installHeader(upstream.path("src/include/postgres_ext.h"), "postgres_ext.h");
common.installHeader(upstream.path("src/include/postgres_fe.h"), "postgresql/internal/postgres_fe.h");
libport.installHeader(upstream.path("src/include/c.h"), "postgresql/internal/c.h");
libport.installHeader(upstream.path("src/include/port.h"), "postgresql/internal/port.h");
lib.installHeader(upstream.path("src/include/libpq/libpq-fs.h"), "libpq/libpq-fs.h");
lib.installHeader(upstream.path("src/include/libpq/pqcomm.h"), "postgresql/internal/libpq/pqcomm.h");
lib.installHeader(upstream.path("src/include/pg_config_manual.h"), "pg_config_manual.h");
lib.installHeader(upstream.path("src/include/postgres_ext.h"), "postgres_ext.h");
lib.installHeader(upstream.path("src/include/postgres_fe.h"), "postgresql/internal/postgres_fe.h");
lib.installHeader(upstream.path("src/include/c.h"), "postgresql/internal/c.h");
lib.installHeader(upstream.path("src/include/port.h"), "postgresql/internal/port.h");

// Build executables to ensure no symbols are left undefined
const test_step = b.step("examples", "Build example programs");
Expand All @@ -251,8 +237,7 @@ pub fn build(b: *std.Build) !void {
const tests = [_]*std.Build.Step.Compile{ test1, test2, test3, test4, test5 };
for (tests) |t| {
t.linkLibC();
for (libs) |lib|
t.linkLibrary(lib);
t.linkLibrary(lib);
const install_test = b.addInstallArtifact(t, .{});
test_step.dependOn(&install_test.step);
}
Expand Down