rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

Makefile (8474B)


      1 # Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2 #
      3 # This program is free software: you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation, either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
     15 
     16 .POSIX:
     17 .SUFFIXES: # Clean up default inference rules
     18 
     19 include config.mk
     20 
     21 LIBNAME_STATIC = librsys.a
     22 LIBNAME_SHARED = librsys.so
     23 LIBNAME = $(LIBNAME_$(LIB_TYPE))
     24 
     25 default: library
     26 all: library tests
     27 
     28 ################################################################################
     29 # RSys building
     30 ################################################################################
     31 SRC =\
     32  src/clock_time.c\
     33  src/cstr.c\
     34  src/hash.c\
     35  src/image.c\
     36  src/library.c\
     37  src/logger.c\
     38  src/mem_allocator.c\
     39  src/mem_lifo_allocator.c\
     40  src/mem_proxy_allocator.c\
     41  src/pthread/pthread_condition.c\
     42  src/pthread/pthread_mutex.c\
     43  src/quaternion.c\
     44  src/str.c\
     45  src/text_reader.c
     46 OBJ = $(SRC:.c=.o)
     47 DEP = $(SRC:.c=.d)
     48 
     49 CFLAGS_LIB = $(CFLAGS_SO) -DRSYS_SHARED_BUILD
     50 LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS)
     51 
     52 library: $(DEP)
     53 	@$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \
     54 	$$(if [ -n "$(LIBNAME)" ]; then\
     55 	     echo "$(LIBNAME)";\
     56 	   else\
     57 	     echo "$(LIBNAME_SHARED)";\
     58 	   fi)
     59 
     60 $(DEP) $(OBJ): config.mk
     61 
     62 $(LIBNAME_SHARED): $(OBJ)
     63 	$(CC) $(CFLAGS_LIB) -o $@ $(OBJ) $(LDFLAGS_LIB)
     64 
     65 $(LIBNAME_STATIC): librsys.o
     66 	$(AR) -rc $@ $?
     67 	$(RANLIB) $@
     68 
     69 librsys.o: $(OBJ)
     70 	$(LD) -r $(OBJ) -o $@
     71 	$(OBJCOPY) $(OCPFLAGS) $@
     72 
     73 .SUFFIXES: .c .d .o
     74 .c.d:
     75 	@$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@
     76 
     77 .c.o:
     78 	$(CC) $(CFLAGS_LIB) -c $< -o $@
     79 
     80 ################################################################################
     81 # Installation
     82 ################################################################################
     83 API =\
     84  src/algorithm.h\
     85  src/binary_heap.h\
     86  src/clock_time.h\
     87  src/condition.h\
     88  src/cstr.h\
     89  src/double2.h\
     90  src/double3.h\
     91  src/double4.h\
     92  src/double22.h\
     93  src/double33.h\
     94  src/double44.h\
     95  src/dynamic_array.h\
     96  src/dynamic_array_char.h\
     97  src/dynamic_array_double.h\
     98  src/dynamic_array_float.h\
     99  src/dynamic_array_int.h\
    100  src/dynamic_array_uchar.h\
    101  src/dynamic_array_u32.h\
    102  src/dynamic_array_u64.h\
    103  src/dynamic_array_uint.h\
    104  src/dynamic_array_size_t.h\
    105  src/dynamic_array_str.h\
    106  src/endianness.h\
    107  src/float2.h\
    108  src/float3.h\
    109  src/float4.h\
    110  src/float22.h\
    111  src/float33.h\
    112  src/float44.h\
    113  src/free_list.h\
    114  src/hash.h\
    115  src/hash_table.h\
    116  src/image.h\
    117  src/library.h\
    118  src/list.h\
    119  src/logger.h\
    120  src/math.h\
    121  src/mem_allocator.h\
    122  src/morton.h\
    123  src/mutex.h\
    124  src/quaternion.h\
    125  src/real2.h\
    126  src/real3.h\
    127  src/realX.h\
    128  src/realX_begin.h\
    129  src/realX_end.h\
    130  src/real22.h\
    131  src/real33.h\
    132  src/real44.h\
    133  src/realXY.h\
    134  src/realXY_begin.h\
    135  src/realXY_end.h\
    136  src/ref_count.h\
    137  src/rsys.h\
    138  src/signal.h\
    139  src/str.h\
    140  src/stretchy_array.h\
    141  src/text_reader.h
    142 
    143 pkg:
    144 	sed -e 's,@PREFIX@,$(PREFIX),g'\
    145 	    -e 's,@VERSION@,$(VERSION),g'\
    146 	    rsys.pc.in > rsys.pc
    147 
    148 # Remove the include directive rather than setting it to "./src". to prevent
    149 # the source directory from having a higher priority than the system include
    150 # directories. In such a situation, the local "math.h" file could be included
    151 # instead of the "math.h" header provided by the C standard library. Note that
    152 # this is no longer a problem with the common pc file: the "math.h" file is
    153 # installed in the "rsys" subdirectory, which is therefore a prefix of the
    154 # header file allowing it to be distinguished from the header of the standard
    155 # library
    156 rsys-local.pc: rsys.pc.in
    157 	sed -e '1,2d'\
    158 	    -e 's,^libdir=.*,libdir=./,'\
    159 	    -e 's,@PREFIX@,$(PREFIX),g'\
    160 	    -e 's,@VERSION@,$(VERSION),g'\
    161 	    -e 's,-I$${includedir},,g'\
    162 	    rsys.pc.in > $@
    163 
    164 install: library pkg
    165 	install() { mode="$$1"; prefix="$$2"; shift 2; \
    166 	  mkdir -p "$${prefix}"; \
    167 	  for i in "$$@"; do \
    168 	   if ! cmp -s "$${i}" "$${prefix}/$${i##*/}"; then \
    169 	     cp "$${i}" "$${prefix}"; \
    170 	     chmod "$${mode}" "$$@"; \
    171 	   fi; \
    172 	  done; \
    173 	}; \
    174 	install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \
    175 	install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" rsys.pc; \
    176 	install 644 "$(DESTDIR)$(INCPREFIX)/rsys" $(API); \
    177 	install 644 "$(DESTDIR)$(PREFIX)/share/doc/rsys" COPYING README.md
    178 
    179 uninstall:
    180 	rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)"
    181 	rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/rsys.pc"
    182 	rm -f "$(DESTDIR)$(PREFIX)/share/doc/rsys/COPYING"
    183 	rm -f "$(DESTDIR)$(PREFIX)/share/doc/rsys/README.md"
    184 	rm -f $$(echo $(API) | sed 's,src/,$(DESTDIR)$(INCPREFIX)/rsys/,g')
    185 
    186 clean: clean_test
    187 	rm -f $(OBJ) $(DEP) $(LIBNAME) librsys.o
    188 	rm -f rsys.pc rsys-local.pc
    189 	rm -f libtest_lib.so test_lib.o
    190 	rm -f .test rsys.pc .test.ppm test_text_reader.txt test.ppm
    191 
    192 ################################################################################
    193 # Tests
    194 ################################################################################
    195 TEST_SRC =\
    196  src/test_algorithm.c\
    197  src/test_atomic.c\
    198  src/test_binary_heap.c\
    199  src/test_condition.c\
    200  src/test_cstr.c\
    201  src/test_double22.c\
    202  src/test_double2.c\
    203  src/test_double33.c\
    204  src/test_double3.c\
    205  src/test_double44.c\
    206  src/test_double4.c\
    207  src/test_dynamic_array.c\
    208  src/test_endianness.c\
    209  src/test_float22.c\
    210  src/test_float2.c\
    211  src/test_float33.c\
    212  src/test_float3.c\
    213  src/test_float44.c\
    214  src/test_float4.c\
    215  src/test_free_list.c\
    216  src/test_func_name.c\
    217  src/test_hash_table.c\
    218  src/test_hash_sha256.c\
    219  src/test_image.c\
    220  src/test_library.c\
    221  src/test_list.c\
    222  src/test_logger.c\
    223  src/test_math.c\
    224  src/test_mem_allocator.c\
    225  src/test_misc.c\
    226  src/test_morton.c\
    227  src/test_mutex.c\
    228  src/test_quaternion.c\
    229  src/test_ref.c\
    230  src/test_signal.c\
    231  src/test_str.c\
    232  src/test_stretchy_array.c\
    233  src/test_text_reader.c\
    234  src/test_time.c\
    235  src/test_vmacros.c
    236 TEST_OBJ = $(TEST_SRC:.c=.o)
    237 TEST_DEP = $(TEST_SRC:.c=.d)
    238 TEST_TGT = $(TEST_SRC:.c=.t)
    239 
    240 PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG)
    241 
    242 INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys-local.pc)
    243 LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys-local.pc) -lm
    244 
    245 CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST)
    246 LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST)
    247 
    248 tests: library $(TEST_DEP) $(TEST_TGT)
    249 	@$(MAKE) -fMakefile \
    250 	$$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \
    251 	$$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \
    252 	test_all
    253 
    254 $(TEST_TGT):
    255 	@{ \
    256 	  exe="$$(basename "$@" ".t")"; \
    257 	  printf '%s: %s\n' "$${exe}" $(@:.t=.o); \
    258 	  printf 'test_all: %s\n' "$${exe}"; \
    259 	} > $@
    260 
    261 test_lib.o: src/test_library.c src/rsys.h config.mk
    262 	$(CC) $(CFLAGS_SO) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@
    263 
    264 libtest_lib.so: test_lib.o config.mk
    265 	$(CC) $(CFLAGS_SO) -o $@ test_lib.o $(LDFLAGS_SO)
    266 
    267 test_library: libtest_lib.so
    268 
    269 $(TEST_DEP): config.mk rsys-local.pc
    270 	@$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
    271 
    272 $(TEST_OBJ): config.mk rsys-local.pc
    273 	$(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@
    274 
    275 test_algorithm \
    276 test_atomic \
    277 test_binary_heap \
    278 test_condition \
    279 test_cstr \
    280 test_double2 \
    281 test_double22 \
    282 test_double3 \
    283 test_double33 \
    284 test_double4 \
    285 test_double44 \
    286 test_dynamic_array \
    287 test_endianness \
    288 test_float2 \
    289 test_float22 \
    290 test_float3 \
    291 test_float33 \
    292 test_float4 \
    293 test_float44 \
    294 test_free_list \
    295 test_func_name \
    296 test_hash_sha256 \
    297 test_hash_table \
    298 test_image \
    299 test_library \
    300 test_list \
    301 test_logger \
    302 test_math \
    303 test_mem_allocator \
    304 test_misc \
    305 test_morton \
    306 test_mutex \
    307 test_quaternion \
    308 test_ref \
    309 test_signal \
    310 test_str \
    311 test_stretchy_array \
    312 test_text_reader \
    313 test_time \
    314 test_vmacros \
    315 : config.mk rsys-local.pc $(LIBNAME)
    316 	$(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
    317 
    318 clean_test:
    319 	rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT)
    320 	rm -f libtest_lib.so test_lib.o test_text_reader.txt test.ppm
    321 	for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
    322 
    323 test: tests
    324 	@err=0; \
    325 	for i in $(TEST_SRC); do \
    326 	  test="$$(basename "$${i}" ".c")"; \
    327 	  printf '%s' "$${test}"; \
    328 	  if "./$${test}" > /dev/null 2>&1; then \
    329 	    printf '\n'; \
    330 	  else \
    331 	    printf ': error %s\n' "$$?"; \
    332 	    err=$$((err+1)); \
    333 	  fi \
    334 	done; \
    335 	[ "$${err}" -eq 0 ]