Darwin 10.4.x: "-rpath" is unnecessary when linking shared libraries.
[oota-llvm.git] / Makefile.rules
1 #===-- Makefile.rules - Common make rules for LLVM ---------*- Makefile -*--===#
2 #
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
7 #
8 #===------------------------------------------------------------------------===#
9 #
10 # This file is included by all of the LLVM makefiles.  For details on how to use
11 # it properly, please see the document MakefileGuide.html in the docs directory.
12 #
13 #===-----------------------------------------------------------------------====#
14
15 ################################################################################
16 # TARGETS: Define standard targets that can be invoked
17 ################################################################################
18
19 #--------------------------------------------------------------------
20 # Define the various target sets
21 #--------------------------------------------------------------------
22 RecursiveTargets := all clean clean-all install uninstall install-bytecode
23 LocalTargets     := all-local clean-local clean-all-local check-local \
24                     install-local printvars uninstall-local \
25                     install-bytecode-local unittests
26 TopLevelTargets  := check dist dist-check dist-clean dist-gzip dist-bzip2 \
27                     dist-zip unittests
28 UserTargets      := $(RecursiveTargets) $(LocalTargets) $(TopLevelTargets)
29 InternalTargets  := preconditions distdir dist-hook
30
31 ################################################################################
32 # INITIALIZATION: Basic things the makefile needs
33 ################################################################################
34
35 #--------------------------------------------------------------------
36 # Set the VPATH so that we can find source files.
37 #--------------------------------------------------------------------
38 VPATH=$(PROJ_SRC_DIR)
39
40 #--------------------------------------------------------------------
41 # Reset the list of suffixes we know how to build.
42 #--------------------------------------------------------------------
43 .SUFFIXES:
44 .SUFFIXES: .c .cpp .cc .h .hpp .o .a .bc .td .ps .dot .ll
45 .SUFFIXES: $(SHLIBEXT) $(SUFFIXES)
46
47 #--------------------------------------------------------------------
48 # Mark all of these targets as phony to avoid implicit rule search
49 #--------------------------------------------------------------------
50 .PHONY: $(UserTargets) $(InternalTargets)
51
52 #--------------------------------------------------------------------
53 # Make sure all the user-target rules are double colon rules and
54 # they are defined first.
55 #--------------------------------------------------------------------
56
57 $(UserTargets)::
58
59 ################################################################################
60 # PRECONDITIONS: that which must be built/checked first
61 ################################################################################
62
63 SrcMakefiles       := $(filter %Makefile %Makefile.tests,\
64                       $(wildcard $(PROJ_SRC_DIR)/Makefile*))
65 ObjMakefiles       := $(subst $(PROJ_SRC_DIR),$(PROJ_OBJ_DIR),$(SrcMakefiles))
66 ConfigureScript    := $(PROJ_SRC_ROOT)/configure
67 ConfigStatusScript := $(PROJ_OBJ_ROOT)/config.status
68 MakefileConfigIn   := $(strip $(wildcard $(PROJ_SRC_ROOT)/Makefile.config.in))
69 MakefileCommonIn   := $(strip $(wildcard $(PROJ_SRC_ROOT)/Makefile.common.in))
70 MakefileConfig     := $(PROJ_OBJ_ROOT)/Makefile.config
71 MakefileCommon     := $(PROJ_OBJ_ROOT)/Makefile.common
72 PreConditions      := $(ConfigStatusScript) $(ObjMakefiles)
73 ifneq ($(MakefileCommonIn),)
74 PreConditions      += $(MakefileCommon)
75 endif
76
77 ifneq ($(MakefileConfigIn),)
78 PreConditions      += $(MakefileConfig)
79 endif
80
81 preconditions: $(PreConditions)
82
83 #------------------------------------------------------------------------
84 # Make sure the BUILT_SOURCES are built first
85 #------------------------------------------------------------------------
86 $(filter-out clean clean-local,$(UserTargets)):: $(BUILT_SOURCES)
87
88 clean-all-local::
89 ifneq ($(strip $(BUILT_SOURCES)),)
90         -$(Verb) $(RM) -f $(BUILT_SOURCES)
91 endif
92
93 ifneq ($(PROJ_OBJ_ROOT),$(PROJ_SRC_ROOT))
94 spotless:
95         $(Verb) if test -x config.status ; then \
96           $(EchoCmd) Wiping out $(PROJ_OBJ_ROOT) ; \
97           $(MKDIR) .spotless.save ; \
98           $(MV) config.status .spotless.save ; \
99           $(MV) mklib  .spotless.save ; \
100           $(MV) projects  .spotless.save ; \
101           $(RM) -rf * ; \
102           $(MV) .spotless.save/config.status . ; \
103           $(MV) .spotless.save/mklib . ; \
104           $(MV) .spotless.save/projects . ; \
105           $(RM) -rf .spotless.save ; \
106           $(EchoCmd) Rebuilding configuration of $(PROJ_OBJ_ROOT) ; \
107           $(ConfigStatusScript) --recheck $(ConfigureScriptFLAGS) && \
108           $(ConfigStatusScript) ; \
109         else \
110           $(EchoCmd) "make spotless" can only be run from $(PROJ_OBJ_ROOT); \
111         fi
112 else
113 spotless:
114         $(EchoCmd) "spotless target not supported for objdir == srcdir"
115 endif
116
117 $(BUILT_SOURCES) : $(ObjMakefiles)
118
119 #------------------------------------------------------------------------
120 # Make sure we're not using a stale configuration
121 #------------------------------------------------------------------------
122 reconfigure:
123         $(Echo) Reconfiguring $(PROJ_OBJ_ROOT)
124         $(Verb) cd $(PROJ_OBJ_ROOT) && \
125           if test -w $(PROJ_OBJ_ROOT)/config.cache ; then \
126             $(RM) $(PROJ_OBJ_ROOT)/config.cache ; \
127           fi ; \
128           $(ConfigStatusScript) --recheck $(ConfigureScriptFLAGS) && \
129           $(ConfigStatusScript)
130
131 .PRECIOUS: $(ConfigStatusScript)
132 $(ConfigStatusScript): $(ConfigureScript)
133         $(Echo) Reconfiguring with $<
134         $(Verb) cd $(PROJ_OBJ_ROOT) && \
135           if test -w $(PROJ_OBJ_ROOT)/config.cache ; then \
136             $(RM) $(PROJ_OBJ_ROOT)/config.cache ; \
137           fi ; \
138           $(ConfigStatusScript) --recheck $(ConfigureScriptFLAGS) && \
139           $(ConfigStatusScript)
140
141 #------------------------------------------------------------------------
142 # Make sure the configuration makefile is up to date
143 #------------------------------------------------------------------------
144 ifneq ($(MakefileConfigIn),)
145 $(MakefileConfig): $(MakefileConfigIn) $(ConfigStatusScript)
146         $(Echo) Regenerating $@
147         $(Verb) cd $(PROJ_OBJ_ROOT) ; $(ConfigStatusScript) Makefile.config
148 endif
149
150 ifneq ($(MakefileCommonIn),)
151 $(MakefileCommon): $(MakefileCommonIn) $(ConfigStatusScript)
152         $(Echo) Regenerating $@
153         $(Verb) cd $(PROJ_OBJ_ROOT) ; $(ConfigStatusScript) Makefile.common
154 endif
155
156 #------------------------------------------------------------------------
157 # If the Makefile in the source tree has been updated, copy it over into the
158 # build tree. But, only do this if the source and object makefiles differ
159 #------------------------------------------------------------------------
160 ifneq ($(PROJ_OBJ_DIR),$(PROJ_SRC_DIR))
161
162 Makefile: $(PROJ_SRC_DIR)/Makefile $(ExtraMakefiles)
163         $(Echo) "Updating Makefile"
164         $(Verb) $(MKDIR) $(@D)
165         $(Verb) $(CP) -f $< $@
166
167 # Copy the Makefile.* files unless we're in the root directory which avoids
168 # the copying of Makefile.config.in or other things that should be explicitly
169 # taken care of.
170 $(PROJ_OBJ_DIR)/Makefile% : $(PROJ_SRC_DIR)/Makefile%
171         @case '$?' in \
172           *Makefile.rules) ;; \
173           *.in) ;; \
174           *) $(EchoCmd) "Updating $(@F)" ; \
175              $(MKDIR) $(@D) ; \
176              $(CP) -f $< $@ ;; \
177         esac
178
179 endif
180
181 #------------------------------------------------------------------------
182 # Set up the basic dependencies
183 #------------------------------------------------------------------------
184 $(UserTargets):: $(PreConditions)
185
186 all:: all-local
187 clean:: clean-local
188 clean-all:: clean-local clean-all-local
189 install:: install-local
190 uninstall:: uninstall-local
191 install-local:: all-local
192 install-bytecode:: install-bytecode-local
193
194 ###############################################################################
195 # LLVMC: Provide rules for compiling llvmc plugins
196 ###############################################################################
197
198 ifdef LLVMC_PLUGIN
199
200 LIBRARYNAME := $(patsubst %,plugin_llvmc_%,$(LLVMC_PLUGIN))
201 REQUIRES_EH := 1
202
203 # Build a dynamic library if the user runs `make` directly from the plugin
204 # directory.
205 ifndef LLVMC_BUILTIN_PLUGIN
206 LOADABLE_MODULE = 1
207 endif
208
209 # TableGen stuff...
210 ifneq ($(BUILT_SOURCES),)
211 LLVMC_BUILD_AUTOGENERATED_INC=1
212 endif
213
214 endif # LLVMC_PLUGIN
215
216 ###############################################################################
217 # VARIABLES: Set up various variables based on configuration data
218 ###############################################################################
219
220 # Variable for if this make is for a "cleaning" target
221 ifneq ($(strip $(filter clean clean-local dist-clean,$(MAKECMDGOALS))),)
222   IS_CLEANING_TARGET=1
223 endif
224
225 #--------------------------------------------------------------------
226 # Variables derived from configuration we are building
227 #--------------------------------------------------------------------
228
229 CPP.Defines :=
230 # OPTIMIZE_OPTION - The optimization level option we want to build LLVM with
231 # this can be overridden on the make command line.
232 ifneq ($(OS),MingW)
233   OPTIMIZE_OPTION := -O3
234 else
235   OPTIMIZE_OPTION := -O2
236 endif
237
238 ifdef ENABLE_PROFILING
239   BuildMode := Profile
240   CXX.Flags += $(OPTIMIZE_OPTION) -pg -g
241   C.Flags   += $(OPTIMIZE_OPTION) -pg -g
242   LD.Flags  += $(OPTIMIZE_OPTION) -pg -g
243   KEEP_SYMBOLS := 1
244 else
245   ifeq ($(ENABLE_OPTIMIZED),1)
246     BuildMode := Release
247     # Don't use -fomit-frame-pointer on Darwin or FreeBSD.
248     ifneq ($(OS),FreeBSD)
249     ifneq ($(OS),Darwin)
250       OmitFramePointer := -fomit-frame-pointer
251     endif
252     endif
253
254     # Darwin requires -fstrict-aliasing to be explicitly enabled.
255     ifeq ($(OS),Darwin)
256       EXTRA_OPTIONS += -fstrict-aliasing -Wstrict-aliasing
257     endif
258
259     CXX.Flags += $(OPTIMIZE_OPTION) $(OmitFramePointer)
260     C.Flags   += $(OPTIMIZE_OPTION) $(OmitFramePointer)
261     LD.Flags  += $(OPTIMIZE_OPTION)
262   else
263     BuildMode := Debug
264     CXX.Flags += -g
265     C.Flags   += -g
266     LD.Flags  += -g
267     KEEP_SYMBOLS := 1
268   endif
269 endif
270
271 #ifeq ($(ENABLE_VISIBILITY_INLINES_HIDDEN),1)
272 #    CXX.Flags += -fvisibility-inlines-hidden
273 #endif
274
275 # IF REQUIRES_EH=1 is specified then don't disable exceptions
276 ifndef REQUIRES_EH
277   CXX.Flags += -fno-exceptions
278 endif
279
280 # IF REQUIRES_RTTI=1 is specified then don't disable run-time type id
281 ifndef REQUIRES_RTTI
282 #  CXX.Flags += -fno-rtti
283 endif
284
285 # If DISABLE_ASSERTIONS=1 is specified (make command line or configured),
286 # then disable assertions by defining the appropriate preprocessor symbols.
287 ifdef DISABLE_ASSERTIONS
288   BuildMode := $(BuildMode)-Asserts
289   CPP.Defines += -DNDEBUG
290 else
291   CPP.Defines += -D_DEBUG
292 endif
293
294 # If ENABLE_EXPENSIVE_CHECKS=1 is specified (make command line or
295 # configured), then enable expensive checks by defining the
296 # appropriate preprocessor symbols.
297 ifdef ENABLE_EXPENSIVE_CHECKS
298   BuildMode := $(BuildMode)+Checks
299   CPP.Defines += -D_GLIBCXX_DEBUG -DXDEBUG
300 endif
301
302 # LOADABLE_MODULE implies several other things so we force them to be
303 # defined/on.
304 ifdef LOADABLE_MODULE
305   SHARED_LIBRARY := 1
306   DONT_BUILD_RELINKED := 1
307   LINK_LIBS_IN_SHARED := 1
308 endif
309
310 ifdef SHARED_LIBRARY
311   ENABLE_PIC := 1
312   PIC_FLAG = "(PIC)"
313 endif
314
315 ifeq ($(ENABLE_PIC),1)
316   ifeq ($(OS), $(filter $(OS), Cygwin MingW))
317     # Nothing. Win32 defaults to PIC and warns when given -fPIC
318   else
319     ifeq ($(OS),Darwin)
320       # Common symbols not allowed in dylib files
321       CXX.Flags += -fno-common
322       C.Flags   += -fno-common
323     else
324       # Linux and others; pass -fPIC
325       CXX.Flags += -fPIC
326       C.Flags   += -fPIC
327     endif
328   endif
329 endif
330
331 CXX.Flags     += $(CXXFLAGS) -Woverloaded-virtual
332 C.Flags       += $(CFLAGS)
333 CPP.Defines   += $(CPPFLAGS)
334 CPP.BaseFlags += $(CPP.Defines)
335 LD.Flags      += $(LDFLAGS)
336 AR.Flags      := cru
337
338 # Make Floating point IEEE compliant on Alpha.
339 ifeq ($(ARCH),Alpha)
340   CXX.Flags     += -mieee
341   CPP.BaseFlags += -mieee
342 ifeq ($(ENABLE_PIC),0)
343   CXX.Flags     += -fPIC
344   CPP.BaseFlags += -fPIC
345 endif
346 endif
347
348 ifeq ($(ARCH),Alpha)
349   LD.Flags += -Wl,--no-relax
350 endif
351
352 #--------------------------------------------------------------------
353 # Directory locations
354 #--------------------------------------------------------------------
355 TargetMode :=
356 ifeq ($(LLVM_CROSS_COMPILING),1)
357   BuildLLVMToolDir := $(LLVM_OBJ_ROOT)/BuildTools/$(BuildMode)/bin
358 endif
359
360 ObjRootDir  := $(PROJ_OBJ_DIR)/$(BuildMode)
361 ObjDir      := $(ObjRootDir)
362 LibDir      := $(PROJ_OBJ_ROOT)/$(BuildMode)/lib
363 ToolDir     := $(PROJ_OBJ_ROOT)/$(BuildMode)/bin
364 ExmplDir    := $(PROJ_OBJ_ROOT)/$(BuildMode)/examples
365 LLVMLibDir  := $(LLVM_OBJ_ROOT)/$(BuildMode)/lib
366 LLVMToolDir := $(LLVM_OBJ_ROOT)/$(BuildMode)/bin
367 LLVMExmplDir:= $(LLVM_OBJ_ROOT)/$(BuildMode)/examples
368 CFERuntimeLibDir := $(LLVMGCCDIR)/lib
369
370 #--------------------------------------------------------------------
371 # Full Paths To Compiled Tools and Utilities
372 #--------------------------------------------------------------------
373 EchoCmd  = $(ECHO) llvm[$(MAKELEVEL)]:
374 Echo     = @$(EchoCmd)
375 ifndef LLVMAS
376 LLVMAS   := $(LLVMToolDir)/llvm-as$(EXEEXT)
377 endif
378 ifndef TBLGEN
379   ifeq ($(LLVM_CROSS_COMPILING),1)
380     TBLGEN   := $(BuildLLVMToolDir)/tblgen$(BUILD_EXEEXT)
381   else
382     TBLGEN   := $(LLVMToolDir)/tblgen$(EXEEXT)
383   endif
384 endif
385 LLVM_CONFIG := $(LLVMToolDir)/llvm-config
386 ifndef LLVMLD
387 LLVMLD    := $(LLVMToolDir)/llvm-ld$(EXEEXT)
388 endif
389 ifndef LLVMDIS
390 LLVMDIS  := $(LLVMToolDir)/llvm-dis$(EXEEXT)
391 endif
392 ifndef LLI
393 LLI      := $(LLVMToolDir)/lli$(EXEEXT)
394 endif
395 ifndef LLC
396 LLC      := $(LLVMToolDir)/llc$(EXEEXT)
397 endif
398 ifndef LOPT
399 LOPT     := $(LLVMToolDir)/opt$(EXEEXT)
400 endif
401 ifndef LBUGPOINT
402 LBUGPOINT := $(LLVMToolDir)/bugpoint$(EXEEXT)
403 endif
404 ifndef LUPGRADE
405 LUPGRADE := $(LLVMToolDir)/llvm-upgrade$(EXEEXT)
406 endif
407 ifeq ($(LLVMGCC_MAJVERS),3)
408 UPGRADE_MSG = $(Echo) "Upgrading $(1) assembly to latest."
409 UPGRADE_LL  = $(Verb)$(LUPGRADE) $(1) -o $(1).up.tmp -f ; $(MV) $(1).up.tmp $(1)
410 LLVMGCCWITHPATH  := PATH="$(LLVMToolDir):$(PATH)" $(LLVMGCC)
411 LLVMGXXWITHPATH  := PATH="$(LLVMToolDir):$(PATH)" $(LLVMGXX)
412 else
413 UPGRADE_MSG =
414 UPGRADE_LL  =
415 LLVMGCCWITHPATH  := $(LLVMGCC)
416 LLVMGXXWITHPATH  := $(LLVMGXX)
417 endif
418
419 #--------------------------------------------------------------------
420 # Adjust to user's request
421 #--------------------------------------------------------------------
422
423 ifeq ($(OS),Darwin)
424   DARWIN_VERSION := `sw_vers -productVersion`
425   # Strip a number like 10.4.7 to 10.4
426   DARWIN_VERSION := $(shell echo $(DARWIN_VERSION)| sed -E 's/(10.[0-9]).*/\1/')
427   # Get "4" out of 10.4 for later pieces in the makefile.
428   DARWIN_MAJVERS := $(shell echo $(DARWIN_VERSION)| sed -E 's/10.([0-9]).*/\1/')
429      
430   SharedLinkOptions=-Wl,-flat_namespace -Wl,-undefined -Wl,suppress -bundle \
431                     -mmacosx-version-min=$(DARWIN_VERSION)
432   CompileCommonOpts += -mmacosx-version-min=$(DARWIN_VERSION)
433 else
434   ifeq ($(OS),Cygwin)
435     SharedLinkOptions=-shared -nostdlib -Wl,--export-all-symbols \
436                       -Wl,--enable-auto-import -Wl,--enable-auto-image-base \
437                       -Wl,--enable-runtime-pseudo-relocs
438   else
439     SharedLinkOptions=-shared
440   endif
441 endif
442
443 # Adjust LD.Flags depending on the kind of library that is to be built. Note
444 # that if LOADABLE_MODULE is specified then the resulting shared library can
445 # be opened with dlopen.
446 ifdef LOADABLE_MODULE
447   LD.Flags += -module
448 endif
449
450 ifdef SHARED_LIBRARY
451 ifneq ($(DARWIN_MAJVERS),4)
452   LD.Flags += $(RPATH) -Wl,$(LibDir)
453 endif
454 endif
455
456 ifdef TOOL_VERBOSE
457   C.Flags += -v
458   CXX.Flags += -v
459   LD.Flags += -v
460   VERBOSE := 1
461 endif
462
463 # Adjust settings for verbose mode
464 ifndef VERBOSE
465   Verb := @
466   AR.Flags += >/dev/null 2>/dev/null
467   ConfigureScriptFLAGS += >$(PROJ_OBJ_DIR)/configure.out 2>&1
468 else
469   ConfigureScriptFLAGS :=
470 endif
471
472 # By default, strip symbol information from executable
473 ifndef KEEP_SYMBOLS
474   Strip := $(PLATFORMSTRIPOPTS)
475   StripWarnMsg := "(without symbols)"
476   Install.StripFlag += -s
477 endif
478
479 # Adjust linker flags for building an executable
480 ifneq ($(OS),Darwin)
481 ifneq ($(DARWIN_MAJVERS),4)
482 ifdef TOOLNAME
483 ifdef EXAMPLE_TOOL
484   LD.Flags += $(RPATH) -Wl,$(ExmplDir) $(RDYNAMIC)
485 else
486   LD.Flags += $(RPATH) -Wl,$(ToolDir) $(RDYNAMIC)
487 endif
488 endif
489 endif
490 endif
491
492 #----------------------------------------------------------
493 # Options To Invoke Tools
494 #----------------------------------------------------------
495
496 CompileCommonOpts += -pedantic -Wall -W -Wwrite-strings -Wno-long-long \
497                      -Wunused -Wno-unused-parameter $(EXTRA_OPTIONS)
498
499 ifeq ($(OS),HP-UX)
500   CompileCommonOpts := -D_REENTRANT -D_HPUX_SOURCE
501 endif
502
503 # If we are building a universal binary on Mac OS/X, pass extra options.  This
504 # is useful to people that want to link the LLVM libraries into their universal
505 # apps.
506 #
507 # The following can be optionally specified:
508 #   UNIVERSAL_SDK_PATH variable can be specified as a path to the SDK to use.
509 #      For Mac OS/X 10.4 Intel machines, the traditional one is:
510 #      UNIVERSAL_SDK_PATH=/Developer/SDKs/MacOSX10.4u.sdk/
511 #   UNIVERSAL_ARCH can be optionally specified to be a list of architectures
512 #      to build for, e.g. UNIVERSAL_ARCH="i386 ppc ppc64".  This defaults to
513 #      i386/ppc only.
514 ifdef UNIVERSAL
515   ifndef UNIVERSAL_ARCH
516     UNIVERSAL_ARCH := i386 ppc
517   endif
518   UNIVERSAL_ARCH_OPTIONS := $(UNIVERSAL_ARCH:%=-arch %)
519   CompileCommonOpts += $(UNIVERSAL_ARCH_OPTIONS)
520   Relink.Flags := $(UNIVERSAL_ARCH_OPTIONS)
521   ifdef UNIVERSAL_SDK_PATH
522     CompileCommonOpts += -isysroot $(UNIVERSAL_SDK_PATH)
523     Relink.Flags      += -isysroot $(UNIVERSAL_SDK_PATH)
524   endif
525
526   # Building universal cannot compute dependencies automatically.
527   DISABLE_AUTO_DEPENDENCIES=1
528 else
529   ifeq ($(OS),Darwin)
530     ifeq ($(ARCH),x86_64)
531       CompileCommonOpts += -m64
532     else
533       ifeq ($(ARCH),x86)
534         CompileCommonOpts += -m32
535       endif
536     endif
537   endif
538 endif
539
540 ifeq ($(OS),SunOS)
541 CPP.BaseFlags += -include llvm/System/Solaris.h
542 endif
543
544 LD.Flags      += -L$(LibDir) -L$(LLVMLibDir)
545 CPP.BaseFlags += -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS
546 # All -I flags should go here, so that they don't confuse llvm-config.
547 CPP.Flags     += $(sort -I$(PROJ_OBJ_DIR) -I$(PROJ_SRC_DIR) \
548                  $(patsubst %,-I%/include,\
549                  $(PROJ_OBJ_ROOT) $(PROJ_SRC_ROOT) \
550                  $(LLVM_OBJ_ROOT) $(LLVM_SRC_ROOT))) \
551                  $(CPP.BaseFlags)
552
553  ifeq ($(BUILD_COMPONENT), 1)
554   Compile.C     = $(BUILD_CC) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts) -c
555   Compile.CXX   = $(BUILD_CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) -c
556   Preprocess.CXX= $(BUILD_CXX) $(CPP.Flags) $(CompileCommonOpts) $(CXX.Flags) -E
557   Link          = $(BUILD_CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
558                   $(LD.Flags) $(Strip)
559   Relink        = $(BUILD_CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
560                   $(Relink.Flags)
561 else
562   Compile.C     = $(CC) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts) -c
563   Compile.CXX   = $(CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) -c
564   Preprocess.CXX= $(CXX) $(CPP.Flags) $(CompileCommonOpts) $(CXX.Flags) -E
565   Link          = $(CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
566                   $(LD.Flags) $(Strip)
567   Relink        = $(CXX) $(CPP.Flags) $(CXX.Flags) $(CompileCommonOpts) \
568                   $(Relink.Flags)
569 endif
570
571 BCCompile.C   = $(LLVMGCCWITHPATH) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts)
572 Preprocess.C  = $(CC) $(CPP.Flags) $(C.Flags) $(CompileCommonOpts) -E
573
574 BCCompile.CXX = $(LLVMGXXWITHPATH) $(CPP.Flags) $(CXX.Flags) \
575                 $(CompileCommonOpts)
576
577 ProgInstall   = $(INSTALL) $(Install.StripFlag) -m 0755
578 ScriptInstall = $(INSTALL) -m 0755
579 DataInstall   = $(INSTALL) -m 0644
580
581 # When compiling under Mingw/Cygwin, the tblgen tool expects Windows
582 # paths. In this case, the SYSPATH function (defined in
583 # Makefile.config) transforms Unix paths into Windows paths.
584 TableGen      = $(TBLGEN) -I $(call SYSPATH, $(PROJ_SRC_DIR)) \
585                 -I $(call SYSPATH, $(LLVM_SRC_ROOT)/include) \
586                 -I $(call SYSPATH, $(PROJ_SRC_ROOT)/include) \
587                 -I $(call SYSPATH, $(PROJ_SRC_ROOT)/lib/Target)
588
589 Archive       = $(AR) $(AR.Flags)
590 LArchive      = $(LLVMToolDir)/llvm-ar rcsf
591 ifdef RANLIB
592 Ranlib        = $(RANLIB)
593 else
594 Ranlib        = ranlib
595 endif
596
597 #----------------------------------------------------------
598 # Get the list of source files and compute object file
599 # names from them.
600 #----------------------------------------------------------
601
602 ifndef SOURCES
603   Sources := $(notdir $(wildcard $(PROJ_SRC_DIR)/*.cpp \
604              $(PROJ_SRC_DIR)/*.cc $(PROJ_SRC_DIR)/*.c))
605 else
606   Sources := $(SOURCES)
607 endif
608
609 ifdef BUILT_SOURCES
610 Sources += $(filter %.cpp %.c %.cc,$(BUILT_SOURCES))
611 endif
612
613 BaseNameSources := $(sort $(basename $(Sources)))
614
615 ObjectsO  := $(BaseNameSources:%=$(ObjDir)/%.o)
616 ObjectsBC := $(BaseNameSources:%=$(ObjDir)/%.bc)
617
618 ###############################################################################
619 # DIRECTORIES: Handle recursive descent of directory structure
620 ###############################################################################
621
622 #---------------------------------------------------------
623 # Provide rules to make install dirs. This must be early
624 # in the file so they get built before dependencies
625 #---------------------------------------------------------
626
627 $(PROJ_bindir) $(PROJ_libdir) $(PROJ_includedir) $(PROJ_etcdir)::
628         $(Verb) $(MKDIR) $@
629
630 # To create other directories, as needed, and timestamp their creation
631 %/.dir:
632         $(Verb) $(MKDIR) $* > /dev/null
633         $(Verb) $(DATE) > $@
634
635 .PRECIOUS: $(ObjDir)/.dir $(LibDir)/.dir $(ToolDir)/.dir $(ExmplDir)/.dir
636 .PRECIOUS: $(LLVMLibDir)/.dir $(LLVMToolDir)/.dir $(LLVMExmplDir)/.dir
637
638 #---------------------------------------------------------
639 # Handle the DIRS options for sequential construction
640 #---------------------------------------------------------
641
642 SubDirs :=
643 ifdef DIRS
644 SubDirs += $(DIRS)
645
646 ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT))
647 $(RecursiveTargets)::
648         $(Verb) for dir in $(DIRS); do \
649           if [ ! -f $$dir/Makefile ]; then \
650             $(MKDIR) $$dir; \
651             $(CP) $(PROJ_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
652           fi; \
653           ($(MAKE) -C $$dir $@ ) || exit 1; \
654         done
655 else
656 $(RecursiveTargets)::
657         $(Verb) for dir in $(DIRS); do \
658           ($(MAKE) -C $$dir $@ ) || exit 1; \
659         done
660 endif
661
662 endif
663
664 #---------------------------------------------------------
665 # Handle the EXPERIMENTAL_DIRS options ensuring success
666 # after each directory is built.
667 #---------------------------------------------------------
668 ifdef EXPERIMENTAL_DIRS
669 $(RecursiveTargets)::
670         $(Verb) for dir in $(EXPERIMENTAL_DIRS); do \
671           if [ ! -f $$dir/Makefile ]; then \
672             $(MKDIR) $$dir; \
673             $(CP) $(PROJ_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
674           fi; \
675           ($(MAKE) -C $$dir $@ ) || exit 0; \
676         done
677 endif
678
679 #-----------------------------------------------------------
680 # Handle the OPTIONAL_PARALLEL_DIRS options for optional parallel construction
681 #-----------------------------------------------------------
682 ifdef OPTIONAL_PARALLEL_DIRS
683   PARALLEL_DIRS += $(foreach T,$(OPTIONAL_PARALLEL_DIRS),$(shell test -d $(PROJ_SRC_DIR)/$(T) && echo "$(T)"))
684 endif
685
686 #-----------------------------------------------------------
687 # Handle the PARALLEL_DIRS options for parallel construction
688 #-----------------------------------------------------------
689 ifdef PARALLEL_DIRS
690
691 SubDirs += $(PARALLEL_DIRS)
692
693 # Unfortunately, this list must be maintained if new recursive targets are added
694 all      :: $(addsuffix /.makeall      ,$(PARALLEL_DIRS))
695 clean    :: $(addsuffix /.makeclean    ,$(PARALLEL_DIRS))
696 clean-all:: $(addsuffix /.makeclean-all,$(PARALLEL_DIRS))
697 install  :: $(addsuffix /.makeinstall  ,$(PARALLEL_DIRS))
698 uninstall:: $(addsuffix /.makeuninstall,$(PARALLEL_DIRS))
699 install-bytecode  :: $(addsuffix /.makeinstall-bytecode,$(PARALLEL_DIRS))
700
701 ParallelTargets := $(foreach T,$(RecursiveTargets),%/.make$(T))
702
703 $(ParallelTargets) :
704         $(Verb) if [ ! -f $(@D)/Makefile ]; then \
705           $(MKDIR) $(@D); \
706           $(CP) $(PROJ_SRC_DIR)/$(@D)/Makefile $(@D)/Makefile; \
707         fi; \
708         $(MAKE) -C $(@D) $(subst $(@D)/.make,,$@)
709 endif
710
711 #---------------------------------------------------------
712 # Handle the OPTIONAL_DIRS options for directores that may
713 # or may not exist.
714 #---------------------------------------------------------
715 ifdef OPTIONAL_DIRS
716
717 SubDirs += $(OPTIONAL_DIRS)
718
719 ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT))
720 $(RecursiveTargets)::
721         $(Verb) for dir in $(OPTIONAL_DIRS); do \
722           if [ -d $(PROJ_SRC_DIR)/$$dir ]; then\
723             if [ ! -f $$dir/Makefile ]; then \
724               $(MKDIR) $$dir; \
725               $(CP) $(PROJ_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
726             fi; \
727             ($(MAKE) -C$$dir $@ ) || exit 1; \
728           fi \
729         done
730 else
731 $(RecursiveTargets)::
732         $(Verb) for dir in $(OPTIONAL_DIRS); do \
733           ($(MAKE) -C$$dir $@ ) || exit 1; \
734         done
735 endif
736 endif
737
738 #---------------------------------------------------------
739 # Handle the CONFIG_FILES options
740 #---------------------------------------------------------
741 ifdef CONFIG_FILES
742
743 ifdef NO_INSTALL
744 install-local::
745         $(Echo) Install circumvented with NO_INSTALL
746 uninstall-local::
747         $(Echo) UnInstall circumvented with NO_INSTALL
748 else
749 install-local:: $(PROJ_etcdir) $(CONFIG_FILES)
750         $(Echo) Installing Configuration Files To $(PROJ_etcdir)
751         $(Verb)for file in $(CONFIG_FILES); do \
752           if test -f $(PROJ_OBJ_DIR)/$${file} ; then \
753             $(DataInstall) $(PROJ_OBJ_DIR)/$${file} $(PROJ_etcdir) ; \
754           elif test -f $(PROJ_SRC_DIR)/$${file} ; then \
755             $(DataInstall) $(PROJ_SRC_DIR)/$${file} $(PROJ_etcdir) ; \
756           else \
757             $(ECHO) Error: cannot find config file $${file}. ; \
758           fi \
759         done
760
761 uninstall-local::
762         $(Echo) Uninstalling Configuration Files From $(PROJ_etcdir)
763         $(Verb)for file in $(CONFIG_FILES); do \
764           $(RM) -f $(PROJ_etcdir)/$${file} ; \
765         done
766 endif
767
768 endif
769
770 ###############################################################################
771 # Set up variables for building libararies
772 ###############################################################################
773
774 #---------------------------------------------------------
775 # Define various command line options pertaining to the
776 # libraries needed when linking. There are "Proj" libs
777 # (defined by the user's project) and "LLVM" libs (defined
778 # by the LLVM project).
779 #---------------------------------------------------------
780
781 ifdef USEDLIBS
782 ProjLibsOptions := $(patsubst %.a.o, -l%, $(addsuffix .o, $(USEDLIBS)))
783 ProjLibsOptions := $(patsubst %.o, $(LibDir)/%.o,  $(ProjLibsOptions))
784 ProjUsedLibs    := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(USEDLIBS)))
785 ProjLibsPaths   := $(addprefix $(LibDir)/,$(ProjUsedLibs))
786 endif
787
788 ifdef LLVMLIBS
789 LLVMLibsOptions := $(patsubst %.a.o, -l%, $(addsuffix .o, $(LLVMLIBS)))
790 LLVMLibsOptions := $(patsubst %.o, $(LLVMLibDir)/%.o, $(LLVMLibsOptions))
791 LLVMUsedLibs    := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(LLVMLIBS)))
792 LLVMLibsPaths   := $(addprefix $(LLVMLibDir)/,$(LLVMUsedLibs))
793 endif
794
795 ifndef IS_CLEANING_TARGET
796 ifdef LINK_COMPONENTS
797
798 # If LLVM_CONFIG doesn't exist, build it.  This can happen if you do a make
799 # clean in tools, then do a make in tools (instead of at the top level).
800 $(LLVM_CONFIG):
801         @echo "*** llvm-config doesn't exist - rebuilding it."
802         @$(MAKE) -C $(PROJ_OBJ_ROOT)/tools/llvm-config
803
804 $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT): $(LLVM_CONFIG)
805
806 LLVMLibsOptions += $(shell $(LLVM_CONFIG) --libs     $(LINK_COMPONENTS))
807 LLVMLibsPaths   += $(LLVM_CONFIG) \
808                   $(shell $(LLVM_CONFIG) --libfiles $(LINK_COMPONENTS))
809 endif
810 endif
811
812 ###############################################################################
813 # Library Build Rules: Four ways to build a library
814 ###############################################################################
815
816 #---------------------------------------------------------
817 # Bytecode Module Targets:
818 #   If the user set MODULE_NAME then they want to build a
819 #   bytecode module from the sources. We compile all the
820 #   sources and link it together into a single bytecode
821 #   module.
822 #---------------------------------------------------------
823
824 ifdef MODULE_NAME
825 ifeq ($(strip $(LLVMGCC)),)
826 $(warning Modules require llvm-gcc but no llvm-gcc is available ****)
827 else
828
829 Module     := $(LibDir)/$(MODULE_NAME).bc
830 LinkModule := $(LLVMLD) -L$(CFERuntimeLibDir) -r
831
832
833 ifdef EXPORTED_SYMBOL_FILE
834 LinkModule += -internalize-public-api-file=$(EXPORTED_SYMBOL_FILE)
835 endif
836
837 $(Module): $(BUILT_SOURCES) $(ObjectsBC) $(LibDir)/.dir $(LLVMLD)
838         $(Echo) Building $(BuildMode) Bytecode Module $(notdir $@)
839         $(Verb) $(LinkModule) -o $@ $(ObjectsBC)
840
841 all-local:: $(Module)
842
843 clean-local::
844 ifneq ($(strip $(Module)),)
845         -$(Verb) $(RM) -f $(Module)
846 endif
847
848 ifdef BYTECODE_DESTINATION
849 ModuleDestDir := $(BYTECODE_DESTINATION)
850 else
851 ModuleDestDir := $(PROJ_libdir)
852 endif
853
854 ifdef NO_INSTALL
855 install-local::
856         $(Echo) Install circumvented with NO_INSTALL
857 uninstall-local::
858         $(Echo) Uninstall circumvented with NO_INSTALL
859 else
860 DestModule := $(ModuleDestDir)/$(MODULE_NAME).bc
861
862 install-module:: $(DestModule)
863 install-local:: $(DestModule)
864
865 $(DestModule): $(ModuleDestDir) $(Module)
866         $(Echo) Installing $(BuildMode) Bytecode Module $(DestModule)
867         $(Verb) $(DataInstall) $(Module) $(DestModule)
868
869 uninstall-local::
870         $(Echo) Uninstalling $(BuildMode) Bytecode Module $(DestModule)
871         -$(Verb) $(RM) -f $(DestModule)
872 endif
873
874 endif
875 endif
876
877 # if we're building a library ...
878 ifdef LIBRARYNAME
879
880 # Make sure there isn't any extranous whitespace on the LIBRARYNAME option
881 LIBRARYNAME := $(strip $(LIBRARYNAME))
882 ifdef LOADABLE_MODULE
883 LibName.A  := $(LibDir)/$(LIBRARYNAME).a
884 LibName.SO := $(LibDir)/$(LIBRARYNAME)$(SHLIBEXT)
885 else
886 LibName.A  := $(LibDir)/lib$(LIBRARYNAME).a
887 LibName.SO := $(LibDir)/lib$(LIBRARYNAME)$(SHLIBEXT)
888 endif
889 LibName.O  := $(LibDir)/$(LIBRARYNAME).o
890 LibName.BCA:= $(LibDir)/lib$(LIBRARYNAME).bca
891
892 #---------------------------------------------------------
893 # Shared Library Targets:
894 #   If the user asked for a shared library to be built
895 #   with the SHARED_LIBRARY variable, then we provide
896 #   targets for building them.
897 #---------------------------------------------------------
898 ifdef SHARED_LIBRARY
899
900 all-local:: $(LibName.SO)
901
902 ifdef LINK_LIBS_IN_SHARED
903 ifdef LOADABLE_MODULE
904 SharedLibKindMessage := "Loadable Module"
905 else
906 SharedLibKindMessage := "Shared Library"
907 endif
908 $(LibName.SO): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths) $(LibDir)/.dir
909         $(Echo) Linking $(BuildMode) $(SharedLibKindMessage) \
910           $(LIBRARYNAME)$(SHLIBEXT)
911         $(Verb) $(Link) $(SharedLinkOptions) -o $@ $(ObjectsO) \
912           $(ProjLibsOptions) $(LLVMLibsOptions)
913 else
914 $(LibName.SO): $(ObjectsO) $(LibDir)/.dir
915         $(Echo) Linking $(BuildMode) Shared Library $(LIBRARYNAME)$(SHLIBEXT)
916         $(Verb) $(Link) $(SharedLinkOptions) -o $@ $(ObjectsO)
917 endif
918
919 clean-local::
920 ifneq ($(strip $(LibName.SO)),)
921         -$(Verb) $(RM) -f $(LibName.SO)
922 endif
923
924 ifdef NO_INSTALL
925 install-local::
926         $(Echo) Install circumvented with NO_INSTALL
927 uninstall-local::
928         $(Echo) Uninstall circumvented with NO_INSTALL
929 else
930 DestSharedLib = $(PROJ_libdir)/lib$(LIBRARYNAME)$(SHLIBEXT)
931
932 install-local:: $(DestSharedLib)
933
934 $(DestSharedLib): $(LibName.SO) $(PROJ_libdir)
935         $(Echo) Installing $(BuildMode) Shared Library $(DestSharedLib)
936         $(Verb) $(INSTALL) $(LibName.SO) $(DestSharedLib)
937
938 uninstall-local::
939         $(Echo) Uninstalling $(BuildMode) Shared Library $(DestSharedLib)
940         -$(Verb) $(RM) -f $(PROJ_libdir)/lib$(LIBRARYNAME).*
941 endif
942 endif
943
944 #---------------------------------------------------------
945 # Bytecode Library Targets:
946 #   If the user asked for a bytecode library to be built
947 #   with the BYTECODE_LIBRARY variable, then we provide
948 #   targets for building them.
949 #---------------------------------------------------------
950 ifdef BYTECODE_LIBRARY
951 ifeq ($(strip $(LLVMGCC)),)
952 $(warning Bytecode libraries require llvm-gcc which could not be found ****)
953 else
954
955 all-local:: $(LibName.BCA)
956
957 ifdef EXPORTED_SYMBOL_FILE
958 BCLinkLib = $(LLVMLD) -L$(CFERuntimeLibDir) \
959                      -internalize-public-api-file=$(EXPORTED_SYMBOL_FILE)
960
961 $(LibName.BCA): $(ObjectsBC) $(LibDir)/.dir $(LLVMLD) \
962                 $(LLVMToolDir)/llvm-ar
963         $(Echo) Building $(BuildMode) Bytecode Archive $(notdir $@) \
964           "(internalize)"
965         $(Verb) $(BCLinkLib) -o $(ObjDir)/$(LIBRARYNAME).o $(ObjectsBC)
966         $(Verb) $(RM) -f $@
967         $(Verb) $(LArchive) $@ $(ObjDir)/$(LIBRARYNAME).o
968 else
969 $(LibName.BCA): $(ObjectsBC) $(LibDir)/.dir \
970                 $(LLVMToolDir)/llvm-ar
971         $(Echo) Building $(BuildMode) Bytecode Archive $(notdir $@)
972         $(Verb) $(RM) -f $@
973         $(Verb) $(LArchive) $@ $(ObjectsBC)
974
975 endif
976
977 clean-local::
978 ifneq ($(strip $(LibName.BCA)),)
979         -$(Verb) $(RM) -f $(LibName.BCA)
980 endif
981
982 ifdef BYTECODE_DESTINATION
983 BytecodeDestDir := $(BYTECODE_DESTINATION)
984 else
985 BytecodeDestDir := $(PROJ_libdir)
986 endif
987
988 DestBytecodeLib = $(BytecodeDestDir)/lib$(LIBRARYNAME).a
989
990 install-bytecode-local:: $(DestBytecodeLib)
991
992 ifdef NO_INSTALL
993 install-local::
994         $(Echo) Install circumvented with NO_INSTALL
995 uninstall-local::
996         $(Echo) Uninstall circumvented with NO_INSTALL
997 else
998 install-local:: $(DestBytecodeLib)
999
1000 $(DestBytecodeLib): $(LibName.BCA) $(BytecodeDestDir)
1001         $(Echo) Installing $(BuildMode) Bytecode Archive $(DestBytecodeLib)
1002         $(Verb) $(DataInstall) $(LibName.BCA) $(DestBytecodeLib)
1003
1004 uninstall-local::
1005         $(Echo) Uninstalling $(BuildMode) Bytecode Archive $(DestBytecodeLib)
1006         -$(Verb) $(RM) -f $(DestBytecodeLib)
1007 endif
1008 endif
1009 endif
1010
1011 #---------------------------------------------------------
1012 # ReLinked Library Targets:
1013 #   If the user explicitly requests a relinked library with
1014 #   BUILD_RELINKED, provide it.  Otherwise, if they specify
1015 #   neither of BUILD_ARCHIVE or DONT_BUILD_RELINKED, give
1016 #   them one.
1017 #---------------------------------------------------------
1018 ifndef BUILD_ARCHIVE
1019 ifndef DONT_BUILD_RELINKED
1020 BUILD_RELINKED = 1
1021 endif
1022 endif
1023
1024 ifdef BUILD_RELINKED
1025
1026 all-local:: $(LibName.O)
1027
1028 $(LibName.O): $(ObjectsO) $(LibDir)/.dir
1029         $(Echo) Linking $(BuildMode) Object Library $(notdir $@)
1030         $(Verb) $(Relink) -Wl,-r -nodefaultlibs -nostdlib -nostartfiles -o $@ $(ObjectsO)
1031
1032 clean-local::
1033 ifneq ($(strip $(LibName.O)),)
1034         -$(Verb) $(RM) -f $(LibName.O)
1035 endif
1036
1037 ifdef NO_INSTALL
1038 install-local::
1039         $(Echo) Install circumvented with NO_INSTALL
1040 uninstall-local::
1041         $(Echo) Uninstall circumvented with NO_INSTALL
1042 else
1043 DestRelinkedLib = $(PROJ_libdir)/$(LIBRARYNAME).o
1044
1045 install-local:: $(DestRelinkedLib)
1046
1047 $(DestRelinkedLib): $(LibName.O) $(PROJ_libdir)
1048         $(Echo) Installing $(BuildMode) Object Library $(DestRelinkedLib)
1049         $(Verb) $(INSTALL) $(LibName.O) $(DestRelinkedLib)
1050
1051 uninstall-local::
1052         $(Echo) Uninstalling $(BuildMode) Object Library $(DestRelinkedLib)
1053         -$(Verb) $(RM) -f $(DestRelinkedLib)
1054 endif
1055 endif
1056
1057 #---------------------------------------------------------
1058 # Archive Library Targets:
1059 #   If the user wanted a regular archive library built,
1060 #   then we provide targets for building them.
1061 #---------------------------------------------------------
1062 ifdef BUILD_ARCHIVE
1063
1064 all-local:: $(LibName.A)
1065
1066 $(LibName.A): $(ObjectsO) $(LibDir)/.dir
1067         $(Echo) Building $(BuildMode) Archive Library $(notdir $@)
1068         -$(Verb) $(RM) -f $@
1069         $(Verb) $(Archive) $@ $(ObjectsO)
1070         $(Verb) $(Ranlib) $@
1071
1072 clean-local::
1073 ifneq ($(strip $(LibName.A)),)
1074         -$(Verb) $(RM) -f $(LibName.A)
1075 endif
1076
1077 ifdef NO_INSTALL
1078 install-local::
1079         $(Echo) Install circumvented with NO_INSTALL
1080 uninstall-local::
1081         $(Echo) Uninstall circumvented with NO_INSTALL
1082 else
1083 DestArchiveLib := $(PROJ_libdir)/lib$(LIBRARYNAME).a
1084
1085 install-local:: $(DestArchiveLib)
1086
1087 $(DestArchiveLib): $(LibName.A) $(PROJ_libdir)
1088         $(Echo) Installing $(BuildMode) Archive Library $(DestArchiveLib)
1089         $(Verb) $(MKDIR) $(PROJ_libdir)
1090         $(Verb) $(INSTALL) $(LibName.A) $(DestArchiveLib)
1091
1092 uninstall-local::
1093         $(Echo) Uninstalling $(BuildMode) Archive Library $(DestArchiveLib)
1094         -$(Verb) $(RM) -f $(DestArchiveLib)
1095 endif
1096 endif
1097
1098 # endif LIBRARYNAME
1099 endif
1100
1101 ###############################################################################
1102 # Tool Build Rules: Build executable tool based on TOOLNAME option
1103 ###############################################################################
1104
1105 ifdef TOOLNAME
1106
1107 #---------------------------------------------------------
1108 # Set up variables for building a tool.
1109 #---------------------------------------------------------
1110 ifdef EXAMPLE_TOOL
1111 ToolBuildPath   := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
1112 else
1113 ToolBuildPath   := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
1114 endif
1115
1116 #---------------------------------------------------------
1117 # Prune Exports
1118 #---------------------------------------------------------
1119
1120 # If the tool opts in with TOOL_NO_EXPORTS, optimize startup time of the app by
1121 # not exporting all of the weak symbols from the binary.  This reduces dyld
1122 # startup time by 4x on darwin in some cases.
1123 ifdef TOOL_NO_EXPORTS
1124 ifeq ($(OS),Darwin)
1125
1126 # Tiger tools don't support this.
1127 ifneq ($(DARWIN_MAJVERS),4)
1128 LD.Flags += -Wl,-exported_symbol -Wl,_main
1129 endif
1130 endif
1131
1132 ifeq ($(OS), $(filter $(OS), Linux NetBSD FreeBSD))
1133 LD.Flags += -Wl,--version-script=$(LLVM_SRC_ROOT)/autoconf/ExportMap.map
1134 endif
1135 endif
1136
1137
1138 #---------------------------------------------------------
1139 # Provide targets for building the tools
1140 #---------------------------------------------------------
1141 all-local:: $(ToolBuildPath)
1142
1143 clean-local::
1144 ifneq ($(strip $(ToolBuildPath)),)
1145         -$(Verb) $(RM) -f $(ToolBuildPath)
1146 endif
1147
1148 ifdef EXAMPLE_TOOL
1149 $(ToolBuildPath): $(ExmplDir)/.dir
1150 else
1151 $(ToolBuildPath): $(ToolDir)/.dir
1152 endif
1153
1154 $(ToolBuildPath): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
1155         $(Echo) Linking $(BuildMode) executable $(TOOLNAME) $(StripWarnMsg)
1156         $(Verb) $(Link) -o $@ $(TOOLLINKOPTS) $(ObjectsO) $(ProjLibsOptions) \
1157         $(LLVMLibsOptions) $(ExtraLibs) $(TOOLLINKOPTSB) $(LIBS)
1158         $(Echo) ======= Finished Linking $(BuildMode) Executable $(TOOLNAME) \
1159           $(StripWarnMsg)
1160
1161 ifdef NO_INSTALL
1162 install-local::
1163         $(Echo) Install circumvented with NO_INSTALL
1164 uninstall-local::
1165         $(Echo) Uninstall circumvented with NO_INSTALL
1166 else
1167 DestTool = $(PROJ_bindir)/$(TOOLNAME)
1168
1169 install-local:: $(DestTool)
1170
1171 $(DestTool): $(ToolBuildPath) $(PROJ_bindir)
1172         $(Echo) Installing $(BuildMode) $(DestTool)
1173         $(Verb) $(ProgInstall) $(ToolBuildPath) $(DestTool)
1174
1175 uninstall-local::
1176         $(Echo) Uninstalling $(BuildMode) $(DestTool)
1177         -$(Verb) $(RM) -f $(DestTool)
1178 endif
1179 endif
1180
1181 ###############################################################################
1182 # Object Build Rules: Build object files based on sources
1183 ###############################################################################
1184
1185 # FIXME: This should be checking for "if not GCC or ICC", not for "if HP-UX"
1186 ifeq ($(OS),HP-UX)
1187   DISABLE_AUTO_DEPENDENCIES=1
1188 endif
1189
1190 # Provide rule sets for when dependency generation is enabled
1191 ifndef DISABLE_AUTO_DEPENDENCIES
1192
1193 #---------------------------------------------------------
1194 # Create .o files in the ObjDir directory from the .cpp and .c files...
1195 #---------------------------------------------------------
1196
1197 DEPEND_OPTIONS = -MMD -MP -MF "$(ObjDir)/$*.d.tmp" \
1198          -MT "$(ObjDir)/$*.o" -MT "$(ObjDir)/$*.d"
1199
1200 # If the build succeeded, move the dependency file over.  If it failed, put an
1201 # empty file there.
1202 DEPEND_MOVEFILE = then $(MV) -f "$(ObjDir)/$*.d.tmp" "$(ObjDir)/$*.d"; \
1203                   else $(RM) "$(ObjDir)/$*.d.tmp"; exit 1; fi
1204
1205 $(ObjDir)/%.o: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
1206         $(Echo) "Compiling $*.cpp for $(BuildMode) build " $(PIC_FLAG)
1207         $(Verb) if $(Compile.CXX) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
1208                 $(DEPEND_MOVEFILE)
1209
1210 $(ObjDir)/%.o: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
1211         $(Echo) "Compiling $*.cc for $(BuildMode) build" $(PIC_FLAG)
1212         $(Verb) if $(Compile.CXX) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
1213                 $(DEPEND_MOVEFILE)
1214
1215 $(ObjDir)/%.o: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
1216         $(Echo) "Compiling $*.c for $(BuildMode) build" $(PIC_FLAG)
1217         $(Verb) if $(Compile.C) $(DEPEND_OPTIONS) $< -o $(ObjDir)/$*.o ; \
1218                 $(DEPEND_MOVEFILE)
1219
1220 #---------------------------------------------------------
1221 # Create .bc files in the ObjDir directory from .cpp .cc and .c files...
1222 #---------------------------------------------------------
1223
1224 $(ObjDir)/%.ll: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
1225         $(Echo) "Compiling $*.cpp for $(BuildMode) build (bytecode)"
1226         $(Verb) if $(BCCompile.CXX) -MD -MT $@ -MP -MF "$(ObjDir)/$*.BCCXXd" \
1227                               $< -o $@ -S -emit-llvm ; \
1228         then $(MV) -f "$(ObjDir)/$*.BCCXXd" "$(ObjDir)/$*.d"; \
1229         else $(RM) -f "$(ObjDir)/$*.BCCXXd"; exit 1; fi
1230         $(call UPGRADE_MSG,$@)
1231         $(call UPGRADE_LL,$@)
1232
1233 $(ObjDir)/%.ll: %.cc $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
1234         $(Echo) "Compiling $*.cc for $(BuildMode) build (bytecode)"
1235         $(Verb) if $(BCCompile.CXX) -MD -MT $@ -MP -MF "$(ObjDir)/$*.BCCXXd" \
1236                               $< -o $@ -S -emit-llvm ; \
1237         then $(MV) -f "$(ObjDir)/$*.BCCXXd" "$(ObjDir)/$*.d"; \
1238         else $(RM) -f "$(ObjDir)/$*.BCCXXd"; exit 1; fi
1239         $(call UPGRADE_MSG,$@)
1240         $(call UPGRADE_LL,$@)
1241
1242 $(ObjDir)/%.ll: %.c $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGCC)
1243         $(Echo) "Compiling $*.c for $(BuildMode) build (bytecode)"
1244         $(Verb) if $(BCCompile.C) -MD -MT $@ -MP -MF "$(ObjDir)/$*.BCCd" \
1245                             $< -o $@ -S -emit-llvm ; \
1246         then $(MV) -f "$(ObjDir)/$*.BCCd" "$(ObjDir)/$*.d"; \
1247         else $(RM) -f "$(ObjDir)/$*.BCCd"; exit 1; fi
1248         $(call UPGRADE_MSG,$@)
1249         $(call UPGRADE_LL,$@)
1250
1251 # Provide alternate rule sets if dependencies are disabled
1252 else
1253
1254 $(ObjDir)/%.o: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
1255         $(Echo) "Compiling $*.cpp for $(BuildMode) build" $(PIC_FLAG)
1256         $(Compile.CXX) $< -o $@
1257
1258 $(ObjDir)/%.o: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
1259         $(Echo) "Compiling $*.cc for $(BuildMode) build" $(PIC_FLAG)
1260         $(Compile.CXX) $< -o $@
1261
1262 $(ObjDir)/%.o: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
1263         $(Echo) "Compiling $*.c for $(BuildMode) build" $(PIC_FLAG)
1264         $(Compile.C) $< -o $@
1265
1266 $(ObjDir)/%.ll: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
1267         $(Echo) "Compiling $*.cpp for $(BuildMode) build (bytecode)"
1268         $(BCCompile.CXX) $< -o $@ -S -emit-llvm
1269         $(call UPGRADE_MSG,$@)
1270         $(call UPGRADE_LL,$@)
1271
1272 $(ObjDir)/%.ll: %.cc $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGXX)
1273         $(Echo) "Compiling $*.cc for $(BuildMode) build (bytecode)"
1274         $(BCCompile.CXX) $< -o $@ -S -emit-llvm
1275         $(call UPGRADE_MSG,$@)
1276         $(call UPGRADE_LL,$@)
1277
1278 $(ObjDir)/%.ll: %.c $(ObjDir)/.dir $(BUILT_SOURCES) $(LLVMGCC)
1279         $(Echo) "Compiling $*.c for $(BuildMode) build (bytecode)"
1280         $(BCCompile.C) $< -o $@ -S -emit-llvm
1281         $(call UPGRADE_MSG,@)
1282         $(call UPGRADE_LL,@)
1283
1284 endif
1285
1286
1287 ## Rules for building preprocessed (.i/.ii) outputs.
1288 $(BuildMode)/%.ii: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
1289         $(Echo) "Compiling $*.cpp for $(BuildMode) build to .ii file"
1290         $(Verb) $(Preprocess.CXX) $< -o $@
1291
1292 $(BuildMode)/%.ii: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
1293         $(Echo) "Compiling $*.cc for $(BuildMode) build to .ii file"
1294         $(Verb) $(Preprocess.CXX) $< -o $@
1295
1296 $(BuildMode)/%.i: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
1297         $(Echo) "Compiling $*.c for $(BuildMode) build to .i file"
1298         $(Verb) $(Preprocess.C) $< -o $@
1299
1300
1301 $(ObjDir)/%.s: %.cpp $(ObjDir)/.dir $(BUILT_SOURCES)
1302         $(Echo) "Compiling $*.cpp to asm for $(BuildMode) build" $(PIC_FLAG)
1303         $(Compile.CXX) $< -o $@ -S
1304
1305 $(ObjDir)/%.s: %.cc $(ObjDir)/.dir $(BUILT_SOURCES)
1306         $(Echo) "Compiling $*.cc to asm for $(BuildMode) build" $(PIC_FLAG)
1307         $(Compile.CXX) $< -o $@ -S
1308
1309 $(ObjDir)/%.s: %.c $(ObjDir)/.dir $(BUILT_SOURCES)
1310         $(Echo) "Compiling $*.c to asm for $(BuildMode) build" $(PIC_FLAG)
1311         $(Compile.C) $< -o $@ -S
1312
1313
1314 # make the C and C++ compilers strip debug info out of bytecode libraries.
1315 ifdef DEBUG_RUNTIME
1316 $(ObjectsBC): $(ObjDir)/%.bc: $(ObjDir)/%.ll $(LLVMAS) $(LOPT)
1317         $(Echo) "Compiling $*.ll to $*.bc for $(BuildMode) build (bytecode)"
1318         $(Verb) $(LLVMAS) $< -o - | $(LOPT) -std-compile-opts -o $@ -f
1319 else
1320 $(ObjectsBC): $(ObjDir)/%.bc: $(ObjDir)/%.ll $(LLVMAS) $(LOPT)
1321         $(Echo) "Compiling $*.ll to $*.bc for $(BuildMode) build (bytecode)"
1322         $(Verb) $(LLVMAS) $< -o - | \
1323            $(LOPT) -std-compile-opts -strip-debug -o $@ -f
1324 endif
1325
1326
1327 #---------------------------------------------------------
1328 # Provide rule to build .bc files from .ll sources,
1329 # regardless of dependencies
1330 #---------------------------------------------------------
1331 $(ObjDir)/%.bc: %.ll $(ObjDir)/.dir $(LLVMAS)
1332         $(Echo) "Compiling $*.ll for $(BuildMode) build"
1333         $(Verb) $(LLVMAS) $< -f -o $@
1334
1335 ###############################################################################
1336 # TABLEGEN: Provide rules for running tblgen to produce *.inc files
1337 ###############################################################################
1338
1339 ifdef TARGET
1340 TABLEGEN_INC_FILES_COMMON = 1
1341 endif
1342
1343 ifdef LLVMC_BUILD_AUTOGENERATED_INC
1344 TABLEGEN_INC_FILES_COMMON = 1
1345 endif
1346
1347 ifdef TABLEGEN_INC_FILES_COMMON
1348
1349 INCFiles := $(filter %.inc,$(BUILT_SOURCES))
1350 INCTMPFiles := $(INCFiles:%=$(ObjDir)/%.tmp)
1351 .PRECIOUS: $(INCTMPFiles) $(INCFiles)
1352
1353 # INCFiles rule: All of the tblgen generated files are emitted to
1354 # $(ObjDir)/%.inc.tmp, instead of emitting them directly to %.inc.  This allows
1355 # us to only "touch" the real file if the contents of it change.  IOW, if
1356 # tblgen is modified, all of the .inc.tmp files are regenerated, but no
1357 # dependencies of the .inc files are, unless the contents of the .inc file
1358 # changes.
1359 $(INCFiles) : %.inc : $(ObjDir)/%.inc.tmp
1360         $(Verb) $(CMP) -s $@ $< || $(CP) $< $@
1361
1362 endif # TABLEGEN_INC_FILES_COMMON
1363
1364 ifdef TARGET
1365
1366 TDFiles := $(strip $(wildcard $(PROJ_SRC_DIR)/*.td) \
1367            $(LLVM_SRC_ROOT)/include/llvm/Target/Target.td \
1368            $(LLVM_SRC_ROOT)/include/llvm/Target/TargetCallingConv.td \
1369            $(LLVM_SRC_ROOT)/include/llvm/Target/TargetSchedule.td \
1370            $(LLVM_SRC_ROOT)/include/llvm/Target/TargetSelectionDAG.td \
1371            $(LLVM_SRC_ROOT)/include/llvm/CodeGen/ValueTypes.td) \
1372            $(wildcard $(LLVM_SRC_ROOT)/include/llvm/Intrinsics*.td)
1373
1374 # All of these files depend on tblgen and the .td files.
1375 $(INCTMPFiles) : $(TBLGEN) $(TDFiles)
1376
1377 $(TARGET:%=$(ObjDir)/%GenRegisterNames.inc.tmp): \
1378 $(ObjDir)/%GenRegisterNames.inc.tmp : %.td $(ObjDir)/.dir
1379         $(Echo) "Building $(<F) register names with tblgen"
1380         $(Verb) $(TableGen) -gen-register-enums -o $(call SYSPATH, $@) $<
1381
1382 $(TARGET:%=$(ObjDir)/%GenRegisterInfo.h.inc.tmp): \
1383 $(ObjDir)/%GenRegisterInfo.h.inc.tmp : %.td $(ObjDir)/.dir
1384         $(Echo) "Building $(<F) register information header with tblgen"
1385         $(Verb) $(TableGen) -gen-register-desc-header -o $(call SYSPATH, $@) $<
1386
1387 $(TARGET:%=$(ObjDir)/%GenRegisterInfo.inc.tmp): \
1388 $(ObjDir)/%GenRegisterInfo.inc.tmp : %.td $(ObjDir)/.dir
1389         $(Echo) "Building $(<F) register info implementation with tblgen"
1390         $(Verb) $(TableGen) -gen-register-desc -o $(call SYSPATH, $@) $<
1391
1392 $(TARGET:%=$(ObjDir)/%GenInstrNames.inc.tmp): \
1393 $(ObjDir)/%GenInstrNames.inc.tmp : %.td $(ObjDir)/.dir
1394         $(Echo) "Building $(<F) instruction names with tblgen"
1395         $(Verb) $(TableGen) -gen-instr-enums -o $(call SYSPATH, $@) $<
1396
1397 $(TARGET:%=$(ObjDir)/%GenInstrInfo.inc.tmp): \
1398 $(ObjDir)/%GenInstrInfo.inc.tmp : %.td $(ObjDir)/.dir
1399         $(Echo) "Building $(<F) instruction information with tblgen"
1400         $(Verb) $(TableGen) -gen-instr-desc -o $(call SYSPATH, $@) $<
1401
1402 $(TARGET:%=$(ObjDir)/%GenAsmWriter.inc.tmp): \
1403 $(ObjDir)/%GenAsmWriter.inc.tmp : %.td $(ObjDir)/.dir
1404         $(Echo) "Building $(<F) assembly writer with tblgen"
1405         $(Verb) $(TableGen) -gen-asm-writer -o $(call SYSPATH, $@) $<
1406
1407 $(TARGET:%=$(ObjDir)/%GenAsmWriter1.inc.tmp): \
1408 $(ObjDir)/%GenAsmWriter1.inc.tmp : %.td $(ObjDir)/.dir
1409         $(Echo) "Building $(<F) assembly writer #1 with tblgen"
1410         $(Verb) $(TableGen) -gen-asm-writer -asmwriternum=1 -o $(call SYSPATH, $@) $<
1411
1412 $(TARGET:%=$(ObjDir)/%GenCodeEmitter.inc.tmp): \
1413 $(ObjDir)/%GenCodeEmitter.inc.tmp: %.td $(ObjDir)/.dir
1414         $(Echo) "Building $(<F) code emitter with tblgen"
1415         $(Verb) $(TableGen) -gen-emitter -o $(call SYSPATH, $@) $<
1416
1417 $(TARGET:%=$(ObjDir)/%GenDAGISel.inc.tmp): \
1418 $(ObjDir)/%GenDAGISel.inc.tmp : %.td $(ObjDir)/.dir
1419         $(Echo) "Building $(<F) DAG instruction selector implementation with tblgen"
1420         $(Verb) $(TableGen) -gen-dag-isel -o $(call SYSPATH, $@) $<
1421
1422 $(TARGET:%=$(ObjDir)/%GenFastISel.inc.tmp): \
1423 $(ObjDir)/%GenFastISel.inc.tmp : %.td $(ObjDir)/.dir
1424         $(Echo) "Building $(<F) \"fast\" instruction selector implementation with tblgen"
1425         $(Verb) $(TableGen) -gen-fast-isel -o $(call SYSPATH, $@) $<
1426
1427 $(TARGET:%=$(ObjDir)/%GenSubtarget.inc.tmp): \
1428 $(ObjDir)/%GenSubtarget.inc.tmp : %.td $(ObjDir)/.dir
1429         $(Echo) "Building $(<F) subtarget information with tblgen"
1430         $(Verb) $(TableGen) -gen-subtarget -o $(call SYSPATH, $@) $<
1431
1432 $(TARGET:%=$(ObjDir)/%GenCallingConv.inc.tmp): \
1433 $(ObjDir)/%GenCallingConv.inc.tmp : %.td $(ObjDir)/.dir
1434         $(Echo) "Building $(<F) calling convention information with tblgen"
1435         $(Verb) $(TableGen) -gen-callingconv -o $(call SYSPATH, $@) $<
1436
1437 $(TARGET:%=$(ObjDir)/%GenIntrinsics.inc.tmp): \
1438 $(ObjDir)/%GenIntrinsics.inc.tmp : Intrinsics%.td $(ObjDir)/.dir
1439         $(Echo) "Building $(<F) calling convention information with tblgen"
1440         $(Verb) $(TableGen) -gen-tgt-intrinsic -o $(call SYSPATH, $@) $<
1441
1442 clean-local::
1443         -$(Verb) $(RM) -f $(INCFiles)
1444
1445 endif # TARGET
1446
1447 ifdef LLVMC_BUILD_AUTOGENERATED_INC
1448
1449 LLVMCPluginSrc := $(strip $(wildcard $(PROJ_SRC_DIR)/*.td))
1450
1451 TDFiles := $(LLVMCPluginSrc) \
1452         $(strip $(wildcard $(LLVM_SRC_ROOT)/include/llvm/CompilerDriver/*.td))
1453
1454 $(ObjDir)/AutoGenerated.inc.tmp: $(LLVMCPluginSrc) $(ObjDir)/.dir \
1455                                 $(TBLGEN) $(TD_COMMON)
1456         $(Echo) "Building LLVMC configuration library with tblgen"
1457         $(Verb) $(TableGen) -gen-llvmc -o $(call SYSPATH, $@) $<
1458
1459 endif # LLVMC_BUILD_AUTOGENERATED_INC
1460
1461
1462 ###############################################################################
1463 # OTHER RULES: Other rules needed
1464 ###############################################################################
1465
1466 # To create postscript files from dot files...
1467 ifneq ($(DOT),false)
1468 %.ps: %.dot
1469         $(DOT) -Tps < $< > $@
1470 else
1471 %.ps: %.dot
1472         $(Echo) "Cannot build $@: The program dot is not installed"
1473 endif
1474
1475 # This rules ensures that header files that are removed still have a rule for
1476 # which they can be "generated."  This allows make to ignore them and
1477 # reproduce the dependency lists.
1478 %.h:: ;
1479 %.hpp:: ;
1480
1481 # Define clean-local to clean the current directory. Note that this uses a
1482 # very conservative approach ensuring that empty variables do not cause
1483 # errors or disastrous removal.
1484 clean-local::
1485 ifneq ($(strip $(ObjRootDir)),)
1486         -$(Verb) $(RM) -rf $(ObjRootDir)
1487 endif
1488         -$(Verb) $(RM) -f core core.[0-9][0-9]* *.o *.d *~ *.flc
1489 ifneq ($(strip $(SHLIBEXT)),) # Extra paranoia - make real sure SHLIBEXT is set
1490         -$(Verb) $(RM) -f *$(SHLIBEXT)
1491 endif
1492
1493 clean-all-local::
1494         -$(Verb) $(RM) -rf Debug Release Profile
1495
1496
1497 ###############################################################################
1498 # DEPENDENCIES: Include the dependency files if we should
1499 ###############################################################################
1500 ifndef DISABLE_AUTO_DEPENDENCIES
1501
1502 # If its not one of the cleaning targets
1503 ifndef IS_CLEANING_TARGET
1504
1505 # Get the list of dependency files
1506 DependFiles := $(basename $(filter %.cpp %.c %.cc, $(Sources)))
1507 DependFiles := $(DependFiles:%=$(PROJ_OBJ_DIR)/$(BuildMode)/%.d)
1508
1509 -include $(DependFiles) ""
1510
1511 endif
1512
1513 endif
1514
1515 ###############################################################################
1516 # CHECK: Running the test suite
1517 ###############################################################################
1518
1519 check::
1520         $(Verb) if test -d "$(PROJ_OBJ_ROOT)/test" ; then \
1521           if test -f "$(PROJ_OBJ_ROOT)/test/Makefile" ; then \
1522             $(EchoCmd) Running test suite ; \
1523             $(MAKE) -C $(PROJ_OBJ_ROOT)/test check-local \
1524               TESTSUITE=$(TESTSUITE) ; \
1525           else \
1526             $(EchoCmd) No Makefile in test directory ; \
1527           fi ; \
1528         else \
1529           $(EchoCmd) No test directory ; \
1530         fi
1531
1532 ###############################################################################
1533 # UNITTESTS: Running the unittests test suite
1534 ###############################################################################
1535
1536 unittests::
1537         $(Verb) if test -d "$(PROJ_OBJ_ROOT)/unittests" ; then \
1538           if test -f "$(PROJ_OBJ_ROOT)/unittests/Makefile" ; then \
1539             $(EchoCmd) Running unittests test suite ; \
1540             $(MAKE) -C $(PROJ_OBJ_ROOT)/unittests ; \
1541           else \
1542             $(EchoCmd) No Makefile in unittests directory ; \
1543           fi ; \
1544         else \
1545           $(EchoCmd) No unittests directory ; \
1546         fi
1547
1548 ###############################################################################
1549 # DISTRIBUTION: Handle construction of a distribution tarball
1550 ###############################################################################
1551
1552 #------------------------------------------------------------------------
1553 # Define distribution related variables
1554 #------------------------------------------------------------------------
1555 DistName    := $(PROJECT_NAME)-$(PROJ_VERSION)
1556 DistDir     := $(PROJ_OBJ_ROOT)/$(DistName)
1557 TopDistDir  := $(PROJ_OBJ_ROOT)/$(DistName)
1558 DistTarGZip := $(PROJ_OBJ_ROOT)/$(DistName).tar.gz
1559 DistZip     := $(PROJ_OBJ_ROOT)/$(DistName).zip
1560 DistTarBZ2  := $(PROJ_OBJ_ROOT)/$(DistName).tar.bz2
1561 DistAlways  := CREDITS.TXT LICENSE.TXT README.txt README AUTHORS COPYING \
1562                ChangeLog INSTALL NEWS Makefile Makefile.common Makefile.rules \
1563                Makefile.config.in configure autoconf
1564 DistOther   := $(notdir $(wildcard \
1565                $(PROJ_SRC_DIR)/*.h \
1566                $(PROJ_SRC_DIR)/*.td \
1567                $(PROJ_SRC_DIR)/*.def \
1568                $(PROJ_SRC_DIR)/*.ll \
1569                $(PROJ_SRC_DIR)/*.in))
1570 DistSubDirs := $(SubDirs)
1571 DistSources  = $(Sources) $(EXTRA_DIST)
1572 DistFiles    = $(DistAlways) $(DistSources) $(DistOther)
1573
1574 #------------------------------------------------------------------------
1575 # We MUST build distribution with OBJ_DIR != SRC_DIR
1576 #------------------------------------------------------------------------
1577 ifeq ($(PROJ_SRC_DIR),$(PROJ_OBJ_DIR))
1578 dist dist-check dist-clean dist-gzip dist-bzip2 dist-zip ::
1579         $(Echo) ERROR: Target $@ only available with OBJ_DIR != SRC_DIR
1580
1581 else
1582
1583 #------------------------------------------------------------------------
1584 # Prevent attempt to run dist targets from anywhere but the top level
1585 #------------------------------------------------------------------------
1586 ifneq ($(LEVEL),.)
1587 dist dist-check dist-clean dist-gzip dist-bzip2 dist-zip ::
1588         $(Echo) ERROR: You must run $@ from $(PROJ_OBJ_ROOT)
1589 else
1590
1591 #------------------------------------------------------------------------
1592 # Provide the top level targets
1593 #------------------------------------------------------------------------
1594
1595 dist-gzip:: $(DistTarGZip)
1596
1597 $(DistTarGZip) : $(TopDistDir)/.makedistdir
1598         $(Echo) Packing gzipped distribution tar file.
1599         $(Verb) cd $(PROJ_OBJ_ROOT) ; $(TAR) chf - "$(DistName)" | \
1600           $(GZIP) -c > "$(DistTarGZip)"
1601
1602 dist-bzip2:: $(DistTarBZ2)
1603
1604 $(DistTarBZ2) : $(TopDistDir)/.makedistdir
1605         $(Echo) Packing bzipped distribution tar file.
1606         $(Verb) cd $(PROJ_OBJ_ROOT) ; $(TAR) chf - $(DistName) | \
1607           $(BZIP2) -c >$(DistTarBZ2)
1608
1609 dist-zip:: $(DistZip)
1610
1611 $(DistZip) : $(TopDistDir)/.makedistdir
1612         $(Echo) Packing zipped distribution file.
1613         $(Verb) rm -f $(DistZip)
1614         $(Verb) cd $(PROJ_OBJ_ROOT) ; $(ZIP) -rq $(DistZip) $(DistName)
1615
1616 dist :: $(DistTarGZip) $(DistTarBZ2) $(DistZip)
1617         $(Echo) ===== DISTRIBUTION PACKAGING SUCESSFUL =====
1618
1619 DistCheckDir := $(PROJ_OBJ_ROOT)/_distcheckdir
1620
1621 dist-check:: $(DistTarGZip)
1622         $(Echo) Checking distribution tar file.
1623         $(Verb) if test -d $(DistCheckDir) ; then \
1624           $(RM) -rf $(DistCheckDir) ; \
1625         fi
1626         $(Verb) $(MKDIR) $(DistCheckDir)
1627         $(Verb) cd $(DistCheckDir) && \
1628           $(MKDIR) $(DistCheckDir)/build && \
1629           $(MKDIR) $(DistCheckDir)/install && \
1630           gunzip -c $(DistTarGZip) | $(TAR) xf - && \
1631           cd build && \
1632           ../$(DistName)/configure --prefix="$(DistCheckDir)/install" \
1633             --srcdir=../$(DistName) $(DIST_CHECK_CONFIG_OPTIONS) && \
1634           $(MAKE) all && \
1635           $(MAKE) check && \
1636           $(MAKE) unittests && \
1637           $(MAKE) install && \
1638           $(MAKE) uninstall && \
1639           $(MAKE) dist-clean && \
1640           $(EchoCmd) ===== $(DistTarGZip) Ready For Distribution =====
1641
1642 dist-clean::
1643         $(Echo) Cleaning distribution files
1644         -$(Verb) $(RM) -rf $(DistTarGZip) $(DistTarBZ2) $(DistZip) $(DistName) \
1645           $(DistCheckDir)
1646
1647 endif
1648
1649 #------------------------------------------------------------------------
1650 # Provide the recursive distdir target for building the distribution directory
1651 #------------------------------------------------------------------------
1652 distdir: $(DistDir)/.makedistdir
1653 $(DistDir)/.makedistdir: $(DistSources)
1654         $(Verb) if test "$(DistDir)" = "$(TopDistDir)" ; then \
1655           if test -d "$(DistDir)" ; then \
1656             find $(DistDir) -type d ! -perm -200 -exec chmod u+w {} ';'  || \
1657               exit 1 ; \
1658           fi ; \
1659           $(EchoCmd) Removing old $(DistDir) ; \
1660           $(RM) -rf $(DistDir); \
1661           $(EchoCmd) Making 'all' to verify build ; \
1662           $(MAKE) ENABLE_OPTIMIZED=1 all ; \
1663         fi
1664         $(Echo) Building Distribution Directory $(DistDir)
1665         $(Verb) $(MKDIR) $(DistDir)
1666         $(Verb) srcdirstrip=`echo "$(PROJ_SRC_DIR)" | sed 's|.|.|g'`; \
1667         srcrootstrip=`echo "$(PROJ_SRC_ROOT)" | sed 's|.|.|g'`; \
1668         for file in $(DistFiles) ; do \
1669           case "$$file" in \
1670             $(PROJ_SRC_DIR)/*) \
1671               file=`echo "$$file" | sed "s#^$$srcdirstrip/##"` \
1672               ;; \
1673             $(PROJ_SRC_ROOT)/*) \
1674               file=`echo "$$file" | \
1675                 sed "s#^$$srcrootstrip/##"` \
1676               ;; \
1677           esac; \
1678           if test -f "$(PROJ_SRC_DIR)/$$file" || \
1679              test -d "$(PROJ_SRC_DIR)/$$file" ; then \
1680             from_dir="$(PROJ_SRC_DIR)" ; \
1681           elif test -f "$$file" || test -d "$$file" ; then \
1682             from_dir=. ; \
1683           fi ; \
1684           to_dir=`echo "$$file" | sed -e 's#/[^/]*$$##'` ; \
1685           if test "$$to_dir" != "$$file" && test "$$to_dir" != "."; then \
1686             to_dir="$(DistDir)/$$dir"; \
1687             $(MKDIR) "$$to_dir" ; \
1688           else \
1689             to_dir="$(DistDir)"; \
1690           fi; \
1691           mid_dir=`echo "$$file" | sed -n -e 's#^\(.*\)/[^/]*$$#\1#p'`; \
1692           if test -n "$$mid_dir" ; then \
1693             $(MKDIR) "$$to_dir/$$mid_dir" || exit 1; \
1694           fi ; \
1695           if test -d "$$from_dir/$$file"; then \
1696             if test -d "$(PROJ_SRC_DIR)/$$file" && \
1697                test "$$from_dir" != "$(PROJ_SRC_DIR)" ; then \
1698                cd $(PROJ_SRC_DIR) ; \
1699                $(TAR) cf - $$file --exclude .svn --exclude CVS | \
1700                  ( cd $$to_dir ; $(TAR) xf - ) ; \
1701                cd $(PROJ_OBJ_DIR) ; \
1702             else \
1703                cd $$from_dir ; \
1704                $(TAR) cf - $$file --exclude .svn --exclude CVS | \
1705                  ( cd $$to_dir ; $(TAR) xf - ) ; \
1706                cd $(PROJ_OBJ_DIR) ; \
1707             fi; \
1708           elif test -f "$$from_dir/$$file" ; then \
1709             $(CP) -p "$$from_dir/$$file" "$(DistDir)/$$file" || exit 1; \
1710           elif test -L "$$from_dir/$$file" ; then \
1711             $(CP) -pd "$$from_dir/$$file" $(DistDir)/$$file || exit 1; \
1712           elif echo "$(DistAlways)" | grep -v "$$file" >/dev/null ; then \
1713             $(EchoCmd) "===== WARNING: Distribution Source " \
1714               "$$from_dir/$$file Not Found!" ; \
1715           elif test "$(Verb)" != '@' ; then \
1716             $(EchoCmd) "Skipping non-existent $$from_dir/$$file" ; \
1717           fi; \
1718         done
1719         $(Verb) for subdir in $(DistSubDirs) ; do \
1720           if test "$$subdir" \!= "." ; then \
1721             new_distdir="$(DistDir)/$$subdir" ; \
1722             test -d "$$new_distdir" || $(MKDIR) "$$new_distdir" || exit 1; \
1723             ( cd $$subdir && $(MAKE) ENABLE_OPTIMIZED=1 \
1724               DistDir="$$new_distdir" distdir ) || exit 1; \
1725           fi; \
1726         done
1727         $(Verb) if test "$(DistDir)" = "$(TopDistDir)" ; then \
1728           $(EchoCmd) Eliminating CVS/.svn directories from distribution ; \
1729           $(RM) -rf `find $(TopDistDir) -type d \( -name CVS -o \
1730                                   -name .svn \) -print` ;\
1731           $(MAKE) dist-hook ; \
1732           $(FIND) $(TopDistDir) -type d ! -perm -777 -exec chmod a+rwx {} \; \
1733             -o ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; \
1734             -o ! -type d ! -perm -400 -exec chmod a+r {} \; \
1735             -o ! -type d ! -perm -444 -exec \
1736               $(SHELL) $(INSTALL_SH) -c -m a+r {} {} \; \
1737             || chmod -R a+r $(DistDir) ; \
1738         fi
1739
1740 # This is invoked by distdir target, define it as a no-op to avoid errors if not
1741 # defined by user.
1742 dist-hook::
1743
1744 endif
1745
1746 ###############################################################################
1747 # TOP LEVEL - targets only to apply at the top level directory
1748 ###############################################################################
1749
1750 ifeq ($(LEVEL),.)
1751
1752 #------------------------------------------------------------------------
1753 # Install support for the project's include files:
1754 #------------------------------------------------------------------------
1755 ifdef NO_INSTALL
1756 install-local::
1757         $(Echo) Install circumvented with NO_INSTALL
1758 uninstall-local::
1759         $(Echo) Uninstall circumvented with NO_INSTALL
1760 else
1761 install-local::
1762         $(Echo) Installing include files
1763         $(Verb) $(MKDIR) $(PROJ_includedir)
1764         $(Verb) if test -d "$(PROJ_SRC_ROOT)/include" ; then \
1765           cd $(PROJ_SRC_ROOT)/include && \
1766           for  hdr in `find . -type f '!' '(' -name '*~' \
1767               -o -name '.#*' -o -name '*.in' ')' -print | grep -v CVS | \
1768               grep -v .svn` ; do \
1769             instdir=`dirname "$(PROJ_includedir)/$$hdr"` ; \
1770             if test \! -d "$$instdir" ; then \
1771               $(EchoCmd) Making install directory $$instdir ; \
1772               $(MKDIR) $$instdir ;\
1773             fi ; \
1774             $(DataInstall) $$hdr $(PROJ_includedir)/$$hdr ; \
1775           done ; \
1776         fi
1777 ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT))
1778         $(Verb) if test -d "$(PROJ_OBJ_ROOT)/include" ; then \
1779           cd $(PROJ_OBJ_ROOT)/include && \
1780           for hdr in `find . -type f -print | grep -v CVS` ; do \
1781             $(DataInstall) $$hdr $(PROJ_includedir)/$$hdr ; \
1782           done ; \
1783         fi
1784 endif
1785
1786 uninstall-local::
1787         $(Echo) Uninstalling include files
1788         $(Verb) if [ -d "$(PROJ_SRC_ROOT)/include" ] ; then \
1789           cd $(PROJ_SRC_ROOT)/include && \
1790             $(RM) -f `find . -path '*/Internal' -prune -o '(' -type f \
1791               '!' '(' -name '*~' -o -name '.#*' \
1792         -o -name '*.in' ')' -print ')' | \
1793         grep -v CVS | sed 's#^#$(PROJ_includedir)/#'` ; \
1794           cd $(PROJ_SRC_ROOT)/include && \
1795             $(RM) -f `find . -path '*/Internal' -prune -o '(' -type f -name '*.in' \
1796       -print ')' | sed 's#\.in$$##;s#^#$(PROJ_includedir)/#'` ; \
1797         fi
1798 endif
1799 endif
1800
1801 check-line-length:
1802         @echo searching for overlength lines in files: $(Sources)
1803         @echo
1804         @echo
1805         egrep -n '.{81}' $(Sources) /dev/null
1806
1807 check-for-tabs:
1808         @echo searching for tabs in files: $(Sources)
1809         @echo
1810         @echo
1811         egrep -n '      ' $(Sources) /dev/null
1812
1813 check-footprint:
1814         @ls -l $(LibDir) | awk '\
1815           BEGIN { sum = 0; } \
1816                 { sum += $$5; } \
1817           END   { printf("Libraries: %6.3f MBytes\n", sum/(1024.0*1024.0)); }'
1818         @ls -l $(ToolDir) | awk '\
1819           BEGIN { sum = 0; } \
1820                 { sum += $$5; } \
1821           END   { printf("Programs:  %6.3f MBytes\n", sum/(1024.0*1024.0)); }'
1822 #------------------------------------------------------------------------
1823 # Print out the directories used for building
1824 #------------------------------------------------------------------------
1825 printvars::
1826         $(Echo) "BuildMode    : " '$(BuildMode)'
1827         $(Echo) "PROJ_SRC_ROOT: " '$(PROJ_SRC_ROOT)'
1828         $(Echo) "PROJ_SRC_DIR : " '$(PROJ_SRC_DIR)'
1829         $(Echo) "PROJ_OBJ_ROOT: " '$(PROJ_OBJ_ROOT)'
1830         $(Echo) "PROJ_OBJ_DIR : " '$(PROJ_OBJ_DIR)'
1831         $(Echo) "LLVM_SRC_ROOT: " '$(LLVM_SRC_ROOT)'
1832         $(Echo) "LLVM_OBJ_ROOT: " '$(LLVM_OBJ_ROOT)'
1833         $(Echo) "PROJ_prefix  : " '$(PROJ_prefix)'
1834         $(Echo) "PROJ_bindir  : " '$(PROJ_bindir)'
1835         $(Echo) "PROJ_libdir  : " '$(PROJ_libdir)'
1836         $(Echo) "PROJ_etcdir  : " '$(PROJ_etcdir)'
1837         $(Echo) "PROJ_includedir  : " '$(PROJ_includedir)'
1838         $(Echo) "UserTargets  : " '$(UserTargets)'
1839         $(Echo) "ObjMakefiles : " '$(ObjMakefiles)'
1840         $(Echo) "SrcMakefiles : " '$(SrcMakefiles)'
1841         $(Echo) "ObjDir       : " '$(ObjDir)'
1842         $(Echo) "LibDir       : " '$(LibDir)'
1843         $(Echo) "ToolDir      : " '$(ToolDir)'
1844         $(Echo) "ExmplDir     : " '$(ExmplDir)'
1845         $(Echo) "Sources      : " '$(Sources)'
1846         $(Echo) "TDFiles      : " '$(TDFiles)'
1847         $(Echo) "INCFiles     : " '$(INCFiles)'
1848         $(Echo) "INCTMPFiles  : " '$(INCTMPFiles)'
1849         $(Echo) "PreConditions: " '$(PreConditions)'
1850         $(Echo) "Compile.CXX  : " '$(Compile.CXX)'
1851         $(Echo) "Compile.C    : " '$(Compile.C)'
1852         $(Echo) "Archive      : " '$(Archive)'
1853         $(Echo) "YaccFiles    : " '$(YaccFiles)'
1854         $(Echo) "LexFiles     : " '$(LexFiles)'
1855         $(Echo) "Module       : " '$(Module)'
1856         $(Echo) "FilesToConfig: " '$(FilesToConfigPATH)'
1857         $(Echo) "SubDirs      : " '$(SubDirs)'
1858         $(Echo) "ProjLibsPaths: " '$(ProjLibsPaths)'
1859         $(Echo) "ProjLibsOptions: " '$(ProjLibsOptions)'
1860
1861 ###
1862 # Debugging
1863
1864 # General debugging rule, use 'make dbg-print-XXX' to print the
1865 # definition, value and origin of XXX.
1866 make-print-%:
1867         $(error PRINT: $(value $*) = "$($*)" (from $(origin $*)))