-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
207 lines (172 loc) · 7.76 KB
/
makefile
File metadata and controls
207 lines (172 loc) · 7.76 KB
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
# MIT License
#
# Copyright (c) 2021 Caio Alves Garcia Prado
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
#############################################################################
#
# Generic makefile intended for C/C++/Fortran projects.
#
# Coupled with makedepend.py, it attempts to automatically setup linking
# dependencies (which .o files are necessary for linking a certain program?)
# without ever having to manually modify the makefile itself.
# Note that header dependencies (which .h files are necessary to compile a
# given .c file) are not dealt with directly and instead are taken care of by
# GCC's -MMD -MP -MF flags.
#
# In order for the setup to work, a couple of conventions must be followed.
# • Name source files containing 'main()' function or 'PROGRAM' subroutine
# as 'main_name.*', which will generate binary files 'bin/name'
# • Put all header files (.h, .hpp, .inc, ...) inside 'include/' directory
# • Put all source files (.c, .cpp, ...) inside 'src/' directory
# • For Fortran only, 'name_*.f' are set as dependencies of 'bin/name'
#
# Targets include:
# cleanbuild delete all temporary files (*.o, *.mod)
# clean cleanbuild + delete binary programs (bin/*)
# cleanall clean + delete dependency information (.d/)
# [default] when called without target, builds every 'main_name.*'
#
#############################################################################
# • Variables {{{1
# - paths {{{2
BASEDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
INCDIR := $(BASEDIR)/include
SRCDIR := $(BASEDIR)/src
DEPDIR := $(BASEDIR)/.d
BINDIR := $(abspath .)/bin
ALLDIRS := $(INCDIR) $(SRCDIR) $(DEPDIR) $(BINDIR)
# - programs {{{2
CC := gcc
CXX := g++
FC := gfortran
LD := g++
MKDEPEND := $(BASEDIR)/makedepend.py
MKDIR := mkdir -p
RM := rm -f
SHELL := /bin/sh
# - functions {{{2
interact := $(if $(strip $(MAKE_TERMOUT)$(MAKE_TERMERR)),1)
color = $(if $(interact),[3$(word $1, 4 6 1)m$2[39m,$2)
bold = $(if $(interact),[1m$1[m,$1)
level = $(if $(MSGLEVEL),$(word $(words $(MSGLEVEL)), ╰─> ├) )
format = $1$(if $2, $(call color,$3,‘$(notdir $2)’))…
msg = $(info $(call bold,$(level)$(call format,$1,$2,$3)))
# - flags {{{2
DEFBASE := -D'BASEDIR=$(BASEDIR)/'
CFLAGS += -O2
CPPFLAGS += -Wall -Wno-misleading-indentation -I$(INCDIR) $(DEFBASE)
CXXFLAGS += -g -O4 -fPIC -flto -pthread -std=c++20 -m64
LDFLAGS += -Wl,--as-needed $(CXXFLAGS)
FFLAGS += -I$(INCDIR) -g -fno-underscoring
FFLAGS += -fbacktrace -ffpe-trap=zero,overflow,underflow,invalid
FPPFLAGS += $(DEFBASE)
LIBS =
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $(basename $<)).d
# - targets {{{2
find_src = $(foreach EXT, $1, $(wildcard $(SRCDIR)/*.$(EXT)))
CSOURCES := $(call find_src, c cpp)
FSOURCES := $(call find_src, f f90 F F90)
SOURCES := $(CSOURCES) $(FSOURCES)
MAINSRC := $(filter $(SRCDIR)/main_%,$(SOURCES))
FMAINSRC := $(filter $(SRCDIR)/main_%,$(FSOURCES))
OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
FORMODS := $(addsuffix .mod,$(basename $(SOURCES)))
DEPFILE := $(DEPDIR)/depfile
DEPENDS := $(patsubst $(SRCDIR)/%,$(DEPDIR)/%,$(OBJECTS:.o=.d)) $(DEPFILE)
TARGETS := $(patsubst $(SRCDIR)/main_%,$(BINDIR)/%,$(basename $(MAINSRC)))
FTARGETS := $(patsubst $(SRCDIR)/main_%,$(BINDIR)/%,$(basename $(FMAINSRC)))
# • General rules {{{1
all: $(TARGETS)
.PHONY: all
# create directories
$(ALLDIRS):
@$(call msg,Create path,$@,1)
@$(MKDIR) $@
# message level
$(TARGETS) $(OBJECTS) $(DEPENDS) $(ALLDIRS): MSGLEVEL+=-
# • Cleaning rules {{{1
# clean intermediate
cleanbuild:
@$(call msg,Clean temporaries)
@$(RM) $(OBJECTS) $(FORMODS)
.PHONY: cleanbuild
# clean intermediate + target
clean: cleanbuild
@$(call msg,Clean targets)
@$(RM) -r $(TARGETS) $(BINDIR)
.PHONY: clean
# clean intermediate + target + dependencies
cleanall: clean
@$(call msg,Clean dependencies)
@$(RM) -r $(DEPDIR)
.PHONY: cleanall
# • Compilation rules {{{1
# linking the final executable
$(TARGETS): $(BINDIR)/%: $(SRCDIR)/main_%.o | $(BINDIR)
@$(call msg,Link executable,$@,3)
@$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
$(FTARGETS): LIBS += -lgfortran
# for Fortran, let's assume that binary 'name' links to all files name_*.o
.SECONDEXPANSION:
SUBPAT := %
$(FTARGETS): $(BINDIR)/%: $$(filter $(SRCDIR)/%_$$(SUBPAT),$(OBJECTS))
# compiling source code: generate .o and .d files from sources
# .d files are created together with .o files: this makes .d always
# updated for next run. (modification time of .d should be before
# .o, in case of gcc)
# .d should not depend on the sources: not necessary, since sources
# changes will force .o to be rebuilt (and in turn, .d to be updated).
# .d files are dependencies of .o files: if user deletes .d, it will
# be rebuilt.
# .d files have empty rule: make will assume they are always created/
# updated instead of complaining it can'd do it.
$(SRCDIR)/%.o: $(SRCDIR)/%.c $(DEPDIR)/%.d
@$(call msg,Compile,$<,2)
@$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
$(SRCDIR)/%.o: $(SRCDIR)/%.cpp $(DEPDIR)/%.d
@$(call msg,Compile,$<,2)
@$(CXX) $(DEPFLAGS) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
# compile fortran code: 4 different file extensions and 2 "flavors"
define fortran
$(SRCDIR)/%.o $(SRCDIR)/%.mod: $(SRCDIR)/%.$1
@$$(call msg,Compile,$$<,2)
@$(FC) $2 $(FFLAGS) -J$(SRCDIR) -o $$(basename $$@).o -c $$<
endef
$(eval $(call fortran,f))
$(eval $(call fortran,f90))
$(eval $(call fortran,F, $(FPPFLAGS)))
$(eval $(call fortran,F90, $(FPPFLAGS)))
# • Dependencies management {{{1
# ensure depdir always exists so make won't ignore recipes depending on it
$(DEPENDS): | $(DEPDIR)
.PRECIOUS: %.d
# create dependency tree for linking final executable
$(DEPFILE): $(MKDEPEND) $(MAINSRC) $(FSOURCES)
@$(call msg,Update dependencies)
@$(MKDEPEND) -I $(INCDIR) -s $(SRCDIR) -o $@ $(MAINSRC) $(FSOURCES)
# if MAKECMDGOALS is not 'clean*' include dependencies to Makefile:
ifneq ($(sort $(patsubst clean%,clean,$(MAKECMDGOALS))),clean)
-include $(wildcard $(DEPENDS)) $(DEPFILE)
# for all relative paths in goals, set as dependency the absolute path file
# this is a hack to make dependencies work when file uses relative path
MAKECMDGOALS := $(filter-out /%,$(MAKECMDGOALS))
$(MAKECMDGOALS): $(abspath $(MAKECMDGOALS)) ;
endif