OSDN Git Service

2004-01-26 Ed Schonberg <schonberg@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / Makefile.generic
1 # Generic Makefile to support compilation for multiple languages.
2 # See also Makefile.prolog
3 #
4 #   Copyright (C) 2001-2004 Free Software Foundation, Inc.
5
6 # This file is part of GCC.
7
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.
12  
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.
17  
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.
22
23 # This Makefile provides a very generic framework of the following
24 # functionalities:
25 #
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
33
34 # Here are the rules that can be used from the command line:
35 #
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
45
46 # This Makefile expects the following variables to be set by the caller
47 # (typically another Makefile):
48 #
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)
80
81 # Set the source search path for C and C++ if needed
82
83 ifndef MAIN
84    MAIN=ada
85 endif
86
87 ifndef ADA_SPEC
88    ADA_SPEC=.ads
89 endif
90
91 ifndef ADA_BODY
92    ADA_BODY=.adb
93 endif
94
95 ifndef CC
96    CC=gcc
97 endif
98
99 ifndef CXX
100    CXX=gcc
101 endif
102
103 ifndef CXX_EXT
104    CXX_EXT=.cc
105 endif
106
107 vpath %$(C_EXT) $(SRC_DIRS)
108 vpath %$(CXX_EXT) $(SRC_DIRS)
109
110 ifndef OBJ_EXT
111    OBJ_EXT=.o
112 endif
113
114 ifndef AR_EXT
115    AR_EXT=.a
116 endif
117
118 ifndef AR_CMD
119    AR_CMD=ar rc
120 endif
121
122 ifndef RANLIB
123    RANLIB=ranlib
124 endif
125
126 ifndef GNATMAKE
127    GNATMAKE=gnatmake
128 endif
129
130 ifndef ARCHIVE
131    ARCHIVE=$(OBJ_DIR)/lib$(PROJECT_BASE)-full$(AR_EXT)
132 endif
133
134 ifeq ($(EXEC_DIR),)
135    EXEC_DIR=$(OBJ_DIR)
136 endif
137
138 # Set the object search path
139
140 vpath %$(OBJ_EXT) $(OBJ_DIR)
141 vpath %$(AR_EXT) $(OBJ_DIR)
142
143 # A target can't have a character ':' otherwise it will confuse make. We
144 # replace ':' by a pipe character. Note that there is less chance than a pipe
145 # character be part of a pathname on UNIX and this character can't be used in
146 # a pathname on Windows.
147
148 clean_deps = $(subst :,|,$(DEPS_PROJECTS:%=clean_%))
149 compile_deps = $(subst :,|,$(DEPS_PROJECTS:%=compile_%))
150 object_deps = $(subst :,|,$(DEPS_PROJECTS:%=object_%))
151 ada_deps = $(subst :,|,$(DEPS_PROJECTS:%=ada_%))
152 c_deps = $(subst :,|,$(DEPS_PROJECTS:%=c_%))
153 c++_deps = $(subst :,|,$(DEPS_PROJECTS:%=c++_%))
154
155 # Default target is to build (compile/bind/link)
156 all: build
157
158 clean: $(clean_deps) internal-clean
159 build: $(compile_deps) internal-compile internal-build
160 compile: $(compile_deps) internal-compile $(ADA_SOURCES)
161 ada: $(ada_deps) internal-ada
162 archive-objects: $(object_deps) internal-archive-objects
163 c: $(c_deps) internal-c
164 c++: $(c++deps) internal-c++
165
166 $(clean_deps): force
167         @$(MAKE) -C $(dir $(subst |,:,$(@:clean_%=%))) -f Makefile.$(notdir $@) internal-clean
168
169 $(compile_deps): force
170         @$(MAKE) -C $(dir $(subst |,:,$(@:compile_%=%))) -f Makefile.$(notdir $@) internal-compile
171
172 $(object_deps): force
173         @$(MAKE) -C $(dir $(subst |,:,$(@:object_%=%))) -f Makefile.$(notdir $@) internal-archive-objects ARCHIVE=$(ARCHIVE)
174
175 $(ada_deps): force
176         @$(MAKE) -C $(dir $(subst |,:,$(@:ada_%=%))) -f Makefile.$(notdir $@) internal-ada
177
178 $(c_deps): force
179         @$(MAKE) -C $(dir $(subst |,:,$(@:c_%=%))) -f Makefile.$(notdir $@) internal-c
180
181 $(c++_deps): force
182         @$(MAKE) -C $(dir $(subst |,:,$(@:c++_%=%))) -f Makefile.$(notdir $@) internal-c++
183
184 ifneq ($(EXEC),)
185    EXEC_RULE=-o $(EXEC)
186 endif
187
188 PROJECT_BASE = $(notdir $(PROJECT_FILE))
189
190 # Set C/C++ linker command & target
191
192 ifeq ($(filter c++,$(LANGUAGES)),c++)
193    LINKER = $(CXX)
194
195    ifeq ($(filter ada,$(LANGUAGES)),ada)
196       # C++ and Ada mixed
197       LINKER = $(OBJ_DIR)/c++linker
198       LARGS = --LINK=$(LINKER)
199
200       ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
201          # Case of GNU C++ and GNAT
202
203 $(LINKER): Makefile.$(PROJECT_BASE)
204         @echo \#!/bin/sh > $(LINKER)
205         @echo unset BINUTILS_ROOT >> $(LINKER)
206         @echo unset GCC_ROOT >> $(LINKER)
207         @echo $(CXX) $$\* >> $(LINKER)
208         @chmod +x $(LINKER)
209
210       else
211 $(LINKER): Makefile.$(PROJECT_BASE)
212         @echo \#!/bin/sh > $(LINKER)
213         @echo $(CXX) $$\* $(shell gcc -print-libgcc-file-name) >> $(LINKER)
214         @chmod +x $(LINKER)
215       endif
216    endif
217 else
218    ifeq ($(strip $(LANGUAGES)),c)
219       # Case of C only
220       LINKER = $(CC)
221    endif
222 endif
223
224 C_INCLUDES := $(foreach name,$(SRC_DIRS),-I$(name))
225 ALL_CFLAGS = $(CFLAGS) $(C_INCLUDES) $(DEP_CFLAGS)
226 ALL_CXXFLAGS = $(CXXFLAGS) $(C_INCLUDES) $(DEP_CFLAGS)
227 LDFLAGS := $(LIBS) $(LDFLAGS)
228
229 # Compute list of objects based on languages
230
231 ifeq ($(strip $(filter c,$(LANGUAGES))),c)
232    # Compute list of C sources automatically unless already specified
233
234    ifndef C_SRCS_DEFINED
235       ifndef C_SRCS
236          C_SRCS := \
237            $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(C_EXT))))
238       endif
239    endif
240
241    C_OBJECTS := $(C_SRCS:$(C_EXT)=$(OBJ_EXT))
242    OBJECTS += $(C_OBJECTS)
243 endif
244
245 ifeq ($(strip $(filter c++,$(LANGUAGES))),c++)
246    # Compute list of C++ sources automatically unless already specified
247
248    ifndef CXX_SRCS_DEFINED
249       ifndef CXX_SRCS
250          CXX_SRCS := \
251          $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(CXX_EXT))))
252       endif
253    endif
254
255    CXX_OBJECTS := $(CXX_SRCS:$(CXX_EXT)=$(OBJ_EXT))
256    OBJECTS += $(CXX_OBJECTS)
257 endif
258
259 OBJ_FILES := $(foreach name,$(OBJECTS),$(OBJ_DIR)/$(name))
260
261 # To handle C/C++ dependencies, we associate a small file for each
262 # source that will list the dependencies as a make rule, so that we can then
263 # include these rules in this makefile, and recompute them on a file by file
264 # basis
265
266 DEP_FILES := $(OBJ_FILES:$(OBJ_EXT)=.d)
267
268 # Ada compilations are taken care of automatically, so do not mess with Ada
269 # objects, only with main sources.
270
271 ifeq ($(strip $(OBJECTS)),)
272 internal-compile:
273 internal-archive-objects:
274
275 else
276 internal-compile: lib$(PROJECT_BASE)$(AR_EXT)
277
278 lib$(PROJECT_BASE)$(AR_EXT): $(OBJECTS)
279         @echo creating archive file for $(PROJECT_BASE)
280         cd $(OBJ_DIR); $(AR_CMD) $@ $(strip $(OBJECTS))
281         -$(RANLIB) $(OBJ_DIR)/$@
282
283 internal-archive-objects: $(OBJECTS)
284 #       @echo $(AR_CMD) $(ARCHIVE) $(strip $(OBJECTS))
285 #       cd $(OBJ_DIR); $(AR_CMD) $(ARCHIVE) $(strip $(OBJECTS))
286 #       -$(RANLIB) $(OBJ_DIR)/$@
287
288 endif
289
290 # Linking rules
291
292 # There are three cases:
293 #
294 # - C/C++ sources
295 #
296 # - Ada/C/C++, main program is in Ada
297 #
298 # - Ada/C/C++, main program is in C/C++
299
300 ifeq ($(strip $(filter-out c c++,$(LANGUAGES))),)
301 # link with C/C++
302 ifeq ($(MAIN_OBJECT),)
303 link:
304         @echo link: no main object specified, exiting...
305         exit 1
306 else
307 ifeq ($(EXEC),)
308
309 link:
310         @echo link: no executable specified, exiting...
311         exit 1
312 else
313
314 link: $(EXEC_DIR)/$(EXEC) archive-objects
315 $(EXEC_DIR)/$(EXEC): $(OBJ_FILES)
316         @echo $(LINKER) -o $(EXEC_DIR)/$(EXEC) $(OBJ_DIR)/$(MAIN_OBJECT) $(LDFLAGS)
317         $(LINKER) -o $(EXEC_DIR)/$(EXEC) $(OBJ_DIR)/$(MAIN_OBJECT) $(LDFLAGS)
318 endif
319 endif
320
321 internal-build: internal-compile link
322
323 else
324 ifeq ($(strip $(filter-out c c++ ada,$(LANGUAGES))),)
325 # link with Ada/C/C++
326
327 ifeq ($(MAIN),ada)
328 # Ada main
329 link: $(LINKER) archive-objects force
330         $(GNATMAKE) -b -l -P$(PROJECT_FILE) $(ADA_SOURCES) \
331                  -largs $(LARGS) $(LDFLAGS)
332
333 internal-build: $(LINKER) archive-objects force
334         @echo $(GNATMAKE) -P$(PROJECT_FILE) $(ADA_SOURCES) $(EXEC_RULE) $(ADAFLAGS)
335         @$(GNATMAKE) -P$(PROJECT_FILE) $(EXEC_RULE) $(ADA_SOURCES) $(ADAFLAGS) \
336          -largs $(LARGS) $(LDFLAGS)
337
338 else
339 # C/C++ main
340
341 link: $(LINKER) archive-objects force
342         $(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) \
343                  -largs $(MAIN_OBJECT) $(LARGS) $(LDFLAGS)
344
345 internal-build: $(LINKER) archive-objects force
346         @echo $(GNATMAKE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) $(EXEC_RULE) $(ADAFLAGS)
347         @$(GNATMAKE) $(EXEC_RULE) \
348                  -B -P$(PROJECT_FILE) $(ADA_SOURCES) $(ADAFLAGS) \
349                  -largs $(MAIN_OBJECT) $(LARGS) $(LDFLAGS)
350 endif
351
352 else
353 # unknown set of languages, fail
354 link:
355         @echo do not know how to link with the following languages: $(LANGUAGES)
356         exit 1
357 endif
358 endif
359
360 # Automatic handling of dependencies
361
362 ifeq ($(strip $(filter-out %gcc %g++,$(CC) $(CXX))),)
363 # Compiler is GCC, take avantage of the preprocessor option -MD
364 DEP_CFLAGS = -Wp,-MD,$(OBJ_DIR)/$(*F).d
365
366 define post-compile
367   @gprcmd deps $(OBJ_EXT) $(OBJ_DIR)/$(*F).d gcc
368 endef
369
370 # Default rule to create dummy dependency files the first time
371
372 $(OBJ_DIR)/%.d:
373         @echo $(*F)$(OBJ_EXT): > $@
374
375 else
376 # Compiler unknown, use a more general approach based on the output of $(CC) -M
377
378 DEP_FLAGS  = -M
379 DEP_CFLAGS =
380
381 define post-compile
382 endef
383
384 $(OBJ_DIR)/%.d: %$(C_EXT)
385         @$(CC) $(DEP_FLAGS) $(ALL_CFLAGS) $< > $@
386         @gprcmd deps $(OBJ_EXT) $@
387
388 $(OBJ_DIR)/%.d: %$(CXX_EXT)
389         @$(CXX) $(DEP_FLAGS) $(ALL_CXXFLAGS) $< > $@
390         @gprcmd deps $(OBJ_EXT) $@
391 endif
392
393 ifneq ($(DEP_FILES),)
394 -include $(DEP_FILES)
395 endif
396
397 # Compilation rules
398
399 # File rules
400
401 # Compile C files individually
402 %$(OBJ_EXT) : %$(C_EXT)
403         @echo $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
404 ifndef FAKE_COMPILE
405         @$(CC) -c $(ALL_CFLAGS) $< -o $(OBJ_DIR)/$@
406         @$(post-compile)
407 endif
408
409 # Compile C++ files individually
410 %$(OBJ_EXT) : %$(CXX_EXT)
411         @echo $(CXX) -c $(CXXFLAGS) $< -o $(OBJ_DIR)/$@
412 ifndef FAKE_COMPILE
413         @$(CXX) -c $(ALL_CXXFLAGS) $< -o $(OBJ_DIR)/$@
414         @$(post-compile)
415 endif
416
417 # Compile Ada body files individually
418 %$(ADA_BODY) : force
419         $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
420
421 # Compile Ada spec files individually
422 %$(ADA_SPEC) : force
423         $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
424
425 # Languages rules
426
427 # Compile all Ada files in the project
428 internal-ada :
429         $(GNATMAKE) -c -P$(PROJECT_FILE) $(ADAFLAGS)
430
431 # Compile all C files in the project
432 internal-c : $(C_OBJECTS)
433
434 # Compile all C++ files in the project
435 internal-c++ : $(CXX_OBJECTS)
436
437 .PHONY: force internal-clean internal-archive internal-build internal-compile internal-ada internal-c internal-c++ build compile clean ada c c++
438
439 internal-clean:
440         @echo $(RM) $(OBJ_DIR)/*$(OBJ_EXT)
441         @$(RM) $(OBJ_DIR)/*$(OBJ_EXT)
442         @echo $(RM) $(OBJ_DIR)/*.ali
443         @$(RM) $(OBJ_DIR)/*.ali
444         @echo $(RM) $(OBJ_DIR)/b~*
445         @$(RM) $(OBJ_DIR)/b~*
446         @echo $(RM) $(OBJ_DIR)/b_*
447         @$(RM) $(OBJ_DIR)/b_*
448         @echo $(RM) $(OBJ_DIR)/*$(AR_EXT)
449         @$(RM) $(OBJ_DIR)/*$(AR_EXT)
450         @echo $(RM) $(OBJ_DIR)/*.d
451         @$(RM) $(OBJ_DIR)/*.d
452 ifneq ($(EXEC),)
453         @echo $(RM) $(EXEC_DIR)/$(EXEC)
454         @$(RM) $(EXEC_DIR)/$(EXEC)
455 endif
456
457 force:
458