/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:47:12 UTC
  • Revision ID: edam@waxworlds.org-20090305134712-grzh787t06ojomf8
Tags: 2.0
- smartened up comments
- added PROFILEMODE
- renamed STATICLIBS to LINKSTATIC
- renamed lib target switched to MKSHAREDLIB and MKSTATICLIB
- added setting of software variables
- added debug & profile suffixes
- added dependency file generation system
- added rules to build various source types and added dependency generation

Show diffs side-by-side

added added

removed removed

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