I am working on an zig library called zfitsio
which wraps around the c library cfitsio
. During the build process of zfitsio, the cfitsio library will be built. For that build process is important that it can access fitsio.h and longnam.h.
The header files do not get “transported” from the building proccess of cfitsio to and test project.
zfitsio build.zig
const std = @import("std");
const cfitsio = @import("libs/cfitsio.zig");
const zlib = @import("libs/zlib.zig");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const libcfitsio = cfitsio.create(b, target, optimize) orelse return;
const libzlib = zlib.create(b, target, optimize) orelse return;
libcfitsio.linkLibrary(libzlib);
b.installArtifact(libcfitsio);
const lib = b.addStaticLibrary(.{
.name = "zfitsio",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
lib.addIncludePath(b.path("libs/cfitsio"));
lib.installHeadersDirectory(b.path("libs"), "", .{});
lib.linkLibrary(libcfitsio);
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
_ = b.addModule("zfitsio", .{
.root_source_file = b.path("src/root.zig"),
});
const exe = b.addExecutable(.{
.name = "zfitsio",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.addIncludePath(b.path("libs/cfitsio"));
exe.linkLibrary(lib);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}
libs/cfitsio.zig which builds cfitsio from source:
const std = @import("std");
const srcs = &.{
"buffers.c",
"cfileio.c",
"checksum.c",
"drvrfile.c",
"drvrmem.c",
"drvrnet.c",
"drvrsmem.c",
"editcol.c",
"edithdu.c",
"fitscore.c",
"fits_hcompress.c",
"fits_hdecompress.c",
"getcol.c",
"getcolb.c",
"getcold.c",
"getcole.c",
"getcoli.c",
"getcolj.c",
"getcolk.c",
"getcoll.c",
"getcolsb.c",
"getcols.c",
"getcolui.c",
"getcoluj.c",
"getcoluk.c",
"getkey.c",
"group.c",
"grparser.c",
"histo.c",
"imcompress.c",
"iraffits.c",
"modkey.c",
"pliocomp.c",
"putcol.c",
"putcolb.c",
"putcold.c",
"putcole.c",
"putcoli.c",
"putcolj.c",
"putcolk.c",
"putcoll.c",
"putcolsb.c",
"putcols.c",
"putcolu.c",
"putcolui.c",
"putcoluj.c",
"putcoluk.c",
"putkey.c",
"quantize.c",
"region.c",
"ricecomp.c",
"scalnull.c",
"simplerng.c",
"swapproc.c",
"wcssub.c",
"wcsutil.c",
"zcompress.c",
"zuncompress.c",
"eval_f.c",
"eval_y.c",
"eval_l.c",
};
pub fn create(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{
.name = "cfitsio",
.target = target,
.optimize = optimize,
.link_libc = true,
});
const cfitsio_dep = b.lazyDependency("cfitsio", .{
.target = target,
.optimize = optimize,
}) orelse return null;
inline for (srcs) |s| {
lib.addCSourceFile(.{
.file = cfitsio_dep.path(s),
.flags = &.{ "-std=c11", "-D_POSIX_C_SOURCE=200809L" },
});
}
// Install headers for `cfitsio`
lib.installHeader(cfitsio_dep.path("fitsio.h"), "fitsio.h");
lib.installHeader(cfitsio_dep.path("longnam.h"), "longnam.h");
b.installArtifact(lib);
return lib;
}
zlib.zig is very similiar to cfitsio.zig.
I feel like there is a problem with how the header file are installed.
My goal is to be able to fetch using zig fetch --save and then getting an successful build of cfitsio, zlib and of course zfitsio.
When building on an test project:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "zigfit",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "zigfit",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.linkLibC();
const zfitsio = b.dependency("zfitsio", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zfitsio", zfitsio.module("zfitsio"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}
it can not find the fitsio.h (and longnam.h) file(s).
PS C:\Users\chris\zigfit> zig build
install
└─ install zigfit
└─ zig build-exe zigfit Debug native 2 errors
C:\Users\chris\AppData\Local\zig\p\1220737aac01cf456c106e58841fea86ea1710be734012ffe164e3388c9edaee5214\src\utility.zig:2:15: error: C import failed
pub const c = @cImport({
^~~~~~~~
referenced by:
c: C:\Users\chris\AppData\Local\zig\p\1220737aac01cf456c106e58841fea86ea1710be734012ffe164e3388c9edaee5214\src\fitsfile.zig:2:33
FitsFile: C:\Users\chris\AppData\Local\zig\p\1220737aac01cf456c106e58841fea86ea1710be734012ffe164e3388c9edaee5214\src\fitsfile.zig:20:12
remaining reference traces hidden; use '-freference-trace' to see all reference traces
C:\Users\chris\zigfit\.zig-cache\o\a8dea7ca85043e9ef0883cbf1ab26596\cimport.h:1:10: error: 'fitsio.h' file not found
#include <fitsio.h>
^
error: the following command failed with 2 compilation errors:
C:\ProgramData\chocolatey\lib\zig\tools\zig-windows-x86_64-0.13.0\zig.exe build-exe -ODebug -I C:\Users\chris\zigfit\inc --dep zfitsio -Mroot=C:\Users\chris\zigfit\src\main.zig -Mzfitsio=C:\Users\chris\AppData\Local\zig\p\1220737aac01cf456c106e58841fea86ea1710be734012ffe164e3388c9edaee5214\src\root.zig -lc --cache-dir C:\Users\chris\zigfit\.zig-cache --global-cache-dir C:\Users\chris\AppData\Local\zig --name zigfit --listen=-
Build Summary: 2/5 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install zigfit transitive failure
└─ zig build-exe zigfit Debug native 2 errors
error: the following build command failed with exit code 1:
C:\Users\chris\zigfit\.zig-cache\o\411aa523833ea9ec9920c9b9319f6722\build.exe C:\ProgramData\chocolatey\lib\zig\tools\zig-windows-x86_64-0.13.0\zig.exe C:\Users\chris\zigfit C:\Users\chris\zigfit\.zig-cache C:\Users\chris\AppData\Local\zig --seed 0xc060c86c -Z7eb98b65123e5deb
PS C:\Users\chris\zigfit>