* Minor fixes to support C files.
[oota-llvm.git] / Makefile.rules
1 #===-- Makefile.common - Common make rules for LLVM -------*- makefile -*--====
2 #
3 # This file is included by all of the LLVM makefiles.  This file defines common
4 # rules to do things like compile a .cpp file or generate dependancy info.
5 # These are platform dependant, so this is the file used to specify these
6 # system dependant operations.
7 #
8 # The following functionality can be set by setting incoming variables.
9 # The variable $(LEVEL) *must* be set:
10 #
11 # 1. LEVEL - The level of the current subdirectory from the top of the 
12 #    MagicStats view.  This level should be expressed as a path, for 
13 #    example, ../.. for two levels deep.
14 #
15 # 2. DIRS - A list of subdirectories to be built.  Fake targets are set up
16 #    so that each of the targets "all", "install", and "clean" each build
17 #    the subdirectories before the local target.  DIRS are guaranteed to be
18 #    built in order.
19 #
20 # 3. PARALLEL_DIRS - A list of subdirectories to be built, but that may be
21 #    built in any order.  All DIRS are built in order before PARALLEL_DIRS are
22 #    built, which are then built in any order.
23 #
24 # 4. Source - If specified, this sets the source code filenames.  If this
25 #    is not set, it defaults to be all of the .cpp, .c, .y, and .l files 
26 #    in the current directory.  Also, if you want to build files in addition
27 #    to the local files, you can use the ExtraSource variable
28 #
29 #===-----------------------------------------------------------------------====
30
31 # Configuration file to set paths specific to local installation of LLVM
32
33 include $(LEVEL)/Makefile.config
34
35 # These are options that can either be enabled here, or can be enabled on the
36 # make command line (ie, make ENABLE_PROFILING=1)
37 #
38
39 # When ENABLE_PROFILING is enabled, the llvm source base is built with profile
40 # information to allow gprof to be used to get execution frequencies.
41 #
42 #ENABLE_PROFILING = 1
43
44 # When ENABLE_PURIFY is enabled, the LLVM tools are linked with purify (which
45 # must be locally installed) to allow for some automated memory error debugging.
46 #
47 #ENABLE_PURIFY    = 1
48
49 # When ENABLE_OPTIMIZED is enabled, Release builds of all of the LLVM code are
50 # turned on, and Debug builds are turned off.
51 #
52 #ENABLE_OPTIMIZED = 1
53
54
55 # Figure out how to do platform specific stuff on this platform.  This is really
56 # gross and should be autoconfiscated (automake actually), but should hopefully
57 # work on Linux and solaris (SunOS).
58 #
59 UNAME := $(shell uname)
60 include $(LEVEL)/Makefile.$(UNAME)
61
62 ifdef SHARED_LIBRARY
63 # if SHARED_LIBRARY is specified, the default is to build the dynamic lib
64 dynamic ::
65 endif
66
67 # Default Rule:  Make sure it's also a :: rule
68 all ::
69
70 # Default for install is to at least build everything...
71 install ::
72
73
74 # Figure out which directory to build stuff into.  We want to build into the
75 # /shared directory by default because it is guaranteed to be local to the
76 # current machine.
77 #
78 ifeq ($(LLVM_OBJ_DIR),.)
79 BUILD_ROOT     = $(LLVM_OBJ_DIR)
80 BUILD_ROOT_TOP = $(LEVEL)
81 else
82
83 BUILD_ROOT := $(LLVM_OBJ_DIR)$(patsubst $(HOME)%,%,$(shell pwd))
84
85 # Calculate the BUILD_ROOT_TOP variable, which is the top of the llvm/ tree.
86 # Note that although this is just equal to $(BUILD_ROOT)/$(LEVEL), we cannot use
87 # this expression because some of the directories on the source tree may not
88 # exist in the build tree (for example the test/ heirarchy).  Thus we evaluate
89 # the directory to eliminate the ../'s
90 #
91 TOP_DIRECTORY := $(shell cd $(LEVEL); pwd)
92 BUILD_ROOT_TOP := $(LLVM_OBJ_DIR)$(patsubst $(HOME)%,%,$(TOP_DIRECTORY))
93 endif
94
95 #--------------------------------------------------------------------
96 # Variables derived from configuration options... 
97 #--------------------------------------------------------------------
98
99 #BinInstDir=/usr/local/bin
100 #LibInstDir=/usrl/local/lib/xxx
101 #DocInstDir=/usr/doc/xxx
102
103 BURG_OPTS = -I
104
105 PURIFY := $(PURIFY) -cache-dir="$(BUILD_ROOT_TOP)/../purifycache" -chain-length="30" -messages=all 
106
107 # Shorthand for commonly accessed directories
108 LIBDEBUG    := $(BUILD_ROOT_TOP)/lib/Debug
109 LIBRELEASE  := $(BUILD_ROOT_TOP)/lib/Release
110 LIBPROFILE  := $(BUILD_ROOT_TOP)/lib/Profile
111 TOOLDEBUG   := $(BUILD_ROOT_TOP)/tools/Debug
112 TOOLRELEASE := $(BUILD_ROOT_TOP)/tools/Release
113 TOOLPROFILE := $(BUILD_ROOT_TOP)/tools/Profile
114
115 # Verbosity levels
116 ifdef VERBOSE
117 VERB := 
118 else
119 VERB := @
120 endif
121
122 #---------------------------------------------------------
123 # Compilation options...
124 #---------------------------------------------------------
125
126 # Special tools used while building the LLVM tree.  Burg is built as part of the
127 # utils directory.
128 #
129 BURG    := $(TOOLDEBUG)/burg
130 RunBurg := $(BURG) $(BURG_OPTS)
131
132
133 # Enable this for profiling support with 'gprof'
134 # This automatically enables optimized builds.
135 ifdef ENABLE_PROFILING
136   PROFILE = -pg
137   ENABLE_OPTIMIZED = 1
138 else
139   PROFILE =
140 endif
141
142 # By default, strip symbol information from executable
143 ifdef KEEP_SYMBOLS
144 STRIP =
145 else
146 STRIP = -s
147 endif
148
149 # Allow gnu extensions...
150 CPPFLAGS += -D_GNU_SOURCE
151
152 # -Wno-unused-parameter
153 CompileCommonOpts := -Wall -W  -Wwrite-strings -Wno-unused -I$(LEVEL)/include
154
155 # Compile a cpp file, don't link...
156 Compile  := $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(CompileCommonOpts)
157 CompileG := $(Compile) -g  -D_DEBUG
158 CompileO := $(Compile) -O3 -DNDEBUG -finline-functions -felide-constructors -fshort-enums ## DISABLE -freg-struct-return because of gcc3.2 bug
159 CompileP := $(CompileO) $(PROFILE)
160
161 # Compile a c file, don't link...
162 CompileC  := $(CC) -c $(CPPFLAGS) $(CCFLAGS) $(CompileCommonOpts)
163 CompileCG := $(CompileC) -g  -D_DEBUG
164 CompileCO := $(CompileC) -O3 -DNDEBUG -finline-functions -felide-constructors -fshort-enums ## DISABLE -freg-struct-return because of gcc3.2 bug
165 CompileCP := $(CompileCO) $(PROFILE)
166
167
168 # Link final executable
169
170 ifdef ENABLE_PURIFY # To enable purify, build with 'gmake ENABLE_PURIFY=1'
171 Link     := $(PURIFY) $(CXX) -static
172 else
173 Link     := $(CXX)
174 endif
175 LinkG    := $(Link) -g  -L $(LIBDEBUG) $(STRIP)
176 LinkO    := $(Link) -O3 -L $(LIBRELEASE)
177 LinkP    := $(Link) -O3 -L $(LIBPROFILE) $(PROFILE)
178
179 # Create one .o file from a bunch of .o files...
180 Relink = ld -r
181
182 # MakeSO - Create a .so file from a .o files...
183 MakeSO   := $(CXX) $(MakeSharedObjectOption)
184 MakeSOO  := $(MakeSO) -O3
185 MakeSOP  := $(MakeSOO) $(PROFILE)
186
187 # Create dependancy file from CPP file, send to stdout.
188 Depend   := $(CXX) -MM -I$(LEVEL)/include $(CPPFLAGS) 
189 DependC  := $(CC)  -MM -I$(LEVEL)/include $(CPPFLAGS) 
190
191 # Archive a bunch of .o files into a .a file...
192 AR       = ar cq 
193 BISON    = bison
194
195 #----------------------------------------------------------
196
197 # Source includes all of the cpp files, and objects are derived from the
198 # source files...
199 # The local Makefile can list other Source files via ExtraSource = ...
200
201 Source  := $(ExtraSource) $(wildcard *.cpp *.c *.y *.l)
202
203 Objs := $(sort $(patsubst Debug/%.o, %.o, $(addsuffix .o,$(basename $(Source)))))
204 ObjectsO := $(addprefix $(BUILD_ROOT)/Release/,$(Objs))
205 ObjectsP := $(addprefix $(BUILD_ROOT)/Profile/,$(Objs))
206 ObjectsG := $(addprefix $(BUILD_ROOT)/Debug/,$(Objs))
207
208
209 #---------------------------------------------------------
210 # Handle the DIRS and PARALLEL_DIRS options
211 #---------------------------------------------------------
212
213 ifdef DIRS
214 all install clean ::
215         $(VERB) for dir in ${DIRS}; do \
216                 (cd $$dir; $(MAKE) $@) || exit 1; \
217         done
218 endif
219
220 # Handle PARALLEL_DIRS
221 ifdef PARALLEL_DIRS
222 all     :: $(addsuffix /.makeall    , $(PARALLEL_DIRS))
223 install :: $(addsuffix /.makeinstall, $(PARALLEL_DIRS))
224 clean   :: $(addsuffix /.makeclean  , $(PARALLEL_DIRS))
225
226 %/.makeall %/.makeinstall %/.makeclean:
227         $(VERB) cd $(@D); $(MAKE) $(subst $(@D)/.make,,$@)
228
229 endif
230
231 #---------------------------------------------------------
232 # Handle the LIBRARYNAME option - used when building libs...
233 #---------------------------------------------------------
234 #
235 #  When libraries are built, they are allowed to optionally define the
236 #  DONT_BUILD_RELINKED make variable, which, when defined, prevents a .o file
237 #  from being built for the library. This .o files may then be linked to by a
238 #  tool if the tool does not need (or want) the semantics a .a file provides
239 #  (linking in only object files that are "needed").  If a library is never to
240 #  be used in this way, it is better to define DONT_BUILD_RELINKED, and define
241 #  BUILD_ARCHIVE instead.
242 #
243 #  Some libraries must be built as .a files (libscalar for example) because if
244 #  it's built as a .o file, then all of the constituent .o files in it will be
245 #  linked into tools (for example gccas) even if they only use one of the parts
246 #  of it.  For this reason, sometimes it's useful to use libraries as .a files.
247
248 ifdef LIBRARYNAME
249
250 # Make sure there isn't any extranous whitespace on the LIBRARYNAME option
251 LIBRARYNAME := $(strip $(LIBRARYNAME))
252
253 LIBNAME_O    := $(LIBRELEASE)/lib$(LIBRARYNAME).so
254 LIBNAME_P    := $(LIBPROFILE)/lib$(LIBRARYNAME).so
255 LIBNAME_G    := $(LIBDEBUG)/lib$(LIBRARYNAME).so
256 LIBNAME_AO   := $(LIBRELEASE)/lib$(LIBRARYNAME).a
257 LIBNAME_AP   := $(LIBPROFILE)/lib$(LIBRARYNAME).a
258 LIBNAME_AG   := $(LIBDEBUG)/lib$(LIBRARYNAME).a
259 LIBNAME_OBJO := $(LIBRELEASE)/$(LIBRARYNAME).o
260 LIBNAME_OBJP := $(LIBPROFILE)/$(LIBRARYNAME).o
261 LIBNAME_OBJG := $(LIBDEBUG)/$(LIBRARYNAME).o
262
263
264 ifndef ENABLE_OPTIMIZED
265 BUILD_LIBNAME_G := $(LIBNAME_G)
266 ifndef DONT_BUILD_RELINKED
267 BUILD_LIBNAME_OBJG := $(LIBNAME_OBJG)
268 endif
269 ifdef BUILD_ARCHIVE
270 BUILD_LIBNAME_AG := $(LIBNAME_AG)
271 endif
272 endif
273
274 # If optimized builds are enabled...
275 ifdef ENABLE_OPTIMIZED
276   ifdef ENABLE_PROFILING
277     BUILD_LIBNAME_O  := $(LIBNAME_P)
278     ifndef DONT_BUILD_RELINKED
279       BUILD_LIBNAME_OBJO := $(LIBNAME_OBJP)
280     endif
281     ifdef BUILD_ARCHIVE
282       BUILD_LIBNAME_AO := $(LIBNAME_AP)
283     endif
284   else
285     BUILD_LIBNAME_O  := $(LIBNAME_O)
286     ifndef DONT_BUILD_RELINKED
287       BUILD_LIBNAME_OBJO := $(LIBNAME_OBJO)
288     endif
289     ifdef BUILD_ARCHIVE
290       BUILD_LIBNAME_AO := $(LIBNAME_AO)
291     endif
292   endif
293 endif
294
295 all:: $(BUILD_LIBNAME_AG) $(BUILD_LIBNAME_OBJG)      # Debug
296 all:: $(BUILD_LIBNAME_AO) $(BUILD_LIBNAME_OBJO)      # Release
297 all:: $(BUILD_LIBNAME_AP) $(BUILD_LIBNAME_OBJP)      # Profile
298 dynamic:: $(BUILD_LIBNAME_G) $(BUILD_LIBNAME_O) $(BUILD_LIBNAME_P) # .so files
299
300 $(LIBNAME_O): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
301         @echo ======= Linking $(LIBRARYNAME) release library =======
302         $(VERB) $(MakeSOO) -o $@ $(ObjectsO) $(LibSubDirs) $(LibLinkOpts)
303
304 $(LIBNAME_P): $(ObjectsP) $(LibSubDirs) $(LIBPROFILE)/.dir
305         @echo ======= Linking $(LIBRARYNAME) profile library =======
306         $(VERB) $(MakeSOP) -o $@ $(ObjectsP) $(LibSubDirs) $(LibLinkOpts)
307
308 $(LIBNAME_G): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
309         @echo ======= Linking $(LIBRARYNAME) debug library =======
310         $(VERB) $(MakeSO) -g -o $@ $(ObjectsG) $(LibSubDirs) $(LibLinkOpts)
311
312 $(LIBNAME_AO): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
313         @echo ======= Linking $(LIBRARYNAME) release library =======
314         @rm -f $@
315         $(VERB) $(AR) $@ $(ObjectsO) $(LibSubDirs)
316
317 $(LIBNAME_AP): $(ObjectsP) $(LibSubDirs) $(LIBPROFILE)/.dir
318         @echo ======= Linking $(LIBRARYNAME) profile library =======
319         @rm -f $@
320         $(VERB) $(AR) $@ $(ObjectsP) $(LibSubDirs)
321
322 $(LIBNAME_AG): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
323         @echo ======= Linking $(LIBRARYNAME) debug library =======
324         @rm -f $@
325         $(VERB) $(AR) $@ $(ObjectsG) $(LibSubDirs)
326
327 $(LIBNAME_OBJO): $(ObjectsO) $(LibSubDirs) $(LIBRELEASE)/.dir
328         @echo "Linking $@"
329         $(VERB) $(Relink) -o $@ $(ObjectsO) $(LibSubDirs)
330
331 $(LIBNAME_OBJP): $(ObjectsP) $(LibSubDirs) $(LIBPROFILE)/.dir
332         @echo "Linking $@"
333         $(VERB) $(Relink) -o $@ $(ObjectsP) $(LibSubDirs)
334
335 $(LIBNAME_OBJG): $(ObjectsG) $(LibSubDirs) $(LIBDEBUG)/.dir
336         $(VERB) $(Relink) -o $@ $(ObjectsG) $(LibSubDirs)
337
338 endif
339
340 #------------------------------------------------------------------------
341 # Create a TAGS database for emacs
342 #------------------------------------------------------------------------
343
344 ifeq ($(LEVEL), .)
345 tags:
346         etags -l c++ `find include lib tools -name '*.cpp' -o -name '*.h'`
347 all:: tags
348 endif
349
350 #------------------------------------------------------------------------
351 # Handle the TOOLNAME option - used when building tool executables...
352 #------------------------------------------------------------------------
353 #
354 # The TOOLNAME option should be used with a USEDLIBS variable that tells the
355 # libraries (and the order of the libs) that should be linked to the
356 # tool. USEDLIBS should contain a list of library names (some with .a extension)
357 # that are automatically linked in as .o files unless the .a suffix is added.
358 #
359 ifdef TOOLNAME
360
361 # TOOLEXENAME* - These compute the output filenames to generate...
362 TOOLEXENAME_G := $(BUILD_ROOT_TOP)/tools/Debug/$(TOOLNAME)
363 TOOLEXENAME_O := $(BUILD_ROOT_TOP)/tools/Release/$(TOOLNAME)
364 TOOLEXENAME_P := $(BUILD_ROOT_TOP)/tools/Profile/$(TOOLNAME)
365
366 ifndef ENABLE_OPTIMIZED
367   TOOLEXENAMES  := $(TOOLEXENAME_G)
368 else
369   ifdef ENABLE_PROFILING
370     TOOLEXENAMES := $(TOOLEXENAME_P)
371   else
372     TOOLEXENAMES := $(TOOLEXENAME_O)
373   endif
374 endif
375
376 # USED_LIBS_OPTIONS - Compute the options line that add -llib1 -llib2, etc.
377 USED_LIBS_OPTIONS   := $(patsubst %.a.o, -l%, $(addsuffix .o, $(USEDLIBS)))
378 USED_LIBS_OPTIONS_G := $(patsubst %.o, $(LIBDEBUG)/%.o,  $(USED_LIBS_OPTIONS))
379 USED_LIBS_OPTIONS_O := $(patsubst %.o, $(LIBRELEASE)/%.o,$(USED_LIBS_OPTIONS))
380 USED_LIBS_OPTIONS_P := $(patsubst %.o, $(LIBPROFILE)/%.o,$(USED_LIBS_OPTIONS))
381
382
383 # USED_LIB_PATHS - Compute the path of the libraries used so that tools are
384 # rebuilt if libraries change.  This has to make sure to handle .a/.so and .o
385 # files seperately.
386 #
387 STATICUSEDLIBS   := $(patsubst %.a.o, lib%.a, $(addsuffix .o, $(USEDLIBS)))
388 USED_LIB_PATHS_G := $(addprefix $(LIBDEBUG)/, $(STATICUSEDLIBS))
389 USED_LIB_PATHS_O := $(addprefix $(LIBRELEASE)/, $(STATICUSEDLIBS))
390 USED_LIB_PATHS_P := $(addprefix $(LIBPROFILE)/, $(STATICUSEDLIBS))
391
392 all::   $(TOOLEXENAMES)
393 clean::
394         $(VERB) rm -f $(TOOLEXENAMES)
395
396 $(TOOLEXENAME_G): $(ObjectsG) $(USED_LIB_PATHS_G) $(BUILD_ROOT_TOP)/tools/Debug/.dir
397         @echo ======= Linking $(TOOLNAME) debug executable =======
398         $(VERB) $(LinkG) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS_G) $(TOOLLINKOPTS)
399
400 $(TOOLEXENAME_O): $(ObjectsO) $(USED_LIB_PATHS_O) $(BUILD_ROOT_TOP)/tools/Release/.dir
401         @echo ======= Linking $(TOOLNAME) release executable =======
402         $(VERB) $(LinkO) -o $@ $(ObjectsG) $(USED_LIBS_OPTIONS_O) $(TOOLLINKOPTS)
403
404 $(TOOLEXENAME_P): $(ObjectsP) $(USED_LIB_PATHS_P) $(BUILD_ROOT_TOP)/tools/Profile/.dir
405         @echo ======= Linking $(TOOLNAME) profile executable =======
406         $(VERB) $(LinkP) -o $@ $(ObjectsP) $(USED_LIBS_OPTIONS_P) $(TOOLLINKOPTS)
407
408 endif
409
410
411
412 #---------------------------------------------------------
413 .PRECIOUS: $(BUILD_ROOT)/Depend/.dir
414 .PRECIOUS: $(BUILD_ROOT)/Debug/.dir $(BUILD_ROOT)/Release/.dir
415
416 # Create dependencies for the *.cpp files...
417 $(BUILD_ROOT)/Depend/%.d: %.cpp $(BUILD_ROOT)/Depend/.dir
418         $(VERB) $(Depend) $< | sed 's|$*\.o *|$(BUILD_ROOT)/Release/& $(BUILD_ROOT)/Profile/& $(BUILD_ROOT)/Debug/& $(BUILD_ROOT)/Depend/$(@F)|g' > $@
419
420 # Create dependencies for the *.c files...
421 $(BUILD_ROOT)/Depend/%.d: %.c $(BUILD_ROOT)/Depend/.dir
422         $(VERB) $(DependC) $< | sed 's|$*\.o *|Release/& Profile/& Debug/& Depend/$(@F)|g' > $@
423
424 # Create .o files in the ObjectFiles directory from the .cpp and .c files...
425 $(BUILD_ROOT)/Release/%.o: %.cpp $(BUILD_ROOT)/Release/.dir
426         @echo "Compiling $<"
427         $(VERB) $(CompileO) $< -o $@
428
429 $(BUILD_ROOT)/Release/%.o: %.c $(BUILD_ROOT)/Release/.dir
430         $(VERB) $(CompileOC) $< -o $@
431
432 $(BUILD_ROOT)/Profile/%.o: %.cpp $(BUILD_ROOT)/Profile/.dir
433         @echo "Compiling $<"
434         $(VERB) $(CompileP) $< -o $@
435
436 $(BUILD_ROOT)/Profile/%.o: %.c $(BUILD_ROOT)/Profile/.dir
437         @echo "Compiling $<"
438         $(VERB) $(CompileCP) $< -o $@
439
440 $(BUILD_ROOT)/Debug/%.o: %.cpp $(BUILD_ROOT)/Debug/.dir
441         @echo "Compiling $<"
442         $(VERB) $(CompileG) $< -o $@
443
444 $(BUILD_ROOT)/Debug/%.o: %.c $(BUILD_ROOT)/Debug/.dir 
445         $(VERB) $(CompileCG) $< -o $@
446
447 # Create a .cpp source file from a flex input file... this uses sed to cut down
448 # on the warnings emited by GCC...
449 %.cpp: %.l
450         flex -t $< | sed '/^find_rule/d' | sed 's/void yyunput/inline void yyunput/' | sed 's/void \*yy_flex_realloc/inline void *yy_flex_realloc/' > $@
451
452 # Rule for building the bison parsers...
453
454 %.cpp %.h : %.y
455         $(VERB) $(BISON) -v -d -p $(<:%Parser.y=%) $(basename $@).y
456         $(VERB) mv -f $(basename $@).tab.c $(basename $@).cpp
457         $(VERB) mv -f $(basename $@).tab.h $(basename $@).h
458
459 # To create the directories...
460 %/.dir:
461         $(VERB) mkdir -p $(@D)
462         @date > $@
463
464 # 'make clean' nukes the tree
465 clean::
466         $(VERB) rm -rf $(BUILD_ROOT)/Debug $(BUILD_ROOT)/Release $(BUILD_ROOT)/Profile $(BUILD_ROOT)/Depend
467         $(VERB) rm -f core *.o *.d *.so *~ *.flc
468
469 # If dependancies were generated for the file that included this file,
470 # include the dependancies now...
471 #
472 SourceDepend := $(addsuffix .d,$(addprefix $(BUILD_ROOT)/Depend/,$(basename $(filter-out Debug/%, $(Source)))))
473 ifneq ($(SourceDepend),)
474 include $(SourceDepend)
475 endif