44#
55
66CC := gcc
7+ CXX := g++
78AR := ar
89PERL := perl
910ARFLAGS := rcs
@@ -64,38 +65,52 @@ ifeq ($(OS),Windows_NT)
6465 LIB_TARGET := libinfix.a
6566 LDFLAGS := -lkernel32
6667 CLEAN_CMD := del /F /Q
68+ EXPORT_FLAGS := -Wl,--export-all-symbols
69+ SO_EXT := dll
6770else
6871 # Native POSIX (Linux, macOS, BSD) Configuration
6972 EXE_EXT :=
70- LRT_FLAG := $(shell printf '#include <sys/mman.h>\n#include <fcntl.h>\nint main() { shm_open(" ", 0, 0); return 0; }' > .check_lrt.c; if $(CC ) .check_lrt.c -o .check_lrt.out -lrt >/dev/null 2>&1; then printf -- "-lrt"; fi; rm -f .check_lrt.c .check_lrt.out)
73+ LRT_FLAG := $(shell printf '#include <sys/mman.h>\n#include <fcntl.h>\nint main() { shm_open(\"\ ", 0, 0); return 0; }' > .check_lrt.c; if $(CC ) .check_lrt.c -o .check_lrt.out -lrt >/dev/null 2>&1; then printf -- "-lrt"; fi; rm -f .check_lrt.c .check_lrt.out)
7174 ifeq ($(LRT_FLAG),-lrt)
7275 $(info Linker check : -lrt is required and will be added.)
7376 endif
7477 LDFLAGS := -lpthread $(LRT_FLAG ) -ldl
7578 LIB_TARGET := libinfix.a
7679 CLEAN_CMD := rm -f
80+ EXPORT_FLAGS := -rdynamic
81+ SO_EXT := so
7782endif
7883
7984# Finalize Source and Object Lists
8085LIB_SRCS := $(BASE_SRCS )
8186LIB_OBJS := $(LIB_SRCS:.c=.o )
87+ SHARED_LIB_OBJS := $(LIB_SRCS:.c=.shared.o )
8288
83- TEST_SRCS := $(wildcard t/* .c)
84- TEST_TARGETS: = $(patsubst % .c,% $(EXE_EXT ) ,$(TEST_SRCS ) )
89+ TEST_SRCS := $(wildcard t/* .c) $(wildcard t/* .cpp)
90+ TEST_TARGETS: = $(patsubst t/% .c,t/% $(EXE_EXT ) ,$(filter % .c,$(TEST_SRCS ) ) )
91+ TEST_TARGETS+ = $(patsubst t/% .cpp,t/% $(EXE_EXT ) ,$(filter % .cpp,$(TEST_SRCS ) ) )
8592
8693# Target-Specific Variables for Special Test Cases
8794# Define extra source files and flags needed ONLY for the regression test.
8895t/850_regression_cases$(EXE_EXT)_EXTRA_SRCS := fuzz/fuzz_helpers.c
8996t/850_regression_cases$(EXE_EXT)_EXTRA_CFLAGS := -Ifuzz
9097
98+ # 880_exports needs to export its own symbols to test dlsym(NULL)
99+ t/880_exports$(EXE_EXT)_EXTRA_LDFLAGS := $(EXPORT_FLAGS )
100+
91101# Build Targets
92- .PHONY : all test clean
102+ .PHONY : all test clean shared
93103
94104# Default target: Build only the static library.
95105all : $(LIB_TARGET )
96106 @echo
97107 @echo " Library '$( LIB_TARGET) ' built successfully."
98108
109+ # Shared library target
110+ shared : libinfix.$(SO_EXT )
111+ @echo
112+ @echo " Shared Library 'libinfix.$( SO_EXT) ' built successfully."
113+
99114# Test target: Build all test executables, then run them using the embedded Perl script.
100115test : $(TEST_TARGETS )
101116 @echo
@@ -107,22 +122,37 @@ $(LIB_TARGET): $(LIB_OBJS)
107122 @echo " AR $@ "
108123 $(AR ) $(ARFLAGS ) $@ $^
109124
110- # Generic pattern rule to build a test executable.
111- # It uses automatic variables like `$@` (the target) and `$<` (the first prerequisite)
112- # and also checks for our target-specific variables.
113- $(TEST_TARGETS ) : % $(EXE_EXT ) : % .c $(LIB_TARGET )
125+ # Rule to build the shared library
126+ libinfix.$(SO_EXT ) : $(SHARED_LIB_OBJS )
114127 @echo " LD $@ "
115- $(CC ) $(TEST_CFLAGS ) $($(@ ) _EXTRA_CFLAGS) $< $($(@ ) _EXTRA_SRCS) -o $@ -L. -linfix $(LDFLAGS )
128+ $(CC ) -shared -o $@ $^ $(LDFLAGS )
129+
130+ # Generic pattern rule to build a C test executable.
131+ t/% $(EXE_EXT ) : t/% .c $(LIB_TARGET )
132+ @echo " LD (C) $@ "
133+ $(CC ) $(TEST_CFLAGS ) $($(@ ) _EXTRA_CFLAGS) $< $($(@ ) _EXTRA_SRCS) -o $@ -L. -linfix $(LDFLAGS ) $($(@ ) _EXTRA_LDFLAGS)
134+
135+ # Generic pattern rule to build a C++ test executable.
136+ t/% $(EXE_EXT ) : t/% .cpp $(LIB_TARGET )
137+ @echo " LD (C++) $@ "
138+ $(CXX ) $(TEST_CFLAGS ) $($(@ ) _EXTRA_CFLAGS) $< $($(@ ) _EXTRA_SRCS) -o $@ -L. -linfix $(LDFLAGS ) $($(@ ) _EXTRA_LDFLAGS)
116139
117140# Generic rule to compile library .c files to .o files, using LIB_CFLAGS
118141% .o : % .c
119142 @echo " CC $<"
120143 $(CC ) $(LIB_CFLAGS ) -c $< -o $@
121144
145+ # Rule to compile for shared library (with -fPIC and -DINFIX_BUILDING_DLL)
146+ % .shared.o : % .c
147+ @echo " CC (Shared) $<"
148+ $(CC ) $(LIB_CFLAGS ) -fPIC -DINFIX_BUILDING_DLL -c $< -o $@
149+
122150# Clean up build artifacts
123151clean :
124152 @echo " CLEAN"
125- -$(CLEAN_CMD ) $(subst /,\,$(LIB_OBJS ) )
126- -$(CLEAN_CMD ) $(subst /,\,$(TEST_TARGETS ) )
127- -$(CLEAN_CMD ) $(subst /,\,$(LIB_TARGET ) )
153+ -$(CLEAN_CMD ) $(subst /uyễn,uyễn,$(LIB_OBJS ) )
154+ -$(CLEAN_CMD ) $(subst /uyễn,uyễn,$(SHARED_LIB_OBJS ) )
155+ -$(CLEAN_CMD ) $(subst /uyễn,uyễn,$(TEST_TARGETS ) )
156+ -$(CLEAN_CMD ) $(subst /uyễn,uyễn,$(LIB_TARGET ) )
157+ -$(CLEAN_CMD ) libinfix.so libinfix.dll infix.dll infix.lib infix.exp
128158 -rm -f .check_lrt.out
0 commit comments