summaryrefslogtreecommitdiff
path: root/Makefile.rules
blob: 8c24bbba9b3afd79e34a40459f9ea948513cfb1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#===-- Makefile.rules - Common make rules for LLVM ---------*- Makefile -*--===#
#
#                     The LLVM Compiler Infrastructure
#
# This file was developed by the LLVM research group and is distributed under
# the University of Illinois Open Source License. See LICENSE.TXT for details.
# 
#===------------------------------------------------------------------------===#
#
# This file is included by all of the LLVM makefiles.  This file defines common
# rules to do things like compile a .cpp file or generate dependency info.
# These are platform dependent, so this is the file used to specify these
# system dependent operations.
#
# The following functionality can be set by setting incoming variables.
# The variable $(LEVEL) *must* be set:
#
# 1. LEVEL - The level of the current subdirectory from the top of the 
#    MagicStats view.  This level should be expressed as a path, for 
#    example, ../.. for two levels deep.
#
# 2. DIRS - A list of subdirectories to be built.  Fake targets are set up
#    so that each of the targets "all", "install", and "clean" each build
#    the subdirectories before the local target.  DIRS are guaranteed to be
#    built in order.
#
# 3. PARALLEL_DIRS - A list of subdirectories to be built, but that may be
#    built in any order.  All DIRS are built in order before PARALLEL_DIRS are
#    built, which are then built in any order.
#
# 4. Source - If specified, this sets the source code filenames.  If this
#    is not set, it defaults to be all of the .cpp, .c, .y, and .l files 
#    in the current directory.  Also, if you want to build files in addition
#    to the local files, you can use the BUILT_SOURCES variable
#
# 6. LLVM_SRC_ROOT - If specified, points to the top of the LLVM source tree.
#
# 8. BUILD_SRC_DIR - The directory which contains the current set of Makefiles
#    and usually the source code too (unless SourceDir is set).
#
# 9. BUILD_SRC_ROOT - The root directory of the source code being compiled.
#
# 10. BUILD_OBJ_DIR - The directory where object code should be placed.
#
# 11. BUILD_OBJ_ROOT - The root directory for where object code should be
#     placed.
#
# For building,
# 	LLVM, LLVM_SRC_ROOT = BUILD_SRC_ROOT
#
#===-----------------------------------------------------------------------====

#
# Set the VPATH so that we can find source files.
#
VPATH=$(BUILD_SRC_DIR)

###########################################################################
# Default Targets:
#	The following targets are the standard top level targets for
#	building.
###########################################################################


# Ensure all preconditions are met
all:: preconditions

# Make sure all the user-target rules are double colon rules
all :: preconditions
clean:: preconditions
install :: preconditions
uninstall :: preconditions
check:: preconditions
dist:: preconditions
distcheck:: preconditions
distclean:: preconditions


###########################################################################
# Suffixes and implicit rules:
#	Empty out the list of suffixes, generate a list that is only
#	used by this Makefile, and cancel useless implicit rules.   This
#	will hopefully speed up compilation a little bit.
###########################################################################
.SUFFIXES:
.SUFFIXES: .c .cpp .h .hpp .y .l
.SUFFIXES: .lo .o .a $(SHLIBEXT) .bc .td
.SUFFIXES: .ps .dot .d

#
# Mark all of these targets as phony.  This will hopefully speed up builds
# slightly since GNU Make will not try to find implicit rules for targets
# which are marked as Phony.
#
ALL_TARGETS= all dynamic bytecodelib install-bytecode-library clean distclean install test bytecode prdirs
.PHONY: $(ALL_TARGETS) preconditions

###########################################################################
# Miscellaneous paths and commands:
#	This section defines various configuration macros, such as where
#	to find burg, tblgen, and libtool.
###########################################################################

#--------------------------------------------------------------------
# Variables derived from configuration options... 
#--------------------------------------------------------------------

ifdef ENABLE_PROFILING
  CONFIGURATION := Profile
  CXXFLAGS += -O3 -DNDEBUG -felide-constructors -finline-functions -pg
  CFLAGS   += -O3 -DNDEBUG -pg
  LDFLAGS  += -O3 -DNDEBUG -pg 
else
  ifdef ENABLE_OPTIMIZED
    CONFIGURATION := Release
    CXXFLAGS  += -O3 -DNDEBUG -finline-functions -felide-constructors -fomit-frame-pointer
    CFLAGS    += -O3 -DNDEBUG -fomit-frame-pointer
    LDFLAGS   += -O3 -DNDEBUG 
  else
    CONFIGURATION := Debug
    CXXFLAGS += -g -D_DEBUG 
    CFLAGS   += -g -D_DEBUG
    LDFLAGS  += -g -D_DEBUG 
    KEEP_SYMBOLS := 1
  endif
endif

ARFLAGS := cru
#------------------------------------------------------------------------------
# Directory locations
OBJDIR      := $(BUILD_OBJ_DIR)/$(CONFIGURATION)
LIBDIR      := $(BUILD_OBJ_ROOT)/lib/$(CONFIGURATION)
TOOLDIR     := $(BUILD_OBJ_ROOT)/tools/$(CONFIGURATION)
LLVMLIBDIR  := $(LLVM_OBJ_ROOT)/lib/$(CONFIGURATION)
LLVMTOOLDIR := $(LLVM_OBJ_ROOT)/tools/$(CONFIGURATION)

#------------------------------------------------------------------------------
# Full Paths To Compiled Tools and Utilities
LIBTOOL  := $(LLVM_OBJ_ROOT)/mklib
LLVMAS   := $(LLVMTOOLDIR)/llvm-as$(EXEEXT)
BURG     := $(LLVMTOOLDIR)/burg$(EXEEXT)
TBLGEN   := $(LLVMTOOLDIR)/tblgen$(EXEEXT)
GCCLD    := $(LLVMTOOLDIR)/gccld$(EXEEXT)
LLVMGCC  := PATH=$(LLVMTOOLDIR):$(PATH) $(LLVMGCCDIR)/bin/gcc
LLVMGXX  := PATH=$(LLVMTOOLDIR):$(PATH) $(LLVMGCCDIR)/bin/g++

# Need a better way to compute this.
LLVMGCCLIBDIR := $(dir $(shell $(LLVMGCC) -print-file-name=libgcc.a))/

#------------------------------------------------------------------------------
# Adjust to user's request

ifndef SHARED_LIBRARY
  LIBTOOL += --tag=disable-shared
else
  LDFLAGS += -rpath $(LIBDIR)
endif

#
# Verbosity levels
#
ifndef VERBOSE
  VERB := @
  LIBTOOL += --silent
  AR += >/dev/null 2>/dev/null
endif

# By default, strip symbol information from executable
ifndef KEEP_SYMBOLS
  STRIP = $(PLATFORMSTRIPOPTS)
  STRIP_WARN_MSG = "(without symbols)"
endif

ifdef TOOLNAME
  LDFLAGS += -rpath $(TOOLDIR) -export-dynamic $(TOOLLINKOPTS)
endif

# TOOLLINKOPTSB to pass options to the linker like library search path etc
# Note that this is different from TOOLLINKOPTS, these options
# are passed to the linker *before* the USEDLIBS options are passed.
# e.g. usage TOOLLINKOPTSB =  -L/home/xxx/lib
ifdef TOOLLINKOPTSB
LDFLAGS += $(TOOLLINKOPTSB)
endif

LDFLAGS += -L$(LIBDIR) -L$(LLVMLIBDIR)

#------------------------------------------------------------------------------
# Options To Invoke Tools

CompileCommonOpts := -Wall -W -Wwrite-strings -Wno-unused

CPPFLAGS += -I$(BUILD_OBJ_DIR) \
            -I$(BUILD_SRC_DIR) \
            -I$(BUILD_SRC_ROOT)/include \
            -I$(BUILD_OBJ_ROOT)/include \
            -I$(LLVM_OBJ_ROOT)/include \
            -I$(LLVM_SRC_ROOT)/include \
            -D_GNU_SOURCE -D__STDC_LIMIT_MACROS

CXXCompile   := $(CXX) $(CPPFLAGS) $(CompileCommonOpts) $(CXXFLAGS) -c
LTCXXCompile := $(LIBTOOL) --tag=CXX --mode=compile $(CXXCompile)
CCompile     := $(CC) $(CPPFLAGS) $(CompileCommonOpts) -c $(CFLAGS)
LTCCompile   := $(LIBTOOL) --mode=compile $(CCompile)
BCCXXCompile := $(LLVMGXX) $(CPPFLAGS) $(CompileCommonOpts) $(CXXFLAGS) -c
BCCCompile   := $(LLVMGCC) $(CPPFLAGS) $(CompileCommonOpts) $(CFLAGS) -c
Link         := $(LIBTOOL) --tag=CXX --mode=link $(CXX) $(CPPFLAGS) $(CompileCommonOpts) $(LDFLAGS) $(STRIP)
Relink       := $(LIBTOOL) --tag=CXX --mode=link $(CXX)
BCLinkLib    := $(LLVMGCC) -shared -nostdlib
Burg         := $(BURG) -I $(BUILD_SRC_DIR)
TableGen     := $(TBLGEN) -I $(BUILD_SRC_DIR)
Archive      := $(AR) $(ARFLAGS)
ifdef RANLIB
Ranlib       := $(RANLIB)
else
Ranlib       := ranlib
endif

#----------------------------------------------------------
# Source includes all of the cpp files, and objects are derived from the
# source files...
# The local Makefile can list other Source files via BUILT_SOURCES = ...
# 
ifndef Source

Source  := $(notdir $(wildcard $(BUILD_SRC_DIR)/*.cpp \
                    $(BUILD_SRC_DIR)/*.cc $(BUILD_SRC_DIR)/*.c $(BUILD_SRC_DIR)/*.y \
                    $(BUILD_SRC_DIR)/*.l))
endif

ifdef BUILT_SOURCES
Source += $(filter %.cpp %.c %.cc %.y %.l,$(BUILT_SOURCES))
endif

#
# Libtool Objects
#
Srcs := $(sort $(basename $(Source)))
ObjectsO  := $(Srcs:%=$(OBJDIR)/%.o)
ObjectsLO := $(Srcs:%=$(OBJDIR)/%.lo)
ObjectsBC := $(Srcs:%=$(OBJDIR)/%.bc)

#---------------------------------------------------------
# Handle the DIRS and PARALLEL_DIRS options
#---------------------------------------------------------

ifdef DIRS
$(ALL_TARGETS)::
	$(VERB) for dir in $(DIRS); do \
	  if [ ! -f $$dir/Makefile ]; then \
	    $(MKDIR) $$dir; \
	    cp $(BUILD_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
	  fi; \
	  ($(MAKE) -C $$dir $@ $(MFLAGS)) || exit 1; \
	done
endif

# Handle PARALLEL_DIRS
ifdef PARALLEL_DIRS
all      :: $(addsuffix /.makeall     , $(PARALLEL_DIRS))
install  :: $(addsuffix /.makeinstall , $(PARALLEL_DIRS))
clean    :: $(addsuffix /.makeclean   , $(PARALLEL_DIRS))
test     :: $(addsuffix /.maketest    , $(PARALLEL_DIRS))
bytecode :: $(addsuffix /.makebytecode, $(PARALLEL_DIRS))
stripped-bytecode :: $(addsuffix /.makestripped-bytecode, $(PARALLEL_DIRS))
install-bytecode :: $(addsuffix /.makeinstall-bytecode, $(PARALLEL_DIRS))

%/.makeall %/.makeinstall %/.makeclean %/.maketest %/.makebytecode %/.makestripped-bytecode %/.makeinstall-bytecode:
	$(VERB) if [ ! -f $(@D)/Makefile ]; then \
	  $(MKDIR) $(@D); \
	  cp $(BUILD_SRC_DIR)/$(@D)/Makefile $(@D)/Makefile; \
	fi; \
	$(MAKE) -C $(@D) $(subst $(@D)/.make,,$@) $(MFLAGS)
endif

# Handle directories that may or may not exist
ifdef OPTIONAL_DIRS
$(ALL_TARGETS)::
	$(VERB) for dir in $(OPTIONAL_DIRS); do \
	  if [ -d $(BUILD_SRC_DIR)/$$dir ]; then\
	    if [ ! -f $$dir/Makefile ]; then \
	      $(MKDIR) $$dir; \
	      cp $(BUILD_SRC_DIR)/$$dir/Makefile $$dir/Makefile; \
	    fi; \
	    ($(MAKE) -C$$dir $@ $(MFLAGS)) || exit 1; \
	  fi \
	done
endif

#---------------------------------------------------------
# Handle the CONFIG_FILES options
#---------------------------------------------------------
ifdef CONFIG_FILES
install:: install-config-files

$(sysconfdir):
	$(MKDIR) $(sysconfdir)

install-config-files: $(sysconfdir) $(CONFIG_FILES)
	$(VERB)$(ECHO) Installing Configuration Files To $(sysconfdir)
	$(VERB)for file in $(CONFIG_FILES); do \
		$(INSTALL) $(BUILD_SRC_DIR)/$${file} $(sysconfdir) ; \
	done
endif

###########################################################################
# Library Build Rules:
#
#---------------------------------------------------------
# Handle the LIBRARYNAME option - used when building libs...
#---------------------------------------------------------
#
#  When libraries are built, they are allowed to optionally define the
#  DONT_BUILD_RELINKED make variable, which, when defined, prevents a .o file
#  from being built for the library. This .o files may then be linked to by a
#  tool if the tool does not need (or want) the semantics a .a file provides
#  (linking in only object files that are "needed").  If a library is never to
#  be used in this way, it is better to define DONT_BUILD_RELINKED, and define
#  BUILD_ARCHIVE instead.
#
#  Some libraries must be built as .a files (libscalar for example) because if
#  it's built as a .o file, then all of the constituent .o files in it will be
#  linked into tools (for example gccas) even if they only use one of the parts
#  of it.  For this reason, sometimes it's useful to use libraries as .a files.
###########################################################################

# Install rule for making bytecode library directory if it does not exist.
# Trigger this by making libraries that need to be installed here depend on it.
$(DESTDIR)$(bytecode_libdir):
	$(MKDIR) $@

ifdef LIBRARYNAME

# Make sure there isn't any extranous whitespace on the LIBRARYNAME option
LIBRARYNAME := $(strip $(LIBRARYNAME))
LIBNAME_LA := $(LIBDIR)/lib$(LIBRARYNAME).la
LIBNAME_A  := $(LIBDIR)/lib$(LIBRARYNAME).a
LIBNAME_O  := $(LIBDIR)/$(LIBRARYNAME).o
LIBNAME_BC := $(LIBDIR)/lib$(LIBRARYNAME).bc

#-------------------------------------------------------------------------------
# Shared Library Targets
#	Modify the top level targets to build the desired libraries.
#-------------------------------------------------------------------------------

# Does the library want a shared library version built?
ifdef SHARED_LIBRARY

all:: $(LIBNAME_LA)

$(LIBNAME_LA): $(BUILT_SOURCES) $(ObjectsLO) $(LIBDIR)/.dir
	@$(ECHO) Linking shared library $(notdir $@)
	$(VERB) $(Link) -o $@ $(ObjectsLO)
	$(VERB) $(LIBTOOL) --mode=install $(INSTALL) $@ $(LIBDIR)
	@$(ECHO) Finished linking shared library $(LIBRARYNAME).la 
clean::
	$(VERB) $(RM) -f $(LIBNAME_LA)

install:: $(DESTDIR)$(libdir)/lib$(LIBRARYNAME)$(SHLIBEXT)

$(DESTDIR)/lib/lib$(LIBRARYNAME)$(SHLIBEXT): $(LIBNAME_LA)
	@$(ECHO) Installing shared library $(notdir $@)
	$(VERB) $(MKDIR) $(DESTDIR)
	$(VERB) $(LIBTOOL) --mode=install $(INSTALL) $(LIBNAME_LA) $(DESTDIR)$(libdir)/lib$(LIBRARYNAME)$(SHLIBEXT)
	$(VERB) $(LIBTOOL) --finish $(DESTDIR)$(libdir)
endif

# Does the library want a bytecode version built?
ifdef BYTECODE_LIBRARY

ifdef EXPORTED_SYMBOL_LIST
  BCLinkLib += -Xlinker -internalize-public-api-list=$(EXPORTED_SYMBOL_LIST)
else
  ifdef EXPORTED_SYMBOL_FILE
    BCLinkLib += -Xlinker -internalize-public-api-file=$(EXPORTED_SYMBOL_FILE)
  else
    BCLinkLib += -Xlinker -disable-internalize
  endif
endif

all:: $(LIBNAME_BC)

$(LIBNAME_BC): $(BUILT_SOURCES) $(ObjectsBC) $(LIBDIR)/.dir
	@$(ECHO) Linking bytecode library $(notdir $@)
	$(VERB) $(BCLinkLib) -o $@ $(ObjectsBC)

clean::
	$(VERB) $(RM) -f $(LIBNAME_BC)

install:: $(DESTDIR)$(bytecode_libdir)/lib$(LIBRARYNAME).bc

$(DESTDIR)$(bytecode_libdir)/lib$(LIBRARYNAME).bc: $(LIBNAME_BC) $(DESTDIR)$(bytecode_libdir)
	@$(ECHO) Installing bytecode library $(notdir $@)
	$(VERB)$(INSTALL) $< $@

endif

# Does the library want a .o version built?
ifndef DONT_BUILD_RELINKED
all:: $(LIBNAME_O)

$(LIBNAME_O): $(BUILT_SOURCES) $(ObjectsO) $(LIBDIR)/.dir
	@$(ECHO) Linking object $(notdir $@)
	$(VERB) $(Relink) -o $@ $(ObjectsO)

install:: $(DESTDIR)$(libdir)/$(LIBRARYNAME).o

$(DESTDIR)$(libdir)/$(LIBRARYNAME).o: $(LIBNAME_O)
	@$(ECHO) Installing object library $(notdir $@)
	$(VERB) $(MKDIR) $(DESTDIR)$(libdir)
	$(VERB) $(LIBTOOL) --mode=install $(INSTALL) $(LIBNAME_O) $(DESTDIR)$(libdir)/$(LIBRARYNAME).o

clean::
	$(VERB) $(RM) -f $(LIBNAME_O)

endif

# Does the library want an archive version built?
ifdef BUILD_ARCHIVE
all:: $(LIBNAME_A)

$(LIBNAME_A): $(BUILT_SOURCES) $(ObjectsO) $(LIBDIR)/.dir
	@$(ECHO) Linking archive $(notdir $@) library
	$(VERB)$(RM) -f $@
	$(VERB) $(Archive) $@ $(ObjectsO)
	$(VERB) $(Ranlib) $@

clean::
	$(VERB) $(RM) -f $(LIBNAME_A)

install:: $(DESTDIR)$(libdir)/lib$(LIBRARYNAME).a

$(DESTDIR)$(libdir)/lib$(LIBRARYNAME).a: $(LIBNAME_A)
	$(MKDIR) $(DESTDIR)$(libdir)
	$(VERB) $(LIBTOOL) --mode=install $(INSTALL) $(LIBNAME_A) $(DESTDIR)$(libdir)/lib$(LIBRARYNAME).a
endif

# if LIBRARYNAME
endif 

#------------------------------------------------------------------------
# Handle the TOOLNAME option - used when building tool executables...
#------------------------------------------------------------------------
#
# The TOOLNAME option should be used with a USEDLIBS variable that tells the
# libraries (and the order of the libs) that should be linked to the
# tool. USEDLIBS should contain a list of library names (some with .a extension)
# that are automatically linked in as .o files unless the .a suffix is added.
#
ifdef TOOLNAME

# TOOLLINKOPTSB to pass options to the linker like library search path etc
# Note that this is different from TOOLLINKOPTS, these options
# are passed to the linker *before* the USEDLIBS options are passed.
# e.g. usage TOOLLINKOPTSB =  -L/home/xxx/lib
ifdef TOOLLINKOPTSB
Link    += $(TOOLLINKOPTSB) 
endif

# TOOLEXENAME* - These compute the output filenames to generate...
TOOLEXENAME := $(TOOLDIR)/$(TOOLNAME)

# USED_LIBS_OPTIONS - Compute the options line that add -llib1 -llib2, etc.
PROJ_LIBS_OPTIONS := $(patsubst %.a.o, -l%, $(addsuffix .o, $(USEDLIBS)))
PROJ_LIBS_OPTIONS := $(patsubst %.o, $(LIBDIR)/%.o,  $(PROJ_LIBS_OPTIONS))
LLVM_LIBS_OPTIONS := $(patsubst %.a.o, -l%, $(addsuffix .o, $(LLVMLIBS)))
LLVM_LIBS_OPTIONS := $(patsubst %.o, $(LLVMLIBDIR)/%.o, $(LLVM_LIBS_OPTIONS))

PROJ_USED_LIBS    := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(USEDLIBS)))
LLVM_USED_LIBS    := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(LLVMLIBS)))
PROJ_LIBS_PATHS   := $(addprefix $(LIBDIR)/,$(PROJ_USED_LIBS))
LLVM_LIBS_PATHS   := $(addprefix $(LLVMLIBDIR)/,$(LLVM_USED_LIBS))

LINK_OPTS := $(TOOLLINKOPTS) $(PROJ_LIBS_OPTIONS) $(LLVM_LIBS_OPTIONS)

#
# Libtool link options:
#	Ensure that all binaries have their symbols exported so that they can
#	by dlsym'ed.
#

# Handle compression libraries automatically
ifeq ($(HAVE_BZIP2),1)
LIBS += -lbz2
endif
ifeq ($(HAVE_ZLIB),1)
LIBS += -lz
endif

# Tell make that we need to rebuild subdirectories before we can link the tool.
# This affects things like LLI which has library subdirectories.
$(LIBS): $(addsuffix /.makeall, $(PARALLEL_DIRS))

all::   $(TOOLEXENAME)

clean::
	$(VERB) $(RM) -f $(TOOLEXENAME)

$(TOOLEXENAME): $(BUILT_SOURCES) $(ObjectsO) $(PROJ_LIBS_PATHS) $(LLVM_LIBS_PATHS) $(TOOLDIR)/.dir
	@$(ECHO) Linking $(CONFIGURATION) executable $(TOOLNAME) $(STRIP_WARN_MSG)
	$(VERB) $(Link) -o $@ $(ObjectsO) $(PROJ_LIBS_OPTIONS) $(LLVM_LIBS_OPTIONS) $(LIBS)
	@$(ECHO) ======= Finished linking $(CONFIGURATION) executable $(TOOLNAME) $(STRIP_WARN_MSG) 

install:: $(TOOLEXENAME)
	@$(ECHO) Installing $(TOOLNAME)
	$(VERB) $(INSTALL) $(TOOLEXENAME) $(DESTDIR)/bin
endif

ifndef DISABLE_AUTO_DEPENDENCIES

# Create .lo files in the OBJDIR directory from the .cpp and .c files...
$(OBJDIR)/%.lo: %.cpp $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp For Shared Library"
	$(VERB) if $(LTCXXCompile) -MD -MT $@ -MP -MF $(OBJDIR)/$*.Td $< -o $@ ; \
	then mv -f "$(OBJDIR)/$*.Td" "$(OBJDIR)/$*.d"; else rm -f "$(OBJDIR)/$*.Td"; exit 1; fi

$(OBJDIR)/%.o: %.cpp $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp For Archive"
	$(VERB) if $(CXXCompile) -MD -MT $@ -MP -MF $(OBJDIR)/$*.Td $< -o $@ ; \
	then mv -f "$(OBJDIR)/$*.Td" "$(OBJDIR)/$*.d"; else rm -f "$(OBJDIR)/$*.Td"; exit 1; fi

$(OBJDIR)/%.lo: %.c $(OBJDIR)/.dir 
	@$(ECHO) "Compiling $(CONFIGURATION) $*.c For Shared Library"
	$(VERB) if $(LTCCompile) -MD -MT $@ -MP -MF $(OBJDIR)/$*.Td $< -o $@ ; \
	then mv -f "$(OBJDIR)/$*.Td" "$(OBJDIR)/$*.d"; else rm -f "$(OBJDIR)/$*.Td"; exit 1; fi

$(OBJDIR)/%.o: %.c $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.c For Archive"
	$(VERB) if $(CCompile) -MD -MT $@ -MP -MF $(OBJDIR)/$*.Td $< -o $@ ; \
	then mv -f "$(OBJDIR)/$*.Td" "$(OBJDIR)/$*.d"; else rm -f "$(OBJDIR)/$*.Td"; exit 1; fi

# Create .bc files in the OBJDIR directory from .cpp and .c files...
$(OBJDIR)/%.bc: %.cpp $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp to bytecode"
	$(VERB) if $(BCCXXCompile) -MD -MT $@ -MP -MF "$(OBJDIR)/$*.Td" $< -o $@ ; \
	then mv -f "$(OBJDIR)/$*.Td" "$(OBJDIR)/$*.d"; else rm -f "$(OBJDIR)/$*.Td"; exit 1; fi

$(OBJDIR)/%.bc: %.c $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.c to bytecode"
	$(VERB) if $(BCCCompile) -MD -MT $@ -MP -MF "$(OBJDIR)/$*.Td" $< -o $@ ; \
	then mv -f "$(OBJDIR)/$*.Td" "$(OBJDIR)/$*.d"; else rm -f "$(OBJDIR)/$*.Tpo"; exit 1; fi

else

$(OBJDIR)/%.lo: %.cpp $(OBJDIR)/.dir 
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp For Shared Library"
	$(LTCXXCompile) $< -o $@ 

$(OBJDIR)/%.o: %.cpp $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp For Archive"
	$(CXXCompile) $< -o $@ 

$(OBJDIR)/%.lo: %.c $(OBJDIR)/.dir 
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp For Shared Library"
	$(LTCCompile) $< -o $@ 

$(OBJDIR)/%.o: %.c $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp For Archive"
	$(CCompile) $< -o $@ 

# Create .bc files in the OBJDIR directory from .cpp and .c files...
$(OBJDIR)/%.bc: %.cpp $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.cpp To Bytecode"
	$(BCCompileCPP) $< -o $@ 

$(OBJDIR)/%.bc: %.c $(OBJDIR)/.dir
	@$(ECHO) "Compiling $(CONFIGURATION) $*.c To Bytecode"
	$(BCCompileC) $< -o $@

endif

$(OBJDIR)/%.bc: %.ll $(OBJDIR)/.dir $(LLVMAS)
	@$(ECHO) "Compiling $*.ll To Bytecode"
	$(VERB) $(LLVMAS) $< -f -o $@

ifdef TARGET

TDFILES := $(strip $(wildcard $(BUILD_SRC_DIR)/*.td) $(BUILD_SRC_DIR)/../Target.td)

$(BUILT_SOURCES): $(TDFILES) 

%GenRegisterNames.inc : %.td
	@echo "Building $(<F) register names with tblgen"
	$(VERB) $(TableGen) -gen-register-enums -o $@ $<

%GenRegisterInfo.h.inc : %.td
	@echo "Building $(<F) register information header with tblgen"
	$(VERB) $(TableGen) -gen-register-desc-header -o $@ $<

%GenRegisterInfo.inc : %.td
	@echo "Building $(<F) register info implementation with tblgen"
	$(VERB) $(TableGen) -gen-register-desc -o $@ $<

%GenInstrNames.inc : %.td
	@echo "Building $(<F) instruction names with tblgen"
	$(VERB) $(TableGen) -gen-instr-enums -o $@ $<

%GenInstrInfo.inc : %.td
	@echo "Building $(<F) instruction information with tblgen"
	$(VERB) $(TableGen) -gen-instr-desc -o $@ $<

%GenAsmWriter.inc : %.td
	@echo "Building $(<F) assembly writer with tblgen"
	$(VERB) $(TableGen) -gen-asm-writer -o $@ $<

%GenATTAsmWriter.inc : %.td
	@echo "Building $(<F) AT&T assembly writer with tblgen"
	$(VERB) $(TableGen) -gen-asm-writer -o $@ $< 

%GenIntelAsmWriter.inc : %.td
	@echo "Building $(<F) Intel assembly writer with tblgen"
	$(VERB) $(TableGen) -gen-asm-writer -asmwriternum=1 -o $@ $< 

%GenInstrSelector.inc: %.td
	@echo "Building $(<F) instruction selector with tblgen"
	$(VERB) $(TableGen) -gen-instr-selector -o $@ $< 

%GenCodeEmitter.inc:: %.td
	@echo "Building $(<F) code emitter with tblgen"
	$(VERB) $(TableGen) -gen-emitter -o $@ $<

clean::
	$(VERB) rm -f *.inc

endif

#
# Rules for building lex/yacc files
#
LEX_FILES   = $(filter %.l, $(Source))
LEX_OUTPUT  = $(LEX_FILES:%.l=%.cpp)
YACC_FILES  = $(filter %.y, $(Source))
YACC_OUTPUT = $(addprefix $(YACC_FILES:%.y=%), .h .cpp .output)
.PRECIOUS: $(LEX_OUTPUT) $(YACC_OUTPUT)

# Create a .cpp source file from a flex input file... this uses sed to cut down
# on the warnings emited by GCC...
#
# The last line is a gross hack to work around flex aparently not being able to
# resize the buffer on a large token input.  Currently, for uninitialized string
# buffers in LLVM we can generate very long tokens, so this is a hack around it.
# FIXME.  (f.e. char Buffer[10000] )
#
%.cpp: %.l
	@$(ECHO) Flexing $<
	$(VERB) $(FLEX) -t $< | \
	$(SED) '/^find_rule/d' | \
	$(SED) 's/void yyunput/inline void yyunput/' | \
	$(SED) 's/void \*yy_flex_realloc/inline void *yy_flex_realloc/' | \
	$(SED) 's/#define YY_BUF_SIZE 16384/#define YY_BUF_SIZE (16384*64)/' \
	  > $@.tmp
	$(VERB) cmp -s $@ $@.tmp > /dev/null || $(MV) -f $@.tmp $@
	@# remove the output of flex if it didn't get moved over...
	@rm -f $@.tmp

# Rule for building the bison parsers...
%.c: %.y     # Cancel built-in rules for yacc
%.h: %.y     # Cancel built-in rules for yacc
%.cpp %.h : %.y
	@$(ECHO) "Bisoning $*.y"
	$(VERB) $(BISON) -v -d -p $(<F:%Parser.y=%) -o $*.tab.c  $<
	$(VERB) cmp -s $*.tab.c $*.cpp > /dev/null || $(MV) -f $*.tab.c $*.cpp
	$(VERB) cmp -s $*.tab.h $*.h   > /dev/null || $(MV) -f $*.tab.h $*.h
	@# If the files were not updated, don't leave them lying around...
	@rm -f $*.tab.c $*.tab.h

# To create the directories...
%/.dir:
	$(VERB) $(MKDIR) $* > /dev/null
	@$(DATE) > $@

.PRECIOUS: $(OBJDIR)/.dir $(LIBDIR)/.dir $(TOOLDIR)/.dir $(LLVMLIBDIR)/.dir
.PRECIOUS: $(LLVMTOOLDIR)/.dir

# To create postscript files from dot files...
ifneq ($(DOT),false)
%.ps: %.dot
	$(DOT) -Tps < $< > $@
else
%.ps: %.dot
	$(ECHO) "Cannot build $@: The program dot is not installed"
endif

#
# This rules ensures that header files that are removed still have a rule for
# which they can be "generated."  This allows make to ignore them and
# reproduce the dependency lists.
#
%.h:: ;

# 'make clean' nukes the tree
clean::
	$(VERB) $(RM) -rf $(OBJDIR)
	$(VERB) $(RM) -f core core.[0-9][0-9]* *.o *.d *~ *.flc
ifneq ($(strip $(SHLIBEXT)),) # Extra paranoia - make real sure SHLIBEXT is set
	$(VERB)	$(RM) -f *$(SHLIBEXT)
endif
	$(VERB) $(RM) -f $(LEX_OUTPUT) $(YACC_OUTPUT)

###########################################################################
# C/C++ Dependencies
#	Define variables and rules that generate header file dependencies
#	from C/C++ source files.
###########################################################################

ifndef DISABLE_AUTO_DEPENDENCIES

# If dependencies were generated for the file that included this file,
# include the dependencies now...
#

SourceBaseNames := $(basename $(Source))
SourceDepend := $(SourceBaseNames:%=$(BUILD_OBJ_DIR)/$(CONFIGURATION)/%.d)

#
# Include dependencies generated from C/C++ source files, but not if we
# are cleaning (this example taken from the GNU Make Manual).
#
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),distclean)
-include /dev/null $(SourceDepend)
endif
endif

endif  # ifndef DISABLE_AUTO_DEPENDENCIES

###############################################################################
# PRECONDITIONS - that which must be built/checked first
###############################################################################

OBJMKFILE := $(BUILD_OBJ_DIR)/Makefile
SRCMKFILE := $(BUILD_SRC_DIR)/Makefile
CONFIGURE := $(LLVM_SRC_ROOT)/configure
CONFIG_STATUS := $(LLVM_OBJ_ROOT)/config.status
MAKE_CONFIG_IN := $(LLVM_SRC_ROOT)/Makefile.config.in
MAKE_CONFIG := $(LLVM_OBJ_ROOT)/Makefile.config

#------------------------------------------------------------------------
# List of the preconditions
preconditions: $(CONFIG_STATUS) $(MAKE_CONFIG) $(OBJMKFILE) 

all dist distcheck install:: $(BUILT_SOURCES)

clean::
	$(VERB) $(RM) -f $(BUILT_SOURCES)

#------------------------------------------------------------------------
# Make sure we're not using a stale configuration
.PRECIOUS: $(CONFIG_STATUS)
$(CONFIG_STATUS): $(CONFIGURE)
	@$(ECHO) Reconfiguring with $@
	$(VERB) $(CONFIG_STATUS) --recheck

#------------------------------------------------------------------------
# Make sure the configuration makefile is up to date
$(MAKE_CONFIG): $(MAKE_CONFIG_IN)
	@$(ECHO) Regenerating $@
	$(VERB) cd $(LLVM_OBJ_ROOT) ; $(CONFIG_STATUS) Makefile.config
	$(VERB) $(MAKE) $(MAKEFLAGS) $(MAKECMDGOALS)
	@exit 0;

#------------------------------------------------------------------------
# If the Makefile in the source tree has been updated, copy it over into the
# build tree.
.PRECIOUS: $(OBJMKFILE)
$(OBJMKFILE): $(SRCMKFILE)
	@$(ECHO) "Updating Makefile from : $(dir $<)"
	$(VERB) $(MKDIR) $(@D)
	$(VERB) cp -f $< $@
	$(VERB) $(MAKE) $(MAKEFLAGS) $(MAKECMDGOALS)
	@exit 0;

###############################################################################
# MISCELLANEOUS - utility targets
###############################################################################

#------------------------------------------------------------------------
# Print out the directories used for building
printvars:
	@$(ECHO) "BUILD_SRC_ROOT: " $(BUILD_SRC_ROOT)
	@$(ECHO) "BUILD_SRC_DIR : " $(BUILD_SRC_DIR)
	@$(ECHO) "BUILD_OBJ_ROOT: " $(BUILD_OBJ_ROOT)
	@$(ECHO) "BUILD_OBJ_DIR : " $(BUILD_OBJ_DIR)
	@$(ECHO) "LLVM_SRC_ROOT : " $(LLVM_SRC_ROOT)
	@$(ECHO) "LLVM_OBJ_ROOT : " $(LLVM_OBJ_ROOT)
	@$(ECHO) "CONFIGURATION : " $(CONFIGURATION)
	@$(ECHO) "OBJDIR: " $(OBJDIR)
	@$(ECHO) "LIBDIR: " $(LIBDIR)
	@$(ECHO) "TOOLDIR: " $(TOOLDIR)
	@$(ECHO) "TDFILES:" '$(TDFILES)'
	@$(ECHO) "CXXCompile: " '$(CXXCompile)'
	@$(ECHO) "CCompile: " '$(CCompile)'