forked from libocca/occa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
300 lines (250 loc) · 11.3 KB
/
Makefile
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
rmSlash = $(patsubst %/,%,$1)
OCCA_DIR := $(call rmSlash,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
PROJ_DIR := $(OCCA_DIR)
# Clear the list of suffixes
.SUFFIXES:
.SUFFIXES: .hpp .h .tpp .cpp .mm .o .so .dylib
# Cancel some implicit rules
%: %.o
%.o: %.cpp
%: %.cpp
%: %.mm
include $(OCCA_DIR)/scripts/build/Makefile
#---[ Working Paths ]-----------------------------
ifeq ($(usingWinux),0)
compilerFlags += $(picFlag)
fCompilerFlags += $(picFlag)
else
sharedFlag += $(picFlag)
endif
# [-L$OCCA_DIR/lib -locca] are kept for examples using $OCCA_DIR/scripts/build/Makefile
paths := $(filter-out -L$(OCCA_DIR)/lib,$(paths))
linkerFlags := $(filter-out -locca,$(linkerFlags))
ifeq (${PREFIX},)
OCCA_BUILD_DIR := $(OCCA_DIR)
else
OCCA_BUILD_DIR := ${PREFIX}
endif
#=================================================
#---[ Flags ]-------------------------------------
# Expand and overwrite flag variables to avoid
# multiple expansions which are slow.
compilerFlags := $(compilerFlags)
flags := $(flags)
sharedFlag := $(sharedFlag)
pthreadFlag := $(pthreadFlag)
#=================================================
#---[ Compilation Variables ]---------------------
srcToObject = $(subst $(PROJ_DIR)/src,$(PROJ_DIR)/obj,$(1:.cpp=.o))
sources = $(realpath $(shell find $(PROJ_DIR)/src -type f -name '*.cpp'))
headers = $(realpath $(shell find $(PROJ_DIR)/include -type f -name '*.hpp' -o -name "*.h"))
testSources = $(realpath $(shell find $(PROJ_DIR)/tests/src -type f -name '*.cpp'))
testSources := $(filter-out $(testPath)/src/fortran/%.cpp,$(testSources))
tests = $(subst $(testPath)/src,$(testPath)/bin,$(testSources:.cpp=))
objects = $(call srcToObject,$(sources))
# Only compile Objective-C++ if Metal is enabled
ifeq ($(metalEnabled),1)
objcSrcToObject = $(subst $(PROJ_DIR)/src,$(PROJ_DIR)/obj,$(1:.mm=.o))
objcSources = $(realpath $(shell find $(PROJ_DIR)/src -type f -name '*.mm'))
objects += $(call objcSrcToObject,$(objcSources))
endif
outputs = $(libPath)/libocca.$(soExt) $(binPath)/occa
# Add Fortran lib and tests
ifeq ($(fortranEnabled),1)
fPaths += $(fModuleDirFlag)$(modPath)
fPaths := $(filter-out -I$(modPath),$(fPaths))
outputs += $(libPath)/libocca_fortran.$(soExt)
tests += $(fTests)
endif
ifdef SANITIZER_ENABLED
testFlags = $(compilerFlags) -fsanitize=address -fno-omit-frame-pointer
else
testFlags = $(compilerFlags)
endif
#=================================================
PLACE_GENERATED_CODES := $(shell mkdir -p $(OCCA_DIR)/include/occa/core/codegen | \
mkdir -p $(OCCA_DIR)/src/core/codegen | \
mkdir -p $(OCCA_DIR)/src/occa/internal/utils/codegen | \
mkdir -p $(OCCA_DIR)/include/occa/defines/codegen)
PLACE_GENERATED_CODES := $(shell cp $(OCCA_DIR)/scripts/codegen/kernelOperators.hpp_codegen.in $(OCCA_DIR)/include/occa/core/codegen/kernelOperators.hpp_codegen | \
cp $(OCCA_DIR)/scripts/codegen/kernelOperators.cpp_codegen.in $(OCCA_DIR)/src/core/codegen/kernelOperators.cpp_codegen | \
cp $(OCCA_DIR)/scripts/codegen/runFunction.cpp_codegen.in $(OCCA_DIR)/src/occa/internal/utils/codegen/runFunction.cpp_codegen | \
cp $(OCCA_DIR)/scripts/codegen/macros.hpp_codegen.in $(OCCA_DIR)/include/occa/defines/codegen/macros.hpp_codegen)
#---[ Compile Library ]---------------------------
# Setup compiled defines and force rebuild if defines changed
COMPILED_DEFINES := $(OCCA_DIR)/include/occa/defines/compiledDefines.hpp
NEW_COMPILED_DEFINES := $(OCCA_DIR)/.compiledDefines
MAKE_COMPILED_DEFINES := $(shell cat "$(OCCA_DIR)/scripts/build/compiledDefinesTemplate.hpp" | \
sed "s,@@OCCA_OS@@,$(OCCA_OS),g;\
s,@@OCCA_USING_VS@@,$(OCCA_USING_VS),g;\
s,@@OCCA_UNSAFE@@,$(OCCA_UNSAFE),g;\
s,@@OCCA_OPENMP_ENABLED@@,$(OCCA_OPENMP_ENABLED),g;\
s,@@OCCA_CUDA_ENABLED@@,$(OCCA_CUDA_ENABLED),g;\
s,@@OCCA_HIP_ENABLED@@,$(OCCA_HIP_ENABLED),g;\
s,@@OCCA_OPENCL_ENABLED@@,$(OCCA_OPENCL_ENABLED),g;\
s,@@OCCA_METAL_ENABLED@@,$(OCCA_METAL_ENABLED),g;\
s,@@OCCA_DPCPP_ENABLED@@,$(OCCA_DPCPP_ENABLED),g;\
s,@@OCCA_THREAD_SHARABLE_ENABLED@@,$(OCCA_THREAD_SHARABLE_ENABLED),g;\
s,@@OCCA_MAX_ARGS@@,$(OCCA_MAX_ARGS),g;\
s,@@OCCA_BUILD_DIR@@,$(OCCA_BUILD_DIR),g;"\
> "$(NEW_COMPILED_DEFINES)")
MAKE_COMPILED_DEFINES := $(shell \
[ ! -f "$(COMPILED_DEFINES)" -o -n "$(shell diff -q $(COMPILED_DEFINES) $(NEW_COMPILED_DEFINES))" ] \
&& cp "$(NEW_COMPILED_DEFINES)" "$(COMPILED_DEFINES)" \
)
MAKE_COMPILED_DEFINES := $(shell rm $(NEW_COMPILED_DEFINES))
all: $(objects) $(outputs)
@(. $(OCCA_DIR)/scripts/build/shellTools.sh && installOcca)
@echo "> occa info"
# Don't break the build if [occa info] fails
@$(OCCA_DIR)/bin/occa info; true
$(COMPILED_DEFINES_CHANGED):
#=================================================
#---[ Builds ]------------------------------------
# ---[ libocca ]-------------
$(libPath)/libocca.$(soExt): $(objects) $(headers) $(COMPILED_DEFINES)
@mkdir -p $(libPath)
@echo " - [lib] $@"
@$(compiler) $(compilerFlags) $(sharedFlag) $(pthreadFlag) $(soNameFlag) -o "$@" $(flags) $(objects) $(paths) $(linkerFlags)
$(libPath)/libocca_fortran.$(soExt): $(fObjects) $(libPath)/libocca.$(soExt)
@mkdir -p $(libPath)
@echo " - [lib] $@"
@$(fCompiler) $(fCompilerFlags) $(sharedFlag) $(pthreadFlag) $(fSoNameFlag) -o "$@" $^ $(flags) $(fPaths) $(filter-out -locca_fortran, $(fLinkerFlags))
$(binPath)/occa: $(OCCA_DIR)/bin/occa.cpp $(libPath)/libocca.$(soExt) $(COMPILED_DEFINES_CHANGED)
@mkdir -p $(binPath)
@echo " - [bin] $@"
@$(compiler) $(compilerFlags) -o $(binPath)/occa -Wl,-rpath,$(libPath) $(flags) $(OCCA_DIR)/bin/occa.cpp $(paths) $(linkerFlags) -L$(OCCA_DIR)/lib -locca
# ===========================
# ---[ Source ]--------------
# C++
$(OCCA_DIR)/obj/%.o: $(OCCA_DIR)/src/%.cpp \
$(wildcard $(OCCA_DIR)/src/%.hpp) \
$(wildcard $(OCCA_DIR)/src/%.tpp) \
$(wildcard $(OCCA_DIR)/include/%.hpp) \
$(wildcard $(OCCA_DIR)/include/%.tpp) \
$(wildcard $(OCCA_DIR)/src/%.h) \
$(wildcard $(OCCA_DIR)/include/%.h) \
$(COMPILED_DEFINES_CHANGED)
@mkdir -p $(abspath $(dir $@))
@echo " - [C++] $<"
@$(compiler) $(compilerFlags) -o "$@" $(flags) -c $(paths) "$<"
# Objective-C++
$(OCCA_DIR)/obj/%.o: $(OCCA_DIR)/src/%.mm \
$(wildcard $(OCCA_DIR)/src/%.hpp) \
$(wildcard $(OCCA_DIR)/include/%.hpp) \
$(COMPILED_DEFINES_CHANGED)
@mkdir -p $(abspath $(dir $@))
@echo " - [ObjC] $<"
@clang++ -x objective-c++ $(cpp11Flags) -o "$@" $(flags) -c $(paths) "$<"
# Fortran
include $(OCCA_DIR)/scripts/build/Make.fortran_rules
$(OCCA_DIR)/obj/%.o: $(OCCA_DIR)/src/%.f90
@mkdir -p $(modPath)
@mkdir -p $(abspath $(dir $@))
@echo " - [F90] $<"
@$(fCompiler) $(fCompilerFlags) -o "$@" $(flags) -c $(fPaths) "$<"
# ===========================
#=================================================
#---[ Testing ]-----------------------------------
tests: $(tests)
test: unit-tests e2e-tests bin-tests
unit-tests: $(tests)
@$(testPath)/run_tests
@if [ $$? -ne 0 ]; then \
@exit 1; \
fi
e2e-tests: unit-tests
@FORTRAN_EXAMPLES=$(fortranEnabled) $(testPath)/run_examples
@if [ $$? -ne 0 ]; then \
@exit 1; \
fi
bin-tests: e2e-tests
@$(testPath)/run_bin_tests
@if [ $$? -ne 0 ]; then \
@exit 1; \
fi
$(testPath)/bin/%:$(testPath)/src/%.cpp $(outputs)
@mkdir -p $(abspath $(dir $@))
@echo " - [C++] $<"
@$(compiler) $(testFlags) $(pthreadFlag) -o "$@" -Wl,-rpath,$(libPath) $(flags) "$<" $(paths) $(linkerFlags) -L$(OCCA_DIR)/lib -locca
# ---[ Fortran ]-------------
fTests: $(fTests)
$(objPath)/%.o: $(testPath)/src/fortran/typedefs_helper.cpp
@mkdir -p $(abspath $(dir $@))
@echo " - [F90] $<"
@$(compiler) $(compilerFlags) -o "$@" $(flags) -c $(paths) "$<"
$(testPath)/bin/fortran/typedefs: $(objPath)/tests/fortran/typedefs_helper.o
$(testPath)/bin/%: $(testPath)/src/%.f90 $(libPath)/libocca_fortran.$(soExt)
@mkdir -p $(abspath $(dir $@))
@echo " - [F90] $<"
@$(fCompiler) $(fCompilerFlags) -o "$@" $^ -Wl,-rpath,$(libPath) $(flags) $(fPaths) $(fLinkerFlags)
# ===========================
#=================================================
#---[ Clean ]-------------------------------------
clean:
rm -rf $(objPath)/*
rm -rf $(modPath)/*
rm -rf $(binPath)/occa
rm -rf $(testPath)/bin
rm -rf $(testPath)/src/io/locks
rm -f $(libPath)/libocca.$(soExt)
rm -f $(libPath)/libocca_fortran.$(soExt)
#=================================================
#---[ Info ]--------------------------------------
info:
$(info --------------------------------)
$(info CXX = $(or $(CXX),(empty)))
$(info CXXFLAGS = $(or $(CXXFLAGS),(empty)))
$(info CC = $(or $(CC),(empty)))
$(info CFLAGS = $(or $(CFLAGS),(empty)))
$(info FC = $(or $(FC),(empty)))
$(info FFLAGS = $(or $(FFLAGS),(empty)))
$(info LDFLAGS = $(or $(LDFLAGS),(empty)))
$(info --------------------------------)
$(info compiler = $(value compiler))
$(info vendor = $(vendor))
$(info compilerFlags = $(compilerFlags))
$(info cCompiler = $(value cCompiler))
$(info cCompilerFlags = $(cCompilerFlags))
$(info fCompiler = $(value fCompiler))
$(info fCompilerFlags = $(fCompilerFlags))
$(info flags = $(flags))
$(info paths = $(paths))
$(info fPaths = $(fPaths))
$(info sharedFlag = $(sharedFlag))
$(info pthreadFlag = $(pthreadFlag))
$(info linkerFlags = $(linkerFlags))
$(info --------------------------------)
$(info debugEnabled = $(debugEnabled))
$(info checkEnabled = $(checkEnabled))
$(info debugFlags = $(debugFlags))
$(info releaseFlags = $(releaseFlags))
$(info picFlag = $(picFlag))
$(info --------------------------------)
$(info fortranEnabled = $(fortranEnabled))
$(info mpiEnabled = $(mpiEnabled))
$(info openmpEnabled = $(openmpEnabled))
$(info openclEnabled = $(openclEnabled))
$(info metalEnabled = $(metalEnabled))
$(info cudaEnabled = $(cudaEnabled))
$(info hipEnabled = $(hipEnabled))
$(info dpcppEnabled = $(dpcppEnabled))
$(info --------------------------------)
@true
#=================================================
#---[ Print ]-------------------------------------
# Print the contents of a makefile variable, e.g.: 'make print-compiler'.
print-%:
$(info [ variable name]: $*)
$(info [ origin]: $(origin $*))
$(info [ value]: $(value $*))
$(info [expanded value]: $($*))
$(info )
@true
#=================================================
#---[ Documentation ]-----------------------------
# Auto-generate the API documentation
.PHONY: docs
docs:
@${OCCA_DIR}/scripts/docs/api-docgen
#=================================================