Skip to content

Commit f62e035

Browse files
author
meator
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 d4b7508 commit f62e035

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

Makefile

+16-12
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,39 @@ 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
24+
if [ "$(OLDINCLUDEDIR)" -a "$(OLDINCLUDEDIR)" != "$(INCLUDEDIR)" ] ; then\
25+
install -m 644 -D include/x86emu.h $(DESTDIR)$(OLDINCLUDEDIR)/x86emu.h ;\
26+
fi
2427

2528
uninstall:
2629
rm -f $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
2730
rm -f $(DESTDIR)$(LIBDIR)/$(LIB_SONAME)
2831
rm -f $(DESTDIR)$(LIBDIR)/$(LIBX86).so
29-
rm -f $(DESTDIR)/usr/include/x86emu.h
32+
rm -f $(DESTDIR)$(INCLUDEDIR)/x86emu.h
33+
rm -f $(DESTDIR)$(OLDINCLUDEDIR)/x86emu.h
3034

3135
$(LIB_NAME): .depend $(OBJS)
32-
$(CC) -shared -Wl,-soname,$(LIB_SONAME) $(OBJS) -o $(LIB_NAME) $(LDFLAGS)
36+
$(CC) -shared -Wl,-soname,$(LIB_SONAME) $(ALL_CFLAGS) $(OBJS) -o $(LIB_NAME) $(LDFLAGS)
3337
@ln -snf $(LIB_NAME) $(LIB_SONAME)
3438
@ln -snf $(LIB_SONAME) $(LIBX86).so
3539

3640
test:
37-
make -C test
41+
$(MAKE) -C test
3842

3943
demo:
40-
make -C demo
44+
$(MAKE) -C demo
4145

4246
archive: changelog
4347
@if [ ! -d .git ] ; then echo no git repo ; false ; fi
4448
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
49+
git archive --prefix=$(ARCHIVE_PREFIX)/ $(BRANCH) > package/$(ARCHIVE_PREFIX).tar
50+
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
51+
xz -f package/$(ARCHIVE_PREFIX).tar
4852

4953
clean:
50-
make -C test clean
51-
make -C demo clean
54+
$(MAKE) -C test clean
55+
$(MAKE) -C demo clean
5256
rm -f *.o *~ include/*~ *.so.* *.so .depend
5357
rm -rf package
5458

@@ -57,6 +61,6 @@ distclean: clean
5761

5862
ifneq "$(MAKECMDGOALS)" "clean"
5963
.depend: $(CFILES)
60-
@$(CC) -MG -MM $(CFLAGS) $(CFILES) >$@
64+
@$(CC) -MG -MM $(ALL_CFLAGS) $(CFILES) >$@
6165
-include .depend
6266
endif

config.mk

+17-5
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,26 @@ 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
25+
OLDINCLUDEDIR ?= /usr/include
1526

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

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

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

2537
CFILES = $(wildcard *.c)

demo/Makefile

+3-2
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

+3-3
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)