aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/Makefile.generic
blob: 61d0ff9e839c8246ebd168f763f4c5b579d1d154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Generic Makefile to support compilation for multiple languages.
# See also Makefile.prolog
#
#   Copyright (C) 2001-2004 Free Software Foundation, Inc.

# This file is part of GCC.

# GCC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# GCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# This Makefile provides a very generic framework of the following
# functionalities:
#
# Multi-language support (currently any combination of Ada/C/C++ supported)
# Automatic handling of source dependencies
# Handling of various C/C++ compilers
# Handling of Ada sources using the GNAT toolchain
# Complete build process (compile/bind/link)
# Individual compilation (on a file, or on a language)
# Handling of an object directory

# Here are the rules that can be used from the command line:
#
# build:         complete compile/bind/link process
# compile:       compile all files that are not up-to-date
# link:          bind/link
# ada:           compile all Ada files that are not up-to-date
# c:             ditto for C files
# c++:           ditto for C++ files
# <ada file>:    compile the specified file if needed.
# <object file>: compile the corresponding C/C++ source file if needed.
# clean:         remove all temporary files

# This Makefile expects the following variables to be set by the caller
# (typically another Makefile):
#
# ADA_SPEC         extension of Ada spec files (optional, default to .ads)
# ADA_BODY         extension of Ada body files (optional, default to .adb)
# C_EXT            extension of C files (optional, default to .c)
# CXX_EXT          extension of C++ files (optional, default to .cc)
# OBJ_EXT          extension of object files (optional, default to .o)
# SRC_DIRS         blank separated list of source directories
# C_SRCS           explicit list of C sources (optional)
# C_SRCS_DEFINED   if set, indicates that C_SRCS is already set
# CXX_SRCS         explicit list of C++ sources (optional)
# CXX_SRCS_DEFINED is set, indicates that CXX_SRCS is already set
# OBJ_DIR          a single directory where object files should be put
# EXEC_DIR         a single directory where executables should be put (optional)
# LANGUAGES        a blank separated list of languages supported, e.g "ada c"
#                  the current list of recognized languages is: ada, c, c++
# CC               name of the C compiler (optional, default to gcc)
# CXX              name of the C++ compiler (optional, default to gcc)
# AR_CMD           command to create an archive (optional, default to "ar rc")
# AR_EXT           file extension of an archive (optional, default to ".a")
# RANLIB           command to generate an index (optional, default to "ranlib")
# GNATMAKE         name of the GNAT builder (optional, default to "gnatmake")
# ADAFLAGS         additional Ada compilation switches, e.g "-gnatf" (optional)
# CFLAGS           default C compilation switches, e.g "-O2 -g" (optional)
# CXXFLAGS         default C++ compilation switches (optional)
# LIBS             libraries to link with (optional)
# LDFLAGS          linker switches (optional)
# ADA_SOURCES      list of main Ada sources (optional)
# EXEC             name of the final executable (optional)
# MAIN             language of the main program (optional)
# MAIN_OBJECT      main object file (optional)
# PROJECT_FILE     name of the project file, without the .gpr extension
# DEPS_PROJECTS    list of project dependencies (optional)

# SILENT           (optional) when defined, make -s will not output anything
#                  when all commands are successful.

# Set the source search path for C and C++ if needed

ifndef MAIN
   MAIN=ada
endif

ifndef ADA_SPEC
   ADA_SPEC=.ads
endif

ifndef ADA_BODY
   ADA_BODY=.adb
endif

ifndef CC
   CC=gcc
endif

ifndef CXX
   CXX=gcc
endif

ifndef CXX_EXT
   CXX_EXT=.cc
endif

vpath %$(C_EXT) $(SRC_DIRS)
vpath %$(CXX_EXT) $(SRC_DIRS)

ifndef OBJ_EXT
   OBJ_EXT=.o
endif

ifndef AR_EXT
   AR_EXT=.a
endif

ifndef AR_CMD
   AR_CMD=ar rc
endif

ifndef RANLIB
   RANLIB=ranlib
endif

ifndef GNATMAKE
   GNATMAKE:=gnatmake
endif

ifndef ARCHIVE
   ARCHIVE=$(OBJ_DIR)/lib$(PROJECT_BASE)-full$(AR_EXT)
endif

ifeq ($(EXEC_DIR),)
   EXEC_DIR=$(OBJ_DIR)
endif

# Define display to echo only when SILENT is not defined

ifdef SILENT
define display
   @gprcmd ignore
endef

else
define display
   @echo
endef
endif

# Make sure gnatmake is called silently when SILENT is set
ifdef SILENT
   GNATMAKE:=$(GNATMAKE) -q
endif

# If C/C++ compiler is gcc, make sure gcc is called with the switch indicating
# the language, in case the extension is not standard.

ifeq ($(strip $(filter-out %gcc,$(CC))),)
   C_Compiler=$(CC) -x c
else
   C_Compiler=$(CC)
endif

ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
   CXX_Compiler=$(CXX) -x c++
else
   CXX_Compiler=$(CXX)
endif

# Set the object search path

vpath %$(OBJ_EXT) $(OBJ_DIR)
vpath %$(AR_EXT) $(OBJ_DIR)

# A target can't have a character ':' otherwise it will confuse make. We
# replace ':' by a pipe character. Note that there is less chance than a pipe
# character be part of a pathname on UNIX and this character can't be used in
# a pathname on Windows.

clean_deps = $(subst :,|,$(DEPS_PROJECTS:%=clean_%))
compile_deps = $(subst :,|,$(DEPS_PROJECTS:%=compile_%))
object_deps = $(subst :,|,$(DEPS_PROJECTS:%=object_%))
ada_deps = $(subst :,|,$(DEPS_PROJECTS:%=ada_%))
c_deps = $(subst :,|,$(DEPS_PROJECTS:%=c_%))
c++_deps = $(subst :,|,$(DEPS_PROJECTS:%=c++_%))

# Default target is to build (compile/bind/link)
all: build

clean: $(clean_deps) internal-clean
build: $(compile_deps) internal-compile internal-build
compile: $(compile_deps) internal-compile $(ADA_SOURCES)
ada: $(ada_deps) internal-ada
archive-objects: $(object_deps) internal-archive-objects
c: $(c_deps) internal-c
c++: $(c++deps) internal-c++

$(clean_deps): force
	@$(MAKE) -C $(dir $(subst |,:,$(@:clean_%=%))) -f Makefile.$(notdir $@) internal-clean

$(compile_deps): force
	@$(MAKE) -C $(dir $(subst |,:,$(@:compile_%=%))) -f Makefile.$(notdir $@) internal-compile

$(object_deps): force
	@$(MAKE) -C $(dir $(subst |,:,$(@:object_%=%))) -f Makefile.$(notdir $@) internal-archive-objects ARCHIVE=$(ARCHIVE)

$(ada_deps): force
	@$(MAKE) -C $(dir $(subst |,:,$(@:ada_%=%))) -f Makefile.$(notdir $@) internal-ada

$(c_deps): force
	@$(MAKE) -C $(dir $(subst |,:,$(@:c_%=%))) -f Makefile.$(notdir $@) internal-c

$(c++_deps): force
	@$(MAKE) -C $(dir $(subst |,:,$(@:c++_%=%))) -f Makefile.$(notdir $@) internal-c++

ifneq ($(EXEC),)
   EXEC_RULE=-o $(EXEC)
endif

PROJECT_BASE = $(notdir $(PROJECT_FILE))

# Set C/C++ linker command & target

ifeq ($(filter c++,$(LANGUAGES)),c++)
   LINKER = $(CXX)

   ifeq ($(filter ada,$(LANGUAGES)),ada)
      # C++ and Ada mixed
      LINKER = $(OBJ_DIR)/c++linker
      LARGS = --LINK=$(LINKER)

      ifeq ($(strip $(filter-out %gcc %g++,$(CXX))),)
         # Case of GNU C++ and GNAT

$(LINKER): Makefile.$(PROJECT_BASE)
	@echo \#!/bin/sh > $(LINKER)
	@echo unset BINUTILS_ROOT >> $(LINKER)
	@echo unset GCC_ROOT >> $(LINKER)
	@echo $(CXX) $$\* >> $(LINKER)
	@chmod +x $(LINKER)

      else
$(LINKER): Makefile.$(PROJECT_BASE)
	@echo \#!/bin/sh > $(LINKER)
	@echo $(CXX) $$\* $(shell gcc -print-libgcc-file-name) >> $(LINKER)
	@chmod +x $(LINKER)
      endif
   endif
else
   ifeq ($(strip $(LANGUAGES)),c)
      # Case of C only
      LINKER = $(CC)
   endif
endif

C_INCLUDES := $(foreach name,$(SRC_DIRS),-I$(name))
ALL_CFLAGS = $(CFLAGS) $(DEP_CFLAGS)
ALL_CXXFLAGS = $(CXXFLAGS) $(DEP_CFLAGS)
LDFLAGS := $(LIBS) $(LDFLAGS)

# Compute list of objects based on languages

ifeq ($(strip $(filter c,$(LANGUAGES))),c)
   # Compute list of C sources automatically unless already specified

   ifndef C_SRCS_DEFINED
      ifndef C_SRCS
         C_SRCS := \
           $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(C_EXT))))
      endif
   endif

   C_OBJECTS := $(C_SRCS:$(C_EXT)=$(OBJ_EXT))
   OBJECTS += $(C_OBJECTS)
endif

ifeq ($(strip $(filter c++,$(LANGUAGES))),c++)
   # Compute list of C++ sources automatically unless already specified

   ifndef CXX_SRCS_DEFINED
      ifndef CXX_SRCS
         CXX_SRCS := \
         $(foreach name,$(SRC_DIRS),$(notdir $(wildcard $(name)/*$(CXX_EXT))))
      endif
   endif

   CXX_OBJECTS := $(CXX_SRCS:$(CXX_EXT)=$(OBJ_EXT))
   OBJECTS += $(CXX_OBJECTS)
endif

OBJ_FILES := $(foreach name,$(OBJECTS),$(OBJ_DIR)/$(name))

# To handle C/C++ dependencies, we associate a small file for each
# source that will list the dependencies as a make rule, so that we can then
# include these rules in this makefile, and recompute them on a file by file
# basis

DEP_FILES := $(OBJ_FILES:$(OBJ_EXT)=.d)

# Ada compilations are taken care of automatically, so do not mess with Ada
# objects, only with main sources.

ifeq ($(strip $(OBJECTS)),)
internal-compile:
internal-archive-objects:

else
internal-compile: lib$(PROJECT_BASE)$(AR_EXT)

lib$(PROJECT_BASE)$(AR_EXT): $(OBJECTS)
	@$(display) creating archive file for $(PROJECT_BASE)
	cd $(OBJ_DIR); $(AR_CMD) $@ $(strip $(OBJECTS))
	-$(RANLIB) $(OBJ_DIR)/$@

internal-archive-objects: $(OBJECTS)
#	@echo $(AR_CMD) $(ARCHIVE) $(strip $(OBJECTS))
#	cd $(OBJ_DIR); $(AR_CMD) $(ARCHIVE) $(strip $(OBJECTS))
#	-$(RANLIB) $(OBJ_DIR)/$@

endif

# Linking rules

# There are three cases:
#
# - C/C++ sources
#
# - Ada/C/C++, main program is in Ada
#
# - Ada/C/C++, main program is in C/C++

ifeq ($(strip $(filter-out c c++,$(LANGUAGES))),)
# link with C/C++
ifeq ($(MAIN_OBJECT),)
link:
	@echo link: no main object specified, exiting...
	exit 1
else
ifeq ($(EXEC),)

link:
	@echo link: no executable specified, exiting...
	exit 1
else

link: $(EXEC_DIR)/$(EXEC) archive-objects
$(EXEC_DIR)/$(EXEC): $(OBJ_FILES)
	@$(display) $(LINKER) -o $(EXEC_DIR)/$(EXEC) $(OBJ_DIR)/$(MAIN_OBJECT) $(LDFLAGS)
	$(LINKER) -o $(EXEC_DIR)/$(EXEC) $(OBJ_DIR)/$(MAIN_OBJECT) $(LDFLAGS)
endif
endif

internal-build: internal-compile link

else
ifeq ($(strip $(filter-out c c++ ada,$(LANGUAGES))),)
# link with Ada/C/C++

ifeq ($(MAIN),ada)
# Ada main
link: $(LINKER) archive-objects force
	@(display) $(GNATMAKE) -b -l -P$(PROJECT_FILE) $(ADA_SOURCES)
	@$(GNATMAKE) -b -l -P$(PROJECT_FILE) $(ADA_SOURCES) \
		 -largs $(LARGS) $(LDFLAGS)

internal-build: $(LINKER) archive-objects force
	@$(display) $(GNATMAKE) -P$(PROJECT_FILE) $(ADA_SOURCES) $(EXEC_RULE) $(ADAFLAGS)
	@$(GNATMAKE) -P$(PROJECT_FILE) $(EXEC_RULE) $(ADA_SOURCES) $(ADAFLAGS) \
	 -largs $(LARGS) $(LDFLAGS)

else
# C/C++ main

link: $(LINKER) archive-objects force
	@(display) $(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES)
	@$(GNATMAKE) $(EXEC_RULE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) \
		 -largs $(MAIN_OBJECT) $(LARGS) $(LDFLAGS)

internal-build: $(LINKER) archive-objects force
	@$(display) $(GNATMAKE) -B -P$(PROJECT_FILE) $(ADA_SOURCES) $(EXEC_RULE) $(ADAFLAGS)
	@$(GNATMAKE) $(EXEC_RULE) \
		 -B -P$(PROJECT_FILE) $(ADA_SOURCES) $(ADAFLAGS) \
		 -largs $(MAIN_OBJECT) $(LARGS) $(LDFLAGS)
endif

else
# unknown set of languages, fail
link:
	@echo do not know how to link with the following languages: $(LANGUAGES)
	exit 1
endif
endif

# Automatic handling of dependencies

ifeq ($(strip $(filter-out %gcc %g++,$(CC) $(CXX))),)
# Compiler is GCC, take avantage of the preprocessor option -MD and
# C*_INCLUDE_PATH environment variables

export C_INCLUDE_PATH:=$(shell gprcmd path $(SRC_DIRS))$(C_INCLUDE_PATH)
export CXX_INCLUDE_PATH:=$(shell gprcmd path $(SRC_DIRS))$(CXX_INCLUDE_PATH)

DEP_CFLAGS = -Wp,-MD,$(OBJ_DIR)/$(*F).d

define post-compile
  @gprcmd deps $(OBJ_EXT) $(OBJ_DIR)/$(*F).d gcc
endef

# Default rule to create dummy dependency files the first time

$(OBJ_DIR)/%.d:
	@echo $(*F)$(OBJ_EXT): > $@

else
# Compiler unknown, use a more general approach based on the output of $(CC) -M

ALL_CFLAGS := $(ALL_CFLAGS) $(C_INCLUDES)
ALL_CXXFLAGS := $(ALL_CXXFLAGS) $(C_INCLUDES)

DEP_FLAGS  = -M
DEP_CFLAGS =

define post-compile
endef

$(OBJ_DIR)/%.d: %$(C_EXT)
	@$(CC) $(DEP_FLAGS) $(ALL_CFLAGS) $< > $@
	@gprcmd deps $(OBJ_EXT) $@

$(OBJ_DIR)/%.d: %$(CXX_EXT)
	@$(CXX) $(DEP_FLAGS) $(ALL_CXXFLAGS) $< > $@
	@gprcmd deps $(OBJ_EXT) $@
endif

ifneq ($(DEP_FILES),)
-include $(DEP_FILES)
endif

# Compilation rules

# File rules

# Compile C files individually
%$(OBJ_EXT) : %$(C_EXT)
	@$(display) $(C_Compiler) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
ifndef FAKE_COMPILE
	@$(C_Compiler) -c $(ALL_CFLAGS) $< -o $(OBJ_DIR)/$@
	@$(post-compile)
endif

# Compile C++ files individually
%$(OBJ_EXT) : %$(CXX_EXT)
	@$(display) $(CXX_Compiler) -c $(CXXFLAGS) $< -o $(OBJ_DIR)/$@
ifndef FAKE_COMPILE
	@$(CXX_Compiler) -c $(ALL_CXXFLAGS) $< -o $(OBJ_DIR)/$@
	@$(post-compile)
endif

# Compile Ada body files individually
%$(ADA_BODY) : force
	$(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)

# Compile Ada spec files individually
%$(ADA_SPEC) : force
	$(GNATMAKE) -c -P$(PROJECT_FILE) $@ $(ADAFLAGS)

# Languages rules

# Compile all Ada files in the project
internal-ada :
	$(GNATMAKE) -c -P$(PROJECT_FILE) $(ADAFLAGS)

# Compile all C files in the project
internal-c : $(C_OBJECTS)

# Compile all C++ files in the project
internal-c++ : $(CXX_OBJECTS)

.PHONY: force internal-clean internal-archive internal-build internal-compile internal-ada internal-c internal-c++ build compile clean ada c c++

internal-clean:
	@echo $(RM) $(OBJ_DIR)/*$(OBJ_EXT)
	@$(RM) $(OBJ_DIR)/*$(OBJ_EXT)
	@echo $(RM) $(OBJ_DIR)/*.ali
	@$(RM) $(OBJ_DIR)/*.ali
	@echo $(RM) $(OBJ_DIR)/b~*
	@$(RM) $(OBJ_DIR)/b~*
	@echo $(RM) $(OBJ_DIR)/b_*
	@$(RM) $(OBJ_DIR)/b_*
	@echo $(RM) $(OBJ_DIR)/*$(AR_EXT)
	@$(RM) $(OBJ_DIR)/*$(AR_EXT)
	@echo $(RM) $(OBJ_DIR)/*.d
	@$(RM) $(OBJ_DIR)/*.d
ifneq ($(EXEC),)
	@echo $(RM) $(EXEC_DIR)/$(EXEC)
	@$(RM) $(EXEC_DIR)/$(EXEC)
endif

force: