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)
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
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
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 # Set the source search path for C and C++ if needed
107 vpath %$(C_EXT) $(SRC_DIRS)
108 vpath %$(CXX_EXT) $(SRC_DIRS)
131 ARCHIVE=$(OBJ_DIR)/lib$(PROJECT_BASE)-full$(AR_EXT)
138 # Set the object search path
140 vpath %$(OBJ_EXT) $(OBJ_DIR)
141 vpath %$(AR_EXT) $(OBJ_DIR)
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.
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++_%))
155 # Default target is to build (compile/bind/link)
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++
167 @$(MAKE) -C $(dir $(subst |,:,$(@:clean_%=%))) -f Makefile.$(notdir $@) internal-clean
169 $(compile_deps): force
170 @$(MAKE) -C $(dir $(subst |,:,$(@:compile_%=%))) -f Makefile.$(notdir $@) internal-compile
172 $(object_deps): force
173 @$(MAKE) -C $(dir $(subst |,:,$(@:object_%=%))) -f Makefile.$(notdir $@) internal-archive-objects ARCHIVE=$(ARCHIVE)
176 @$(MAKE) -C $(dir $(subst |,:,$(@:ada_%=%))) -f Makefile.$(notdir $@) internal-ada
179 @$(MAKE) -C $(dir $(subst |,:,$(@:c_%=%))) -f Makefile.$(notdir $@) internal-c
182 @$(MAKE) -C $(dir $(subst |,:,$(@:c++_%=%))) -f Makefile.$(notdir $@) internal-c++
188 PROJECT_BASE = $(notdir $(PROJECT_FILE))
190 # Set C/C++ linker command & target
192 ifeq ($(filter c++,$(LANGUAGES)),c++)
195 ifeq ($(filter ada,$(LANGUAGES)),ada)
197 LINKER = $(OBJ_DIR)/c++linker
198 LARGS = --LINK=$(LINKER)
200 ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
201 # Case of GNU C++ and GNAT
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)
211 $(LINKER): Makefile.$(PROJECT_BASE)
212 @echo \#!/bin/sh > $(LINKER)
213 @echo $(CXX) $$\* $(shell gcc -print-libgcc-file-name) >> $(LINKER)
218 ifeq ($(strip $(LANGUAGES)),c)
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)
229 # Compute list of objects based on languages
231 ifeq ($(strip $(filter c,$(LANGUAGES))),c)
232 # Compute list of C sources automatically unless already specified
234 ifndef C_SRCS_DEFINED
237 $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(C_EXT))))
241 C_OBJECTS := $(C_SRCS:$(C_EXT)=$(OBJ_EXT))
242 OBJECTS += $(C_OBJECTS)
245 ifeq ($(strip $(filter c++,$(LANGUAGES))),c++)
246 # Compute list of C++ sources automatically unless already specified
248 ifndef CXX_SRCS_DEFINED
251 $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(CXX_EXT))))
255 CXX_OBJECTS := $(CXX_SRCS:$(CXX_EXT)=$(OBJ_EXT))
256 OBJECTS += $(CXX_OBJECTS)
259 OBJ_FILES := $(foreach name,$(OBJECTS),$(OBJ_DIR)/$(name))
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
266 DEP_FILES := $(OBJ_FILES:$(OBJ_EXT)=.d)
268 # Ada compilations are taken care of automatically, so do not mess with Ada
269 # objects, only with main sources.
271 ifeq ($(strip $(OBJECTS)),)
273 internal-archive-objects:
276 internal-compile: lib$(PROJECT_BASE)$(AR_EXT)
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)/$@
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)/$@
292 # There are three cases:
296 # - Ada/C/C++, main program is in Ada
298 # - Ada/C/C++, main program is in C/C++
300 ifeq ($(strip $(filter-out c c++,$(LANGUAGES))),)
302 ifeq ($(MAIN_OBJECT),)
304 @echo link: no main object specified, exiting...
310 @echo link: no executable specified, exiting...
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)
321 internal-build: internal-compile link
324 ifeq ($(strip $(filter-out c c++ ada,$(LANGUAGES))),)
325 # link with Ada/C/C++
329 link: $(LINKER) archive-objects force
330 $(GNATMAKE) -b -l -P$(PROJECT_FILE) $(ADA_SOURCES) \
331 -largs $(LARGS) $(LDFLAGS)
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)
341 link: $(LINKER) archive-objects force
342 $(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) \
343 -largs $(MAIN_OBJECT) $(LARGS) $(LDFLAGS)
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)
353 # unknown set of languages, fail
355 @echo do not know how to link with the following languages: $(LANGUAGES)
360 # Automatic handling of dependencies
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
367 @gprcmd deps $(OBJ_EXT) $(OBJ_DIR)/$(*F).d gcc
370 # Default rule to create dummy dependency files the first time
373 @echo $(*F)$(OBJ_EXT): > $@
376 # Compiler unknown, use a more general approach based on the output of $(CC) -M
384 $(OBJ_DIR)/%.d: %$(C_EXT)
385 @$(CC) $(DEP_FLAGS) $(ALL_CFLAGS) $< > $@
386 @gprcmd deps $(OBJ_EXT) $@
388 $(OBJ_DIR)/%.d: %$(CXX_EXT)
389 @$(CXX) $(DEP_FLAGS) $(ALL_CXXFLAGS) $< > $@
390 @gprcmd deps $(OBJ_EXT) $@
393 ifneq ($(DEP_FILES),)
394 -include $(DEP_FILES)
401 # Compile C files individually
402 %$(OBJ_EXT) : %$(C_EXT)
403 @echo $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
405 @$(CC) -c $(ALL_CFLAGS) $< -o $(OBJ_DIR)/$@
409 # Compile C++ files individually
410 %$(OBJ_EXT) : %$(CXX_EXT)
411 @echo $(CXX) -c $(CXXFLAGS) $< -o $(OBJ_DIR)/$@
413 @$(CXX) -c $(ALL_CXXFLAGS) $< -o $(OBJ_DIR)/$@
417 # Compile Ada body files individually
419 $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
421 # Compile Ada spec files individually
423 $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
427 # Compile all Ada files in the project
429 $(GNATMAKE) -c -P$(PROJECT_FILE) $(ADAFLAGS)
431 # Compile all C files in the project
432 internal-c : $(C_OBJECTS)
434 # Compile all C++ files in the project
435 internal-c++ : $(CXX_OBJECTS)
437 .PHONY: force internal-clean internal-archive internal-build internal-compile internal-ada internal-c internal-c++ build compile clean ada c c++
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
453 @echo $(RM) $(EXEC_DIR)/$(EXEC)
454 @$(RM) $(EXEC_DIR)/$(EXEC)