From a6a16afedef3999d9765697821e1d99c67252df2 Mon Sep 17 00:00:00 2001 From: Jora Troosh Date: Tue, 16 Jul 2024 13:22:00 +0300 Subject: [PATCH] init --- .gitattributes | 2 + .github/workflows/ci.yaml | 37 ++++++++++ .gitignore | 2 + LICENSE | 21 ++++++ README.md | 32 +++++++++ build.zig | 144 ++++++++++++++++++++++++++++++++++++++ build.zig.zon | 18 +++++ src/lib.zig | 3 + 8 files changed, 259 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yaml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 src/lib.zig diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..515471b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.zig text eol=lf +*.zon text eol=lf diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..54f01ac --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,37 @@ +name: Continuous Integration + +on: + push: + branches: [main] + + pull_request: + branches: [main] + + workflow_dispatch: + +jobs: + install: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Zig + uses: mlugg/setup-zig@v1 + + - name: Run `install` + run: zig build install -Duse-curl -Duse-bz2 + + fmt: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Zig + uses: mlugg/setup-zig@v1 + + - name: Run `fmt` + run: zig build fmt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3389c86 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.zig-cache/ +zig-out/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3e5e1e4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Jora Troosh + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ca6dfd --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# cfitsio + +[![CI][ci-shd]][ci-url] +[![LC][lc-shd]][lc-url] + +## Zig build of [CFITSIO library](https://github.com/HEASARC/cfitsio). + +### :rocket: Usage + +- Add `cfitsio` dependency to `build.zig.zon`. + +```sh +zig fetch --save https://github.com/tensorush/cfitsio/archive/.tar.gz +``` + +- Use `cfitsio` as a module in your `build.zig`. + +```zig +const cfitsio_dep = b.dependency("cfitsio", .{ + .target = target, + .optimize = optimize, +}); +const cfitsio_mod = cfitsio_dep.module("cfitsio"); +.root_module.addImport("cfitsio", cfitsio_mod);; +``` + + + +[ci-shd]: https://img.shields.io/github/actions/workflow/status/tensorush/cfitsio/ci.yaml?branch=main&style=for-the-badge&logo=github&label=CI&labelColor=black +[ci-url]: https://github.com/tensorush/cfitsio/blob/main/.github/workflows/ci.yaml +[lc-shd]: https://img.shields.io/github/license/tensorush/cfitsio.svg?style=for-the-badge&labelColor=black +[lc-url]: https://github.com/tensorush/cfitsio/blob/main/LICENSE diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..ff3eb70 --- /dev/null +++ b/build.zig @@ -0,0 +1,144 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const version = std.SemanticVersion{ .major = 4, .minor = 4, .patch = 1 }; + + // Custom options + const use_curl = b.option(bool, "use-curl", "Enable remote file access") orelse false; + const use_bz2 = b.option(bool, "use-bz2", "Enable reading bzip2-compressed files") orelse false; + + // Dependencies + const cfitsio_dep = b.dependency("cfitsio", .{}); + const cfitsio_path = cfitsio_dep.path(""); + + // Library + const lib_step = b.step("lib", "Install library"); + + const lib = b.addStaticLibrary(.{ + .name = "cfitsio", + .target = target, + .version = version, + .optimize = optimize, + }); + + var flags = std.BoundedArray([]const u8, 4){}; + flags.appendSliceAssumeCapacity(&FLAGS); + if (use_curl) { + lib.linkSystemLibrary("curl"); + flags.appendAssumeCapacity("-DCFITSIO_HAVE_CURL"); + } + if (use_bz2) { + lib.linkSystemLibrary("bz2"); + flags.appendAssumeCapacity("-DHAVE_BZIP2=1"); + } + + lib.addCSourceFiles(.{ .root = cfitsio_path, .files = &SOURCES, .flags = flags.constSlice() }); + lib.installHeadersDirectory(cfitsio_path, "", .{ .include_extensions = &HEADERS }); + lib.linkSystemLibrary("z"); + lib.linkLibC(); + + const lib_install = b.addInstallArtifact(lib, .{}); + lib_step.dependOn(&lib_install.step); + b.default_step.dependOn(lib_step); + + // Module + const module = b.addModule("cfitsio", .{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("src/lib.zig"), + }); + module.linkLibrary(lib); + + // Formatting checks + const fmt_step = b.step("fmt", "Run formatting checks"); + + const fmt = b.addFmt(.{ + .paths = &.{ + "build.zig", + }, + .check = true, + }); + fmt_step.dependOn(&fmt.step); + b.default_step.dependOn(fmt_step); +} + +const HEADERS = .{ + "fitsio.h", + "fitsio2.h", + "longnam.h", + "drvrsmem.h", + "cfortran.h", + "f77_wrap.h", + "region.h", +}; + +const SOURCES = .{ + "buffers.c", + "cfileio.c", + "checksum.c", + "drvrfile.c", + "drvrmem.c", + "drvrnet.c", + "drvrsmem.c", + "editcol.c", + "edithdu.c", + "eval_l.c", + "eval_y.c", + "eval_f.c", + "fitscore.c", + "getcol.c", + "getcolb.c", + "getcold.c", + "getcole.c", + "getcoli.c", + "getcolj.c", + "getcolk.c", + "getcoll.c", + "getcols.c", + "getcolsb.c", + "getcoluk.c", + "getcolui.c", + "getcoluj.c", + "getkey.c", + "group.c", + "grparser.c", + "histo.c", + "iraffits.c", + "modkey.c", + "putcol.c", + "putcolb.c", + "putcold.c", + "putcole.c", + "putcoli.c", + "putcolj.c", + "putcolk.c", + "putcoluk.c", + "putcoll.c", + "putcols.c", + "putcolsb.c", + "putcolu.c", + "putcolui.c", + "putcoluj.c", + "putkey.c", + "region.c", + "scalnull.c", + "swapproc.c", + "wcssub.c", + "wcsutil.c", + "imcompress.c", + "quantize.c", + "ricecomp.c", + "pliocomp.c", + "fits_hcompress.c", + "fits_hdecompress.c", + "simplerng.c", + "zcompress.c", + "zuncompress.c", +}; + +const FLAGS = .{ + "-std=c89", + "-D_REENTRANT", +}; diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..67afcae --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,18 @@ +.{ + .name = "cfitsio", + .version = "4.4.1", + .minimum_zig_version = "0.13.0", + .dependencies = .{ + .cfitsio = .{ + .url = "https://github.com/HEASARC/cfitsio/archive/cfitsio4_4_1_20240617.tar.gz", + .hash = "1220d9b5104c0d9cd8158f52ce64d891eb76a083b43962aed62c611addb920e5a9bb", + }, + }, + .paths = .{ + "src/", + "build.zig", + "build.zig.zon", + "LICENSE", + "README.md", + }, +} diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000..78245df --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,3 @@ +pub const c = @cImport({ + @cInclude("fitsio/fitsio.h"); +});