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