/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:32:10 UTC
  • Revision ID: edam@waxworlds.org-20090305133210-9i18m48si42bpidr
Tags: 1.8
- set CC correctly
- change assignments for non-recursive assignments
- added BUILDLIB switch to build a static library

Show diffs side-by-side

added added

removed removed

1
 
#_______________________________________________________________________________
2
 
#
3
 
#                                       edam's general-purpose makefile v2.0
4
 
#_______________________________________________________________________________
5
 
#                                                                COMMON SETTINGS
6
 
 
7
 
#
8
 
# overridable options
9
 
# (better to specify in environment/command line)
10
 
#
11
 
#export DEBUGMODE       := 1
12
 
#export PROFILEMODE := 1
13
 
#export LINKSTATIC      := 1
14
 
 
15
 
#
16
 
# target binary/library
17
 
#
18
 
#MKSTATICLIB    := 1
19
 
#MKSHAREDLIB    := 1
 
1
#                                                       Edam's Makefile v1.8
 
2
#___________________________________________________________________________
 
3
#                                                            S E T T I N G S
 
4
 
 
5
# Target binary
20
6
TARGET          := tim
21
7
 
22
 
#
23
 
# all source files
24
 
#
25
 
SOURCES         := tim.cc
 
8
# All source files
 
9
CC_SRCS         := 
 
10
C_SRCS          := 
 
11
S_SRCS          := 
26
12
 
27
 
#
28
 
# libraries to link against
29
 
#
 
13
# Libraries
30
14
LIBRARIES       := 
31
15
 
32
 
#
33
 
# subdirectories to make first
34
 
#
35
 
SUBDIRS         := 
 
16
# Override options (better: specify in environment or on cmdline)
 
17
#DEBUGMODE      := 1
 
18
CPPBUILD        := 1
 
19
#STATICLIBS     := 1
 
20
BUILDLIB        := 1
36
21
 
37
 
#
38
 
# additional build flags
39
 
#
 
22
# Initial flags
40
23
CPPFLAGS        := 
41
 
CFLAGS          := 
42
 
CXXFLAGS        := 
43
24
ASFLAGS         := 
44
25
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
 
26
 
 
27
#___________________________________________________________________________
 
28
 
 
29
# Software
56
30
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))
83
 
TARGET          := $(patsubst %.so,%,$(patsubst %.a,%,$(TARGET)))
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).
 
31
CC                      := $(if $(CPPBUILD), g++, gcc)
 
32
 
93
33
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
114
 
 
115
 
.PHONY: all subdirs target clean clean_all run depend dep $(SUBDIRS)
116
 
 
117
 
all: subdirs target
118
 
 
119
 
subdirs: $(SUBDIRS)
120
 
 
121
 
target: $(TARGET)
 
34
 
 
35
# Debug build flags
 
36
CPPFLAGS        := -g -D DEBUG $(CPPFLAGS)
 
37
ASFLAGS         := -f elf -g -dDEBUG $(ASFLAGS)
 
38
LDFLAGS         := -Wall $(if $(STATICLIBS), -static) $(LDFLAGS)
 
39
 
 
40
else
 
41
 
 
42
# Release build flags
 
43
CPPFLAGS        := -O2 $(CPPFLAGS)
 
44
ASFLAGS         := -f elf -O2 $(ASFLAGS)
 
45
LDFLAGS         := -Wall -s $(if $(STATICLIBS), -static) $(LDFLAGS)
 
46
 
 
47
endif
 
48
 
 
49
# More variables...
 
50
DEPFILE         := Depends
 
51
_SRCS           := $(CC_SRCS) $(C_SRCS) $(S_SRCS)
 
52
_OBJS           := $(addsuffix .o,$(basename $(_SRCS)))
 
53
LDLIBS          := $(addprefix -l,$(LIBRARIES))
 
54
 
 
55
.PHONY: all clean run depend dep
 
56
 
 
57
all:    depend $(TARGET)
 
58
 
 
59
$(TARGET): $(_OBJS)
 
60
ifdef BUILDLIB
 
61
        $(AR) rcs $(TARGET) $(_OBJS)
 
62
else
 
63
        $(CC) -o $(TARGET) $(LDFLAGS) $(LDLIBS) $(_OBJS)
 
64
endif
122
65
 
123
66
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
135
 
run: target
 
67
        rm -f core $(DEPFILE) $(_OBJS)
 
68
        rm -f *~
 
69
 
 
70
run:    $(TARGET)
136
71
        ./$(TARGET)
137
 
endif
138
 
endif
139
 
 
140
 
$(SUBDIRS):
141
 
        @$(MAKE) -C $@ $(filter-out $(SUBDIRS),$(MAKECMDGOALS))
142
 
        @echo
143
 
 
144
 
$(TARGET): $(OBJECTS)
145
 
ifdef MKSTATICLIB
146
 
        $(AR) rcs $(TARGET) $(OBJECTS)
147
 
else
148
 
        $(LD) $(if $(MKSHAREDLIB),-shared) -o $(TARGET) $(LDFLAGS) $(OBJECTS) $(LDPOSTFLAGS) 
149
 
endif
150
 
 
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)
181
 
 
182
 
#_______________________________________________________________________________
 
72
 
 
73
depend dep:
 
74
#       makedepend -f- -- $(CPPFLAGS) -- $(_SRCS) > $(DEPFILE)
 
75
 
 
76
#___________________________________________________________________________
 
77
#include $(DEPFILE)