Skip to content

Commit 3a45e81

Browse files
committedNov 14, 2024
portet old version, improved API and overall organization
0 parents  commit 3a45e81

12 files changed

+4580
-0
lines changed
 

‎.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.zig-cache/
2+
zig-out/
3+
/test_data
4+
*.exe
5+
*.pdb
6+
*.obj
7+
*.exp
8+
*.lib
9+
/target
10+
*.fit
11+
*.fits

‎build.zig

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
const std = @import("std");
2+
const this = @This();
3+
4+
fn getModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
5+
if (b.modules.contains("zfitsio")) {
6+
return b.modules.get("zfitsio").?;
7+
}
8+
return b.addModule("zfitsio", .{
9+
.root_source_file = b.path("src/root.zig"),
10+
.target = target,
11+
.optimize = optimize,
12+
});
13+
}
14+
15+
pub fn build(b: *std.Build) !void {
16+
const target = b.standardTargetOptions(.{});
17+
const optimize = b.standardOptimizeOption(.{});
18+
const use_system_libs = b.option(bool, "use-system-libs", "Use system-installed libraries instead of building from source") orelse false;
19+
20+
const zlib_lib = if (!use_system_libs)
21+
createZlib(b, target, optimize) orelse unreachable
22+
else
23+
null;
24+
25+
const cfitsio_lib = if (!use_system_libs) blk: {
26+
const lib = createCfitsio(b, target, optimize) orelse unreachable;
27+
if (zlib_lib) |zl| lib.linkLibrary(zl);
28+
break :blk lib;
29+
} else null;
30+
31+
const zfitsio_lib = b.addStaticLibrary(.{
32+
.name = "zfitsio",
33+
.root_source_file = b.path("src/root.zig"),
34+
.target = target,
35+
.optimize = optimize,
36+
});
37+
38+
if (cfitsio_lib) |cfits| {
39+
zfitsio_lib.linkLibrary(cfits);
40+
if (zlib_lib) |zl| zfitsio_lib.linkLibrary(zl);
41+
} else {
42+
zfitsio_lib.linkSystemLibrary("cfitsio");
43+
zfitsio_lib.linkSystemLibrary("z");
44+
}
45+
46+
b.installArtifact(zfitsio_lib);
47+
48+
const zfitsio = this.getModule(b, target, optimize);
49+
50+
const fitsfile_tests = b.addTest(.{
51+
.root_source_file = b.path("src/fitsfile.zig"),
52+
.target = target,
53+
.optimize = optimize,
54+
});
55+
56+
fitsfile_tests.linkLibC(); // Add this
57+
58+
const wrapper = b.addModule("wrapper", .{
59+
.root_source_file = b.path("src/wrapper.zig"),
60+
});
61+
fitsfile_tests.root_module.addImport("wrapper", wrapper);
62+
63+
if (cfitsio_lib) |lib| {
64+
fitsfile_tests.linkLibrary(lib);
65+
if (zlib_lib) |zl| fitsfile_tests.linkLibrary(zl);
66+
} else {
67+
fitsfile_tests.linkSystemLibrary("cfitsio");
68+
fitsfile_tests.linkSystemLibrary("z");
69+
}
70+
71+
const run_fitsfile_tests = b.addRunArtifact(fitsfile_tests);
72+
const test_step = b.step("test", "Run unit tests");
73+
74+
test_step.dependOn(&run_fitsfile_tests.step);
75+
76+
const examples = [_]struct {
77+
name: []const u8,
78+
path: []const u8,
79+
}{
80+
.{ .name = "read_fits", .path = "examples/read_fits.zig" },
81+
.{ .name = "write_fits", .path = "examples/write_fits.zig" },
82+
.{ .name = "modify_header", .path = "examples/modify_header.zig" },
83+
.{ .name = "basic_fits", .path = "examples/basic_fits.zig" },
84+
};
85+
86+
const examples_step = b.step("examples", "Build examples");
87+
88+
for (examples) |ex| {
89+
const exe = b.addExecutable(.{
90+
.name = ex.name,
91+
.root_source_file = b.path(ex.path),
92+
.target = target,
93+
.optimize = optimize,
94+
});
95+
96+
exe.linkLibC();
97+
if (cfitsio_lib) |lib| {
98+
exe.linkLibrary(lib);
99+
if (zlib_lib) |zl| exe.linkLibrary(zl);
100+
} else {
101+
exe.linkSystemLibrary("cfitsio");
102+
exe.linkSystemLibrary("z");
103+
}
104+
105+
exe.root_module.addImport("zfitsio", zfitsio);
106+
107+
examples_step.dependOn(&exe.step);
108+
109+
const run_cmd = b.addRunArtifact(exe);
110+
const run_step = b.step(ex.name, b.fmt("Run the {s} example", .{ex.name}));
111+
run_step.dependOn(&run_cmd.step);
112+
}
113+
}
114+
115+
pub fn createZlib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Step.Compile {
116+
const lib = b.addStaticLibrary(.{
117+
.name = "zlib",
118+
.target = target,
119+
.optimize = optimize,
120+
.link_libc = true,
121+
});
122+
const zlib_dep = b.lazyDependency("zlib", .{
123+
.target = target,
124+
.optimize = optimize,
125+
}) orelse return null;
126+
127+
const srcs = &.{
128+
"adler32.c",
129+
"compress.c",
130+
"crc32.c",
131+
"deflate.c",
132+
"gzclose.c",
133+
"gzlib.c",
134+
"gzread.c",
135+
"gzwrite.c",
136+
"inflate.c",
137+
"infback.c",
138+
"inftrees.c",
139+
"inffast.c",
140+
"trees.c",
141+
"uncompr.c",
142+
"zutil.c",
143+
};
144+
145+
inline for (srcs) |src| {
146+
lib.addCSourceFile(.{
147+
.file = zlib_dep.path(src),
148+
.flags = &.{"-std=c89"},
149+
});
150+
}
151+
lib.installHeader(zlib_dep.path("zlib.h"), "zlib.h");
152+
lib.installHeader(zlib_dep.path("zconf.h"), "zconf.h");
153+
return lib;
154+
}
155+
156+
pub fn createCfitsio(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Step.Compile {
157+
const lib = b.addStaticLibrary(.{
158+
.name = "cfitsio",
159+
.target = target,
160+
.optimize = optimize,
161+
.link_libc = true,
162+
});
163+
const cfitsio_dep = b.lazyDependency("cfitsio", .{
164+
.target = target,
165+
.optimize = optimize,
166+
}) orelse return null;
167+
168+
const srcs = &.{
169+
"buffers.c",
170+
"cfileio.c",
171+
"checksum.c",
172+
"drvrfile.c",
173+
"drvrmem.c",
174+
"drvrnet.c",
175+
"drvrsmem.c",
176+
"editcol.c",
177+
"edithdu.c",
178+
"fitscore.c",
179+
"fits_hcompress.c",
180+
"fits_hdecompress.c",
181+
"getcol.c",
182+
"getcolb.c",
183+
"getcold.c",
184+
"getcole.c",
185+
"getcoli.c",
186+
"getcolj.c",
187+
"getcolk.c",
188+
"getcoll.c",
189+
"getcolsb.c",
190+
"getcols.c",
191+
"getcolui.c",
192+
"getcoluj.c",
193+
"getcoluk.c",
194+
"getkey.c",
195+
"group.c",
196+
"grparser.c",
197+
"histo.c",
198+
"imcompress.c",
199+
"iraffits.c",
200+
"modkey.c",
201+
"pliocomp.c",
202+
"putcol.c",
203+
"putcolb.c",
204+
"putcold.c",
205+
"putcole.c",
206+
"putcoli.c",
207+
"putcolj.c",
208+
"putcolk.c",
209+
"putcoll.c",
210+
"putcolsb.c",
211+
"putcols.c",
212+
"putcolu.c",
213+
"putcolui.c",
214+
"putcoluj.c",
215+
"putcoluk.c",
216+
"putkey.c",
217+
"quantize.c",
218+
"region.c",
219+
"ricecomp.c",
220+
"scalnull.c",
221+
"simplerng.c",
222+
"swapproc.c",
223+
"wcssub.c",
224+
"wcsutil.c",
225+
"zcompress.c",
226+
"zuncompress.c",
227+
"eval_f.c",
228+
"eval_y.c",
229+
"eval_l.c",
230+
};
231+
232+
inline for (srcs) |s| {
233+
lib.addCSourceFile(.{
234+
.file = cfitsio_dep.path(s),
235+
.flags = &.{ "-std=c11", "-D_POSIX_C_SOURCE=200809L" },
236+
});
237+
}
238+
lib.installHeader(cfitsio_dep.path("fitsio.h"), "fitsio.h");
239+
lib.installHeader(cfitsio_dep.path("longnam.h"), "longnam.h");
240+
return lib;
241+
}

‎build.zig.zon

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = "zfitsio",
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "0.0.0",
14+
15+
// This field is optional.
16+
// This is currently advisory only; Zig does not yet do anything
17+
// with this value.
18+
//.minimum_zig_version = "0.11.0",
19+
20+
// This field is optional.
21+
// Each dependency must either provide a `url` and `hash`, or a `path`.
22+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
23+
// Once all dependencies are fetched, `zig build` no longer requires
24+
// internet connectivity.
25+
.dependencies = .{
26+
.cfitsio = .{
27+
.url = "https://github.com/HEASARC/cfitsio/archive/refs/tags/cfitsio4_5_0_20240826.tar.gz",
28+
.hash = "12206640d8d65740b10111f5febda8478caf055f19480c4b590211102f40f5ab3d86",
29+
},
30+
.zlib = .{
31+
.url = "https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz",
32+
.hash = "1220c1854d7b4c2b5cbdd8ce6593c37e4bf1ac4032664735e1537c3848becb3b5834",
33+
},
34+
},
35+
.paths = .{
36+
"build.zig",
37+
"build.zig.zon",
38+
"src",
39+
// For example...
40+
//"LICENSE",
41+
//"README.md",
42+
},
43+
}

‎examples/basic_fits.zig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const std = @import("std");
2+
const fits = @import("zfitsio");
3+
4+
pub fn main() !void {
5+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6+
defer _ = gpa.deinit();
7+
const allocator = gpa.allocator();
8+
9+
var fits_file = try fits.openFits(allocator, "examples/data/sample.fit", fits.Mode.READ_ONLY);
10+
defer fits_file.close() catch |err| {
11+
std.debug.print("Error closing file: {}\n", .{err});
12+
};
13+
14+
const dimensions = try fits_file.getImageDimensions();
15+
std.debug.print("Image dimensions: {d}x{d}\n", .{ dimensions[0], dimensions[1] });
16+
}

‎examples/modify_header.zig

Whitespace-only changes.

‎examples/read_fits.zig

Whitespace-only changes.

‎examples/write_fits.zig

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.