/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:34:42 UTC
  • Revision ID: edam@waxworlds.org-20090305133442-tiu2f46d8xgc9v2u
Tags: 1.9
- added SUBDIRS for recursing
- added check for SUBDIRS to clean
- added new goal, clean_all, which recurses
- added BUILDSO switch to build a shared library
- renamed dependency files

Show diffs side-by-side

added added

removed removed

1
 
#_______________________________________________________________________________
2
 
#
3
 
#                     edam's general-purpose makefile v2.2
4
 
#_______________________________________________________________________________
5
 
#                                                                COMMON SETTINGS
 
1
#                                                       Edam's Makefile v1.9
 
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
 
8
#export STATICLIBS      := 1
14
9
 
15
 
#
16
 
# target binary/library
17
 
#
18
 
#MKSTATICLIB    := 1
19
 
#MKSHAREDLIB    := 1
 
10
# Target binary/library
 
11
#BUILDLIB       := 1
 
12
#BUILDSO        := 1
20
13
TARGET          := tim
21
14
 
22
 
#
23
 
# all source files
24
 
#
25
 
SOURCES         := tim.cc
 
15
# All source files
 
16
CC_SRCS         := tim.cc
 
17
C_SRCS          := 
 
18
S_SRCS          := 
26
19
 
27
 
#
28
 
# libraries to link against
29
 
#
 
20
# Libraries to link
30
21
LIBRARIES       := 
31
22
 
32
 
#
33
 
# subdirectories to make first
34
 
#
 
23
# Subdirectories to make
35
24
SUBDIRS         := 
36
25
 
37
 
#
38
 
# additional build flags
39
 
#
40
 
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
 
26
# Software
 
27
AS                      := nasm
57
28
CC                      := gcc
58
29
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))
96
 
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 dependancy generation build flags and a commands to be executed after
104
 
# generating any dependancy file. The commands append the names of all the
105
 
# depended-on files in the dependancy file to the end of the dependancy file as
106
 
# empty rules with no prerequesits or commands. This causes make not to fail if
107
 
# one of these files becomes non-existant, but causes files dependant on these
108
 
# files to be rebuilt (and thus also have their dependancies regenerated).
109
 
ifdef DEBUGMODE
110
 
ifndef PROFILEMODE
111
 
FIXUP_DEPENDANCY_FILES = \
112
 
        sed 's/\#.*//;s/^[^:]*://;s/^[ \t]*//;s/ *\\$$//;/^$$/d;s/$$/:/' < \
113
 
        $(basename $<).dep > .$$$$~; cat .$$$$~ >> $(basename $<).dep; rm .$$$$~;
114
 
DEPFLAGS        = -MD -MF $(basename $<).dep
115
 
endif
116
 
endif
117
 
 
118
 
# include dependancies
119
 
ifneq "$(MAKECMDGOALS)" "clean"
120
 
ifneq "$(MAKECMDGOALS)" "clean_all"
121
 
-include $(DEPFILES)
122
 
endif
123
 
endif
124
 
 
125
 
# default rule
126
 
.DEFAULT_GOAL := all
127
 
 
128
 
#_______________________________________________________________________________
129
 
#                                                                          RULES
130
 
 
131
 
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
132
 
 
133
 
all: subdirs target
134
 
 
135
 
subdirs: $(SUBDIRS)
136
 
 
137
 
target: $(TARGET)
 
30
 
 
31
# Flags
 
32
CPPFLAGS        :=
 
33
CFLAGS          := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
 
34
CXXFLAGS        := $(if $(DEBUGMODE),-g -D DEBUG,-O2)
 
35
ASFLAGS         := -f elf $(if $(DEBUGMODE),-g -dDEBUG,-O2)
 
36
LDFLAGS         := -Wall $(if $(DEBUGMODE),,-s) $(if $(STATICLIBS), -static)
 
37
 
 
38
#___________________________________________________________________________
 
39
 
 
40
DEPFILE         := depends.mk
 
41
_SRCS           := $(CC_SRCS) $(C_SRCS) $(S_SRCS)
 
42
_OBJS           := $(addsuffix .o,$(basename $(_SRCS)))
 
43
LDLIBS          := $(addprefix -l,$(LIBRARIES))
 
44
 
 
45
.PHONY: all target clean clean_all run depend dep $(SUBDIRS)
 
46
 
 
47
all:    $(SUBDIRS) $(TARGET)
 
48
 
 
49
target: $(TARGET)
138
50
 
139
51
clean:
 
52
ifeq ($(MAKECMDGOALS),clean)
140
53
ifdef SUBDIRS
141
 
ifneq "$(MAKECMDGOALS)" "clean_all"
142
 
        @echo "NOT RECURSING: use 'make clean_all' to clean subdirectories as well."
143
 
endif
144
 
endif
145
 
        rm -f $(OBJECTS) $(TARGET) *.dep core
146
 
 
147
 
clean_all: subdirs clean
148
 
 
149
 
ifndef MKSTATICLIB
150
 
ifndef MKSHAREDLIB
151
 
run: target
152
 
        ./$(TARGET)
153
 
endif
154
 
endif
 
54
        @echo NOT RECURSING: Use \"make clean_all\" to clean recursively..."
 
55
endif
 
56
endif
 
57
        rm -f core $(DEPFILE) $(_OBJS) $(TARGET)
 
58
        rm -f *~
 
59
 
 
60
clean_all: $(SUBDIRS) clean
 
61
 
 
62
run:    $(TARGET)
 
63
        $(if $(BUILDLIB),@echo Can\'t run a library\!,./$(TARGET))
 
64
 
 
65
#depend dep:
 
66
#       makedepend -f- -- $(CPPFLAGS) -- $(_SRCS) > $(DEPFILE)
155
67
 
156
68
$(SUBDIRS):
157
 
        @if [ "$@" = "$(firstword $(SUBDIRS))" ]; then echo; fi
158
 
        @$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
159
 
        @echo
 
69
        @$(MAKE) --no-print-directory -C $@ $(MAKECMDGOALS)
160
70
 
161
 
$(TARGET): $(OBJECTS)
162
 
ifdef MKSTATICLIB
163
 
        $(AR) rcs $(TARGET) $(OBJECTS)
 
71
$(TARGET): $(_OBJS)
 
72
ifdef BUILDLIB
 
73
        $(AR) rcs $(TARGET) $(_OBJS)
164
74
else
165
 
        $(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS) 
 
75
        $(CC) $(if $(BUILDSO),-shared) -o $(TARGET) $(LDFLAGS) $(_OBJS) $(LDLIBS)
166
76
endif
167
77
 
168
 
%.o %_d.o %_p.o: %.c
169
 
        $(CC) -c $(CPPFLAGS) $(DEPFLAGS) $(CFLAGS) -o $@ $<
170
 
        $(FIXUP_DEPENDANCY_FILES)
171
 
 
172
 
%.o %_d.o %_p.o: %.cc
173
 
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
174
 
        $(FIXUP_DEPENDANCY_FILES)
175
 
%.o %_d.o %_p.o: %.C
176
 
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
177
 
        $(FIXUP_DEPENDANCY_FILES)
178
 
%.o %_d.o %_p.o: %.cpp
179
 
        $(CXX) -c $(CPPFLAGS) $(DEPFLAGS) $(CXXFLAGS) -o $@ $<
180
 
        $(FIXUP_DEPENDANCY_FILES)
181
 
 
182
 
%.o %_d.o %_p.o: %.d
183
 
        $(GDC) -c $(CPPFLAGS) $(DFLAGS) -o $@ $<
184
 
 
185
 
%.o %_d.o %_p.o: %.s
186
 
        $(AS) $(ASFLAGS) -o $@ $<
187
 
%.o %_d.o %_p.o: %.S
188
 
        $(AS) $(ASFLAGS) -o $@ $<
189
 
%.o %_d.o %_p.o: %.asm
190
 
        $(AS) $(ASFLAGS) -o $@ $<
191
 
 
192
 
%.dep: %.s
193
 
        $(AS) $(ASFLAGS) -M $< > $@
194
 
        $(FIXUP_DEPENDANCY_FILES)
195
 
%.dep: %.S
196
 
        $(AS) $(ASFLAGS) -M $< > $@
197
 
        $(FIXUP_DEPENDANCY_FILES)
198
 
%.dep: %.asm
199
 
        $(AS) $(ASFLAGS) -M $< > $@
200
 
        $(FIXUP_DEPENDANCY_FILES)
201
 
 
202
 
#_______________________________________________________________________________
 
78
#%.o:   %.c
 
79
#       $(CC) -c $(CPPFLAGS) $(CFLAGS)
 
80
#%.o:   %.cc
 
81
#       $(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
 
82
#%.o:   %.s
 
83
#       $(AS) $(ASFLAGS)
 
84
 
 
85
-include $(DEPFILE)
 
86
 
 
87
#___________________________________________________________________________