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 # 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
110 vpath %$(C_EXT) $(SRC_DIRS)
111 vpath %$(CXX_EXT) $(SRC_DIRS)
134 ARCHIVE=$(OBJ_DIR)/lib$(PROJECT_BASE)-full$(AR_EXT)
141 # Define display to echo only when SILENT is not defined
154 # Make sure gnatmake is called silently when SILENT is set
156 GNATMAKE:=$(GNATMAKE) -q
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
168 ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
169 CXX_Compiler=$(CXX) -x c++
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)
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++
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)
212 @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:ada_%=%))) -f Makefile.$(notdir $@) internal-ada
215 @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:c_%=%))) -f Makefile.$(notdir $@) internal-c
218 @$(MAKE) -C $(dir $(subst __GPRCOLON__,:,$(@:c++_%=%))) -f Makefile.$(notdir $@) internal-c++
224 PROJECT_BASE = $(notdir $(PROJECT_FILE))
226 # Set C/C++ linker command & target
228 ifeq ($(filter c++,$(LANGUAGES)),c++)
231 ifeq ($(filter ada,$(LANGUAGES)),ada)
233 LARGS = --LINK=$(LINKER)
235 ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
236 # Case of GNAT and a GNU C++ compiler
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)
250 ifeq ($(strip $(LANGUAGES)),c)
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
269 $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(C_EXT))))
273 C_OBJECTS := $(C_SRCS:$(C_EXT)=$(OBJ_EXT))
274 OBJECTS += $(C_OBJECTS)
277 ifeq ($(strip $(filter c++,$(LANGUAGES))),c++)
278 # Compute list of C++ sources automatically unless already specified
280 ifndef CXX_SRCS_DEFINED
283 $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(CXX_EXT))))
287 CXX_OBJECTS := $(CXX_SRCS:$(CXX_EXT)=$(OBJ_EXT))
288 OBJECTS += $(CXX_OBJECTS)
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
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)),)
305 internal-archive-objects:
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)/$@
324 # There are three cases:
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))),)
334 ifeq ($(MAIN_OBJECT),)
336 @echo link: no main object specified, exiting...
342 @echo link: no executable specified, exiting...
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)
353 internal-build: internal-compile link
356 ifeq ($(strip $(filter-out c c++ ada,$(LANGUAGES))),)
357 # link with Ada/C/C++
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)
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)
387 # unknown set of languages, fail
389 @echo do not know how to link with the following languages: $(LANGUAGES)
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
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
409 @gprcmd deps $(OBJ_EXT) $(OBJ_DIR)/$(*F).d gcc
412 # Default rule to create dummy dependency files the first time
415 @echo $(*F)$(OBJ_EXT): > $@
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)
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) $@
438 ifneq ($(DEP_FILES),)
439 -include $(DEP_FILES)
446 # Compile C files individually
447 %$(OBJ_EXT) : %$(C_EXT)
448 @$(display) $(C_Compiler) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
450 @$(C_Compiler) -c $(ALL_CFLAGS) $< -o $(OBJ_DIR)/$@
454 # Compile C++ files individually
455 %$(OBJ_EXT) : %$(CXX_EXT)
456 @$(display) $(CXX_Compiler) -c $(CXXFLAGS) $< -o $(OBJ_DIR)/$@
458 @$(CXX_Compiler) -c $(ALL_CXXFLAGS) $< -o $(OBJ_DIR)/$@
462 # Compile Ada body files individually
464 $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
466 # Compile Ada spec files individually
468 $(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)
472 # Compile all Ada files in the project
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++
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
498 @$(display) $(RM) $(EXEC_DIR)/$(EXEC)
499 @$(RM) $(EXEC_DIR)/$(EXEC)