/make/edam-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/edam-mk

« back to all changes in this revision

Viewing changes to Makefile

  • Committer: edam
  • Date: 2009-03-05 13:40:53 UTC
  • Revision ID: edam@waxworlds.org-20090305134053-7afaeqithc87bwp2
Tags: 1.10
- combines sources in to one variable, SOURCES
- renamed switches to build static and shared lib to BUILDALIB and BUILDSOLIB
- fixed run goal
- added subdirs goal to phony list
- fix linking to use CXX, not CC

Show diffs side-by-side

added added

removed removed

1
 
#_______________________________________________________________________________
2
 
#
3
 
#                       edam's general-purpose makefile                     v2.3
4
 
#_______________________________________________________________________________
5
 
#                                                                COMMON SETTINGS
 
1
#                                          Edam's general-purpose makefile v1.10
 
2
#_______________________________________________________________________________
 
3
#                                                                S E T T I N G S
6
4
 
7
 
#
8
 
# overridable options
9
 
# (better to specify in environment/command line)
10
 
#
 
5
# Overridable options
 
6
# (better to specify these in environment/command line)
11
7
#export DEBUGMODE       := 1
12
 
#export PROFILEMODE := 1
13
 
#export LINKSTATIC      := 1
14
 
 
15
 
#
16
 
# target binary/library
17
 
#
18
 
#MKSTATICLIB    := 1
19
 
#MKSHAREDLIB    := 1
20
 
TARGET          := tim
21
 
 
22
 
#
23
 
# all source files
24
 
#
 
8
#export STATICLIBS      := 1
 
9
 
 
10
# Target binary/library
 
11
#BUILDALIB      := 1
 
12
#BUILDSOLIB     := 1
 
13
TARGET          := tim.a
 
14
 
 
15
# All source files
25
16
SOURCES         := tim.cc
26
17
 
27
 
#
28
 
# libraries to link against
29
 
#
 
18
# Libraries to link
30
19
LIBRARIES       := 
31
20
 
32
 
#
33
 
# subdirectories to make first
34
 
#
 
21
# Subdirectories to make
35
22
SUBDIRS         := 
36
23
 
37
 
#
38
 
# additional build flags
39
 
#
 
24
# Software
 
25
AS              := nasm
 
26
CC              := gcc
 
27
CXX             := g++
 
28
 
 
29
# Flags
40
30
CPPFLAGS        := 
41
 
CFLAGS          := 
42
 
CXXFLAGS        := 
43
 
DFLAGS          := 
44
 
ASFLAGS         := 
45
 
LDFLAGS         := 
46
 
LDPOSTFLAGS     := 
47
 
 
48
 
#_______________________________________________________________________________
49
 
#                                                                 OTHER SETTINGS
50
 
 
51
 
# set debug mode if profiling
52
 
ifdef PROFILEMODE
53
 
export DEBUGMODE := 1
54
 
endif
55
 
 
56
 
# software
57
 
CC                      := gcc
58
 
CXX                     := g++
59
 
GDC                     := gdc
60
 
AS                      := nasm
61
 
LD                      := g++
62
 
AR                      := ar
63
 
MAKE            := make
64
 
 
65
 
# debug/profile build flags
66
 
CPPFLAGS        := $(if $(PROFILEMODE),-pg -D PROFILE) $(CPPFLAGS)
67
 
CPPFLAGS        := $(if $(DEBUGMODE),-g -D DEBUG -Wall,-O2) $(CPPFLAGS)
68
 
DFLAGS          := $(if $(DEBUGMODE),,-frelease)
69
 
ASFLAGS         := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2) $(ASFLAGS)
70
 
LDFLAGS         := $(if $(PROFILEMODE),-pg) $(LDFLAGS)
71
 
LDFLAGS         := $(if $(or $(PROFILEMODE), $(DEBUGMODE)),,-Wl,-S) $(LDFLAGS)
72
 
 
73
 
# setup options for shared/static libs
74
 
CPPFLAGS        := $(if $(MKSHAREDLIB),-fPIC) $(CPPFLAGS)
75
 
LDFLAGS         := $(if $(LINKSTATIC),-static) $(LDFLAGS)
76
 
 
77
 
# add libraries for d
78
 
LIBRARIES       := $(LIBRARIES) $(if $(filter %.d, $(SOURCES)), gphobos pthread m)
79
 
 
80
 
# build flags for libraries
81
 
LDPOSTFLAGS := $(addprefix -l,$(LIBRARIES)) $(LDPOSTFLAGS)
82
 
 
83
 
#_______________________________________________________________________________
84
 
#
85
 
 
86
 
# object debug/profile suffix
87
 
BUILDSUFFIX     := $(if $(PROFILEMODE),_p,$(if $(DEBUGMODE),_d))
88
 
 
89
 
# work out object and dependency files
90
 
OBJECTS         := $(addsuffix $(BUILDSUFFIX).o,$(basename $(SOURCES)))
91
 
DEPFILES        := $(addsuffix .dep,$(basename $(SOURCES)))
92
 
 
93
 
# fixup target name
94
 
ifdef TARGET
95
 
TARGET          := $(basename $(TARGET))$(BUILDSUFFIX)$(suffix $(TARGET))
 
31
CFLAGS          := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
 
32
CXXFLAGS        := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
 
33
ASFLAGS         := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2)
 
34
LDFLAGS         := -Wall $(if $(DEBUGMODE),,-s) $(if $(STATICLIBS), -static)
 
35
 
 
36
#_______________________________________________________________________________
 
37
 
 
38
DEPFILE         := depends.mk
 
39
OBJECTS         := $(addsuffix .o,$(basename $(SOURCES)))
 
40
LDLIBS          := $(addprefix -l,$(LIBRARIES))
96
41
TARGET          := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
97
 
ifneq ($(strip $(MKSHAREDLIB) $(MKSTATICLIB)),)
98
 
TARGET          := $(TARGET)$(if $(MKSHAREDLIB),.so,$(if $(MKSTATICLIB),.a))
99
 
TARGET          := lib$(patsubst lib%,%,$(TARGET))
100
 
endif
101
 
endif
102
 
 
103
 
# Set up dependency generation build flags and, for those languages where the
104
 
# the compiler/assembler doesn't support dependency generation, commands to be
105
 
# executed after generating any dependency file. The commands append the names
106
 
# of all the depended-on files in the dependency file to the end of the file as
107
 
# empty rules with no prerequesits or commands. This causes make not to fail if
108
 
# one of these files becomes non-existant, but causes files dependent on these
109
 
# files to be rebuilt (and thus also have their dependencies regenerated).
110
 
ifdef DEBUGMODE
111
 
ifndef PROFILEMODE
112
 
FIXUP_DEPENDENCY_FILES = \
113
 
        sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
114
 
        $(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
115
 
DEPFLAGS        = -MMD -MP -MF $(basename $<).dep
116
 
endif
117
 
endif
118
 
 
119
 
# include dependencies
120
 
ifneq "$(MAKECMDGOALS)" "clean"
121
 
ifneq "$(MAKECMDGOALS)" "clean_all"
122
 
-include $(DEPFILES)
123
 
endif
124
 
endif
125
 
 
126
 
# default rule
127
 
.DEFAULT_GOAL := all
128
 
 
129
 
#_______________________________________________________________________________
130
 
#                                                                          RULES
 
42
TARGET          := $(TARGET)$(if $(BUILDALIB),.a,)$(if $(BUILDSOLIB),.so,)
131
43
 
132
44
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
133
45
 
138
50
target: $(TARGET)
139
51
 
140
52
clean:
 
53
ifeq "$(MAKECMDGOALS)" "clean"
141
54
ifdef SUBDIRS
142
 
ifneq "$(MAKECMDGOALS)" "clean_all"
143
 
        @echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
144
 
endif
145
 
endif
146
 
        rm -f $(OBJECTS) $(TARGET) *.dep core
 
55
        @echo NOT RECURSING: Use \"make clean_all\" to clean subdirectoried as well...
 
56
endif
 
57
endif
 
58
        rm -f core $(DEPFILE) $(OBJECTS) $(TARGET)
 
59
        rm -f *~
147
60
 
148
61
clean_all: subdirs clean
149
62
 
150
 
ifndef MKSTATICLIB
151
 
ifndef MKSHAREDLIB
 
63
ifndef BUILDALIB
 
64
ifndef BUILDSOLIB
152
65
run: target
153
66
        ./$(TARGET)
154
67
endif
155
68
endif
156
69
 
 
70
#depend dep:
 
71
#       makedepend -f- -- $(CPPFLAGS) -- $(SOURCES) > $(DEPFILE)
 
72
 
157
73
$(SUBDIRS):
158
 
        @if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
159
 
        @$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
160
 
        @echo
 
74
        @$(MAKE) --no-print-directory -C $@ $(MAKECMDGOALS)
161
75
 
162
76
$(TARGET): $(OBJECTS)
163
 
ifdef MKSTATICLIB
 
77
ifdef BUILDALIB
164
78
        $(AR) rcs $(TARGET) $(OBJECTS)
165
79
else
166
 
        $(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS) 
167
 
endif
168
 
 
169
 
%.o %_d.o %_p.o: %.c
170
 
        $(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
171
 
 
172
 
%.o %_d.o %_p.o: %.cc
173
 
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
174
 
%.o %_d.o %_p.o: %.C
175
 
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
176
 
%.o %_d.o %_p.o: %.cpp
177
 
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
178
 
 
179
 
%.o %_d.o %_p.o: %.d
180
 
        $(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
181
 
 
182
 
%.o %_d.o %_p.o: %.s
183
 
        $(AS) $(ASFLAGS) -o $@ $<
184
 
ifdef DEBUGMODE
185
 
        $(AS) $(ASFLAGS) -M $< > $(basename $<).dep
186
 
        $(FIXUP_DEPENDENCY_FILES)
187
 
endif
188
 
%.o %_d.o %_p.o: %.S
189
 
        $(AS) $(ASFLAGS) -o $@ $<
190
 
ifdef DEBUGMODE
191
 
        $(AS) $(ASFLAGS) -M $< > $(basename $<).dep
192
 
        $(FIXUP_DEPENDENCY_FILES)
193
 
endif
194
 
%.o %_d.o %_p.o: %.asm
195
 
        $(AS) $(ASFLAGS) -o $@ $<
196
 
ifdef DEBUGMODE
197
 
        $(AS) $(ASFLAGS) -M $< > $(basename $<).dep
198
 
        $(FIXUP_DEPENDENCY_FILES)
199
 
endif
 
80
        $(CXX) $(if $(BUILDSOLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDLIBS) 
 
81
endif
 
82
 
 
83
#%.o:   %.c
 
84
#       $(CC) -c $(CPPFLAGS) $(CFLAGS)
 
85
#%.o:   %.cc
 
86
#       $(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
 
87
#%.o:   %.s
 
88
#       $(AS) $(ASFLAGS)
 
89
 
 
90
-include $(DEPFILE)
200
91
 
201
92
#_______________________________________________________________________________