* Allow a profile'd code build to be done with a simple 'make ENABLE_PROFILING=1'
[oota-llvm.git] / Makefile.rules
1 #                                 Makefile.common
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 # Default Rule:  Make sure it's also a :: rule
25 all ::
26
27 # Default for install is to at least build everything...
28 install ::
29
30 #--------------------------------------------------------------------
31 # Installation configuration options... 
32 #--------------------------------------------------------------------
33
34 #BinInstDir=/usr/local/bin
35 #LibInstDir=/usrl/local/lib/xxx
36 #DocInstDir=/usr/doc/xxx
37
38 BURG = /home/vadve/vadve/Research/DynOpt/Burg/burg
39 BURG_OPTS = -I
40
41
42 PURIFY = /usr/dcs/applications/purify/bin/purify -cache-dir="$(HOME)/purifycache" -chain-length="30" -messages=all 
43
44 #---------------------------------------------------------
45 # Compilation options...
46 #---------------------------------------------------------
47
48 # Special tools used while building
49 RunBurg  = $(BURG) $(BURG_OPTS)
50
51 # Enable this for profiling support with 'gprof'
52 ifdef ENABLE_PROFILING
53 PROFILE = -pg
54 else
55 PROFILE =
56 endif
57
58 # TODO: Get rid of exceptions! : -fno-exceptions -fno-rtti
59 # -Wno-unused-parameter
60 CompileCommonOpts = $(PROFILE) -Wall -W  -Wwrite-strings -Wno-unused -I$(LEVEL)/include
61
62 # Compile a file, don't link...
63 Compile  = $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(CompileCommonOpts) $(PROFILE)
64 CompileG = $(Compile) -g  -D_DEBUG
65 CompileO = $(Compile) -O3 -DNDEBUG -finline-functions -felide-constructors -fnonnull-objects -freg-struct-return -fshort-enums
66
67 # Link final executable
68
69 # To enable purify, do it here:
70 ifdef ENABLE_PURIFY
71 Link     = $(PURIFY) $(CXX) $(Prof) -static
72 else
73 Link     = LD_RUN_PATH=/usr/dcs/software/evaluation/encap/gcc-3.0.4/lib $(CXX) $(PROFILE)
74 endif
75 LinkG    = $(Link) -g  -L $(LEVEL)/lib/Debug
76 LinkO    = $(Link) -O3 -L $(LEVEL)/lib/Release
77
78 # Create a .so file from a .cpp file...
79 #MakeSO   = $(CXX) -shared $(Prof)
80 MakeSO   = $(CXX) -G $(Prof)
81 MakeSOG  = $(MakeSO) -g
82 MakeSOO  = $(MakeSO) -O3
83
84 # Create dependancy file from CPP file, send to stdout.
85 Depend   = $(CXX) -MM -I$(LEVEL)/include $(CPPFLAGS) 
86
87 # Archive a bunch of .o files into a .a file...
88 AR       = ar cq 
89 MakeLib   = $(AR)
90
91 #----------------------------------------------------------
92
93 # Source includes all of the cpp files, and objects are derived from the
94 # source files...
95 # The local Makefile can list other Source files via ExtraSource = ...
96
97 Source  := $(ExtraSource) $(wildcard *.cpp *.c *.y *.l)
98
99 Objs := $(sort $(addsuffix .o,$(basename $(Source))))
100 ObjectsO = $(addprefix Release/,$(Objs))
101 ObjectsG = $(addprefix Debug/,$(Objs))
102
103 #---------------------------------------------------------
104 # Handle the DIRS option
105 #---------------------------------------------------------
106
107 ifdef DIRS  # Only do this if we're using DIRS!
108
109 all     :: $(addsuffix /.makeall    , $(DIRS))
110 install :: $(addsuffix /.makeinstall, $(DIRS))
111 clean   :: $(addsuffix /.makeclean  , $(DIRS))
112
113 %/.makeall %/.makeclean %/.makeinstall:
114         cd $(@D); $(MAKE) $(subst $(@D)/.make,,$@)
115 endif
116
117 #---------------------------------------------------------
118 # Handle the LIBRARYNAME option - used when building libs...
119 #---------------------------------------------------------
120
121 ifdef LIBRARYNAME
122
123 LIBNAME_O  := $(LEVEL)/lib/Release/lib$(LIBRARYNAME).so
124 LIBNAME_G  := $(LEVEL)/lib/Debug/lib$(LIBRARYNAME).so
125 LIBNAME_AO := $(LEVEL)/lib/Release/lib$(LIBRARYNAME).a
126 LIBNAME_AG := $(LEVEL)/lib/Debug/lib$(LIBRARYNAME).a
127
128 all:: $(LIBNAME_AG) ###$(LIBNAME_AO)
129 dynamic:: $(LIBNAME_G)
130 # TODO: Enable optimized builds
131
132 $(LIBNAME_O): $(ObjectsO) $(LibSubDirs) $(LEVEL)/lib/Release/.dir Depend/.dir
133         @echo ======= Linking $(LIBRARYNAME) release library =======
134         $(MakeSOO) -o $@ $(ObjectsO) $(LibSubDirs) $(LibLinkOpts)
135
136 $(LIBNAME_G): $(ObjectsG) $(LibSubDirs) $(LEVEL)/lib/Debug/.dir Depend/.dir
137         @echo ======= Linking $(LIBRARYNAME) debug library =======
138         $(MakeSOG) -o $@ $(ObjectsG) $(LibSubDirs) $(LibLinkOpts)
139
140 $(LIBNAME_AO): $(ObjectsO) $(LibSubDirs) $(LEVEL)/lib/Release/.dir Depend/.dir
141         @echo ======= Linking $(LIBRARYNAME) release library =======
142         @rm -f $@
143         $(MakeLib) $@ $(ObjectsO) $(LibSubDirs)
144
145 $(LIBNAME_AG): $(ObjectsG) $(LibSubDirs) $(LEVEL)/lib/Debug/.dir Depend/.dir
146         @echo ======= Linking $(LIBRARYNAME) debug library =======
147         @rm -f $@
148         $(MakeLib) $@ $(ObjectsG) $(LibSubDirs)
149
150 endif
151
152 #------------------------------------------------------------------------
153 # Create a TAGS database for emacs
154 #------------------------------------------------------------------------
155
156 ifeq ($(LEVEL), .)
157
158 tags:
159         etags -l c++ `find include lib tools -name '*.cpp' -o -name '*.h'`
160
161 all:: tags
162
163 endif
164
165 #------------------------------------------------------------------------
166 # Handle the TOOLNAME option - used when building tool executables...
167 #------------------------------------------------------------------------
168 #
169 # The TOOLNAME option should be used with a USEDLIBS variable that tells the
170 # libraries (and the order of the libs) that should be linked to the tool.
171 #
172 ifdef TOOLNAME
173
174 # TOOLEXENAME* - These compute the output filenames to generate...
175 TOOLEXENAME_G = $(LEVEL)/tools/Debug/$(TOOLNAME)
176 TOOLEXENAME_O = $(LEVEL)/tools/Release/$(TOOLNAME)
177 TOOLEXENAMES := $(TOOLEXENAME_G) ###$(TOOLEXENAME_O)
178
179 # USED_LIBS_OPTIONS - Compute the options line that add -llib1 -llib2, etc.
180 USED_LIBS_OPTIONS = $(addprefix -l, $(USEDLIBS))
181
182 # USED_LIB_PATHS - Compute the path of the libraries used so that tools are
183 # rebuilt if libraries change
184 #
185 STATICUSEDLIBS   := $(addsuffix .a, $(USEDLIBS))
186 USED_LIB_PATHS_G := $(addprefix $(LEVEL)/lib/Debug/lib, $(STATICUSEDLIBS))
187 USED_LIB_PATHS_O := $(addprefix $(LEVEL)/lib/Release/lib, $(STATICUSEDLIBS))
188
189 all::   $(TOOLEXENAMES)
190 clean::
191         rm -f $(TOOLEXENAMES)
192
193 $(TOOLEXENAME_G): $(ObjectsG) $(USED_LIB_PATHS_G) $(LEVEL)/tools/Debug/.dir
194         $(LinkG) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS) $(TOOLLINKOPTS)
195
196 $(TOOLEXENAME_O): $(ObjectsO) $(USED_LIB_PATHS_O) $(LEVEL)/tools/Release/.dir
197         $(LinkO) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS) $(TOOLLINKOPTS)
198
199 endif
200
201
202
203 #---------------------------------------------------------
204 .PRECIOUS: Depend/.dir Debug/.dir Release/.dir
205
206 # Create dependencies for the *.cpp files...
207 Depend/%.d: %.cpp Depend/.dir
208         $(Depend) $< | sed 's|$*\.o *|Release/& Debug/& Depend/$(@F)|g' > $@
209
210 # Create dependencies for the *.c files...
211 Depend/%.d: %.c Depend/.dir
212         $(Depend) $< | sed 's|$*\.o *|Release/& Debug/& Depend/$(@F)|g' > $@
213
214 # Create .o files in the ObjectFiles directory from the .cpp and .c files...
215 Release/%.o: %.cpp Release/.dir Depend/.dir
216         $(CompileO) $< -o $@
217
218 #Release/%.o: %.c Release/.dir Depend/.dir
219 #       $(CompileOC) $< -o $@
220
221 Debug/%.o: %.cpp Debug/.dir Depend/.dir
222         $(CompileG) $< -o $@
223
224 #Debug/%.o: %.c Debug/.dir Depend/.dir
225 #       $(CompileGC) $< -o $@
226
227 # Create a .cpp source file from a burg input file
228 %.burm.cpp: Debug/%.burg Debug/.dir
229         $(RunBurg) $< -o $@
230
231 # Create a .cpp source file from a flex input file... this uses sed to cut down
232 # on the warnings emited by GCC...
233 %.cpp: %.l
234         flex -t $< | sed '/^find_rule/d' | sed 's/void yyunput/inline void yyunput/' | sed 's/void \*yy_flex_realloc/inline void *yy_flex_realloc/' > $@
235
236 # Rule for building the bison parsers...
237
238 %.cpp %.h : %.y
239         bison -v -d -p $(<:%Parser.y=%) $(basename $@).y
240         mv -f $(basename $@).tab.c $(basename $@).cpp
241         mv -f $(basename $@).tab.h $(basename $@).h
242
243 # To create the directories...
244 %/.dir:
245         mkdir -p $(@D)
246         @date > $@
247
248 # Clean does not remove the output files... just the temporaries
249 clean::
250         rm -rf Debug Release Depend
251         rm -f core *.o *.d *.so *~ *.flc
252
253 # If dependancies were generated for the file that included this file,
254 # include the dependancies now...
255 #
256 SourceDepend = $(addsuffix .d,$(addprefix Depend/,$(basename $(Source))))
257 ifneq ($(SourceDepend),)
258 include $(SourceDepend)
259 endif