build: hectic C: shared library in build

This commit is contained in:
2025-04-19 23:36:13 +00:00
parent 0abd4fc70e
commit 7ddceab11c
2 changed files with 33 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ stdenv.mkDerivation {
installPhase = ''
mkdir -p $out/lib $out/include $out/bin
cp target/libhectic.a $out/lib/
cp target/libhectic.so $out/lib/
cp hectic.h $out/include/
# Create hectic-config script
@@ -27,9 +28,10 @@ stdenv.mkDerivation {
#!/bin/sh
usage() {
echo "Usage: hectic-config [--cflags] [--libs]"
echo "Usage: hectic-config [--cflags] [--libs] [--static]"
echo " --cflags Print the compiler flags"
echo " --libs Print the linker flags"
echo " --libs Print the linker flags (dynamic library by default)"
echo " --static When used with --libs, use static linking"
echo " --help Display this help message"
exit \$1
}
@@ -38,13 +40,28 @@ stdenv.mkDerivation {
usage 1
fi
static=0
for arg in "\$@"; do
if [ "\$arg" = "--static" ]; then
static=1
fi
done
while [ \$# -gt 0 ]; do
case "\$1" in
--cflags)
echo "-I$out/include"
;;
--libs)
if [ \$static -eq 1 ]; then
echo "-L$out/lib -static -lhectic"
else
echo "-L$out/lib -lhectic"
fi
;;
--static)
# Already processed above
;;
--help)
usage 0

View File

@@ -54,6 +54,7 @@ esac
RUN_TESTS=1
OPTFLAGS="-O2"
CFLAGS="-Wall -Wextra -Werror -pedantic -fsanitize=address -fanalyzer"
LDFLAGS="-lhectic"
STD_FLAGS="-std=c99"
COLOR_FLAG=""
DEBUG=0
@@ -96,9 +97,18 @@ case "$MODE" in
build)
mkdir -p target
echo "# Build library"
# Build object file with position independent code for shared library
# shellcheck disable=SC2086
cc $CFLAGS $OPTFLAGS $STD_FLAGS -c hectic.c -o target/hectic.o
cc $CFLAGS $OPTFLAGS $STD_FLAGS -fPIC -c hectic.c -o target/hectic.o
# Create static library (.a)
echo "# Creating static library (libhectic.a)"
ar rcs target/libhectic.a target/hectic.o
# Create shared library (.so)
echo "# Creating shared library (libhectic.so)"
# shellcheck disable=SC2086
cc -shared -o target/libhectic.so target/hectic.o
;;
check)
mkdir -p target/test
@@ -137,7 +147,8 @@ case "$MODE" in
exe="target/test/$test_name"
echo "Building test: $test_name"
# shellcheck disable=SC2086
cc $CFLAGS $OPTFLAGS -I. "$test_file" -Ltarget -lhectic $LDFLAGS -o "$exe"
# Explicitly link with the static library to maintain original behavior
cc $CFLAGS $OPTFLAGS -I. "$test_file" -o "$exe" target/libhectic.a
if [ "$?" -ne 0 ]; then
exit 1
fi