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