* Remove some bogus dependencies on Depend/.dir
[oota-llvm.git] / Makefile.common
1 #===-- Makefile.common - Common make rules for LLVM -------*- makefile -*--====
2 #
3 # This file is included by all of the LLVM makefiles.  This file defines common
4 # rules to do things like compile a .cpp file or generate dependancy info.
5 # These are platform dependant, so this is the file used to specify these
6 # system dependant operations.
7 #
8 # The following functionality may be set by setting incoming variables:
9 #
10 # 1. LEVEL - The level of the current subdirectory from the top of the 
11 #    MagicStats view.  This level should be expressed as a path, for 
12 #    example, ../.. for two levels deep.
13 #
14 # 2. DIRS - A list of subdirectories to be built.  Fake targets are set up
15 #    so that each of the targets "all", "install", and "clean" each build.
16 #    the subdirectories before the local target.
17 #
18 # 3. Source - If specified, this sets the source code filenames.  If this
19 #    is not set, it defaults to be all of the .cpp, .c, .y, and .l files 
20 #    in the current directory.  Also, if you want to build files in addition
21 #    to the local files, you can use the ExtraSource variable
22 #
23 #===-----------------------------------------------------------------------====
24
25 # These are options that can either be enabled here, or can be enabled on the
26 # make command line (ie, make ENABLE_PROFILING=1)
27 #
28
29 # When ENABLE_PROFILING is enabled, the llvm source base is built with profile
30 # information to allow gprof to be used to get execution frequencies.
31 #
32 #ENABLE_PROFILING = 1
33
34 # When ENABLE_PURIFY is enabled, the LLVM tools are linked with purify (which
35 # must be locally installed) to allow for some automated memory error debugging.
36 #
37 #ENABLE_PURIFY    = 1
38
39 # When ENABLE_OPTIMIZED is enabled, Release builds of all of the LLVM code are
40 # turned on, and Debug builds are turned off.
41 #
42 #ENABLE_OPTIMIZED = 1
43
44 # If you do not want to build into /shared, uncomment this
45 #
46 #BUILD_ROOT = .
47
48 ifdef SHARED_LIBRARY
49 # if SHARED_LIBRARY is specified, the default is to build the dynamic lib
50 dynamic ::
51 endif
52
53 # Default Rule:  Make sure it's also a :: rule
54 all ::
55
56 # Default for install is to at least build everything...
57 install ::
58
59
60 # Figure out which directory to build stuff into.  We want to build into the
61 # /shared directory by default because it is guaranteed to be local to the
62 # current machine.
63 #
64 ifndef BUILD_ROOT
65 LOGIN_NAME := $(shell whoami)
66 CUR_DIRECTORY := $(shell pwd)
67 BUILD_ROOT := /shared/$(LOGIN_NAME)$(patsubst $(HOME)%,%,$(CUR_DIRECTORY))
68 endif
69
70 #--------------------------------------------------------------------
71 # Installation configuration options... 
72 #--------------------------------------------------------------------
73
74 #BinInstDir=/usr/local/bin
75 #LibInstDir=/usrl/local/lib/xxx
76 #DocInstDir=/usr/doc/xxx
77
78 BURG = /home/vadve/vadve/Research/DynOpt/Burg/burg
79 BURG_OPTS = -I
80
81
82 PURIFY = /usr/dcs/applications/purify/bin/purify -cache-dir="$(HOME)/purifycache" -chain-length="30" -messages=all 
83
84 # Shorthand for commonly accessed directories
85 LIBDEBUG    = $(BUILD_ROOT)/$(LEVEL)/lib/Debug
86 LIBRELEASE  = $(BUILD_ROOT)/$(LEVEL)/lib/Release
87 TOOLDEBUG   = $(BUILD_ROOT)/$(LEVEL)/tools/Debug
88 TOOLRELEASE = $(BUILD_ROOT)/$(LEVEL)/tools/Release
89
90 #---------------------------------------------------------
91 # Compilation options...
92 #---------------------------------------------------------
93
94 # Special tools used while building
95 RunBurg  = $(BURG) $(BURG_OPTS)
96
97 # Enable this for profiling support with 'gprof'
98 ifdef ENABLE_PROFILING
99 PROFILE = -pg
100 else
101 PROFILE =
102 endif
103
104 # -Wno-unused-parameter
105 CompileCommonOpts = $(PROFILE) -Wall -W  -Wwrite-strings -Wno-unused -I$(LEVEL)/include
106
107 # Compile a file, don't link...
108 Compile  = $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(CompileCommonOpts)
109 CompileG = $(Compile) -g  -D_DEBUG
110 CompileO = $(Compile) -O3 -DNDEBUG -finline-functions -felide-constructors -fnonnull-objects -freg-struct-return -fshort-enums
111
112 # Link final executable
113
114 ifdef ENABLE_PURIFY # To enable purify, build with 'gmake ENABLE_PURIFY=1'
115 Link     = $(PURIFY) $(CXX) $(PROFILE) -static
116 else
117 Link     = $(CXX) $(PROFILE)
118 endif
119 LinkG    = $(Link) -g  -L $(LIBDEBUG)
120 LinkO    = $(Link) -O3 -L $(LIBRELEASE)
121
122 # Create a .so file from a .o files...
123 #MakeSO   = $(CXX) -shared $(PROFILE)
124 MakeSO   = $(CXX) -G $(PROFILE)
125 MakeSOO  = $(MakeSO) -O3
126
127 # Create one .o file from a bunch of .o files...
128 Relink = ld -r
129
130 # Create dependancy file from CPP file, send to stdout.
131 Depend   = $(CXX) -MM -I$(LEVEL)/include $(CPPFLAGS) 
132
133 # Archive a bunch of .o files into a .a file...
134 AR       = ar cq 
135 MakeLib   = $(AR)
136
137 #----------------------------------------------------------
138
139 # Source includes all of the cpp files, and objects are derived from the
140 # source files...
141 # The local Makefile can list other Source files via ExtraSource = ...
142
143 Source  := $(ExtraSource) $(wildcard *.cpp *.c *.y *.l)
144
145 Objs := $(sort $(patsubst Debug/%.o, %.o, $(addsuffix .o,$(basename $(Source)))))
146 ObjectsO = $(addprefix $(BUILD_ROOT)/Release/,$(Objs)))
147 ObjectsG = $(addprefix $(BUILD_ROOT)/Debug/,$(Objs))
148
149
150 #---------------------------------------------------------
151 # Handle the DIRS option
152 #---------------------------------------------------------
153
154 ifdef DIRS  # Only do this if we're using DIRS!
155
156 all     :: $(addsuffix /.makeall    , $(DIRS))
157 install :: $(addsuffix /.makeinstall, $(DIRS))
158 clean   :: $(addsuffix /.makeclean  , $(DIRS))
159
160 %/.makeall %/.makeclean %/.makeinstall:
161         cd $(@D); $(MAKE) $(subst $(@D)/.make,,$@)
162 endif
163
164 #---------------------------------------------------------
165 # Handle the LIBRARYNAME option - used when building libs...
166 #---------------------------------------------------------
167 #
168 #  When libraries are built, they are allowed to optionally define the
169 #  DONT_BUILD_RELINKED make variable, which, when defined, prevents a .o file
170 #  from being built for the library. This .o files may then be linked to by a
171 #  tool if the tool does not need (or want) the semantics a .a file provides
172 #  (linking in only object files that are "needed").  If a library is never to
173 #  be used in this way, it is better to define DONT_BUILD_RELINKED, and define
174 #  BUILD_ARCHIVE instead.
175 #
176 #  Some libraries must be built as .a files (libscalar for example) because if
177 #  it's built as a .o file, then all of the constituent .o files in it will be
178 #  linked into tools (for example gccas) even if they only use one of the parts
179 #  of it.  For this reason, sometimes it's useful to use libraries as .a files.
180
181 ifdef LIBRARYNAME
182
183 LIBNAME_O    := $(LIBRELEASE)/lib$(LIBRARYNAME).so
184 LIBNAME_G    := $(LIBDEBUG)/lib$(LIBRARYNAME).so
185 LIBNAME_AO   := $(LIBRELEASE)/lib$(LIBRARYNAME).a
186 LIBNAME_AG   := $(LIBDEBUG)/lib$(LIBRARYNAME).a
187 LIBNAME_OBJO := $(LIBRELEASE)/$(LIBRARYNAME).o
188 LIBNAME_OBJG := $(LIBDEBUG)/$(LIBRARYNAME).o
189
190
191 ifndef ENABLE_OPTIMIZED
192 BUILD_LIBNAME_G := $(LIBNAME_G)
193 ifndef DONT_BUILD_RELINKED
194 BUILD_LIBNAME_OBJG := $(LIBNAME_OBJG)
195 endif
196 ifdef BUILD_ARCHIVE
197 BUILD_LIBNAME_AG := $(LIBNAME_AG)
198 endif
199 endif
200
201 # If optimized builds are enabled...
202 ifdef ENABLE_OPTIMIZED
203 BUILD_LIBNAME_O  := $(LIBNAME_O)
204 ifndef DONT_BUILD_RELINKED
205 BUILD_LIBNAME_OBJO := $(LIBNAME_OBJO)
206 endif
207 ifdef BUILD_ARCHIVE
208 BUILD_LIBNAME_AO := $(LIBNAME_AO)
209 endif
210 endif
211
212 all:: $(BUILD_LIBNAME_AG) $(BUILD_LIBNAME_OBJG)      # Debug
213 all:: $(BUILD_LIBNAME_AO) $(BUILD_LIBNAME_OBJO)      # Release
214 dynamic:: $(BUILD_LIBNAME_G) $(BUILD_LIBNAME_O)      # .so files
215
216 $(LIBNAME_O): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
217         @echo ======= Linking $(LIBRARYNAME) release library =======
218         $(MakeSOO) -o $@ $(ObjectsO) $(LibSubDirs) $(LibLinkOpts)
219
220 $(LIBNAME_G): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
221         @echo ======= Linking $(LIBRARYNAME) debug library =======
222         $(MakeSO) -g -o $@ $(ObjectsG) $(LibSubDirs) $(LibLinkOpts)
223
224 $(LIBNAME_AO): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
225         @echo ======= Linking $(LIBRARYNAME) release library =======
226         @rm -f $@
227         $(MakeLib) $@ $(ObjectsO) $(LibSubDirs)
228
229 $(LIBNAME_AG): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
230         @echo ======= Linking $(LIBRARYNAME) debug library =======
231         @rm -f $@
232         $(MakeLib) $@ $(ObjectsG) $(LibSubDirs)
233
234 $(LIBNAME_OBJO): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
235         $(Relink) -o $@ $(ObjectsO) $(LibSubDirs)
236
237 $(LIBNAME_OBJG): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
238         $(Relink) -o $@ $(ObjectsG) $(LibSubDirs)
239
240 endif
241
242 #------------------------------------------------------------------------
243 # Create a TAGS database for emacs
244 #------------------------------------------------------------------------
245
246 ifeq ($(LEVEL), .)
247 tags:
248         etags -l c++ `find include lib tools -name '*.cpp' -o -name '*.h'`
249 all:: tags
250 endif
251
252 #------------------------------------------------------------------------
253 # Handle the TOOLNAME option - used when building tool executables...
254 #------------------------------------------------------------------------
255 #
256 # The TOOLNAME option should be used with a USEDLIBS variable that tells the
257 # libraries (and the order of the libs) that should be linked to the
258 # tool. USEDLIBS should contain a list of library names (some with .a extension)
259 # that are automatically linked in as .o files unless the .a suffix is added.
260 #
261 ifdef TOOLNAME
262
263 # TOOLEXENAME* - These compute the output filenames to generate...
264 TOOLEXENAME_G = $(LEVEL)/tools/Debug/$(TOOLNAME)
265 TOOLEXENAME_O = $(LEVEL)/tools/Release/$(TOOLNAME)
266 TOOLEXENAMES := $(TOOLEXENAME_G)
267 ifdef ENABLE_OPTIMIZED
268 TOOLEXENAMES += $(TOOLEXENAME_O)
269 endif
270
271 # USED_LIBS_OPTIONS - Compute the options line that add -llib1 -llib2, etc.
272 USED_LIBS_OPTIONS   := $(patsubst %.a.o, -l%, $(addsuffix .o, $(USEDLIBS)))
273 USED_LIBS_OPTIONS_G := $(patsubst %.o, $(LIBDEBUG)/%.o,  $(USED_LIBS_OPTIONS))
274 USED_LIBS_OPTIONS_O := $(patsubst %.o, $(LIBRELEASE)/%.o,$(USED_LIBS_OPTIONS))
275
276
277 # USED_LIB_PATHS - Compute the path of the libraries used so that tools are
278 # rebuilt if libraries change.  This has to make sure to handle .a/.so and .o
279 # files seperately.
280 #
281 STATICUSEDLIBS   := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(USEDLIBS)))
282 USED_LIB_PATHS_G := $(addprefix $(LIBDEBUG)/, $(STATICUSEDLIBS))
283 USED_LIB_PATHS_O := $(addprefix $(LIBRELEASE)/, $(STATICUSEDLIBS))
284
285 all::   $(TOOLEXENAMES)
286 clean::
287         rm -f $(TOOLEXENAMES)
288
289 $(TOOLEXENAME_G): $(ObjectsG) $(USED_LIB_PATHS_G) $(LEVEL)/tools/Debug/.dir
290         $(LinkG) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS_G) $(TOOLLINKOPTS)
291
292 $(TOOLEXENAME_O): $(ObjectsO) $(USED_LIB_PATHS_O) $(LEVEL)/tools/Release/.dir
293         $(LinkO) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS_O) $(TOOLLINKOPTS)
294
295 endif
296
297
298
299 #---------------------------------------------------------
300 .PRECIOUS: $(BUILD_ROOT)/Depend/.dir
301 .PRECIOUS: $(BUILD_ROOT)/Debug/.dir $(BUILD_ROOT)/Release/.dir
302
303 # Create dependencies for the *.cpp files...
304 $(BUILD_ROOT)/Depend/%.d: %.cpp $(BUILD_ROOT)/Depend/.dir
305         $(Depend) $< | sed 's|$*\.o *|Release/& Debug/& Depend/$(@F)|g' > $@
306
307 # Create dependencies for the *.c files...
308 $(BUILD_ROOT)/Depend/%.d: %.c $(BUILD_ROOT)/Depend/.dir
309         $(Depend) $< | sed 's|$*\.o *|Release/& Debug/& Depend/$(@F)|g' > $@
310
311 # Create .o files in the ObjectFiles directory from the .cpp and .c files...
312 $(BUILD_ROOT)/Release/%.o: %.cpp $(BUILD_ROOT)/Release/.dir
313         $(CompileO) $< -o $@
314
315 #Release/%.o: %.c Release/.dir Depend/.dir
316 #       $(CompileOC) $< -o $@
317
318 $(BUILD_ROOT)/Debug/%.o: %.cpp $(BUILD_ROOT)/Debug/.dir
319         $(CompileG) $< -o $@
320
321 #Debug/%.o: %.c Debug/.dir 
322 #       $(CompileGC) $< -o $@
323
324 # Create a .cpp source file from a flex input file... this uses sed to cut down
325 # on the warnings emited by GCC...
326 %.cpp: %.l
327         flex -t $< | sed '/^find_rule/d' | sed 's/void yyunput/inline void yyunput/' | sed 's/void \*yy_flex_realloc/inline void *yy_flex_realloc/' > $@
328
329 # Rule for building the bison parsers...
330
331 %.cpp %.h : %.y
332         bison -v -d -p $(<:%Parser.y=%) $(basename $@).y
333         mv -f $(basename $@).tab.c $(basename $@).cpp
334         mv -f $(basename $@).tab.h $(basename $@).h
335
336 # To create the directories...
337 %/.dir:
338         mkdir -p $(@D)
339         @date > $@
340
341 # Clean nukes the tree
342 clean::
343         rm -rf Debug Release Depend
344         rm -f core *.o *.d *.so *~ *.flc
345
346 # If dependancies were generated for the file that included this file,
347 # include the dependancies now...
348 #
349 SourceDepend = $(addsuffix .d,$(addprefix $(BUILD_ROOT)/Depend/,$(basename $(filter-out Debug/%, $(Source)))))
350 ifneq ($(SourceDepend),)
351 include $(SourceDepend)
352 endif