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