From 2b135d87f5c1f1f307984f5fd78e151f5ee99dae Mon Sep 17 00:00:00 2001 From: zachir Date: Mon, 11 Mar 2024 01:34:22 -0500 Subject: Replace build system with zig Zig is actually handling the building and such, now the Makefile is here just as a wrapper for it. --- .gitignore | 8 +++---- Makefile | 16 ++++--------- build.zig | 28 ++++++++++++++++++++++ include/main.h | 26 ++++++++++++++++++++ main.c | 76 ---------------------------------------------------------- main.h | 26 -------------------- src/main.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 118 deletions(-) create mode 100644 build.zig create mode 100644 include/main.h delete mode 100644 main.c delete mode 100644 main.h create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore index 4c6f5e9..4028033 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -# Ignore object files -*.o +# Ignore Zig build dirs +zig-cache/ -#Ignore the compiled binary -even +# Ignore the compiled binary +zig-out/ diff --git a/Makefile b/Makefile index 1879a14..1336403 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,8 @@ -C_SRC := main.c -C_OBJ := main.o -B_NAM := even -CC := gcc -C_FLG := -Os -Wall -Wpedantic -Werror -O_FLG := ${C_FLG} +ZIG_ARGS := -Doptimize=ReleaseSmall -${B_NAM}: ${C_OBJ} - ${CC} ${O_FLG} -o ${B_NAM} ${C_OBJ} - -main.o: main.c main.h - ${CC} ${C_FLG} -c main.c +all: + zig build ${ZIG_ARGS} clean: - rm -f ${B_NAM} ${C_OBJ} + rm -rf zig-out/ zig-cache/ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..4fba9b6 --- /dev/null +++ b/build.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "even", + .target = target, + .optimize = optimize, + }); + exe.addIncludePath(.{ + .path = "include", + }); + exe.linkLibC(); + exe.addCSourceFiles(&.{ + "src/main.c", + }, &.{ + "-pedantic", + "-Wall", + "-Werror", + "-W", + "-Wno-missing-field-initializers", + "--std=c11", + }); + + b.installArtifact(exe); +} diff --git a/include/main.h b/include/main.h new file mode 100644 index 0000000..d6d758e --- /dev/null +++ b/include/main.h @@ -0,0 +1,26 @@ +// Copyright (c) 2024 Zachary Smith +#ifndef MAIN_H_ +#define MAIN_H_ + +#include +#include +#include +#include +#include + +#include +#include + +#define VERSION "1.0.0" + +enum return_t { + SUCCESS = 0, + ERR +}; + +void printhelp(void); +void printcopyright(void); +void printversion(void); +int main(int, char**); + +#endif // MAIN_H_ diff --git a/main.c b/main.c deleted file mode 100644 index d986924..0000000 --- a/main.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "./main.h" - -void printhelp() { - fprintf(stderr, "even: a binary by ZachIR to check if a number is even or "); - fprintf(stderr, "odd.\n"); - fprintf(stderr, "even [-hcv]\n"); - fprintf(stderr, "even [--help]\n"); - fprintf(stderr, "even [--copyright]\n"); - fprintf(stderr, "even [--version]\n"); - fprintf(stderr, "even [-q] [--quiet] NUMBER\n"); - fprintf(stderr, "\t-h: prints this help message\n"); - fprintf(stderr, "\t-c: prints the copyright information\n"); - fprintf(stderr, "\t-v: prints the version\n"); - fprintf(stderr, "\t-q: just uses error codes, no text\n"); - return; -} - -void printcopyright() { - fprintf(stderr, "Copyright (c) 2024 Zachary Smith\n"); - fprintf(stderr, "even is Free software under the MIT license, and is "); - fprintf(stderr, "provided without warranty of any kind.\n"); - return; -} - -void printversion() { - fprintf(stderr, "even version %s, 2024\n", VERSION); - return; -} - -int main(int argc, char **argv) { - bool quiet; - bool value; - long result; - - quiet = false; - value = false; - result = 0; - char *output; - - if (argc > 1) { - for (size_t i = 1; i < argc; i++) { - if (argv[i][0] == '-') { - switch (argv[i][1]) { - case 'h': - printhelp(); - return SUCCESS; - case 'c': - printcopyright(); - return SUCCESS; - case 'v': - printversion(); - return SUCCESS; - case 'q': - quiet = true; - break; - } - } - result = strtol(argv[i], &output, 10); - value = true; - } - } - if (value) { - if (result % 2 == 0) { - if (quiet) { - fprintf(stdout, "even\n"); - } - return 0; - } else { - if (quiet) { - fprintf(stdout, "odd\n"); - } - return 1; - } - } - return ERR; -} diff --git a/main.h b/main.h deleted file mode 100644 index d6d758e..0000000 --- a/main.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2024 Zachary Smith -#ifndef MAIN_H_ -#define MAIN_H_ - -#include -#include -#include -#include -#include - -#include -#include - -#define VERSION "1.0.0" - -enum return_t { - SUCCESS = 0, - ERR -}; - -void printhelp(void); -void printcopyright(void); -void printversion(void); -int main(int, char**); - -#endif // MAIN_H_ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..0e06431 --- /dev/null +++ b/src/main.c @@ -0,0 +1,76 @@ +#include "./main.h" + +void printhelp(void) { + fprintf(stderr, "even: a binary by ZachIR to check if a number is even or "); + fprintf(stderr, "odd.\n"); + fprintf(stderr, "even [-hcv]\n"); + fprintf(stderr, "even [--help]\n"); + fprintf(stderr, "even [--copyright]\n"); + fprintf(stderr, "even [--version]\n"); + fprintf(stderr, "even [-q] [--quiet] NUMBER\n"); + fprintf(stderr, "\t-h: prints this help message\n"); + fprintf(stderr, "\t-c: prints the copyright information\n"); + fprintf(stderr, "\t-v: prints the version\n"); + fprintf(stderr, "\t-q: just uses error codes, no text\n"); + return; +} + +void printcopyright(void) { + fprintf(stderr, "Copyright (c) 2024 Zachary Smith\n"); + fprintf(stderr, "even is Free software under the MIT license, and is "); + fprintf(stderr, "provided without warranty of any kind.\n"); + return; +} + +void printversion(void) { + fprintf(stderr, "even version %s, 2024\n", VERSION); + return; +} + +int main(int argc, char **argv) { + bool quiet; + bool value; + long result; + + quiet = false; + value = false; + result = 0; + char *output; + + if (argc > 1) { + for (int i = 1; i < argc; i++) { + if (argv[i][0] == '-') { + switch (argv[i][1]) { + case 'h': + printhelp(); + return SUCCESS; + case 'c': + printcopyright(); + return SUCCESS; + case 'v': + printversion(); + return SUCCESS; + case 'q': + quiet = true; + break; + } + } + result = strtol(argv[i], &output, 10); + value = true; + } + } + if (value) { + if (result % 2 == 0) { + if (quiet) { + fprintf(stdout, "even\n"); + } + return 0; + } else { + if (quiet) { + fprintf(stdout, "odd\n"); + } + return 1; + } + } + return ERR; +} -- cgit v1.2.3