-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile.in
189 lines (162 loc) · 7.14 KB
/
Makefile.in
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
# Copyright (c) 2010, Florian Rivoal
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Florian Rivoal ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Florian Rivoal BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# This is a base Makefile intended to suit many different projects. It needs to
# be customized to the specificities of each project, but provides
# infrastructure to make this task easier. It currently supports C and C++
# Features:
# - Provides the following targets: all, clean, tags
# - Handles header file dependencies automatically
# - Builds from one or more source trees, with no restriction on subdirectories
# - Builds into a separate build tree, replicating the structure of the source
# tree(s)
# - Supports putting generated source files in the build tree
#
# It is meant to be used together with GNU autoconf. To use this Makefile.in,
# in addition to creating the configure.ac file, it should be sufficient to
# edit the section labeled "Project specific section", the rest is meant to be
# generic and cross project.
# -- User overridable commands --
MKDIR := mkdir -p
RM := rm -rf
AR := ar r
TAR := tar
INSTALL := install
INSTALL_PROGRAM := $(INSTALL)
INSTALL_DATA := $(INSTALL) -m 644
ifeq ($(origin CC),default)
CC := @CC@
endif
ifeq ($(origin CXX),default)
CXX := @CXX@
endif
# -- User overridable path --
OUTDIR ?= $(if $(findstring $(base_path),.),out,$(base_path)/out)
FULL_NAME ?= @PACKAGE_NAME@-@PACKAGE_VERSION@
# -- Auxiliary functions --
## Expands to a non empty string when the dependency files can be skipped.
## @param 1: List of targets for which dependency files are unneeded
deps_unneeded = $(and $(MAKECMDGOALS),$(findstring $(MAKECMDGOALS),$(filter $(1),$(MAKECMDGOALS))))
## Finds all the source files in specified directories and their subdirectories.
## Supported languages are c and c++.
## @param 1: list of directories to search source files in. Each directory's
## path should be relative to base_path.
find_sources = $(shell cd $(base_path) && find $1 -name "*.cpp" -or -name "*.c")
## Expands to the list of object files, including their full path starting with
## OUTDIR..
## @param 1: List of c and c++ source files
obj_from_src = $(addprefix $(OUTDIR)/,$(patsubst %.c,%.o,$(filter %.c,$1)) $(patsubst %.cpp,%.o,$(filter %.cpp,$1)))
## Adds the directories to the include path of specified objects.
## @param 1: List of .o files
## @param 2: List of directories to add to the include path of param 1's .o
## files, and of the matching .d files
add_inc_path = $(eval $1 $(1:.o=.d): CPPFLAGS += $(addprefix -I $(base_path)/,$2))
## Shell command used by the install rules.
## In most cases, there is no need to use this directly, as specifying ##
## dependencies to the various install targets should be enough. However, it
## can be usefull to install files in different paths.
## @param 1: List of files to install
## @param 2: Where to install these files
## @param 3: Optional. When omitted or empty, the file installed is treated as
## an executable. Otherwise, it is treated as data.
install_cmd = $(foreach target,$1,$(if $3,$(INSTALL_DATA),$(INSTALL_PROGRAM)) -D $(target) $2/$(notdir $(target)) &&) true
# -- Default rule --
.PHONY: all
all: ;
# -- Install target base path --
prefix = $(DESTDIR)@prefix@
exec_prefix = @exec_prefix@
###############################################################################
# ---- Project specific section ---- #
# This section must define at least:
# - the 'base_path' variable, to which 'src_dirs', 'dist_targets' and the
# default value of 'OUTDIR' is relative.
# - the 'src_dirs' variable, which lists all the directories under which non
# generated source code shall be found
# - the 'objects' variable, which lists all the .o files for which dependencies
# should be computed
# - the prerequisites of the 'all' target
# - the 'dist_targets' variable, which lists all the directories or files that
# should be included in the tarball created by "make dist".
#
# Keep in mind that generated files should be stored under $(OUTDIR)/
base_path := .
src_dirs := src
dist_targets := Makefile.in configure $(src_dirs) prototype COPYING README
sources := $(call find_sources,$(src_dirs))
objects := $(call obj_from_src,$(sources))
all: mandelbrot
gtk_cflags = @GTK_CFLAGS@
override CFLAGS += -Wall -std=c99 $(gtk_cflags)
$(OUTDIR)/mandelbrot: $(objects)
$(LINK.c) -o $@ $^ @GTK_LIBS@
.PHONY: install_with_proto install_proto
install_with_proto: install install_proto
install_proto: prototype/mandelbrot.rkt
$(call install_cmd,$^,@bindir@)
install_bin: mandelbrot
# ---- End of project specific section ---- #
###############################################################################
# -- Safety check --
ifneq ($(wildcard $(OUTDIR)),)
ifeq ($(wildcard $(OUTDIR)/.exists),)
$(error The output directory ($(OUTDIR)) exists but was not created by this makefile. Aborting to avoid overwriting or deleting existing files)
endif
else
endif
# -- Dependency inclusions --
ifeq ($(call deps_unneeded,clean tags %/.exists),)
include $(patsubst %.o,%.d,$(wildcard $(objects)))
-include $(OUTDIR)/.exists
endif
# -- Top level rules --
.PHONY: tags clean dist install install-bin
tags:
ctags -R $(addprefix $(base_path)/,$(src_dirs))
clean-cmd := $(RM) $(OUTDIR) tags
clean:
$(clean-cmd)
Makefile: Makefile.in
$(clean-cmd)
./config.status
install: install_bin
install_bin:
$(call install_cmd,$^,@bindir@)
tarball := $(OUTDIR)/$(FULL_NAME).tar.gz
dist: $(tarball)
.SECONDEXPANSION:
$(tarball): $(addprefix $(base_path)/,$(dist_targets)) | $$(@D)/.exists
$(MKDIR) $(@D)/$(FULL_NAME)
$(TAR) c -f - -C $(base_path) $(patsubst $(base_path)/%,%,$^)| tar x -C $(@D)/$(FULL_NAME)
$(TAR) czf $@ -C $(@D) $(FULL_NAME)
$(RM) $(@D)/$(FULL_NAME)
# -- Generic build rules --
.PRECIOUS: %/.exists
%/.exists:
$(MKDIR) $(@D)
touch $@
VPATH = $(base_path) $(OUTDIR)
%.o: %.c
$(OUTDIR)/%.d $(OUTDIR)/%.o: %.c $$(@D)/.exists
$(COMPILE.c) -MMD -o $(@D)/$(*F).o $<
%.o: %.cpp
$(OUTDIR)/%.d $(OUTDIR)/%.o: %.cpp $$(@D)/.exists
$(COMPILE.cc) -MMD -MP -o $(@D)/$(*F).o $<