From ed1ffe9955ad05bff36e8be6d51528fc496022d0 Mon Sep 17 00:00:00 2001 From: Ethan Sommer Date: Wed, 13 May 2020 06:54:29 -0400 Subject: [PATCH] Makefile: various improvements, including POSIX compatibility Add .POSIX: target to specify POSIX make behaviour. Change directory variables from lowercase to more common uppercase variants. Replace GNU make wildcard and sort directives with list of include files. Replace install target's dependencies on GNU make specific inference rules with an install target that just installs all relevant files itself. Change GNU make-style inference rule "%.o: %.c" to POSIX make ".c.o:". Replace use of $^ in the library generation rules with $(OBJ). Remove use of $< in the pkgconfig file's build rule, as $< is only specified to be set in inference rules. Replace usage of install command (or locally provided script) with POSIX cp and mkdir. Remove install.sh. Don't set CC or AR, these are specified by POSIX as set by make when C development is supported. Remove call to ranlib, it is non-POSIX, and POSIX specifies ar as generating the symbol table when invoked with an option that changes the archive contents, which will always happen as the archive is removed before ar is called. --- Makefile | 88 ++++++++++++++++++++++++++++++++++-------------------- install.sh | 67 ----------------------------------------- 2 files changed, 55 insertions(+), 100 deletions(-) delete mode 100755 install.sh diff --git a/Makefile b/Makefile index b663cc0..336a1c5 100644 --- a/Makefile +++ b/Makefile @@ -1,63 +1,85 @@ -prefix=/usr/local -libdir=$(prefix)/lib -includedir=$(prefix)/include - -CC=gcc -AR=ar -RANLIB=ranlib -INSTALL=./install.sh +.POSIX: + +PREFIX=/usr/local +INCLUDEDIR=$(PREFIX)/include +LIBDIR=$(PREFIX)/lib +PKGCONFIGDIR=$(LIBDIR)/pkgconfig + +PICFLAGS=-fPIC WFLAGS=-Wall -CFLAGS?=-O2 INCLUDES=-Iinclude SHAREDLIB=libnl-tiny.so STATICLIB=libnl-tiny.a PCFILE=libnl-tiny.pc ALL_LIBS=$(SHAREDLIB) $(STATICLIB) -ALL_INCLUDES=$(sort $(wildcard include/*.h include/*/*.h include/*/*/*.h)) +ALL_INCLUDES=\ + include/netlink-generic.h \ + include/netlink-local.h \ + include/netlink-types.h \ + include/netlink/addr.h \ + include/netlink/attr.h \ + include/netlink/cache-api.h \ + include/netlink/cache.h \ + include/netlink/data.h \ + include/netlink/errno.h \ + include/netlink/genl/ctrl.h \ + include/netlink/genl/family.h \ + include/netlink/genl/genl.h \ + include/netlink/genl/mngt.h \ + include/netlink/handlers.h \ + include/netlink/list.h \ + include/netlink/msg.h \ + include/netlink/netlink-compat.h \ + include/netlink/netlink-kernel.h \ + include/netlink/netlink.h \ + include/netlink/object-api.h \ + include/netlink/object.h \ + include/netlink/socket.h \ + include/netlink/types.h \ + include/netlink/utils.h \ + include/netlink/version.h \ + include/unl.h LIBNL_SRCS=nl.c handlers.c msg.c attr.c cache.c cache_mngt.c object.c socket.c error.c GENL_SRCS=genl.c genl_family.c genl_ctrl.c genl_mngt.c unl.c SRCS=$(LIBNL_SRCS) $(GENL_SRCS) OBJS=$(SRCS:.c=.o) -PICFLAGS=-fPIC - -include config.mak -all: $(ALL_LIBS) $(PCFILE) +ALL_CFLAGS=$(CFLAGS) $(PICFLAGS) $(WFLAGS) $(INCLUDES) -install: $(ALL_LIBS:%=$(DESTDIR)$(libdir)/%) \ - $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/libnl-tiny/%) \ - $(PCFILE:%=$(DESTDIR)$(libdir)/pkgconfig/%) +all: $(ALL_LIBS) $(PCFILE) clean: rm -f $(OBJS) $(ALL_LIBS) $(PCFILE) -%.o: %.c - $(CC) $(CPPFLAGS) -c -o $@ $(INCLUDES) $(CFLAGS) $(PICFLAGS) $< +.c.o: + $(CC) $(CPPFLAGS) -c -o $@ $(ALL_CFLAGS) $< $(SHAREDLIB): $(OBJS) - $(CC) -shared -o $@ $^ $(LDFLAGS) + $(CC) -shared -o $@ $(LDFLAGS) $(OBJS) $(STATICLIB): $(OBJS) rm -f $@ - $(AR) rc $@ $^ - $(RANLIB) $@ + $(AR) -rc $@ $(OBJS) $(PCFILE): $(PCFILE).in - sed s,@prefix@,$(prefix),g $< > $@ - - -$(DESTDIR)$(includedir)/libnl-tiny/%: include/% - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(libdir)/%: % - $(INSTALL) -D -m 644 $< $@ - -$(DESTDIR)$(libdir)/pkgconfig/%: % - $(INSTALL) -D -m 644 $< $@ - + sed 's,@prefix@,$(prefix),g' $(PCFILE).in > $@ + +install: $(ALL_LIBS) $(ALL_INCLUDES) $(PCFILE) + mkdir -p "$(DESTDIR)$(INCLUDEDIR)/libnl-tiny/netlink/genl" + for f in $(ALL_INCLUDES); do \ + f="$${f#include/}"; \ + cp "include/$$f" "$(DESTDIR)$(INCLUDEDIR)/libnl-tiny/$${f%/*}"; \ + done + mkdir -p "$(DESTDIR)$(LIBDIR)" + for f in $(ALL_LIBS); do \ + cp "$$f" "$(DESTDIR)$(LIBDIR)/"; \ + done + mkdir -p "$(DESTDIR)$(PKGCONFIGDIR)" + cp "$(PCFILE)" "$(DESTDIR)$(PKGCONFIGDIR)/" .PHONY: all clean install diff --git a/install.sh b/install.sh deleted file mode 100755 index 0410883..0000000 --- a/install.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/sh -# -# Written by Rich Felker, originally as part of musl libc. -# Multi-licensed under MIT, 0BSD, and CC0. -# -# This is an actually-safe install command which installs the new -# file atomically in the new location, rather than overwriting -# existing files. -# - -usage() { -printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2 -exit 1 -} - -mkdirp= -symlink= -mode=755 - -while getopts Dlm: name ; do -case "$name" in -D) mkdirp=yes ;; -l) symlink=yes ;; -m) mode=$OPTARG ;; -?) usage ;; -esac -done -shift $(($OPTIND - 1)) - -test "$#" -eq 2 || usage -src=$1 -dst=$2 -tmp="$dst.tmp.$$" - -case "$dst" in -*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;; -esac - -set -C -set -e - -if test "$mkdirp" ; then -umask 022 -case "$2" in -*/*) mkdir -p "${dst%/*}" ;; -esac -fi - -trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP - -umask 077 - -if test "$symlink" ; then -ln -s "$1" "$tmp" -else -cat < "$1" > "$tmp" -chmod "$mode" "$tmp" -fi - -mv -f "$tmp" "$2" -test -d "$2" && { -rm -f "$2/$tmp" -printf "%s: %s is a directory\n" "$0" "$dst" 1>&2 -exit 1 -} - -exit 0