From 0b5c556f84c469f7063d1fc78d6e2708c88f8002 Mon Sep 17 00:00:00 2001 From: Slendi Date: Sat, 26 Jul 2025 22:20:41 +0300 Subject: [PATCH] Add build script Signed-off-by: Slendi --- .gitignore | 7 ++++++- build.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/dcfg.c | 19 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100755 build.sh diff --git a/.gitignore b/.gitignore index 4f5f288..c595774 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ -[Bb]uild* +[Bb]uild*/ .cache /*.txt +*.o +*.so +*.a +BUILD +install diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..3cc12e6 --- /dev/null +++ b/build.sh @@ -0,0 +1,54 @@ +#!/bin/sh +set -e + +BUILD_DIR="BUILD" +INSTALL_DIR="$(pwd)/install" +CFLAGS="-std=gnu99 -Wall -Wextra -pedantic -Werror -Wno-newline-eof -Wno-language-extension-token" +BUILD_SHARED=1 +PTHREAD_SUPPORT=1 +POSIX_SUPPORT=1 +SRC_DIR="src" +INCLUDE_DIR="include" +OUTPUT_NAME="libdcfg" + +for arg in "$@"; do + case $arg in + --debug) CFLAGS="$CFLAGS -g -O0" ;; + --release) CFLAGS="$CFLAGS -O2" ;; + --no-pthread) PTHREAD_SUPPORT=0 ;; + --no-posix) POSIX_SUPPORT=0 ;; + --clean) rm -rf "$BUILD_DIR" "$INSTALL_DIR"; echo "Cleaned."; exit 0 ;; + esac +done + +# Setup directories +mkdir -p "$BUILD_DIR" "$INSTALL_DIR/lib" "$INSTALL_DIR/include" + +# Compiler and linker +CC=${CC:-cc} + +if [ "$PTHREAD_SUPPORT" -eq 1 ]; then + CFLAGS="$CFLAGS -DDCFG_PTHREAD_SUPPORT" + LIBS="$LIBS -lpthread" +fi +if [ "$POSIX_SUPPORT" -eq 1 ]; then + CFLAGS="$CFLAGS -DDCFG_POSIX_SUPPORT" +fi + +echo "Building DCFG..." +cd "$BUILD_DIR" + +set -x +$CC $CFLAGS -fPIC -shared "../$SRC_DIR/dcfg.c" -I"../$INCLUDE_DIR" -o "$OUTPUT_NAME.so" $LIBS + +$CC $CFLAGS -c "../$SRC_DIR/dcfg.c" -I"../$INCLUDE_DIR" +ar rcs "$OUTPUT_NAME.a" dcfg.o + +set +x + +echo "Installing..." +cp -r "../$INCLUDE_DIR/"* "$INSTALL_DIR/include/" +cp "$OUTPUT_NAME.so" "$INSTALL_DIR/lib/" +cp "$OUTPUT_NAME.a" "$INSTALL_DIR/lib/" + +echo "Done. Installed to $INSTALL_DIR" diff --git a/src/dcfg.c b/src/dcfg.c index 0c72223..cc76219 100644 --- a/src/dcfg.c +++ b/src/dcfg.c @@ -169,6 +169,8 @@ struct dcfg_Value { Instance *instance; dcfg_ValueType type; SourceLocation location; + int i_sourcev_idx; + int i_source_pathv_idx; union { int64_t i; @@ -194,6 +196,9 @@ struct dcfg_Instance { dcfg_FtellFn ftell; char last_error[256]; + + StringView *sourcev; // Strings should be freed. + StringView *source_pathv; // Strings should be freed. }; #define ALLOC(sz) (instance->alloc((sz))) @@ -248,6 +253,9 @@ dcfg_Instance *dcfg_make_instance(dcfg_InstanceCreateInfo const *create_info) assert(instance->fseek); assert(instance->ftell); + instance->sourcev = vector_create(); + instance->source_pathv = vector_create(); + return instance; } @@ -255,6 +263,9 @@ void dcfg_destroy_instance(dcfg_Instance *instance) { assert(instance); + vector_free(instance->source_pathv); + vector_free(instance->sourcev); + pthread_mutex_lock(&instance->mtx); { // De-init other instance things } @@ -1109,8 +1120,8 @@ dcfg_Value *dcfg_parse(dcfg_Instance *instance, dcfg_StringView const file_path) StringView str = { .size = size, + .data = ALLOC(size), }; - str.data = ALLOC(size); if (!fread((void *)str.data, str.size, 1, fp)) { FREE(abs); FREE((void *)str.data); @@ -1150,6 +1161,12 @@ dcfg_Value *dcfg_parse(dcfg_Instance *instance, dcfg_StringView const file_path) return NULL; } + vector_add(&instance->sourcev, str); + vector_add(&instance->source_pathv, abs_sv); + + v->i_sourcev_idx = vector_size(instance->sourcev) - 1; + v->i_source_pathv_idx = vector_size(instance->source_pathv) - 1; + return v; }