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