* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / ada / Makefile.generic
blobe18511f89f644e27d97bfa9f9db215e691d6b1ca
1 # Generic Makefile to support compilation for multiple languages.
2 # See also Makefile.prolog
4 #   Copyright (C) 2001-2004 Free Software Foundation, Inc.
6 # This file is part of GCC.
8 # GCC is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
13 # GCC is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with GCC; see the file COPYING.  If not, write to
20 # the Free Software Foundation, 59 Temple Place - Suite 330,
21 # Boston, MA 02111-1307, USA.
23 # This Makefile provides a very generic framework of the following
24 # functionalities:
26 # Multi-language support (currently any combination of Ada/C/C++ supported)
27 # Automatic handling of source dependencies
28 # Handling of various C/C++ compilers
29 # Handling of Ada sources using the GNAT toolchain
30 # Complete build process (compile/bind/link)
31 # Individual compilation (on a file, or on a language)
32 # Handling of an object directory
34 # Here are the rules that can be used from the command line:
36 # build:         complete compile/bind/link process
37 # compile:       compile all files that are not up-to-date
38 # link:          bind/link
39 # ada:           compile all Ada files that are not up-to-date
40 # c:             ditto for C files
41 # c++:           ditto for C++ files
42 # <ada file>:    compile the specified file if needed.
43 # <object file>: compile the corresponding C/C++ source file if needed.
44 # clean:         remove all temporary files
46 # This Makefile expects the following variables to be set by the caller
47 # (typically another Makefile):
49 # ADA_SPEC         extension of Ada spec files (optional, default to .ads)
50 # ADA_BODY         extension of Ada body files (optional, default to .adb)
51 # C_EXT            extension of C files (optional, default to .c)
52 # CXX_EXT          extension of C++ files (optional, default to .cc)
53 # OBJ_EXT          extension of object files (optional, default to .o)
54 # SRC_DIRS         blank separated list of source directories
55 # C_SRCS           explicit list of C sources (optional)
56 # C_SRCS_DEFINED   if set, indicates that C_SRCS is already set
57 # CXX_SRCS         explicit list of C++ sources (optional)
58 # CXX_SRCS_DEFINED is set, indicates that CXX_SRCS is already set
59 # OBJ_DIR          a single directory where object files should be put
60 # EXEC_DIR         a single directory where executables should be put (optional)
61 # LANGUAGES        a blank separated list of languages supported, e.g "ada c"
62 #                  the current list of recognized languages is: ada, c, c++
63 # CC               name of the C compiler (optional, default to gcc)
64 # CXX              name of the C++ compiler (optional, default to gcc)
65 # AR_CMD           command to create an archive (optional, default to "ar rc")
66 # AR_EXT           file extension of an archive (optional, default to ".a")
67 # RANLIB           command to generate an index (optional, default to "ranlib")
68 # GNATMAKE         name of the GNAT builder (optional, default to "gnatmake")
69 # ADAFLAGS         additional Ada compilation switches, e.g "-gnatf" (optional)
70 # CFLAGS           default C compilation switches, e.g "-O2 -g" (optional)
71 # CXXFLAGS         default C++ compilation switches (optional)
72 # LIBS             libraries to link with (optional)
73 # LDFLAGS          linker switches (optional)
74 # ADA_SOURCES      list of main Ada sources (optional)
75 # EXEC             name of the final executable (optional)
76 # MAIN             language of the main program (optional)
77 # MAIN_OBJECT      main object file (optional)
78 # PROJECT_FILE     name of the project file, without the .gpr extension
79 # DEPS_PROJECTS    list of project dependencies (optional)
81 # SILENT           (optional) when defined, make -s will not output anything
82 #                  when all commands are successful.
84 # Set the source search path for C and C++ if needed
86 ifndef MAIN
87    MAIN=ada
88 endif
90 ifndef ADA_SPEC
91    ADA_SPEC=.ads
92 endif
94 ifndef ADA_BODY
95    ADA_BODY=.adb
96 endif
98 ifndef CC
99    CC=gcc
100 endif
102 ifndef CXX
103    CXX=gcc
104 endif
106 ifndef CXX_EXT
107    CXX_EXT=.cc
108 endif
110 vpath %$(C_EXT) $(SRC_DIRS)
111 vpath %$(CXX_EXT) $(SRC_DIRS)
113 ifndef OBJ_EXT
114    OBJ_EXT=.o
115 endif
117 ifndef AR_EXT
118    AR_EXT=.a
119 endif
121 ifndef AR_CMD
122    AR_CMD=ar rc
123 endif
125 ifndef RANLIB
126    RANLIB=ranlib
127 endif
129 ifndef GNATMAKE
130    GNATMAKE:=gnatmake
131 endif
133 ifndef ARCHIVE
134    ARCHIVE=$(OBJ_DIR)/lib$(PROJECT_BASE)-full$(AR_EXT)
135 endif
137 ifeq ($(EXEC_DIR),)
138    EXEC_DIR=$(OBJ_DIR)
139 endif
141 # Define display to echo only when SILENT is not defined
143 ifdef SILENT
144 define display
145    @gprcmd ignore
146 endef
148 else
149 define display
150    @echo
151 endef
152 endif
154 # Make sure gnatmake is called silently when SILENT is set
155 ifdef SILENT
156    GNATMAKE:=$(GNATMAKE) -q
157 endif
159 # If C/C++ compiler is gcc, make sure gcc is called with the switch indicating
160 # the language, in case the extension is not standard.
162 ifeq ($(strip $(filter-out %gcc,$(CC))),)
163    C_Compiler=$(CC) -x c
164 else
165    C_Compiler=$(CC)
166 endif
168 ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
169    CXX_Compiler=$(CXX) -x c++
170 else
171    CXX_Compiler=$(CXX)
172 endif
174 # Set the object search path
176 vpath %$(OBJ_EXT) $(OBJ_DIR)
177 vpath %$(AR_EXT) $(OBJ_DIR)
179 # A target can't have a character ':' otherwise it will confuse make. We
180 # replace ':' by a pipe character. Note that there is less chance than a pipe
181 # character be part of a pathname on UNIX and this character can't be used in
182 # a pathname on Windows.
184 clean_deps = $(subst :,__GPRCOLON__,$(DEPS_PROJECTS:%=clean_%))
185 compile_deps = $(subst :,__GPRCOLON__,$(DEPS_PROJECTS:%=compile_%))
186 object_deps = $(subst :,__GPRCOLON__,$(DEPS_PROJECTS:%=object_%))
187 ada_deps = $(subst :,__GPRCOLON__,$(DEPS_PROJECTS:%=ada_%))
188 c_deps = $(subst :,__GPRCOLON__,$(DEPS_PROJECTS:%=c_%))
189 c++_deps = $(subst :,__GPRCOLON__,$(DEPS_PROJECTS:%=c++_%))
191 # Default target is to build (compile/bind/link)
192 all: build
194 clean: $(clean_deps) internal-clean
195 build: $(compile_deps) internal-compile internal-build
196 compile: $(compile_deps) internal-compile $(ADA_SOURCES)
197 ada: $(ada_deps) internal-ada
198 archive-objects: $(object_deps) internal-archive-objects
199 c: $(c_deps) internal-c
200 c++: $(c++deps) internal-c++
202 $(clean_deps): force
203         @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:clean_%=%))) -f Makefile.$(notdir $@) internal-clean
205 $(compile_deps): force
206         @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:compile_%=%))) -f Makefile.$(notdir $@) internal-compile
208 $(object_deps): force
209         @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:object_%=%))) -f Makefile.$(notdir $@) internal-archive-objects ARCHIVE=$(ARCHIVE)
211 $(ada_deps): force
212         @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:ada_%=%))) -f Makefile.$(notdir $@) internal-ada
214 $(c_deps): force
215         @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:c_%=%))) -f Makefile.$(notdir $@) internal-c
217 $(c++_deps): force
218         @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:c++_%=%))) -f Makefile.$(notdir $@) internal-c++
220 ifneq ($(EXEC),)
221    EXEC_RULE=-o $(EXEC)
222 endif
224 PROJECT_BASE = $(notdir $(PROJECT_FILE))
226 # Set C/C++ linker command & target
228 ifeq ($(filter c++,$(LANGUAGES)),c++)
229    LINKER = $(CXX)
231    ifeq ($(filter ada,$(LANGUAGES)),ada)
232       # C++ and Ada mixed
233       LARGS = --LINK=$(LINKER)
235       ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
236          # Case of GNAT and a GNU C++ compiler
237 $(LINKER):
239       else
240          # Case of GNAT and a non GNU C++ compiler
241          LINKER = $(OBJ_DIR)/c++linker
243 $(LINKER): Makefile.$(PROJECT_BASE)
244         @echo \#!/bin/sh > $(LINKER)
245         @echo $(CXX) $$\* $(shell gcc -print-libgcc-file-name) >> $(LINKER)
246         @chmod +x $(LINKER)
247       endif
248    endif
249 else
250    ifeq ($(strip $(LANGUAGES)),c)
251       # Case of C only
252       LINKER = $(CC)
253    endif
254 endif
256 C_INCLUDES := $(foreach name,$(SRC_DIRS),-I$(name))
257 ALL_CFLAGS = $(CFLAGS) $(DEP_CFLAGS)
258 ALL_CXXFLAGS = $(CXXFLAGS) $(DEP_CFLAGS)
259 LDFLAGS := $(LIBS) $(LDFLAGS)
261 # Compute list of objects based on languages
263 ifeq ($(strip $(filter c,$(LANGUAGES))),c)
264    # Compute list of C sources automatically unless already specified
266    ifndef C_SRCS_DEFINED
267       ifndef C_SRCS
268          C_SRCS := \
269            $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(C_EXT))))
270       endif
271    endif
273    C_OBJECTS := $(C_SRCS:$(C_EXT)=$(OBJ_EXT))
274    OBJECTS += $(C_OBJECTS)
275 endif
277 ifeq ($(strip $(filter c++,$(LANGUAGES))),c++)
278    # Compute list of C++ sources automatically unless already specified
280    ifndef CXX_SRCS_DEFINED
281       ifndef CXX_SRCS
282          CXX_SRCS := \
283          $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(CXX_EXT))))
284       endif
285    endif
287    CXX_OBJECTS := $(CXX_SRCS:$(CXX_EXT)=$(OBJ_EXT))
288    OBJECTS += $(CXX_OBJECTS)
289 endif
291 OBJ_FILES := $(foreach name,$(OBJECTS),$(OBJ_DIR)/$(name))
293 # To handle C/C++ dependencies, we associate a small file for each
294 # source that will list the dependencies as a make rule, so that we can then
295 # include these rules in this makefile, and recompute them on a file by file
296 # basis
298 DEP_FILES := $(OBJ_FILES:$(OBJ_EXT)=.d)
300 # Ada compilations are taken care of automatically, so do not mess with Ada
301 # objects, only with main sources.
303 ifeq ($(strip $(OBJECTS)),)
304 internal-compile:
305 internal-archive-objects:
307 else
308 internal-compile: lib$(PROJECT_BASE)$(AR_EXT)
310 lib$(PROJECT_BASE)$(AR_EXT): $(OBJECTS)
311         @$(display) creating archive file for $(PROJECT_BASE)
312         cd $(OBJ_DIR); $(AR_CMD) $@ $(strip $(OBJECTS))
313         -$(RANLIB) $(OBJ_DIR)/$@
315 internal-archive-objects: $(OBJECTS)
316 #       @echo $(AR_CMD) $(ARCHIVE) $(strip $(OBJECTS))
317 #       cd $(OBJ_DIR); $(AR_CMD) $(ARCHIVE) $(strip $(OBJECTS))
318 #       -$(RANLIB) $(OBJ_DIR)/$@
320 endif
322 # Linking rules
324 # There are three cases:
326 # - C/C++ sources
328 # - Ada/C/C++, main program is in Ada
330 # - Ada/C/C++, main program is in C/C++
332 ifeq ($(strip $(filter-out c c++,$(LANGUAGES))),)
333 # link with C/C++
334 ifeq ($(MAIN_OBJECT),)
335 link:
336         @echo link: no main object specified, exiting...
337         exit 1
338 else
339 ifeq ($(EXEC),)
341 link:
342         @echo link: no executable specified, exiting...
343         exit 1
344 else
346 link: $(EXEC_DIR)/$(EXEC) archive-objects
347 $(EXEC_DIR)/$(EXEC): $(OBJECTS)
348         @$(display) $(LINKER) -o $(EXEC_DIR)/$(EXEC) $(OBJ_DIR)/$(MAIN_OBJECT) $(LDFLAGS) $(FLDFLAGS)
349         @$(LINKER) -o $(EXEC_DIR)/$(EXEC) $(OBJ_DIR)/$(MAIN_OBJECT) $(LDFLAGS) $(FLDFLAGS)
350 endif
351 endif
353 internal-build: internal-compile link
355 else
356 ifeq ($(strip $(filter-out c c++ ada,$(LANGUAGES))),)
357 # link with Ada/C/C++
359 ifeq ($(MAIN),ada)
360 # Ada main
361 link: $(LINKER) archive-objects force
362         @$(display) $(GNATMAKE) -b -l -P$(PROJECT_FILE) $(ADA_SOURCES)
363         @$(GNATMAKE) -b -l -P$(PROJECT_FILE) $(ADA_SOURCES) \
364                  -largs $(LARGS) $(LDFLAGS)
366 internal-build: $(LINKER) archive-objects force
367         @$(display) $(GNATMAKE) -P$(PROJECT_FILE) $(ADA_SOURCES) $(EXEC_RULE) $(ADAFLAGS)
368         @$(GNATMAKE) -P$(PROJECT_FILE) $(EXEC_RULE) $(ADA_SOURCES) $(ADAFLAGS) \
369          -largs $(LARGS) $(LDFLAGS)
371 else
372 # C/C++ main
374 link: $(LINKER) archive-objects force
375         @$(display) $(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES)
376         @$(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) \
377                  -largs $(OBJ_DIR)/$(MAIN_OBJECT) $(LARGS) $(LDFLAGS) $(FLDFLAGS)
379 internal-build: $(LINKER) archive-objects force
380         @$(display) $(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) $(ADAFLAGS)
381         @$(GNATMAKE) $(EXEC_RULE) \
382                  -B -P$(PROJECT_FILE) $(ADA_SOURCES) $(ADAFLAGS) \
383                  -largs $(OBJ_DIR)/$(MAIN_OBJECT) $(LARGS) $(LDFLAGS) $(FLDFLAGS)
384 endif
386 else
387 # unknown set of languages, fail
388 link:
389         @echo do not know how to link with the following languages: $(LANGUAGES)
390         exit 1
391 endif
392 endif
394 # Automatic handling of dependencies
396 ifeq ($(strip $(filter-out %gcc %g++,$(CC) $(CXX))),)
397 # Compiler is GCC, take avantage of the preprocessor option -MD and
398 # the CPATH environment variable
400 empty:=
401 space:=$(empty) $(empty)
402 path_sep:=$(shell gprcmd path_sep)
403 SRC_DIRS_PATH:= $(subst $(space),$(path_sep),$(SRC_DIRS))
404 export CPATH:=$(SRC_DIRS_PATH)$(path_sep)$(CPATH)
406 DEP_CFLAGS = -Wp,-MD,$(OBJ_DIR)/$(*F).d
408 define post-compile
409   @gprcmd deps $(OBJ_EXT) $(OBJ_DIR)/$(*F).d gcc
410 endef
412 # Default rule to create dummy dependency files the first time
414 $(OBJ_DIR)/%.d:
415         @echo $(*F)$(OBJ_EXT): > $@
417 else
418 # Compiler unknown, use a more general approach based on the output of $(CC) -M
420 ALL_CFLAGS := $(ALL_CFLAGS) $(C_INCLUDES)
421 ALL_CXXFLAGS := $(ALL_CXXFLAGS) $(C_INCLUDES)
423 DEP_FLAGS  = -M
424 DEP_CFLAGS =
426 define post-compile
427 endef
429 $(OBJ_DIR)/%.d: %$(C_EXT)
430         @$(CC) $(DEP_FLAGS) $(ALL_CFLAGS) $< > $@
431         @gprcmd deps $(OBJ_EXT) $@
433 $(OBJ_DIR)/%.d: %$(CXX_EXT)
434         @$(CXX) $(DEP_FLAGS) $(ALL_CXXFLAGS) $< > $@
435         @gprcmd deps $(OBJ_EXT) $@
436 endif
438 ifneq ($(DEP_FILES),)
439 -include $(DEP_FILES)
440 endif
442 # Compilation rules
444 # File rules
446 # Compile C files individually
447 %$(OBJ_EXT) : %$(C_EXT)
448         @$(display) $(C_Compiler) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
449 ifndef FAKE_COMPILE
450         @$(C_Compiler) -c $(ALL_CFLAGS) $< -o $(OBJ_DIR)/$@
451         @$(post-compile)
452 endif
454 # Compile C++ files individually
455 %$(OBJ_EXT) : %$(CXX_EXT)
456         @$(display) $(CXX_Compiler) -c $(CXXFLAGS) $< -o $(OBJ_DIR)/$@
457 ifndef FAKE_COMPILE
458         @$(CXX_Compiler) -c $(ALL_CXXFLAGS) $< -o $(OBJ_DIR)/$@
459         @$(post-compile)
460 endif
462 # Compile Ada body files individually
463 %$(ADA_BODY) : force
464         $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
466 # Compile Ada spec files individually
467 %$(ADA_SPEC) : force
468         $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
470 # Languages rules
472 # Compile all Ada files in the project
473 internal-ada :
474         $(GNATMAKE) -c -P$(PROJECT_FILE) $(ADAFLAGS)
476 # Compile all C files in the project
477 internal-c : $(C_OBJECTS)
479 # Compile all C++ files in the project
480 internal-c++ : $(CXX_OBJECTS)
482 .PHONY: force internal-clean internal-archive internal-build internal-compile internal-ada internal-c internal-c++ build compile clean ada c c++
484 internal-clean:
485         @$(display) $(RM) $(OBJ_DIR)/*$(OBJ_EXT)
486         @$(RM) $(OBJ_DIR)/*$(OBJ_EXT)
487         @$(display) $(RM) $(OBJ_DIR)/*.ali
488         @$(RM) $(OBJ_DIR)/*.ali
489         @$(display) $(RM) $(OBJ_DIR)/b~*
490         @$(RM) $(OBJ_DIR)/b~*
491         @$(display) $(RM) $(OBJ_DIR)/b_*
492         @$(RM) $(OBJ_DIR)/b_*
493         @$(display) $(RM) $(OBJ_DIR)/*$(AR_EXT)
494         @$(RM) $(OBJ_DIR)/*$(AR_EXT)
495         @$(display) $(RM) $(OBJ_DIR)/*.d
496         @$(RM) $(OBJ_DIR)/*.d
497 ifneq ($(EXEC),)
498         @$(display) $(RM) $(EXEC_DIR)/$(EXEC)
499         @$(RM) $(EXEC_DIR)/$(EXEC)
500 endif
502 force: