Skip to content

Commit

Permalink
Support for compiling as shared lib (#239)
Browse files Browse the repository at this point in the history
* update configure and src/Makefile to support shared-library option
  • Loading branch information
liquidaty authored Oct 25, 2024
1 parent a851a46 commit 40d5ec5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
5 changes: 4 additions & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ ${YAJL_HELPER_OBJ}: external/yajl_helper/yajl_helper.c
@mkdir -p `dirname "$@"`
${CC} ${CFLAGS} ${YAJL_INCLUDE} ${YAJL_HELPER_INCLUDE} -c $< -o $@

check test: test-standalone test-cli
check test: test-standalone test-cli test-shared-lib

test-shared-lib:
@echo "TO DO: create tests for using shared library"

test-standalone:
@${MAKE} -C test test QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG}
Expand Down
17 changes: 15 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Optional features:
--force-avx force compile with (no CPU check) or without -mavx [auto]
--force-sse2 force compile with (no CPU check) or without -msse2 [auto]
--enable-sheet compile with 'sheet' feature (requires ncurses) [yes]
--enable-shared build/install zsv shared library
--ncurses-dynamic compile without NCURSES_STATIC or NCURSESW_STATIC defines
--enable-lto compile with LTO (works with some but not all platforms/compilers) [no]
--enable-whole-program compile without -fwhole-program even if no -flto [yes]
Expand Down Expand Up @@ -312,6 +313,7 @@ MANDIR="$PREFIX/share/man"
MINIMAL=no

TRY_SHEET=auto
BUILD_SHAREDLIB=0
TRY_LTO=no
TRY_WHOLE_PROGRAM=auto
FORCE_AVX2=auto
Expand Down Expand Up @@ -359,6 +361,9 @@ for arg ; do
--enable-sheet=auto) TRY_SHEET=auto;;
--disable-sheet|--enable-sheet=no) TRY_SHEET=no;;

--enable-shared|--enable-shared=yes) BUILD_SHAREDLIB=1;;
--disable-shared|--enable-shared=no) BUILD_SHAREDLIB=0;;

--enable-lto|--enable-lto=yes) TRY_LTO=yes;;
--enable-lto=auto) TRY_LTO=auto;;
--disable-lto|--enable-lto=no) TRY_LTO=no;;
Expand Down Expand Up @@ -653,7 +658,7 @@ elif [ "$TRY_WHOLE_PROGRAM" = "auto" ] ; then
fi
fi

tryflag CFLAGS_OPT -fvisibility=hidden
tryflag CFLAGS_VISIBILITY_HIDDEN -fvisibility=hidden
tryldflag LDFLAGS_AUTO -Wl,--gc-sections

if [ "$ARCH" != "none" ] ; then
Expand Down Expand Up @@ -782,7 +787,7 @@ CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS
CFLAGS_STD = $CFLAGS_STD
LDFLAGS_STD = $LDFLAGS_STD
CFLAGS_OPT = $CFLAGS_OPT
CFLAGS_VISIBILITY_HIDDEN = $CFLAGS_VISIBILITY_HIDDEN
LDFLAGS_OPT = $LDFLAGS_OPT
LDFLAGS_TERMCAP = $LDFLAGS_TERMCAP
JQ_PREFIX = $JQ_PREFIX
Expand Down Expand Up @@ -817,6 +822,8 @@ CFLAGS_NCURSES = $CFLAGS_NCURSES
ZSV_EXTRAS = $ZSV_EXTRAS
BUILD_SHAREDLIB = $BUILD_SHAREDLIB
ZSVSHEET_BUILD = $ZSVSHEET_BUILD
$NO_HAVE
Expand All @@ -842,6 +849,12 @@ else
echo "* - termcap: yes *"
fi

if [ "$BUILD_SHAREDLIB" = "1" ]; then
echo "* - build shared library: yes *"
else
echo "* - build shared library: no *"
fi

if [ "$ZSVSHEET_BUILD" = "1" ]; then
echo "* - build sheet feature: yes *"
echo "* ncurses library: $NCLIB *"
Expand Down
28 changes: 25 additions & 3 deletions include/zsv/zsv_export.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
#ifndef ZSV_EXPORT_H
#define ZSV_EXPORT_H

// clang-format off
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#define ZSV_EXPORT EMSCRIPTEN_KEEPALIVE
# include <emscripten.h>
# define ZSV_EXPORT EMSCRIPTEN_KEEPALIVE
#else
#define ZSV_EXPORT
# ifdef ZSV_BUILD_SHARED
# if defined(_WIN32) || defined(__CYGWIN__)
# ifdef __GNUC__
# define ZSV_EXPORT __attribute__((dllexport))
# else
# define ZSV_EXPORT __declspec(dllexport)
# endif
# else
# ifdef __GNUC__
# define ZSV_EXPORT __attribute__((dllimport))
# else
# define ZSV_EXPORT __declspec(dllimport)
# endif
# endif
# else
# if __GNUC__ >= 4
# define ZSV_EXPORT __attribute__((visibility("default")))
# else
# define ZSV_EXPORT
# endif
# endif
#endif
// clang-format on

#endif
15 changes: 15 additions & 0 deletions make/sharedlib.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ifeq ($(OS),Windows_NT)
WIN=1
endif

ifeq ($(WIN),1)
SHAREDLIB_EXT:=dll
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
SHAREDLIB_EXT := so
endif
ifeq ($(UNAME_S),Darwin)
SHAREDLIB_EXT := dylib
endif
endif
39 changes: 28 additions & 11 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ ifeq ($(WIN),)
endif
endif

include ${THIS_MAKEFILE_DIR}/../make/sharedlib.mk # sets SHAREDLIB_EXT

CFLAGS+= ${CFLAG_O} ${CFLAGS_OPT}
CFLAGS+= ${CFLAGS_AUTO}

Expand Down Expand Up @@ -56,7 +58,6 @@ endif

ifeq ($(DEBUG),0)
CFLAGS+= -DNDEBUG -O3 ${CFLAGS_LTO}
CFLAGS+= ${CFLAGS_OPENMP}

ifeq ($(PGO),1)
CFLAGS+= -fprofile-generate -fprofile-dir=/tmp/p4
Expand Down Expand Up @@ -103,16 +104,28 @@ NO_UTF8_CHECK=1

LIB_SUFFIX?=
ZSV_OBJ=${BUILD_DIR}/objs/zsv.o
LIBZSV_A=libzsv${LIB_SUFFIX}.a
LIBZSV=${BUILD_DIR}/lib/${LIBZSV_A}
LIBZSV_INSTALL=${LIBDIR}/${LIBZSV_A}
LIBZSV_STATIC=libzsv${LIB_SUFFIX}.a
LIBZSV_STATIC_BUILD=${BUILD_DIR}/lib/${LIBZSV_STATIC}
LIBZSV_BUILD=${LIBZSV_STATIC_BUILD}
LIBZSV_INSTALL=${LIBDIR}/${LIBZSV_STATIC}

ifeq ($(BUILD_SHAREDLIB),1)
LIBZSV_SHARED=libzsv.${SHAREDLIB_EXT}
LIBZSV_SHARED_BUILD+=${BUILD_DIR}/lib/${LIBZSV_SHARED}
LIBZSV_BUILD+=${LIBZSV_SHARED_BUILD}
LIBZSV_INSTALL+=${LIBDIR}/${LIBZSV_SHARED}
ifeq ($(WIN),)
CFLAGS+= ${CFLAGS_OPENMP}
endif
else
CFLAGS+= ${CFLAGS_OPENMP}
endif

ZSV_OBJ_OPTS=
ifeq ($(NO_UTF8_CHECK),1)
ZSV_OBJ_OPTS+= -DNO_UTF8_CHECK
endif


help:
@echo "Make options:"
@echo " `basename ${MAKE}` build|install|uninstall|clean"
Expand All @@ -121,16 +134,20 @@ help:
@echo " [CONFIGFILE=config.mk] [NO_UTF8_CHECK=1] [VERBOSE=1] [LIBDIR=${LIBDIR}] [INCLUDEDIR=${INCLUDEDIR}] [LIB_SUFFIX=]"
@echo

build: ../include/zsv.h ${LIBZSV}
build: ../include/zsv.h ${LIBZSV_BUILD}

${LIBZSV}: ${ZSV_OBJ}
${LIBZSV_STATIC_BUILD}: ${ZSV_OBJ}
@mkdir -p `dirname "$@"`
@rm -f $@
$(AR) rcv $@ $?
$(RANLIB) $@
$(AR) -t $@ # check it is there
@echo Built $@

${LIBZSV_SHARED_BUILD}: zsv.c zsv_internal.c
@mkdir -p `dirname "$@"`
${CC} ${CFLAGS} ${CFLAGS_VISIBILITY_HIDDEN} -DZSV_BUILD_SHARED -DZSV_VERSION=\"${VERSION}\" -I${INCLUDE_DIR} ${ZSV_OBJ_OPTS} -shared -fPIC -o $@ $< -I../app/external/sqlite3

../include/zsv.h:
ifeq ($(ZSV_EXTRAS),1)
@sed 's/__ZSV_EXTRAS__DEFINE__/#ifndef ZSV_EXTRAS\n#define ZSV_EXTRAS\n#endif/' < ../include/zsv.h.in > $@
Expand All @@ -144,17 +161,17 @@ install: ../include/zsv.h ${LIBZSV_INSTALL}
@cp -pR ../include/zsv $(INCLUDEDIR)/
@echo "include files copied to $(INCLUDEDIR)"

${LIBZSV_INSTALL}: ${LIBZSV}
${LIBZSV_INSTALL}: ${LIBDIR}/% : ${BUILD_DIR}/lib/%
@mkdir -p `dirname "$@"`
@cp -p ${LIBZSV} "$@"
@echo "libzsv installed to $@"
cp -p "$<" "$@"
@echo "$* installed to $@"

uninstall:
@rm -rf ${INCLUDEDIR}/zsv*
rm -f ${LIBDIR}/libzsv*

clean:
rm -rf ${BUILD_DIR}/objs ${LIBZSV}
rm -rf ${BUILD_DIR}/objs ${LIBZSV_BUILD}
rm -f ../include/zsv.h

.PHONY: build install uninstall clean ${LIBZSV_INSTALL}
Expand Down

0 comments on commit 40d5ec5

Please sign in to comment.