-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSample-GNUmakefile
137 lines (99 loc) · 2.81 KB
/
Sample-GNUmakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Sample GNUmakefile that uses per-platform obj/exe dirs and shows you how
# to build multiple targets from a single "rule".
#
# Needs the portablelib.mk and tools.mk fragments
#
# Author: Sudhi Herle <sw at herle.net>
#
# Placed in the public domain
#
SRCDIR = .
PORTABLE := ../portable-utils
# List your target here
target =
# List your objects here
# The variable for naming the object must be of the form
# TARGETNAME_objs
# where TARGETNAME is the name of your target.
#
# If you have multiple targets, then you must have a separate
# definition of the objects for that target.
#
# e.g.:
#
# CASE 1: Single targets.
# target = foobar
# foobar_objs = foo.o bar.o frungulate.o
#
# CASE 2: Multiple targets
# target = baz gaz
# baz_objs = baz.o foo.o
# gaz_objs = gaz.o bar.o
#---
# TARGETNAME_objs =
# PORTABLE must be defined above..
include $(PORTABLE)/portablelib.mk
#
ifeq ($(exe),)
realtargets = $(target)
else
realtargets = $(addsuffix $(exe),$(target))
endif
# Template for linking.
# $(1) => link target
# $(1)_objs => Name of the variable corresponding to this target
# $($(1)_objs) => Value of the above variable
#
# We need to add an extra '$' in the rules since it gets evaluated _twice_
# -- once at rule definition time and once when it is used.
#
# At the same time, we collect _all_ the objects (for all targets).
# We use $(all-objs) for generating dependencies.
define link_template
all-objs += $$($(1)_objs)
$(1)$(exe): $$($(1)_objs) $(libs)
endef
deps = $(all-objs:.o=.d) $(libobjs:.o=.d)
libs = utils.a
.PHONY: all clean realclean
all: $(realtargets)
# This defines a rule dynamically for linking each target
# named in $(target). It uses the template 'link_template'
# to do the dirty work.
$(foreach prog,$(target),$(eval $(call link_template,$(prog))))
# Single rule for all linkable targets
# XXX We implicitly use the C++ linker even if there are no C++ objs.
# I don't know how to pick the C++ linker only if there are C++ objs.
$(realtargets):
$(LD) $(LDFLAGS) $^ -o $@ $($(platform)_ldlibs)
# Common library used by _all_ objects
$(libs): $(libobjs)
$(AR) $(ARFLAGS) $@ $?
phony:
clean:
rm -f *.o *.exe $(realtargets) $(libs)
realclean: clean
rm -f *.d
depclean: phony
rm -f $(deps)
depend dep deps: $(deps)
date := $(shell date)
ifneq ($(wildcard version),)
version := $(shell cat version)
version.h: version
echo "#define VERSION_STR \"$(version)\"" > $@
echo "#define BUILD_DATE \"$(date)\"" >> $@
else
version := "*-none-*"
version.h: phony
echo "#define VERSION_STR \"$(version)\"" > $@
echo "#define BUILD_DATE \"$(date)\"" >> $@
endif
echo:
@echo "VPATH=$(VPATH)"
@echo "CFLAGS=$(CFLAGS)"
# compiler rules -- include this last
include $(PORTABLE)/tools.mk
# DON'T DELETE BELOW!
# vim: filetype=make
# vim: tw=4:sw=4:noexpandtab:smartindent:tw=76: