Skip to content

Commit 4157cb9

Browse files
committed
Improve Makefile
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.
1 parent 6c2026e commit 4157cb9

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

Makefile

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include config.mk
33
.PHONY: all shared install uninstall test demo clean distclean
44

55
%.o: %.c
6-
$(CC) -c $(CFLAGS) $<
6+
$(CC) -c $(ALL_CFLAGS) $<
77

88
all: changelog shared
99

@@ -20,35 +20,36 @@ install: shared
2020
install -D $(LIB_NAME) $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
2121
ln -snf $(LIB_NAME) $(DESTDIR)$(LIBDIR)/$(LIB_SONAME)
2222
ln -snf $(LIB_SONAME) $(DESTDIR)$(LIBDIR)/$(LIBX86).so
23-
install -m 644 -D include/x86emu.h $(DESTDIR)/usr/include/x86emu.h
23+
install -m 644 -D include/x86emu.h $(DESTDIR)$(INCLUDEDIR)/x86emu.h
2424

2525
uninstall:
2626
rm -f $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
2727
rm -f $(DESTDIR)$(LIBDIR)/$(LIB_SONAME)
2828
rm -f $(DESTDIR)$(LIBDIR)/$(LIBX86).so
29-
rm -f $(DESTDIR)/usr/include/x86emu.h
29+
rm -f $(DESTDIR)$(INCLUDEDIR)/x86emu.h
30+
rm -f $(DESTDIR)$(OLDINCLUDEDIR)/x86emu.h
3031

3132
$(LIB_NAME): .depend $(OBJS)
32-
$(CC) -shared -Wl,-soname,$(LIB_SONAME) $(OBJS) -o $(LIB_NAME) $(LDFLAGS)
33+
$(CC) -shared -Wl,-soname,$(LIB_SONAME) $(ALL_CFLAGS) $(OBJS) -o $(LIB_NAME) $(LDFLAGS)
3334
@ln -snf $(LIB_NAME) $(LIB_SONAME)
3435
@ln -snf $(LIB_SONAME) $(LIBX86).so
3536

3637
test:
37-
make -C test
38+
$(MAKE) -C test
3839

3940
demo:
40-
make -C demo
41+
$(MAKE) -C demo
4142

4243
archive: changelog
4344
@if [ ! -d .git ] ; then echo no git repo ; false ; fi
4445
mkdir -p package
45-
git archive --prefix=$(PREFIX)/ $(BRANCH) > package/$(PREFIX).tar
46-
tar -r -f package/$(PREFIX).tar --mode=0664 --owner=root --group=root --mtime="`git show -s --format=%ci`" --transform='s:^:$(PREFIX)/:' VERSION changelog
47-
xz -f package/$(PREFIX).tar
46+
git archive --prefix=$(ARCHIVE_PREFIX)/ $(BRANCH) > package/$(ARCHIVE_PREFIX).tar
47+
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
48+
xz -f package/$(ARCHIVE_PREFIX).tar
4849

4950
clean:
50-
make -C test clean
51-
make -C demo clean
51+
$(MAKE) -C test clean
52+
$(MAKE) -C demo clean
5253
rm -f *.o *~ include/*~ *.so.* *.so .depend
5354
rm -rf package
5455

@@ -57,6 +58,6 @@ distclean: clean
5758

5859
ifneq "$(MAKECMDGOALS)" "clean"
5960
.depend: $(CFILES)
60-
@$(CC) -MG -MM $(CFLAGS) $(CFILES) >$@
61+
@$(CC) -MG -MM $(ALL_CFLAGS) $(CFILES) >$@
6162
-include .depend
6263
endif

config.mk

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
SHELL = /bin/sh
12
ARCH := $(shell uname -m)
23
ifneq ($(filter i386 i486 i586 i686, $(ARCH)),)
34
ARCH := i386
@@ -11,15 +12,25 @@ VERSION := $(shell $(GIT2LOG) --version VERSION ; cat VERSION)
1112
endif
1213
GITDEPS := $(shell [ -d .git ] && echo .git/HEAD .git/refs/heads .git/refs/tags)
1314
BRANCH := $(shell [ -d .git ] && git branch | perl -ne 'print $$_ if s/^\*\s*//')
14-
PREFIX := libx86emu-$(VERSION)
15+
ARCHIVE_PREFIX := libx86emu-$(VERSION)
16+
17+
PREFIX ?= /usr/local
18+
EXEC_PREFIX ?= $(PREFIX)
19+
ifeq "$(ARCH)" "x86_64"
20+
LIBDIR ?= $(EXEC_PREFIX)/lib64
21+
else
22+
LIBDIR ?= $(EXEC_PREFIX)/lib
23+
endif
24+
INCLUDEDIR ?= $(PREFIX)/include
1525

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

18-
CC = gcc
19-
CFLAGS = -g -O2 -fPIC -fvisibility=hidden -fomit-frame-pointer -Wall
20-
LDFLAGS =
28+
CC ?= gcc
29+
CFLAGS ?= -g -O2 -Wall -fomit-frame-pointer
30+
ALL_CFLAGS = -fPIC -fvisibility=hidden $(CFLAGS)
31+
32+
export CC CFLAGS ARCH
2133

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

2536
CFILES = $(wildcard *.c)

demo/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
CC = gcc
2-
CFLAGS = -g -Wall -fomit-frame-pointer -O2
1+
SHELL = /bin/sh
2+
CC ?= gcc
3+
CFLAGS ?= -g -Wall -fomit-frame-pointer -O2
34

45
.PHONY: clean
56

test/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CC = gcc
2-
CFLAGS = -g -Wall -fomit-frame-pointer -O2
3-
LDFLAGS =
1+
SHELL = /bin/sh
2+
CC ?= gcc
3+
CFLAGS ?= -g -Wall -fomit-frame-pointer -O2
44
TST_FILES = $(sort $(wildcard *.tst))
55
INIT_FILES = $(addsuffix .init,$(basename $(wildcard *.tst)))
66
RES_FILES = $(sort $(addsuffix .result,$(basename $(wildcard *.tst))))

0 commit comments

Comments
 (0)