Makefile.common: Remove commented-out and duplicate rules.
[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 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 # 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.
32 #
33 # 6. LLVM_SRC_ROOT - If specified, points to the top of the LLVM source tree.
34 #
35 # 7. LLVM_OBJ_ROOT - If specified, points to the top directory where LLVM
36 #    object files are placed.
37 #
38 # 8. BUILD_SRC_DIR - The directory which contains the current set of Makefiles
39 #    and usually the source code too (unless SourceDir is set).
40 #
41 # 9. BUILD_SRC_ROOT - The root directory of the source code being compiled.
42 #
43 # 10. BUILD_OBJ_DIR - The directory where object code should be placed.
44 #
45 # 11. BUILD_OBJ_ROOT - The root directory for where object code should be
46 #     placed.
47 #
48 # For building,
49 #       LLVM, LLVM_SRC_ROOT = BUILD_SRC_ROOT, and
50 #       LLVM_OBJ_ROOT = BUILD_OBJ_ROOT.
51 #===-----------------------------------------------------------------------====
52
53 #
54 # Configuration file to set paths specific to local installation of LLVM
55
56 include $(LEVEL)/Makefile.config
57
58 ###########################################################################
59 # Directory Configuration
60 #       This section of the Makefile determines what is where.  To be
61 #       specific, there are several locations that need to be defined:
62 #
63 #       o LLVM_SRC_ROOT  : The root directory of the LLVM source code.
64 #       o LLVM_OBJ_ROOT  : The root directory containing the built LLVM code.
65 #
66 #       o BUILD_SRC_DIR  : The directory containing the code to build.
67 #       o BUILD_SRC_ROOT : The root directory of the code to build.
68 #
69 #       o BUILD_OBJ_DIR  : The directory in which compiled code will be placed.
70 #       o BUILD_OBJ_ROOT : The root directory in which compiled code is placed.
71 #
72 ###########################################################################
73
74 #
75 # Set the source build directory.  That is almost always the current directory.
76 #
77 ifndef BUILD_SRC_DIR
78 BUILD_SRC_DIR = $(shell pwd)
79 endif
80
81 #
82 # Set the source root directory.
83 #
84 ifndef BUILD_SRC_ROOT
85 BUILD_SRC_ROOT = $(shell cd $(BUILD_SRC_DIR)/$(LEVEL); pwd)
86 endif
87
88 #
89 # Determine the path of the source tree relative from $HOME (the mythical
90 # home directory).
91 #
92 HOME_OBJ_ROOT := $(OBJ_ROOT)$(patsubst $(HOME)%,%,$(BUILD_SRC_ROOT))
93
94 #
95 # Set the object build directory.  Its location depends upon the source path
96 # and where object files should go.
97 #
98 ifndef BUILD_OBJ_DIR
99 ifeq ($(OBJ_ROOT),.)
100 BUILD_OBJ_DIR = $(BUILD_SRC_DIR)
101 else
102 BUILD_OBJ_DIR := $(HOME_OBJ_ROOT)$(patsubst $(BUILD_SRC_ROOT)%,%,$(BUILD_SRC_DIR))
103 endif
104 endif
105
106 #
107 # Set the root of the object directory.
108 #
109 ifndef BUILD_OBJ_ROOT
110 ifeq ($(OBJ_ROOT),.)
111 BUILD_OBJ_ROOT = $(BUILD_SRC_ROOT)
112 else
113 BUILD_OBJ_ROOT := $(HOME_OBJ_ROOT)
114 endif
115 endif
116
117 #
118 # Set the LLVM source directory.
119 # It is typically the root directory of what we're compiling now.
120 #
121 ifndef LLVM_SRC_ROOT
122 LLVM_SRC_ROOT = $(BUILD_SRC_ROOT)
123 endif
124
125 #
126 # Set the LLVM object directory.
127 #
128 ifndef LLVM_OBJ_ROOT
129 LLVM_OBJ_ROOT = $(BUILD_OBJ_ROOT)
130 endif
131
132 ###########################################################################
133 # Default Targets:
134 #       The following targets are the standard top level targets for
135 #       building.
136 ###########################################################################
137
138 ifdef SHARED_LIBRARY
139 # if SHARED_LIBRARY is specified, the default is to build the dynamic lib
140 all:: dynamic
141 endif
142
143 # Default Rule:  Make sure it's also a :: rule
144 all ::
145
146 # Default for install is to at least build everything...
147 install ::
148
149 # Default rule for test.  It ensures everything has a test rule
150 test::
151
152 # Default rule for building only bytecode.
153 bytecode::
154
155 # Print out the directories used for building
156 prdirs::
157         echo "Home Offset      : " $(HOME_OBJ_ROOT);
158         echo "Build Source Root: " $(BUILD_SRC_ROOT);
159         echo "Build Source Dir : " $(BUILD_SRC_DIR);
160         echo "Build Object Root: " $(BUILD_OBJ_ROOT);
161         echo "Build Object Dir : " $(BUILD_OBJ_DIR);
162         echo "LLVM  Source Root: " $(LLVM_SRC_ROOT);
163         echo "LLVM  Object Root: " $(LLVM_OBJ_ROOT);
164
165 #
166 # Mark all of these targets as phony.  This will hopefully speed up builds
167 # slightly since GNU Make will not try to find implicit rules for targets
168 # which are marked as Phony.
169 #
170 .PHONY: all dynamic clean distclean install test bytecode prdirs
171
172 ###########################################################################
173 # Miscellaneous paths and commands:
174 #       This section defines various configuration macros, such as where
175 #       to find burg, tblgen, and libtool.
176 ###########################################################################
177
178 #--------------------------------------------------------------------
179 # Variables derived from configuration options... 
180 #--------------------------------------------------------------------
181
182 #BinInstDir=/usr/local/bin
183 #LibInstDir=/usr/local/lib/xxx
184 #DocInstDir=/usr/doc/xxx
185
186 BURG_OPTS = -I
187
188 PURIFY := $(PURIFY) -cache-dir="$(BUILD_OBJ_ROOT)/../purifycache" -chain-length="30" -messages=all 
189
190 ifdef ENABLE_PROFILING
191   ENABLE_OPTIMIZED = 1
192   CONFIGURATION := Profile
193 else
194   ifdef ENABLE_OPTIMIZED
195     CONFIGURATION := Release
196   else
197     CONFIGURATION := Debug
198   endif
199 endif
200
201 #
202 # Enable this for profiling support with 'gprof'
203 # This automatically enables optimized builds.
204 #
205 ifdef ENABLE_PROFILING
206   PROFILE = -pg
207 endif
208
209 #
210 # Suffixes for library compilation rules
211 #
212 .SUFFIXES: .so
213
214 ###########################################################################
215 # Library Locations:
216 #       These variables describe various library locations:
217 #
218 #       DEST* = Location of where libraries that are built will be placed.
219 #       LLVM* = Location of LLVM libraries used for linking.
220 #       PROJ* = Location of previously built libraries used for linking.
221 ###########################################################################
222
223 # Libraries that are being built
224 DESTLIBDEBUG    := $(BUILD_OBJ_ROOT)/lib/Debug
225 DESTLIBRELEASE  := $(BUILD_OBJ_ROOT)/lib/Release
226 DESTLIBPROFILE  := $(BUILD_OBJ_ROOT)/lib/Profile
227 DESTLIBCURRENT  := $(BUILD_OBJ_ROOT)/lib/$(CONFIGURATION)
228
229 # LLVM libraries used for linking
230 LLVMLIBDEBUGSOURCE    := $(LLVM_OBJ_ROOT)/lib/Debug
231 LLVMLIBRELEASESOURCE  := $(LLVM_OBJ_ROOT)/lib/Release
232 LLVMLIBPROFILESOURCE  := $(LLVM_OBJ_ROOT)/lib/Profile
233 LLVMLIBCURRENTSOURCE  := $(LLVM_OBJ_ROOT)/lib/$(CONFIGURATION)
234
235 # Libraries that were built that will now be used for linking
236 PROJLIBDEBUGSOURCE    := $(BUILD_OBJ_ROOT)/lib/Debug
237 PROJLIBRELEASESOURCE  := $(BUILD_OBJ_ROOT)/lib/Release
238 PROJLIBPROFILESOURCE  := $(BUILD_OBJ_ROOT)/lib/Profile
239 PROJLIBCURRENTSOURCE  := $(BUILD_OBJ_ROOT)/lib/$(CONFIGURATION)
240
241 ###########################################################################
242 # Tool Locations
243 #       These variables describe various tool locations:
244 #
245 #       DEST* = Location of where tools that are built will be placed.
246 #       LLVM* = Location of LLVM tools used for building.
247 #       PROJ* = Location of previously built tools used for linking.
248 ###########################################################################
249
250 DESTTOOLDEBUG   := $(BUILD_OBJ_ROOT)/tools/Debug
251 DESTTOOLRELEASE := $(BUILD_OBJ_ROOT)/tools/Release
252 DESTTOOLPROFILE := $(BUILD_OBJ_ROOT)/tools/Profile
253 DESTTOOLCURRENT := $(BUILD_OBJ_ROOT)/tools/$(CONFIGURATION)
254
255 LLVMTOOLDEBUG   := $(LLVM_OBJ_ROOT)/tools/Debug
256 LLVMTOOLRELEASE := $(LLVM_OBJ_ROOT)/tools/Release
257 LLVMTOOLPROFILE := $(LLVM_OBJ_ROOT)/tools/Profile
258 LLVMTOOLCURRENT := $(LLVM_OBJ_ROOT)/tools/$(CONFIGURATION)
259
260 PROJTOOLDEBUG   := $(BUILD_OBJ_ROOT)/tools/Debug
261 PROJTOOLRELEASE := $(BUILD_OBJ_ROOT)/tools/Release
262 PROJTOOLPROFILE := $(BUILD_OBJ_ROOT)/tools/Profile
263 PROJTOOLCURRENT := $(BUILD_OBJ_ROOT)/tools/$(CONFIGURATION)
264
265 #
266 # Libtool is found in the current directory.
267 #
268 ifdef VERBOSE
269 LIBTOOL=$(LLVM_SRC_ROOT)/mklib
270 else
271 LIBTOOL=$(LLVM_SRC_ROOT)/mklib --silent
272 endif
273
274 #
275 # If we're not building a shared library, use the disable-shared tag with
276 # libtool.  This will disable the building of objects for shared libraries and
277 # only generate static library objects.
278 #
279 # For dynamic libraries, we'll take the performance hit for now, since we
280 # almost never build them.
281 #
282 # This should speed up compilation and require no modifications to future
283 # versions of libtool.
284 #
285 ifndef SHARED_LIBRARY
286 LIBTOOL := $(LIBTOOL) --tag=disable-shared
287 endif
288
289 #
290 # Verbosity levels
291 #
292 ifndef VERBOSE
293 VERB := @
294 endif
295
296 ###########################################################################
297 # Miscellaneous paths and commands (part deux):
298 #       This section defines various configuration macros, such as where
299 #       to find burg, tblgen, and libtool.
300 ###########################################################################
301
302 #--------------------------------------------------------------------------
303 # Special tools used while building the LLVM tree.  Burg is built as part
304 # of the utils directory.
305 #--------------------------------------------------------------------------
306 BURG    := $(LLVMTOOLCURRENT)/burg
307 RunBurg := $(BURG) $(BURG_OPTS)
308
309 TBLGEN  := $(LLVMTOOLCURRENT)/tblgen
310
311
312 ###########################################################################
313 # Compile Time Flags
314 ###########################################################################
315
316 #
317 # Include both the project headers and the LLVM headers for compilation and
318 # dependency computation.
319 #
320 CPPFLAGS += -I$(BUILD_SRC_ROOT)/include -I$(LLVM_SRC_ROOT)/include
321
322 # By default, strip symbol information from executable
323 ifndef KEEP_SYMBOLS
324 STRIP = $(PLATFORMSTRIPOPTS)
325 STRIP_WARN_MSG = "(without symbols)"
326 endif
327
328 # Allow gnu extensions...
329 CPPFLAGS += -D_GNU_SOURCE
330
331 # -Wno-unused-parameter
332 CompileCommonOpts := -Wall -W  -Wwrite-strings -Wno-unused -I$(LEVEL)/include
333 CompileOptimizeOpts := -O3 -DNDEBUG -finline-functions -fshort-enums
334
335 #
336 # Compile commands with libtool.
337 #
338 Compile  := $(LIBTOOL) --mode=compile $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(CompileCommonOpts)
339 CompileC  := $(LIBTOOL) --mode=compile $(CC) -c $(CPPFLAGS) $(CCFLAGS) $(CompileCommonOpts)
340
341 # Compile a cpp file, don't link...
342 CompileG := $(Compile) -g  -D_DEBUG
343 CompileO := $(Compile) $(CompileOptimizeOpts) -felide-constructors -fomit-frame-pointer
344 CompileP := $(Compile) $(CompileOptimizeOpts) -felide-constructors $(PROFILE)
345
346 # Compile a c file, don't link...
347 CompileCG := $(CompileC) -g  -D_DEBUG
348 CompileCO := $(CompileC) $(CompileOptimizeOpts) -fomit-frame-pointer
349 CompileCP := $(CompileC) $(CompileOptimizeOpts) $(PROFILE)
350
351 ###########################################################################
352 # Link Time Options
353 ###########################################################################
354
355 #
356 # Link final executable
357 #       (Note that we always link with the C++ compiler).
358 #
359 ifdef ENABLE_PURIFY # To enable purify, build with 'gmake ENABLE_PURIFY=1'
360 Link     := $(PURIFY) $(LIBTOOL) --mode=link $(CXX) -static
361 else
362 Link     := $(LIBTOOL) --mode=link $(CXX)
363 endif
364
365 # link both projlib and llvmlib libraries
366 LinkG    := $(Link) -g -L$(PROJLIBDEBUGSOURCE)  -L$(LLVMLIBDEBUGSOURCE) $(STRIP)
367 LinkO    := $(Link) -O3 -L$(PROJLIBRELEASESOURCE) -L$(LLVMLIBRELEASESOURCE)
368 LinkP    := $(Link) -O3 -L$(PROJLIBPROFILESOURCE) -L$(LLVMLIBPROFILESOURCE) $(PROFILE)
369
370 # Create one .o file from a bunch of .o files...
371 #ifdef SHARED_LIBRARY
372 Relink = ${LIBTOOL} --mode=link $(CXX)
373 #else
374 Relink = ${LIBTOOL} --mode=link $(CXX) -only-static
375 #endif
376
377 # MakeSO - Create a .so file from a .o files...
378 #MakeSO   := $(LIBTOOL) --mode=link $(CXX) $(MakeSharedObjectOption)
379 #MakeSOO  := $(MakeSO) -O3
380 #MakeSOP  := $(MakeSOO) $(PROFILE)
381
382 #
383 # Configure where the item being compiled should go.
384 #
385 ifdef SHARED_LIBRARY
386 Link := $(Link) -rpath $(DESTLIBCURRENT)
387 endif
388
389 ifdef TOOLNAME
390 Link := $(Link) -rpath $(DESTTOOLCURRENT)
391 endif
392
393 # Create dependancy file from CPP file, send to stdout.
394 Depend   := $(CXX) -MM -I$(LEVEL)/include $(CPPFLAGS) 
395 DependC  := $(CC)  -MM -I$(LEVEL)/include $(CPPFLAGS) 
396
397 # Archive a bunch of .o files into a .a file...
398 AR       = ${AR_PATH} cq 
399
400 #----------------------------------------------------------
401
402 # Source includes all of the cpp files, and objects are derived from the
403 # source files...
404 # The local Makefile can list other Source files via ExtraSource = ...
405
406 ifndef Source
407 Source  := $(ExtraSource) $(wildcard *.cpp *.c *.y *.l)
408 endif
409
410 #
411 # Libtool Objects
412 #
413 Objs := $(sort $(patsubst Debug/%.lo, %.lo, $(addsuffix .lo,$(notdir $(basename $(Source))))))
414 ObjectsO := $(addprefix $(BUILD_OBJ_DIR)/Release/,$(Objs))
415 ObjectsP := $(addprefix $(BUILD_OBJ_DIR)/Profile/,$(Objs))
416 ObjectsG := $(addprefix $(BUILD_OBJ_DIR)/Debug/,$(Objs))
417
418 #
419 # The real objects underlying the libtool objects
420 #
421 RObjs := $(sort $(patsubst Debug/%.o, %.o, $(addsuffix .o,$(notdir $(basename $(Source))))))
422 RObjectsO := $(addprefix $(BUILD_OBJ_DIR)/Release/,$(RObjs))
423 RObjectsP := $(addprefix $(BUILD_OBJ_DIR)/Profile/,$(RObjs))
424 RObjectsG := $(addprefix $(BUILD_OBJ_DIR)/Debug/,$(RObjs))
425
426 #---------------------------------------------------------
427 # Handle the DIRS and PARALLEL_DIRS options
428 #---------------------------------------------------------
429
430 ifdef DIRS
431 all install clean test bytecode ::
432         $(VERB) for dir in ${DIRS}; do \
433                 (cd $$dir; $(MAKE) $@) || exit 1; \
434         done
435 endif
436
437 # Handle PARALLEL_DIRS
438 ifdef PARALLEL_DIRS
439 all      :: $(addsuffix /.makeall     , $(PARALLEL_DIRS))
440 install  :: $(addsuffix /.makeinstall , $(PARALLEL_DIRS))
441 clean    :: $(addsuffix /.makeclean   , $(PARALLEL_DIRS))
442 test     :: $(addsuffix /.maketest    , $(PARALLEL_DIRS))
443 bytecode :: $(addsuffix /.makebytecode, $(PARALLEL_DIRS))
444
445 %/.makeall %/.makeinstall %/.makeclean %/.maketest %/.makebytecode:
446         $(VERB) cd $(@D); $(MAKE) $(subst $(@D)/.make,,$@)
447 endif
448
449 # Handle directories that may or may not exist
450 ifdef OPTIONAL_DIRS
451 all install clean test bytecode ::
452         $(VERB) for dir in ${OPTIONAL_DIRS}; do \
453                 if [ -d $$dir ]; \
454                 then\
455                         (cd $$dir; $(MAKE) $@) || exit 1; \
456                 fi \
457         done
458 endif
459
460 ###########################################################################
461 # Library Build Rules:
462 #
463 #---------------------------------------------------------
464 # Handle the LIBRARYNAME option - used when building libs...
465 #---------------------------------------------------------
466 #
467 #  When libraries are built, they are allowed to optionally define the
468 #  DONT_BUILD_RELINKED make variable, which, when defined, prevents a .o file
469 #  from being built for the library. This .o files may then be linked to by a
470 #  tool if the tool does not need (or want) the semantics a .a file provides
471 #  (linking in only object files that are "needed").  If a library is never to
472 #  be used in this way, it is better to define DONT_BUILD_RELINKED, and define
473 #  BUILD_ARCHIVE instead.
474 #
475 #  Some libraries must be built as .a files (libscalar for example) because if
476 #  it's built as a .o file, then all of the constituent .o files in it will be
477 #  linked into tools (for example gccas) even if they only use one of the parts
478 #  of it.  For this reason, sometimes it's useful to use libraries as .a files.
479 ###########################################################################
480
481 ifdef LIBRARYNAME
482
483 # Make sure there isn't any extranous whitespace on the LIBRARYNAME option
484 LIBRARYNAME := $(strip $(LIBRARYNAME))
485
486 LIBNAME_O    := $(DESTLIBRELEASE)/lib$(LIBRARYNAME).so
487 LIBNAME_P    := $(DESTLIBPROFILE)/lib$(LIBRARYNAME).so
488 LIBNAME_G    := $(DESTLIBDEBUG)/lib$(LIBRARYNAME).so
489 LIBNAME_AO   := $(DESTLIBRELEASE)/lib$(LIBRARYNAME).a
490 LIBNAME_AP   := $(DESTLIBPROFILE)/lib$(LIBRARYNAME).a
491 LIBNAME_AG   := $(DESTLIBDEBUG)/lib$(LIBRARYNAME).a
492 LIBNAME_OBJO := $(DESTLIBRELEASE)/$(LIBRARYNAME).o
493 LIBNAME_OBJP := $(DESTLIBPROFILE)/$(LIBRARYNAME).o
494 LIBNAME_OBJG := $(DESTLIBDEBUG)/$(LIBRARYNAME).o
495
496 #--------------------------------------------------------------------
497 # Library Targets
498 #       Modify the top level targets to build the desired libraries.
499 #--------------------------------------------------------------------
500
501 # dynamic target builds a shared object version of the library...
502 dynamic:: $(DESTLIBCURRENT)/lib$(LIBRARYNAME).so
503
504 # Does the library want a .o version built?
505 ifndef DONT_BUILD_RELINKED
506 all:: $(DESTLIBCURRENT)/$(LIBRARYNAME).o
507 endif
508
509 # Does the library want an archive version built?
510 ifdef BUILD_ARCHIVE
511 all:: $(DESTLIBCURRENT)/lib$(LIBRARYNAME).a
512 endif
513
514 #--------------------------------------------------------------------
515 # Rules for building libraries
516 #--------------------------------------------------------------------
517
518 #
519 # Rules for building dynamically linked libraries.
520 #
521 $(LIBNAME_O): $(ObjectsO) $(LibSubDirs) $(DESTLIBRELEASE)/.dir
522         @echo ======= Linking $(LIBRARYNAME) dynamic release library =======
523         $(VERB) $(Link) -o $*.la $(ObjectsO) $(LibSubDirs) $(LibLinkOpts);
524         $(VERB) $(LIBTOOL) --mode=install $(INSTALL) $*.la $(DESTLIBCURRENT);
525
526 $(LIBNAME_P): $(ObjectsP) $(LibSubDirs) $(DESTLIBPROFILE)/.dir
527         @echo ======= Linking $(LIBRARYNAME) dynamic profile library =======
528         $(VERB) $(Link) -o $*.la $(ObjectsP) $(LibSubDirs) $(LibLinkOpts);
529         $(VERB) $(LIBTOOL) --mode=install $(INSTALL) $*.la $(DESTLIBCURRENT);
530
531 $(LIBNAME_G): $(ObjectsG) $(LibSubDirs) $(DESTLIBDEBUG)/.dir
532         @echo ======= Linking $(LIBRARYNAME) dynamic debug library =======
533         $(VERB) $(Link) -o $*.la $(ObjectsG) $(LibSubDirs) $(LibLinkOpts);
534         $(VERB) $(LIBTOOL) --mode=install $(INSTALL) $*.la $(DESTLIBCURRENT);
535
536 #
537 # Rules for building static archive libraries.
538 #
539 $(LIBNAME_AO): $(ObjectsO) $(LibSubDirs) $(DESTLIBRELEASE)/.dir
540         @echo ======= Linking $(LIBRARYNAME) archive release library =======
541         @$(RM) -f $@
542         $(VERB) $(Link) -03 -o $@ $(ObjectsO) $(LibSubDirs) -static
543
544 $(LIBNAME_AP): $(ObjectsP) $(LibSubDirs) $(DESTLIBPROFILE)/.dir
545         @echo ======= Linking $(LIBRARYNAME) archive profile library =======
546         @$(RM) -f $@
547         $(VERB) $(Link) -03 $(PROFILE) -o $@ $(ObjectsP) $(LibSubDirs) -static
548
549 $(LIBNAME_AG): $(ObjectsG) $(LibSubDirs) $(DESTLIBDEBUG)/.dir
550         @echo ======= Linking $(LIBRARYNAME) archive debug library =======
551         @$(RM) -f $@
552         $(VERB) $(Link) -g $(STRIP) -o $@ $(ObjectsG) $(LibSubDirs) -static
553
554 #
555 # Rules for building .o libraries.
556 #
557 #       JTC:
558 #       Note that for this special case, we specify the actual object files
559 #       instead of their libtool counterparts.  This is because libtool
560 #       doesn't want to generate a reloadable object file unless it is given
561 #       .o files explicitly.
562 #
563 #       Note that we're making an assumption here: If we build a .lo file,
564 #       it's corresponding .o file will be placed in the same directory.
565 #
566 #       I think that is safe.
567 #
568 $(LIBNAME_OBJO): $(ObjectsO) $(LibSubDirs) $(DESTLIBRELEASE)/.dir
569         @echo "Linking $@"
570         $(VERB) $(Relink) -o $@ $(RObjectsO) $(LibSubDirs)
571
572 $(LIBNAME_OBJP): $(ObjectsP) $(LibSubDirs) $(DESTLIBPROFILE)/.dir
573         @echo "Linking $@"
574         $(VERB) $(Relink) -o $@ $(RObjectsP) $(LibSubDirs)
575
576 $(LIBNAME_OBJG): $(ObjectsG) $(LibSubDirs) $(DESTLIBDEBUG)/.dir
577         @echo "Linking $@"
578         $(VERB) $(Relink) -o $@ $(RObjectsG) $(LibSubDirs)
579
580 endif
581
582 #------------------------------------------------------------------------
583 # Create a TAGS database for emacs
584 #------------------------------------------------------------------------
585
586 ifdef ETAGS
587 ifeq ($(LEVEL), .)
588 tags:
589         $(ETAGS) -l c++ `find include lib tools -name '*.cpp' -o -name '*.h'`
590 all:: tags
591 endif
592 else
593 tags:
594         ${ECHO} "Cannot build $@: The program etags is not installed"
595 endif
596
597 #------------------------------------------------------------------------
598 # Handle the TOOLNAME option - used when building tool executables...
599 #------------------------------------------------------------------------
600 #
601 # The TOOLNAME option should be used with a USEDLIBS variable that tells the
602 # libraries (and the order of the libs) that should be linked to the
603 # tool. USEDLIBS should contain a list of library names (some with .a extension)
604 # that are automatically linked in as .o files unless the .a suffix is added.
605 #
606 ifdef TOOLNAME
607
608 # TOOLEXENAME* - These compute the output filenames to generate...
609 TOOLEXENAME_G := $(DESTTOOLDEBUG)/$(TOOLNAME)
610 TOOLEXENAME_O := $(DESTTOOLRELEASE)/$(TOOLNAME)
611 TOOLEXENAME_P := $(DESTTOOLPROFILE)/$(TOOLNAME)
612 TOOLEXENAMES  := $(DESTTOOLCURRENT)/$(TOOLNAME)
613
614 # USED_LIBS_OPTIONS - Compute the options line that add -llib1 -llib2, etc.
615 PROJ_LIBS_OPTIONS   := $(patsubst %.a.o, -l%, $(addsuffix .o, $(USEDLIBS)))
616 PROJ_LIBS_OPTIONS_G := $(patsubst %.o, $(PROJLIBDEBUGSOURCE)/%.o,  $(PROJ_LIBS_OPTIONS))
617 PROJ_LIBS_OPTIONS_O := $(patsubst %.o, $(PROJLIBRELEASESOURCE)/%.o,$(PROJ_LIBS_OPTIONS))
618 PROJ_LIBS_OPTIONS_P := $(patsubst %.o, $(PROJLIBPROFILESOURCE)/%.o,$(PROJ_LIBS_OPTIONS))
619
620 LLVM_LIBS_OPTIONS   := $(patsubst %.a.o, -l%, $(addsuffix .o, $(LLVMLIBS)))
621 LLVM_LIBS_OPTIONS_G := $(patsubst %.o, $(LLVMLIBDEBUGSOURCE)/%.o,  $(LLVM_LIBS_OPTIONS))
622 LLVM_LIBS_OPTIONS_O := $(patsubst %.o, $(LLVMLIBRELEASESOURCE)/%.o,$(LLVM_LIBS_OPTIONS))
623 LLVM_LIBS_OPTIONS_P := $(patsubst %.o, $(LLVMLIBPROFILESOURCE)/%.o,$(LLVM_LIBS_OPTIONS))
624
625 LIB_OPTS_G :=  $(LLVM_LIBS_OPTIONS_G) $(PROJ_LIBS_OPTIONS_G)
626 LIB_OPTS_O :=  $(LLVM_LIBS_OPTIONS_O) $(PROJ_LIBS_OPTIONS_O)
627 LIB_OPTS_P :=  $(LLVM_LIBS_OPTIONS_P) $(PROJ_LIBS_OPTIONS_P)
628
629 # USED_LIB_PATHS - Compute the path of the libraries used so that tools are
630 # rebuilt if libraries change.  This has to make sure to handle .a/.so and .o
631 # files separately.
632 #
633 STATICUSEDLIBS   := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(USEDLIBS)))
634 USED_LIB_PATHS_G := $(addprefix $(DESTLIBDEBUG)/, $(STATICUSEDLIBS))
635 USED_LIB_PATHS_O := $(addprefix $(DESTLIBRELEASE)/, $(STATICUSEDLIBS))
636 USED_LIB_PATHS_P := $(addprefix $(DESTLIBPROFILE)/, $(STATICUSEDLIBS))
637 #LINK_OPTS        := $(TOOLLINKOPTS) $(PLATFORMLINKOPTS)
638
639 #
640 # Libtool link options:
641 #       Ensure that all binaries have their symbols exported so that they can
642 #       by dlsym'ed.
643 #
644 LINK_OPTS := -export-dynamic $(TOOLLINKOPTS)
645
646
647
648
649
650 # Tell make that we need to rebuild subdirectories before we can link the tool.
651 # This affects things like LLI which has library subdirectories.
652 $(USED_LIB_PATHS_G) $(USED_LIB_PATHS_O) $(USED_LIB_PATHS_P): \
653         $(addsuffix /.makeall, $(PARALLEL_DIRS))
654
655 all::   $(TOOLEXENAMES)
656
657 clean::
658         $(VERB) $(RM) -f $(TOOLEXENAMES)
659
660 $(TOOLEXENAME_G): $(ObjectsG) $(USED_LIB_PATHS_G) $(DESTTOOLDEBUG)/.dir
661         @echo ======= Linking $(TOOLNAME) debug executable $(STRIP_WARN_MSG) =======
662         $(VERB) $(LinkG) -o $@ $(ObjectsG) $(LIB_OPTS_G) $(LINK_OPTS) $(LIBS)
663
664 $(TOOLEXENAME_O): $(ObjectsO) $(USED_LIB_PATHS_O) $(DESTTOOLRELEASE)/.dir
665         @echo ======= Linking $(TOOLNAME) release executable =======
666         $(VERB) $(LinkO) -o $@ $(ObjectsO) $(LIB_OPTS_O) $(LINK_OPTS) $(LIBS)
667
668 $(TOOLEXENAME_P): $(ObjectsP) $(USED_LIB_PATHS_P) $(DESTTOOLPROFILE)/.dir
669         @echo ======= Linking $(TOOLNAME) profile executable =======
670         $(VERB) $(LinkP) -o $@ $(ObjectsP) $(LIB_OPTS_P) $(LINK_OPTS) $(LIBS)
671
672 endif
673
674
675
676 #---------------------------------------------------------
677 .PRECIOUS: $(BUILD_OBJ_DIR)/Depend/.dir
678 .PRECIOUS: $(BUILD_OBJ_DIR)/Debug/.dir $(BUILD_OBJ_DIR)/Release/.dir
679
680 # Create .lo files in the ObjectFiles directory from the .cpp and .c files...
681 $(BUILD_OBJ_DIR)/Release/%.lo: $(SourceDir)%.cpp $(BUILD_OBJ_DIR)/Release/.dir
682         @echo "Compiling $<"
683         $(VERB) $(CompileO) $< -o $@
684
685 $(BUILD_OBJ_DIR)/Release/%.lo: $(SourceDir)%.c $(BUILD_OBJ_DIR)/Release/.dir
686         @echo "Compiling $<"
687         $(VERB) $(CompileCO) $< -o $@
688
689 $(BUILD_OBJ_DIR)/Profile/%.lo: $(SourceDir)%.cpp $(BUILD_OBJ_DIR)/Profile/.dir
690         @echo "Compiling $<"
691         $(VERB) $(CompileP) $< -o $@
692
693 $(BUILD_OBJ_DIR)/Profile/%.lo: $(SourceDir)%.c $(BUILD_OBJ_DIR)/Profile/.dir
694         @echo "Compiling $<"
695         $(VERB) $(CompileCP) $< -o $@
696
697 $(BUILD_OBJ_DIR)/Debug/%.lo: $(SourceDir)%.cpp $(BUILD_OBJ_DIR)/Debug/.dir
698         @echo "Compiling $<"
699         $(VERB) $(CompileG) $< -o $@
700
701 $(BUILD_OBJ_DIR)/Debug/%.lo: $(SourceDir)%.c $(BUILD_OBJ_DIR)/Debug/.dir 
702         @echo "Compiling $<"
703         $(VERB) $(CompileCG) $< -o $@
704
705 #
706 # Rules for building lex/yacc files
707 #
708 LEX_FILES   = $(filter %.l, $(Source))
709 LEX_OUTPUT  = $(LEX_FILES:%.l=%.cpp)
710 YACC_FILES  = $(filter %.y, $(Source))
711 YACC_OUTPUT = $(addprefix $(YACC_FILES:%.y=%), .h .cpp .output)
712 .PRECIOUS: $(LEX_OUTPUT) $(YACC_OUTPUT)
713
714 # Create a .cpp source file from a flex input file... this uses sed to cut down
715 # on the warnings emited by GCC...
716 #
717 # The last line is a gross hack to work around flex aparently not being able to
718 # resize the buffer on a large token input.  Currently, for uninitialized string
719 # buffers in LLVM we can generate very long tokens, so this is a hack around it.
720 # FIXME.  (f.e. char Buffer[10000]; )
721 #
722 %.cpp: %.l
723         @echo Flex\'ing $<...
724         $(VERB) $(FLEX) -t $< | \
725           $(SED) '/^find_rule/d' | \
726           $(SED) 's/void yyunput/inline void yyunput/' | \
727           $(SED) 's/void \*yy_flex_realloc/inline void *yy_flex_realloc/' | \
728           $(SED) 's/#define YY_BUF_SIZE 16384/#define YY_BUF_SIZE (16384*64)/' \
729               > $@.tmp
730         $(VERB) cmp $@ $@.tmp > /dev/null || ${MV} -f $@.tmp $@
731         @# remove the output of flex if it didn't get moved over...
732         @rm -f $@.tmp
733
734 # Rule for building the bison parsers...
735 %.c: %.y     # Cancel built-in rules for yacc
736 %.h: %.y     # Cancel built-in rules for yacc
737 %.cpp %.h : %.y
738         @echo Bison\'ing $<...
739         $(VERB) $(BISON) -v -d -p $(<:%Parser.y=%) $*.y
740         $(VERB) cmp $*.tab.c $*.cpp > /dev/null || ${MV} -f $*.tab.c $*.cpp
741         $(VERB) cmp $*.tab.h $*.h > /dev/null || ${MV} -f $*.tab.h $*.h
742         @# If the files were not updated, don't leave them lying around...
743         @rm -f $*.tab.c $*.tab.h
744
745 # To create the directories...
746 %/.dir:
747         $(VERB) ${MKDIR} $* > /dev/null
748         @$(DATE) > $@
749
750 # To create postscript files from dot files...
751 ifdef DOT
752 %.ps: %.dot
753         ${DOT} -Tps < $< > $@
754 else
755 %.ps: %.dot
756         ${ECHO} "Cannot build $@: The program dot is not installed"
757 endif
758
759 # 'make clean' nukes the tree
760 clean::
761         $(VERB) $(RM) -rf $(BUILD_OBJ_DIR)/Debug $(BUILD_OBJ_DIR)/Release $(BUILD_OBJ_DIR)/Profile $(BUILD_OBJ_DIR)/Depend
762         $(VERB) $(RM) -f core core.[0-9][0-9]* *.o *.d *.so *~ *.flc
763         $(VERB) $(RM) -f $(LEX_OUTPUT) $(YACC_OUTPUT)
764
765 distclean:: clean
766         $(VERB) (cd $(LLVM_SRC_ROOT); $(RM) -rf $(LEVEL)/Makefile.config \
767                     $(LEVEL)/include/Config/config.h \
768                     $(LEVEL)/autom4te.cache \
769                     $(LEVEL)/config.log)
770
771 ###########################################################################
772 # C/C++ Dependencies
773 #       Define variables and rules that generate header file dependencies
774 #       from C/C++ source files.
775 ###########################################################################
776
777 # If dependencies were generated for the file that included this file,
778 # include the dependancies now...
779 #
780 SourceBaseNames := $(basename $(notdir $(filter-out Debug/%, $(Source))))
781 SourceDepend := $(SourceBaseNames:%=$(BUILD_OBJ_DIR)/Depend/%.d)
782
783 # Create dependencies for the *.cpp files...
784 #$(SourceDepend): \x
785 $(BUILD_OBJ_DIR)/Depend/%.d: $(SourceDir)%.cpp $(BUILD_OBJ_DIR)/Depend/.dir
786         $(VERB) $(Depend) $< | $(SED) 's|\.o|\.lo|' | $(SED) 's|$*\.lo *|$(BUILD_OBJ_DIR)/Release/& $(BUILD_OBJ_DIR)/Profile/& $(BUILD_OBJ_DIR)/Debug/& $(BUILD_OBJ_DIR)/Depend/$(@F)|g' > $@
787
788 # Create dependencies for the *.c files...
789 #$(SourceDepend): \x
790 $(BUILD_OBJ_DIR)/Depend/%.d: $(SourceDir)%.c $(BUILD_OBJ_DIR)/Depend/.dir
791         $(VERB) $(DependC) -o $@ $< | $(SED) 's|\.o|\.lo|' | $(SED) 's|$*\.lo *|$(BUILD_OBJ_DIR)/Release/& $(BUILD_OBJ_DIR)/Profile/& $(BUILD_OBJ_DIR)/Debug/& $(BUILD_OBJ_DIR)/Depend/$(@F)|g' > $@
792
793 #
794 # Include dependencies generated from C/C++ source files, but not if we
795 # are cleaning (this example taken from the GNU Make Manual).
796 #
797 ifneq ($(MAKECMDGOALS),clean)
798 ifneq ($(SourceDepend),)
799 -include $(SourceDepend)
800 endif
801 endif