From 7938c610d19617dd69f600716fdd1eb234b5bcc0 Mon Sep 17 00:00:00 2001 From: kikuchan Date: Fri, 19 Jan 2024 10:18:17 +0900 Subject: [PATCH] Update to add shlib build --- Makefile | 7 +++--- cli/Makefile.inc | 31 ++++++++++++++++++++++++ cli/qrean-detect/Makefile | 24 +----------------- cli/qrean-detect/qrean-detect.c | 6 ++++- cli/qrean/Makefile | 24 +----------------- cli/qrean/qrean.c | 6 ++++- package.json | 2 +- src/Makefile | 43 ++++++++++++++++++++++++++++++--- src/qrean.c | 5 ++++ src/qrean.h | 2 ++ 10 files changed, 95 insertions(+), 55 deletions(-) create mode 100644 cli/Makefile.inc diff --git a/Makefile b/Makefile index 5bd9ef6..bc2b9ab 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ cli: @BUILDDIR=$(BUILDDIR) $(MAKE) -C cli install: cli + @BUILDDIR=$(BUILDDIR) $(MAKE) -C src install @BUILDDIR=$(BUILDDIR) $(MAKE) -C cli install clean: @@ -15,10 +16,10 @@ clean: -rmdir $(BUILDDIR) wasm: - @BUILDDIR=$(abspath ./build/wasm) $(MAKE) -C wasm clean all + @BUILDDIR=$(abspath ./build/wasm) NO_SHLIB=1 $(MAKE) -C wasm clean all win32: - @BUILDDIR=$(abspath ./build/win32) CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar $(MAKE) clean cli + @BUILDDIR=$(abspath ./build/win32) CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar NO_SHLIB=1 DLL=1 $(MAKE) clean cli mac: cli cp build/system/qrean build/system/qrean-detect ./dist/ @@ -28,4 +29,4 @@ dist: wasm win32 cp build/wasm/Qrean.* build/win32/*exe ./dist/ test: cli - @BUILDDIR=$(BUILDDIR) $(MAKE) -C tests + @LD_LIBRARY_PATH=$(BUILDDIR) BUILDDIR=$(BUILDDIR) $(MAKE) -C tests diff --git a/cli/Makefile.inc b/cli/Makefile.inc new file mode 100644 index 0000000..d4b8502 --- /dev/null +++ b/cli/Makefile.inc @@ -0,0 +1,31 @@ +PREFIX?=/usr/bin +BUILDDIR?=$(abspath ../../build/system) +QREAN?=$(abspath ../../src) + +CFLAGS+=-I$(QREAN) +LDFLAGS += -L$(BUILDDIR) -lqrean -lm + +NAME=libqrean + +ifdef DLL +LIBSUFFIX := .dll +else +LIBSUFFIX := .so +endif + +all: $(BUILDDIR)/$(MAIN) + +$(BUILDDIR)/$(MAIN): Makefile $(SRCS) $(BUILDDIR)/$(NAME)$(LIBSUFFIX) + $(CC) -o $(BUILDDIR)/$(MAIN) $(CFLAGS) $(SRCS) $(LDFLAGS) + +$(BUILDDIR)/$(NAME)$(LIBSUFFIX): $(QREAN) + @$(MAKE) -C $(QREAN) + +format: + clang-format -i $(MAIN).c + +clean: + rm -f $(BUILDDIR)/$(MAIN) $(BUILDDIR)/$(MAIN).exe $(BUILDDIR)/*.[do] $(BUILDDIR)/libqrean.* + +install: $(BUILDDIR)/$(MAIN) + install -m 0755 $(BUILDDIR)/$(MAIN) $(PREFIX) diff --git a/cli/qrean-detect/Makefile b/cli/qrean-detect/Makefile index 02d5950..1e799c8 100644 --- a/cli/qrean-detect/Makefile +++ b/cli/qrean-detect/Makefile @@ -1,29 +1,7 @@ -PREFIX ?= /usr/bin -BUILDDIR?=$(abspath ../../build/system) -QREAN?=$(abspath ../../src) - MAIN=qrean-detect SRCS += $(MAIN).c SRCS += pngle.c SRCS += miniz.c -CFLAGS+=-I$(QREAN) -LDFLAGS += $(BUILDDIR)/libqrean.a -lm - -all: $(BUILDDIR)/$(MAIN) - -$(BUILDDIR)/$(MAIN): $(SRCS) $(BUILDDIR)/libqrean.a - $(CC) -o $(BUILDDIR)/$(MAIN) $(CFLAGS) $(SRCS) $(LDFLAGS) - -$(BUILDDIR)/libqrean.a: $(QREAN) - @$(MAKE) -C $(QREAN) - -format: - clang-format -i $(MAIN).c - -clean: - rm -f $(BUILDDIR)/$(MAIN) $(BUILDDIR)/$(MAIN).exe $(BUILDDIR)/*.[do] $(BUILDDIR)/libqrean.a - -install: $(BUILDDIR)/$(MAIN) - install -m 0755 $(BUILDDIR)/$(MAIN) $(PREFIX) +include ../Makefile.inc diff --git a/cli/qrean-detect/qrean-detect.c b/cli/qrean-detect/qrean-detect.c index 807891f..d19fd7f 100644 --- a/cli/qrean-detect/qrean-detect.c +++ b/cli/qrean-detect/qrean-detect.c @@ -215,11 +215,15 @@ int main(int argc, char *argv[]) int len; int ch; - while ((ch = getopt(argc, argv, "ho:g:DO:vBUSE:")) != -1) { + while ((ch = getopt(argc, argv, "hVo:g:DO:vBUSE:")) != -1) { switch (ch) { case 'h': return usage(stdout); + case 'V': + printf("qrean-detect version %s\n", qrean_version()); + return 0; + case 'o': out = fopen(optarg, "w+t"); if (out == NULL) { diff --git a/cli/qrean/Makefile b/cli/qrean/Makefile index 594d0c3..35ceb8f 100644 --- a/cli/qrean/Makefile +++ b/cli/qrean/Makefile @@ -1,29 +1,7 @@ -PREFIX ?= /usr/bin -BUILDDIR?=$(abspath ../../build/system) -QREAN?=$(abspath ../../src) - MAIN=qrean SRCS += $(MAIN).c #SRCS += pngle.c SRCS += miniz.c -CFLAGS+=-I$(QREAN) -LDFLAGS += $(BUILDDIR)/libqrean.a -lm - -all: $(BUILDDIR)/$(MAIN) - -$(BUILDDIR)/$(MAIN): $(SRCS) $(BUILDDIR)/libqrean.a - $(CC) -o $(BUILDDIR)/$(MAIN) $(CFLAGS) $(SRCS) $(LDFLAGS) - -$(BUILDDIR)/libqrean.a: $(QREAN) - @$(MAKE) -C ../../src - -format: - clang-format -i $(MAIN).c - -clean: - rm -f $(BUILDDIR)/$(MAIN) $(BUILDDIR)/$(MAIN).exe $(BUILDDIR)/*.[do] $(BUILDDIR)/libqrean.a - -install: $(BUILDDIR)/$(MAIN) - install -m 0755 $(BUILDDIR)/$(MAIN) $(PREFIX) +include ../Makefile.inc diff --git a/cli/qrean/qrean.c b/cli/qrean/qrean.c index 1a0e7e9..f5e7b62 100644 --- a/cli/qrean/qrean.c +++ b/cli/qrean/qrean.c @@ -148,12 +148,16 @@ int main(int argc, char *argv[]) qrean_data_type_t data_type = QREAN_DATA_TYPE_AUTO; int eci_code = QR_ECI_CODE_LATIN1; - while ((ch = getopt(argc, argv, "hi:o:s:f:t:v:l:m:p:8KANUSE:")) != -1) { + while ((ch = getopt(argc, argv, "hVi:o:s:f:t:v:l:m:p:8KANUSE:")) != -1) { int n; switch (ch) { case 'h': return usage(stdout); + case 'V': + printf("qrean version %s\n", qrean_version()); + return 0; + case 'i': in = fopen(optarg, "rb"); if (in == NULL) { diff --git a/package.json b/package.json index da60376..b122d66 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "qrean", "type": "module", - "version": "0.0.1-alpha.4", + "version": "0.0.1", "description": "A portable QR and Barcode generation / manipulation library written in C", "module": "./dist/Qrean.js", "types": "./dist/Qrean.d.ts", diff --git a/src/Makefile b/src/Makefile index 1926884..e559966 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,5 @@ BUILDDIR ?= $(abspath ../build/system) +DESTDIR ?= /usr SRCS += bitstream.c SRCS += utils.c @@ -43,6 +44,7 @@ SIZE = size CFLAGS += -Wall # -Werror CFLAGS += -Wno-misleading-indentation +CFLAGS += -fPIC #CFLAGS += -D USE_MALLOC_BUFFER #CFLAGS += -D NO_MALLOC @@ -62,13 +64,41 @@ LDFLAGS += -lm OBJS = $(addprefix $(BUILDDIR)/,$(SRCS:.c=.o)) DEPS = $(OBJS:.o=.d) -TARGET = $(BUILDDIR)/libqrean.a +VERSION := $(shell jq -r .version $(abspath ../package.json)) +MAJORVERSION := $(shell echo "$(VERSION)" | sed 's,\..*,,') +LIBRARY = libqrean +TARGET = $(BUILDDIR)/$(LIBRARY) -all: $(TARGET) +CFLAGS += -DQREAN_VERSION=\"$(VERSION)\" -$(TARGET): $(OBJS) Makefile +all: libs + +libs: + @$(MAKE) $(TARGET).a +ifndef NO_SHLIB + @$(MAKE) $(TARGET).so.$(VERSION) +endif +ifdef DLL + @$(MAKE) $(TARGET)-$(VERSION).dll +endif + +$(TARGET).a: $(OBJS) Makefile $(AR) rcs $@ $^ +$(TARGET).so.$(VERSION): $(OBJS) Makefile +ifndef NO_SHLIB + $(CC) -shared -Wl,-soname,$(LIBRARY).so.$(MAJORVERSION) -o $@ $(OBJS) + -ln -sf $(LIBRARY).so.$(VERSION) $(TARGET).so.$(MAJORVERSION) + -ln -sf $(LIBRARY).so.$(VERSION) $(TARGET).so +endif + +ifdef DLL +$(TARGET)-$(VERSION).dll: $(OBJS) Makefile + $(CC) -shared -o $@ $(OBJS) + -ln -sf $(LIBRARY)-$(VERSION).dll $(TARGET)-$(MAJORVERSION).dll + -ln -sf $(LIBRARY)-$(VERSION).dll $(TARGET).dll +endif + -include $(DEPS) $(BUILDDIR)/%.o: %.c Makefile @@ -78,5 +108,12 @@ $(BUILDDIR)/%.o: %.c Makefile clean: rm -f $(BUILDDIR)/*.[do] $(TARGET) +install: libs + install -s $(TARGET).so.$(VERSION) $(DESTDIR)/lib/ + install -m 644 $(TARGET).a $(DESTDIR)/lib/ + -ln -sf $(LIBRARY).so.$(VERSION) $(DESTDIR)/lib/$(LIBRARY).so.$(MAJORVERSION) + -ln -sf $(LIBRARY).so.$(VERSION) $(DESTDIR)/lib/$(LIBRARY).so + mkdir -p $(DESTDIR)/include/qrean/ && install -m 644 *.h $(DESTDIR)/include/qrean/ + format: clang-format -i *.[ch] diff --git a/src/qrean.c b/src/qrean.c index c9ba36a..53ac2c1 100644 --- a/src/qrean.c +++ b/src/qrean.c @@ -707,3 +707,8 @@ qr_eci_code_t qrean_get_eci_code(qrean_t *qrean) { return qrean->eci_code; } + +const char *qrean_version() +{ + return QREAN_VERSION; +} diff --git a/src/qrean.h b/src/qrean.h index 0baed41..89db095 100644 --- a/src/qrean.h +++ b/src/qrean.h @@ -235,4 +235,6 @@ const char *qrean_get_code_type_string(qrean_code_type_t code); bit_t qrean_set_eci_code(qrean_t *qrean, qr_eci_code_t code); qr_eci_code_t qrean_get_eci_code(qrean_t *qrean); +const char *qrean_version(); + #endif /* __QR_QREAN_H__ */