Skip to content

Commit

Permalink
Improve Makefile
Browse files Browse the repository at this point in the history
This commit adds some features mentioned in General Conventions for
Makefiles from the GNU make manual. It also allows the Makefile to
use some variables form the environment.
  • Loading branch information
meator committed Aug 24, 2021
1 parent 9237237 commit 613e984
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
28 changes: 16 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include config.mk
.PHONY: all shared install uninstall test demo clean distclean

%.o: %.c
$(CC) -c $(CFLAGS) $<
$(CC) -c $(ALL_CFLAGS) $<

all: changelog shared

Expand All @@ -20,35 +20,39 @@ install: shared
install -D $(LIB_NAME) $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
ln -snf $(LIB_NAME) $(DESTDIR)$(LIBDIR)/$(LIB_SONAME)
ln -snf $(LIB_SONAME) $(DESTDIR)$(LIBDIR)/$(LIBX86).so
install -m 644 -D include/x86emu.h $(DESTDIR)/usr/include/x86emu.h
install -m 644 -D include/x86emu.h $(DESTDIR)$(INCLUDEDIR)/x86emu.h
if [ "$(OLDINCLUDEDIR)" -a "$(OLDINCLUDEDIR)" != "$(INCLUDEDIR)" ] ; then\
install -m 644 -D include/x86emu.h $(DESTDIR)$(OLDINCLUDEDIR)/x86emu.h ;\
fi

uninstall:
rm -f $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
rm -f $(DESTDIR)$(LIBDIR)/$(LIB_SONAME)
rm -f $(DESTDIR)$(LIBDIR)/$(LIBX86).so
rm -f $(DESTDIR)/usr/include/x86emu.h
rm -f $(DESTDIR)$(INCLUDEDIR)/x86emu.h
rm -f $(DESTDIR)$(OLDINCLUDEDIR)/x86emu.h

$(LIB_NAME): .depend $(OBJS)
$(CC) -shared -Wl,-soname,$(LIB_SONAME) $(OBJS) -o $(LIB_NAME) $(LDFLAGS)
$(CC) -shared -Wl,-soname,$(LIB_SONAME) $(ALL_CFLAGS) $(OBJS) -o $(LIB_NAME) $(LDFLAGS)
@ln -snf $(LIB_NAME) $(LIB_SONAME)
@ln -snf $(LIB_SONAME) $(LIBX86).so

test:
make -C test
$(MAKE) -C test

demo:
make -C demo
$(MAKE) -C demo

archive: changelog
@if [ ! -d .git ] ; then echo no git repo ; false ; fi
mkdir -p package
git archive --prefix=$(PREFIX)/ $(BRANCH) > package/$(PREFIX).tar
tar -r -f package/$(PREFIX).tar --mode=0664 --owner=root --group=root --mtime="`git show -s --format=%ci`" --transform='s:^:$(PREFIX)/:' VERSION changelog
xz -f package/$(PREFIX).tar
git archive --prefix=$(ARCHIVE_PREFIX)/ $(BRANCH) > package/$(ARCHIVE_PREFIX).tar
tar -r -f package/$(ARCHIVE_PREFIX).tar --mode=0664 --owner=root --group=root --mtime="`git show -s --format=%ci`" --transform='s:^:$(ARCHIVE_PREFIX)/:' VERSION changelog
xz -f package/$(ARCHIVE_PREFIX).tar

clean:
make -C test clean
make -C demo clean
$(MAKE) -C test clean
$(MAKE) -C demo clean
rm -f *.o *~ include/*~ *.so.* *.so .depend
rm -rf package

Expand All @@ -57,6 +61,6 @@ distclean: clean

ifneq "$(MAKECMDGOALS)" "clean"
.depend: $(CFILES)
@$(CC) -MG -MM $(CFLAGS) $(CFILES) >$@
@$(CC) -MG -MM $(ALL_CFLAGS) $(CFILES) >$@
-include .depend
endif
22 changes: 17 additions & 5 deletions config.mk
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SHELL = /bin/sh
ARCH := $(shell uname -m)
ifneq ($(filter i386 i486 i586 i686, $(ARCH)),)
ARCH := i386
Expand All @@ -11,15 +12,26 @@ VERSION := $(shell $(GIT2LOG) --version VERSION ; cat VERSION)
endif
GITDEPS := $(shell [ -d .git ] && echo .git/HEAD .git/refs/heads .git/refs/tags)
BRANCH := $(shell [ -d .git ] && git branch | perl -ne 'print $$_ if s/^\*\s*//')
PREFIX := libx86emu-$(VERSION)
ARCHIVE_PREFIX := libx86emu-$(VERSION)

PREFIX ?= /usr/local
EXEC_PREFIX ?= $(PREFIX)
ifeq "$(ARCH)" "x86_64"
LIBDIR ?= $(EXEC_PREFIX)/lib64
else
LIBDIR ?= $(EXEC_PREFIX)/lib
endif
INCLUDEDIR ?= $(PREFIX)/include
OLDINCLUDEDIR ?= /usr/include

MAJOR_VERSION := $(shell cut -d . -f 1 VERSION)

CC = gcc
CFLAGS = -g -O2 -fPIC -fvisibility=hidden -fomit-frame-pointer -Wall
LDFLAGS =
CC ?= gcc
CFLAGS ?= -g -O2 -Wall -fomit-frame-pointer
ALL_CFLAGS = -fPIC -fvisibility=hidden $(CFLAGS)

export CC CFLAGS ARCH

LIBDIR = /usr/lib$(shell ldd /bin/sh | grep -q /lib64/ && echo 64)
LIBX86 = libx86emu

CFILES = $(wildcard *.c)
Expand Down
5 changes: 3 additions & 2 deletions demo/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CC = gcc
CFLAGS = -g -Wall -fomit-frame-pointer -O2
SHELL = /bin/sh
CC ?= gcc
CFLAGS ?= -g -Wall -fomit-frame-pointer -O2

.PHONY: clean

Expand Down
6 changes: 3 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -g -Wall -fomit-frame-pointer -O2
LDFLAGS =
SHELL = /bin/sh
CC ?= gcc
CFLAGS ?= -g -Wall -fomit-frame-pointer -O2
TST_FILES = $(sort $(wildcard *.tst))
INIT_FILES = $(addsuffix .init,$(basename $(wildcard *.tst)))
RES_FILES = $(sort $(addsuffix .result,$(basename $(wildcard *.tst))))
Expand Down

0 comments on commit 613e984

Please sign in to comment.