1 #===-- Makefile.common - Common make rules for LLVM -------*- makefile -*--====
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.
8 # The following functionality can be set by setting incoming variables.
9 # The variable $(LEVEL) *must* be set:
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.
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
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.
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
29 # 5. SourceDir - If specified, this specifies a directory that the source files
30 # are in, if they are not in the current directory. This should include a
31 # trailing / character.
33 #===-----------------------------------------------------------------------====
35 # Configuration file to set paths specific to local installation of LLVM
37 include $(LEVEL)/Makefile.config
39 # These are options that can either be enabled here, or can be enabled on the
40 # make command line (ie, make ENABLE_PROFILING=1)
43 # When ENABLE_PROFILING is enabled, the llvm source base is built with profile
44 # information to allow gprof to be used to get execution frequencies.
48 # When ENABLE_PURIFY is enabled, the LLVM tools are linked with purify (which
49 # must be locally installed) to allow for some automated memory error debugging.
53 # When ENABLE_OPTIMIZED is enabled, Release builds of all of the LLVM code are
54 # turned on, and Debug builds are turned off.
59 # Figure out how to do platform specific stuff on this platform. This is really
60 # gross and should be autoconfiscated (automake actually), but should hopefully
61 # work on Linux and solaris (SunOS).
63 UNAME := $(shell uname)
64 include $(LEVEL)/Makefile.$(UNAME)
67 # if SHARED_LIBRARY is specified, the default is to build the dynamic lib
71 # Default Rule: Make sure it's also a :: rule
74 # Default for install is to at least build everything...
78 # Figure out which directory to build stuff into. We want to build into the
79 # /shared directory by default because it is guaranteed to be local to the
82 ifeq ($(LLVM_OBJ_DIR),.)
83 BUILD_ROOT = $(LLVM_OBJ_DIR)
84 BUILD_ROOT_TOP = $(LEVEL)
87 BUILD_ROOT := $(LLVM_OBJ_DIR)$(patsubst $(HOME)%,%,$(shell pwd))
89 # Calculate the BUILD_ROOT_TOP variable, which is the top of the llvm/ tree.
90 # Note that although this is just equal to $(BUILD_ROOT)/$(LEVEL), we cannot use
91 # this expression because some of the directories on the source tree may not
92 # exist in the build tree (for example the test/ heirarchy). Thus we evaluate
93 # the directory to eliminate the ../'s
95 TOP_DIRECTORY := $(shell cd $(LEVEL); pwd)
96 BUILD_ROOT_TOP := $(LLVM_OBJ_DIR)$(patsubst $(HOME)%,%,$(TOP_DIRECTORY))
99 #--------------------------------------------------------------------
100 # Variables derived from configuration options...
101 #--------------------------------------------------------------------
103 #BinInstDir=/usr/local/bin
104 #LibInstDir=/usrl/local/lib/xxx
105 #DocInstDir=/usr/doc/xxx
109 PURIFY := $(PURIFY) -cache-dir="$(BUILD_ROOT_TOP)/../purifycache" -chain-length="30" -messages=all
111 ifdef ENABLE_PROFILING
113 CONFIGURATION := Profile
115 ifdef ENABLE_OPTIMIZED
116 CONFIGURATION := Release
118 CONFIGURATION := Debug
122 # Shorthand for commonly accessed directories
123 LIBDEBUG := $(BUILD_ROOT_TOP)/lib/Debug
124 LIBRELEASE := $(BUILD_ROOT_TOP)/lib/Release
125 LIBPROFILE := $(BUILD_ROOT_TOP)/lib/Profile
126 LIBCURRENT := $(BUILD_ROOT_TOP)/lib/$(CONFIGURATION)
128 TOOLDEBUG := $(BUILD_ROOT_TOP)/tools/Debug
129 TOOLRELEASE := $(BUILD_ROOT_TOP)/tools/Release
130 TOOLPROFILE := $(BUILD_ROOT_TOP)/tools/Profile
131 TOOLCURRENT := $(BUILD_ROOT_TOP)/tools/$(CONFIGURATION)
138 #---------------------------------------------------------
139 # Compilation options...
140 #---------------------------------------------------------
142 # Special tools used while building the LLVM tree. Burg is built as part of the
145 BURG := $(TOOLCURRENT)/burg
146 RunBurg := $(BURG) $(BURG_OPTS)
149 # Enable this for profiling support with 'gprof'
150 # This automatically enables optimized builds.
151 ifdef ENABLE_PROFILING
155 # By default, strip symbol information from executable
157 STRIP = $(PLATFORMSTRIPOPTS)
158 STRIP_WARN_MSG = "(without symbols) "
161 # Allow gnu extensions...
162 CPPFLAGS += -D_GNU_SOURCE
164 # -Wno-unused-parameter
165 CompileCommonOpts := -Wall -W -Wwrite-strings -Wno-unused -I$(LEVEL)/include
166 CompileOptimizeOpts := -O3 -DNDEBUG -finline-functions -fshort-enums
168 # Compile a cpp file, don't link...
169 Compile := $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(CompileCommonOpts)
170 CompileG := $(Compile) -g -D_DEBUG
171 CompileO := $(Compile) $(CompileOptimizeOpts) -felide-constructors -fomit-frame-pointer
172 CompileP := $(Compile) $(CompileOptimizeOpts) -felide-constructors $(PROFILE)
174 # Compile a c file, don't link...
175 CompileC := $(CC) -c $(CPPFLAGS) $(CCFLAGS) $(CompileCommonOpts)
176 CompileCG := $(CompileC) -g -D_DEBUG
177 CompileCO := $(CompileC) $(CompileOptimizeOpts) -fomit-frame-pointer
178 CompileCP := $(CompileC) $(CompileOptimizeOpts) $(PROFILE)
181 # Link final executable
183 ifdef ENABLE_PURIFY # To enable purify, build with 'gmake ENABLE_PURIFY=1'
184 Link := $(PURIFY) $(CXX) -static
188 LinkG := $(Link) -g -L$(LIBDEBUG) $(STRIP)
189 LinkO := $(Link) -O3 -L$(LIBRELEASE)
190 LinkP := $(Link) -O3 -L$(LIBPROFILE) $(PROFILE)
192 # Create one .o file from a bunch of .o files...
195 # MakeSO - Create a .so file from a .o files...
196 MakeSO := $(CXX) $(MakeSharedObjectOption)
197 MakeSOO := $(MakeSO) -O3
198 MakeSOP := $(MakeSOO) $(PROFILE)
200 # Create dependancy file from CPP file, send to stdout.
201 Depend := $(CXX) -MM -I$(LEVEL)/include $(CPPFLAGS)
202 DependC := $(CC) -MM -I$(LEVEL)/include $(CPPFLAGS)
204 # Archive a bunch of .o files into a .a file...
208 #----------------------------------------------------------
210 # Source includes all of the cpp files, and objects are derived from the
212 # The local Makefile can list other Source files via ExtraSource = ...
215 Source := $(ExtraSource) $(wildcard *.cpp *.c *.y *.l)
218 Objs := $(sort $(patsubst Debug/%.o, %.o, $(addsuffix .o,$(notdir $(basename $(Source))))))
219 ObjectsO := $(addprefix $(BUILD_ROOT)/Release/,$(Objs))
220 ObjectsP := $(addprefix $(BUILD_ROOT)/Profile/,$(Objs))
221 ObjectsG := $(addprefix $(BUILD_ROOT)/Debug/,$(Objs))
224 #---------------------------------------------------------
225 # Handle the DIRS and PARALLEL_DIRS options
226 #---------------------------------------------------------
229 all install clean test ::
230 $(VERB) for dir in ${DIRS}; do \
231 (cd $$dir; $(MAKE) $@) || exit 1; \
235 # Handle PARALLEL_DIRS
237 all :: $(addsuffix /.makeall , $(PARALLEL_DIRS))
238 install :: $(addsuffix /.makeinstall, $(PARALLEL_DIRS))
239 clean :: $(addsuffix /.makeclean , $(PARALLEL_DIRS))
240 test :: $(addsuffix /.maketest , $(PARALLEL_DIRS))
242 %/.makeall %/.makeinstall %/.makeclean %/.maketest:
243 $(VERB) cd $(@D); $(MAKE) $(subst $(@D)/.make,,$@)
246 #---------------------------------------------------------
247 # Handle the LIBRARYNAME option - used when building libs...
248 #---------------------------------------------------------
250 # When libraries are built, they are allowed to optionally define the
251 # DONT_BUILD_RELINKED make variable, which, when defined, prevents a .o file
252 # from being built for the library. This .o files may then be linked to by a
253 # tool if the tool does not need (or want) the semantics a .a file provides
254 # (linking in only object files that are "needed"). If a library is never to
255 # be used in this way, it is better to define DONT_BUILD_RELINKED, and define
256 # BUILD_ARCHIVE instead.
258 # Some libraries must be built as .a files (libscalar for example) because if
259 # it's built as a .o file, then all of the constituent .o files in it will be
260 # linked into tools (for example gccas) even if they only use one of the parts
261 # of it. For this reason, sometimes it's useful to use libraries as .a files.
265 # Make sure there isn't any extranous whitespace on the LIBRARYNAME option
266 LIBRARYNAME := $(strip $(LIBRARYNAME))
268 LIBNAME_O := $(LIBRELEASE)/lib$(LIBRARYNAME).so
269 LIBNAME_P := $(LIBPROFILE)/lib$(LIBRARYNAME).so
270 LIBNAME_G := $(LIBDEBUG)/lib$(LIBRARYNAME).so
271 LIBNAME_AO := $(LIBRELEASE)/lib$(LIBRARYNAME).a
272 LIBNAME_AP := $(LIBPROFILE)/lib$(LIBRARYNAME).a
273 LIBNAME_AG := $(LIBDEBUG)/lib$(LIBRARYNAME).a
274 LIBNAME_OBJO := $(LIBRELEASE)/$(LIBRARYNAME).o
275 LIBNAME_OBJP := $(LIBPROFILE)/$(LIBRARYNAME).o
276 LIBNAME_OBJG := $(LIBDEBUG)/$(LIBRARYNAME).o
278 # dynamic target builds a shared object version of the library...
279 dynamic:: $(LIBCURRENT)/lib$(LIBRARYNAME).so
281 # Does the library want a .o version built?
282 ifndef DONT_BUILD_RELINKED
283 all:: $(LIBCURRENT)/$(LIBRARYNAME).o
286 # Does the library want an archive version built?
288 all:: $(LIBCURRENT)/lib$(LIBRARYNAME).a
291 $(LIBNAME_O): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
292 @echo ======= Linking $(LIBRARYNAME) release library =======
293 $(VERB) $(MakeSOO) -o $@ $(ObjectsO) $(LibSubDirs) $(LibLinkOpts)
295 $(LIBNAME_P): $(ObjectsP) $(LibSubDirs) $(LIBPROFILE)/.dir
296 @echo ======= Linking $(LIBRARYNAME) profile library =======
297 $(VERB) $(MakeSOP) -o $@ $(ObjectsP) $(LibSubDirs) $(LibLinkOpts)
299 $(LIBNAME_G): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
300 @echo ======= Linking $(LIBRARYNAME) debug library =======
301 $(VERB) $(MakeSO) -g -o $@ $(ObjectsG) $(LibSubDirs) $(LibLinkOpts)
303 $(LIBNAME_AO): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
304 @echo ======= Linking $(LIBRARYNAME) release library =======
306 $(VERB) $(AR) $@ $(ObjectsO) $(LibSubDirs)
308 $(LIBNAME_AP): $(ObjectsP) $(LibSubDirs) $(LIBPROFILE)/.dir
309 @echo ======= Linking $(LIBRARYNAME) profile library =======
311 $(VERB) $(AR) $@ $(ObjectsP) $(LibSubDirs)
313 $(LIBNAME_AG): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
314 @echo ======= Linking $(LIBRARYNAME) debug library =======
316 $(VERB) $(AR) $@ $(ObjectsG) $(LibSubDirs)
318 $(LIBNAME_OBJO): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
320 $(VERB) $(Relink) -o $@ $(ObjectsO) $(LibSubDirs)
322 $(LIBNAME_OBJP): $(ObjectsP) $(LibSubDirs) $(LIBPROFILE)/.dir
324 $(VERB) $(Relink) -o $@ $(ObjectsP) $(LibSubDirs)
326 $(LIBNAME_OBJG): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
328 $(VERB) $(Relink) -o $@ $(ObjectsG) $(LibSubDirs)
332 #------------------------------------------------------------------------
333 # Create a TAGS database for emacs
334 #------------------------------------------------------------------------
338 etags -l c++ `find include lib tools -name '*.cpp' -o -name '*.h'`
342 #------------------------------------------------------------------------
343 # Handle the TOOLNAME option - used when building tool executables...
344 #------------------------------------------------------------------------
346 # The TOOLNAME option should be used with a USEDLIBS variable that tells the
347 # libraries (and the order of the libs) that should be linked to the
348 # tool. USEDLIBS should contain a list of library names (some with .a extension)
349 # that are automatically linked in as .o files unless the .a suffix is added.
353 # TOOLEXENAME* - These compute the output filenames to generate...
354 TOOLEXENAME_G := $(BUILD_ROOT_TOP)/tools/Debug/$(TOOLNAME)
355 TOOLEXENAME_O := $(BUILD_ROOT_TOP)/tools/Release/$(TOOLNAME)
356 TOOLEXENAME_P := $(BUILD_ROOT_TOP)/tools/Profile/$(TOOLNAME)
357 TOOLEXENAMES := $(BUILD_ROOT_TOP)/tools/$(CONFIGURATION)/$(TOOLNAME)
359 # USED_LIBS_OPTIONS - Compute the options line that add -llib1 -llib2, etc.
360 USED_LIBS_OPTIONS := $(patsubst %.a.o, -l%, $(addsuffix .o, $(USEDLIBS)))
361 USED_LIBS_OPTIONS_G := $(patsubst %.o, $(LIBDEBUG)/%.o, $(USED_LIBS_OPTIONS))
362 USED_LIBS_OPTIONS_O := $(patsubst %.o, $(LIBRELEASE)/%.o,$(USED_LIBS_OPTIONS))
363 USED_LIBS_OPTIONS_P := $(patsubst %.o, $(LIBPROFILE)/%.o,$(USED_LIBS_OPTIONS))
366 # USED_LIB_PATHS - Compute the path of the libraries used so that tools are
367 # rebuilt if libraries change. This has to make sure to handle .a/.so and .o
370 STATICUSEDLIBS := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(USEDLIBS)))
371 USED_LIB_PATHS_G := $(addprefix $(LIBDEBUG)/, $(STATICUSEDLIBS))
372 USED_LIB_PATHS_O := $(addprefix $(LIBRELEASE)/, $(STATICUSEDLIBS))
373 USED_LIB_PATHS_P := $(addprefix $(LIBPROFILE)/, $(STATICUSEDLIBS))
374 LINK_OPTS := $(TOOLLINKOPTS) $(PLATFORMLINKOPTS)
377 # Tell make that we need to rebuild subdirectories before we can link the tool.
378 # This affects things like LLI which has library subdirectories.
379 $(USED_LIB_PATHS_G) $(USED_LIB_PATHS_O) $(USED_LIB_PATHS_P): \
380 $(addsuffix /.makeall, $(PARALLEL_DIRS))
382 all:: $(TOOLEXENAMES)
384 $(VERB) rm -f $(TOOLEXENAMES)
386 $(TOOLEXENAME_G): $(ObjectsG) $(USED_LIB_PATHS_G) $(TOOLDEBUG)/.dir
387 @echo ======= Linking $(TOOLNAME) debug executable $(STRIP_WARN_MSG) =======
388 $(VERB) $(LinkG) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS_G) $(LINK_OPTS)
390 $(TOOLEXENAME_O): $(ObjectsO) $(USED_LIB_PATHS_O) $(TOOLRELEASE)/.dir
391 @echo ======= Linking $(TOOLNAME) release executable =======
392 $(VERB) $(LinkO) -o $@ $(ObjectsO) $(USED_LIBS_OPTIONS_O) $(LINK_OPTS)
394 $(TOOLEXENAME_P): $(ObjectsP) $(USED_LIB_PATHS_P) $(TOOLPROFILE)/.dir
395 @echo ======= Linking $(TOOLNAME) profile executable =======
396 $(VERB) $(LinkP) -o $@ $(ObjectsP) $(USED_LIBS_OPTIONS_P) $(LINK_OPTS)
402 #---------------------------------------------------------
403 .PRECIOUS: $(BUILD_ROOT)/Depend/.dir
404 .PRECIOUS: $(BUILD_ROOT)/Debug/.dir $(BUILD_ROOT)/Release/.dir
406 # Create .o files in the ObjectFiles directory from the .cpp and .c files...
407 $(BUILD_ROOT)/Release/%.o: $(SourceDir)%.cpp $(BUILD_ROOT)/Release/.dir
409 $(VERB) $(CompileO) $< -o $@
411 $(BUILD_ROOT)/Release/%.o: $(SourceDir)%.c $(BUILD_ROOT)/Release/.dir
412 $(VERB) $(CompileCO) $< -o $@
414 $(BUILD_ROOT)/Profile/%.o: $(SourceDir)%.cpp $(BUILD_ROOT)/Profile/.dir
416 $(VERB) $(CompileP) $< -o $@
418 $(BUILD_ROOT)/Profile/%.o: $(SourceDir)%.c $(BUILD_ROOT)/Profile/.dir
420 $(VERB) $(CompileCP) $< -o $@
422 $(BUILD_ROOT)/Debug/%.o: $(SourceDir)%.cpp $(BUILD_ROOT)/Debug/.dir
424 $(VERB) $(CompileG) $< -o $@
426 $(BUILD_ROOT)/Debug/%.o: $(SourceDir)%.c $(BUILD_ROOT)/Debug/.dir
427 $(VERB) $(CompileCG) $< -o $@
430 # Rules for building lex/yacc files
432 LEX_FILES = $(filter %.l, $(Source))
433 LEX_OUTPUT = $(LEX_FILES:%.l=%.cpp)
434 YACC_FILES = $(filter %.y, $(Source))
435 YACC_OUTPUT = $(addprefix $(YACC_FILES:%.y=%), .h .cpp .output)
436 .PRECIOUS: $(LEX_OUTPUT) $(YACC_OUTPUT)
438 # Create a .cpp source file from a flex input file... this uses sed to cut down
439 # on the warnings emited by GCC...
441 # The last line is a gross hack to work around flex aparently not being able to
442 # resize the buffer on a large token input. Currently, for uninitialized string
443 # buffers in LLVM we can generate very long tokens, so this is a hack around it.
444 # FIXME. (f.e. char Buffer[10000]; )
447 flex -t $< | sed '/^find_rule/d' | \
448 sed 's/void yyunput/inline void yyunput/' | \
449 sed 's/void \*yy_flex_realloc/inline void *yy_flex_realloc/' | \
450 sed 's/#define YY_BUF_SIZE 16384/#define YY_BUF_SIZE (16384*64)/' > $@
452 # Rule for building the bison parsers...
453 %.c: %.y # Cancel built-in rules for yacc
454 %.h: %.y # Cancel built-in rules for yacc
456 @echo Bison\'ing $<...
457 $(VERB) $(BISON) -v -d -p $(<:%Parser.y=%) $*.y
458 $(VERB) mv -f $*.tab.c $*.cpp
459 $(VERB) mv -f $*.tab.h $*.h
461 # To create the directories...
466 # To create postscript files from dot files...
470 # 'make clean' nukes the tree
472 $(VERB) rm -rf $(BUILD_ROOT)/Debug $(BUILD_ROOT)/Release $(BUILD_ROOT)/Profile $(BUILD_ROOT)/Depend
473 $(VERB) rm -f core core.[0-9][0-9]* *.o *.d *.so *~ *.flc
474 $(VERB) rm -f $(LEX_OUTPUT) $(YACC_OUTPUT)
476 # If dependencies were generated for the file that included this file,
477 # include the dependancies now...
479 SourceBaseNames := $(basename $(notdir $(filter-out Debug/%, $(Source))))
480 SourceDepend := $(SourceBaseNames:%=$(BUILD_ROOT)/Depend/%.d)
482 # Create dependencies for the *.cpp files...
484 $(BUILD_ROOT)/Depend/%.d: $(SourceDir)%.cpp $(BUILD_ROOT)/Depend/.dir
485 $(VERB) $(Depend) $< | sed 's|$*\.o *|$(BUILD_ROOT)/Release/& $(BUILD_ROOT)/Profile/& $(BUILD_ROOT)/Debug/& $(BUILD_ROOT)/Depend/$(@F)|g' > $@
487 # Create dependencies for the *.c files...
489 $(BUILD_ROOT)/Depend/%.d: $(SourceDir)%.c $(BUILD_ROOT)/Depend/.dir
490 $(VERB) $(DependC) $< | sed 's|$*\.o *|Release/& Profile/& Debug/& Depend/$(@F)|g' > $@
492 ifneq ($(SourceDepend),)
493 -include $(SourceDepend)