|
| 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 | +} |
0 commit comments