commit ba933c61a8ca899e942d81134d69216e2592f5d8
parent e39719ad6697447924038f2b47730c90411f049f
Author: vaplv <vaplv@free.fr>
Date: Sun, 19 Jun 2022 15:46:00 +0200
Write a POSIX Makefile as an alternative to CMake
Diffstat:
| M | .gitignore | | | 12 | +++++++----- |
| A | Makefile | | | 120 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | config.mk | | | 34 | ++++++++++++++++++++++++++++++++++ |
| A | make.sh | | | 58 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | polygon.pc.in | | | 10 | ++++++++++ |
5 files changed, 229 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,10 +1,12 @@
.gitignore
-CMakeCache.txt
-CMakeFiles
-Makefile
-tmp
[Bb]uild*
*.sw[po]
-*.[ao]
+*.[aod]
+*.so
*~
+test*
+!test*.[ch]
+.test
+.config
tags
+polygon.pc
diff --git a/Makefile b/Makefile
@@ -0,0 +1,120 @@
+# Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr)
+#
+# This CMake script is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This CMake script is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this CMake script. If not, see <http://www.gnu.org/licenses/>.
+
+.POSIX:
+.SUFFIXES: # Clean up default inference rules
+
+include config.mk
+
+################################################################################
+# RSys building
+################################################################################
+SRC = src/polygon.c
+
+OBJ = $(SRC:.c=.o)
+DEP = $(SRC:.c=.d)
+
+build_library: .config $(DEP)
+ @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) libpolygon.so
+
+$(OBJ): config.mk
+
+libpolygon.so: $(OBJ)
+ @echo "LD $@"
+ @$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS)
+
+.config: Makefile config.mk
+ @echo "Find packages"
+ @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \
+ echo "rsys $(RSYS_VERSION) not found"; exit 1; fi
+ @echo "config done" > .config
+
+.SUFFIXES: .c .d .o
+.c.d:
+ @echo "DEP $@"
+ @$(CC) $(CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+
+.c.o:
+ @echo "CC $@"
+ @$(CC) $(CFLAGS) -DPOLYGON_SHARED_BUILD -c $< -o $@
+
+################################################################################
+# Miscellaneous targets
+################################################################################
+all: build_library build_tests
+
+clean: clean_test
+ @rm -f $(OBJ) $(TEST_OBJ) libpolygon.so .test polygon.pc .config
+
+distclean: clean
+ @rm -f $(DEP) $(TEST_DEP)
+
+################################################################################
+# Installation
+################################################################################
+pkg:
+ @echo "Setup polygon.pc"
+ @sed -e 's#@PREFIX@#$(PREFIX)#g' \
+ -e 's#@VERSION@#$(VERSION)#g' \
+ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \
+ polygon.pc.in > polygon.pc
+
+install: build_library pkg
+ mkdir -p $(DESTDIR)$(PREFIX)/lib
+ mkdir -p $(DESTDIR)$(PREFIX)/lib/pkgconfig
+ mkdir -p $(DESTDIR)$(PREFIX)/include/
+ mkdir -p $(DESTDIR)$(PREFIX)/share/doc/polygon
+ cp libpolygon.so $(DESTDIR)$(PREFIX)/lib
+ cp polygon.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig
+ cp COPYING README.md $(DESTDIR)$(PREFIX)/share/doc/polygon
+ cp src/polygon.h $(DESTDIR)$(PREFIX)/include
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/lib/libpolygon.so
+ rm -f $(DESTDIR)$(PREFIX)/lib/pkgconfig/polygon.pc
+ rm -f $(DESTDIR)$(PREFIX)/share/doc/polygon/COPYING
+ rm -f $(DESTDIR)$(PREFIX)/share/doc/polygon/README.md
+ rm -f $(DESTDIR)$(PREFIX)/include/polygon.h
+
+################################################################################
+# Tests
+################################################################################
+TEST_SRC =\
+ src/test_polygon.c\
+ src/test_polygon1.c
+
+TEST_OBJ = $(TEST_SRC:.c=.o)
+TEST_DEP = $(TEST_SRC:.c=.d)
+
+test: build_tests
+ @$(SHELL) make.sh run_test $(TEST_SRC)
+
+build_tests: build_library $(TEST_DEP) .test
+ @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin
+
+.test: Makefile
+ @echo "Setup tests"
+ @$(SHELL) make.sh config_test $(TEST_SRC) > .test
+
+clean_test:
+ @$(SHELL) make.sh clean_test $(TEST_SRC)
+
+$(TEST_OBJ): config.mk
+ @echo "CC $@"
+ @$(CC) $(CFLAGS) $(RSYS_INC) -c $(@:.o=.c) -o $@
+
+test_polygon test_polygon1: libpolygon.so
+ @echo "LD $@"
+ @$(CC) $(CFLAGS) -o $@ src/$@.o -L$$(pwd) -lpolygon $(RSYS_LIB)
diff --git a/config.mk b/config.mk
@@ -0,0 +1,34 @@
+VERSION = 0.1.4 # Library version
+
+PREFIX = /usr/local
+PKG_CONFIG = pkg-config
+
+################################################################################
+# Dependencies
+################################################################################
+RSYS_VERSION = 0.6
+RSYS_INC = $$($(PKG_CONFIG) --cflags rsys)
+RSYS_LIB = $$($(PKG_CONFIG) --libs rsys)
+
+INCS = $(RSYS_INC)
+LIBS = $(RSYS_LIB)
+
+################################################################################
+# Compilation options
+################################################################################
+CC = cc # Compiler
+
+CPPFLAGS = -DNDEBUG
+WFLAGS =\
+ -Wall\
+ -Wconversion\
+ -Wextra\
+ -Wmissing-declarations\
+ -Wmissing-prototypes\
+ -Wshadow
+
+CFLAGS = -O2 -std=c89 -pedantic -fPIC -fvisibility=hidden -fstrict-aliasing\
+ -Wl,--no-undefined $(WFLAGS) $(CPPFLAGS) $(INCS) # Compiler options
+LDFLAGS = -shared $(LIBS) # Linker options
+
+
diff --git a/make.sh b/make.sh
@@ -0,0 +1,58 @@
+#!/bin/sh -e
+
+# Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr)
+#
+# This CMake script is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This CMake script is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this CMake script. If not, see <http://www.gnu.org/licenses/>.
+
+config_test()
+{
+ for i in "$@"; do
+ test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/')
+ test_list="${test_list} ${test}"
+ printf "%s: %s\n" "${test}" "src/${test}.o"
+ done
+ printf "test_bin: %s\n" "${test_list}"
+}
+
+run_test()
+{
+ n=0
+
+ for i in "$@"; do
+ test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/')
+
+ printf "%s " "${test}"
+ if ./"${test}" > /dev/null 2>&1; then
+ printf "\e[1;32mOK\e[m\n"
+ else
+ printf "\e[1;31mErreur\e[m\n"
+ n=$((n+1))
+ fi
+ done
+
+ if [ "${n}" -ne 0 ]; then
+ printf "%d errors\n" "${n}"
+ exit 1
+ fi
+}
+
+clean_test()
+{
+ for i in "$@"; do
+ test=$(echo "${i}" | sed 's/src\/\(.\{1,\}\).c$/\1/')
+ rm -f "${test}"
+ done
+}
+
+"$@"
diff --git a/polygon.pc.in b/polygon.pc.in
@@ -0,0 +1,10 @@
+prefix=@PREFIX@
+includedir=${prefix}/include
+libdir=${prefix}/lib
+
+Requires.private: rsys >= @RSYS_VERSION@
+Name: Polygon
+Description: Polygon library
+Version: @VERSION@
+Libs: -L${libdir} -lpolygon
+CFlags: -I${includedir}