-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
515 lines (406 loc) · 15.4 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
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
505
506
507
508
509
510
511
512
513
514
515
# GRANIITTI Makefile
#
# (c) 2017-2022 Mikael Mieskolainen.
# Licensed under the MIT License <http://opensource.org/licenses/MIT>.
# -----------------------------------------------------------------------
#
# COMPILING:
# make -j4
#
# CLEANING:
# make clean (objects)
# make superclean (objects + binaries)
#
# To compile with clang: make CXX=clang
# -|- unit tests: make TEST=TRUE
#
# To compile with different c++ standards: make CXX_STANDARD=c++2a (c++17, c++20)
#
# To compile with ROOT using -std=c++17: make CXX_ROOT=c++17
#
# To compile without ROOT make ROOT=
#
# To compile with profiling: make VALGRIND=TRUE
#
# -----------------------------------------------------------------------
#
# EXTERNAL LIBRARIES SETUP:
#
# [HEPMC3] and [LHAPDF]: See ./install folder
# [ROOT] Example: export ROOTSYS=/home/user/local/ROOT6
#
# GENERAL:
#
# If you just installed e.g. HEPMC3 libraries
# $> sudo ldconfig
#
# -----------------------------------------------------------------------
# TIPS:
#
# If you see symbol errors related to HepMC such that:
# "undefined symbol: _ZN5HepMC10FilterBase19init_has_end_vertexEv"
# check that you do not have a collision of HEPMC3 with an existing
# HepMC2 shared library installation!
#
# Check below:
#
# For tracking the libraries of the program:
# $> ldd ./executable
#
# Check if the shared object (.so) file contains the missing symbol:
# $> nm -D libHepMC.so
#
# .......................................................................
#
# Creating shared libraries, use -fPIC flag, then:
# g++ -shared MH1.o -o libMH1.so
#
# .......................................................................
#
# Enable MACRO switches via 'g++ -DNAME', where NAME e.g. DEBUG
#
# .......................................................................
#
# Find out the compiler version of libraries (works with gcc, at least)
#
# $> readelf -p .comment ./obj/MGraniitti.o
#
#
# -----------------------------------------------------------------------
# USE [TABS] for intendation while modifying this file!
# -----------------------------------------------------------------------
# =======================================================================
# Detect compiler version if using by default g++
ifeq ($(CXX),g++)
# KERNEL_GCC_VERSION
# := $(shell cat /proc/version | sed -n 's/.*gcc version \([[:digit:]]\.[[:digit:]]\.[[:digit:]]\).*/\1/p')
# $(info Info KERNEL_GCC_VERSION = $(KERNEL_GCC_VERSION))
CXX_VERSION = $(shell g++ -dumpversion)
$(info Found CXX_VERSION = $(CXX_VERSION))
CXX_REQUIRED = 7.0
# Use bc command for double comparison, a common utility on Unix platforms
FOO=$(shell echo "$(CXX_VERSION) < $(CXX_REQUIRED)" | bc)
ifeq ($(FOO),1)
$(error Your CXX_VERSION is too old, need at least CXX_VERSION = $(CXX_REQUIRED), update your GCC chain)
endif
endif
# =======================================================================
# PATH setup
# HEPMC3 installation
#HEPMC3SYS = $(shell HepMC-config --prefix)
# LHAPDF installation
#LHAPDFSYS = $(shell lhapdf-config --prefix)
# ROOT installation
#ROOTSYS = $(shell root-config --prefix)
ifeq ($(HEPMC3SYS),)
$(error Please set HEPMC3SYS environment variable and paths [perhaps 'source ./install/setenv.sh'])
else
$(info Found HEPMC3SYS = $(HEPMC3SYS))
endif
ifeq ($(LHAPDFSYS),)
$(error Please set LHAPDFSYS environment variable and paths [perhaps 'source ./install/setenv.sh'])
else
$(info Found LHAPDFSYS = $(LHAPDFSYS))
endif
# If ROOT seems to be installed, tag ROOT dependent compilation on
ifeq ($(ROOTSYS),)
$(info Did not find ROOTSYS - compilation without ROOT <root.cern.ch> dependent tools)
ROOT=FALSE
else
$(info Found ROOTSYS = $(ROOTSYS))
ROOT=TRUE
# Info messages
$(info )
$(info ************************************************************************************)
$(info ** **)
$(info ** If compilation with ROOT libraries failes, try with: make -j4 CXX_ROOT=c++17 **)
$(info ** **)
$(info ************************************************************************************)
$(info )
endif
ifeq ($(LIBTORCHSYS),)
$(info Did not find LIBTORCHSYS - compilation without <libtorch> dependent tools)
LIBTORCH=FALSE
else
$(info Found LIBTORCHSYS = $(LIBTORCHSYS))
LIBTORCH=TRUE
endif
# =======================================================================
# External libraries to be linked
# HEPMC3 (lib64 needed on some systems)
HEPMC3lib = -L$(HEPMC3SYS)/lib -L$(HEPMC3SYS)/lib64 -lHepMC3 -lHepMC3search
# LHAPDF6 (lib64 needed on some systems)
LHAPDF6lib = -L$(LHAPDFSYS)/lib -L$(LHAPDFSYS)/lib64 -lLHAPDF
# LIBTORCH
# Note -Wl,-R handles the recursive dependency (for linker)
ifeq ($(LIBTORCH),TRUE)
LIBTORCHlib = -L${LIBTORCHSYS}/lib \
-Wl,-R${LIBTORCHSYS}/lib \
-ltorch -ltorch_cpu -lc10 -lgomp
endif
# ROOT
ifeq ($(ROOT),TRUE)
ROOTlib = -L$(ROOTSYS)/lib -L$(ROOTSYS)/lib/root -lCore -lRIO -lNet \
-lHist -lGraf -lGraf3d -lGpad -lTree -lRint \
-lPostscript -lMatrix -lPhysics -lMathCore \
-lThread -lGui -lRooFit -lMinuit
endif
# C++ standard
STANDARDlib = -lstdc++ -lm -pthread -lrt
# -ldl -rdynamic
# GSL, on ubuntu run: sudo apt-get install libgsl-dev
#GSLlib = -lgsl -lgslcblas
# External libraries (THESE TWO FIRST for priority!!)
LDLIBS = $(HEPMC3lib)
LDLIBS += $(LHAPDF6lib)
# The rest
LDLIBS += $(STANDARDlib)
# LIBTORCH
ifeq ($(LIBTORCH),TRUE)
LDLIBS += $(LIBTORCHlib)
endif
# BLAS AND LAPACK
#LDLIBS += -llapack -lblas
# =======================================================================
# Header files
# External libraries (THESE TWO FIRST for priority!!)
INCLUDES += -I$(HEPMC3SYS)/include
INCLUDES += -I$(LHAPDFSYS)/include
# C++
INCLUDES += -I/usr/include
INCLUDES += -I/usr/local/include
# Own
INCLUDES += -I.
INCLUDES += -Iinclude
# Internal libraries
INCLUDES += -Ilibs
# FTensor
INCLUDES += -Ilibs/FTensor
# Armadillo
#INCLUDES += -Ilibs/armadillo
# Ensmallen
#INCLUDES += -Ilibs/ensmallen
# Eigen
INCLUDES += -Ilibs/Eigen
INCLUDES += -Ilibs/Eigen/unsupported
# optimlib
#INCLUDES += -Ilibs/optim
# LIBTORCH
ifeq ($(LIBTORCH),TRUE)
INCLUDES += -I${LIBTORCHSYS}/include
INCLUDES += -I${LIBTORCHSYS}/include/torch/csrc/api/include
endif
# =======================================================================
# Compiler, language version and its options
CXX=g++
# Ubuntu 20.1 GCC 9.3 + pre-compiled ROOT 6.18 for Ubuntu (from root.cern.ch) tested default
ifneq ($(CXX_STANDARD),)
CXXVER = -std=$(CXX_STANDARD)
else
CXXVER = -std=c++2a
endif
# Needed for ROOT library
ifneq ($(CXX_ROOT),)
CXXVER_OLD = -std=$(CXX_ROOT)
else
CXXVER_OLD = -std=c++14
endif
OPTIM = -O3 -DNDEBUG -ftree-vectorize -fno-signed-zeros
CXXFLAGS = -Wall -fPIC -pipe $(OPTIM)
# Profiling & debug
ifeq ($(VALGRIND),TRUE)
OPTIM += -pg
endif
# Automatic dependencies on
CXXFLAGS += -MMD -MP
# -Wall, compiler warnings full on
# -free-vectorize, Autovectorization on
# -fno-signed-zeros Optimization (floating point) which ignore the signedness of zero
# -fPIC, Position independent code (PIC) for shared libraries
# -O2, -O3 Optimization level
# -DNDEBUG Release flag, no debug
# -pipe, Faster compilation using pipes
# -O0 -pg, Profiling and debugging (HUGE PERFORMANCE HIT)
# -rpath-link Needed for .so which links to another .so
#
# Fortran like complex number treatments (faster than C++)
# -fcx-fortran-rules -fcx-limited-range
#
# Check your CPU instruction set with: cat /proc/cpuinfo
# DANGEROUS:
# -march=native, CPU spesific instruction set usage
# (gives unknown flops problem, factor 1/4 wrong results on i5-4570 with g++7.4, do not use!)
# -ffast-math, Heavy floating point optimization
# (fast but breaks IEEE flops standards, do not use!)
# =======================================================================
# Sources, objects and dependency files
OBJ_DIR = obj
# -----------------------------------------------------------------------
# LIBRARY
## ======================================================================
SRC_DIR_0 = src
SRC_0 = $(wildcard $(SRC_DIR_0)/*.cc)
OBJ_0 = $(SRC_0:$(SRC_DIR_0)/%.cc=$(OBJ_DIR)/%.o)
DEP_0 = $(OBJ_0:$(OBJ_DIR)/%.o=.d)
## ======================================================================
## ======================================================================
SRC_DIR_1 = src/Amplitude
SRC_1 = $(wildcard $(SRC_DIR_1)/*.cc)
OBJ_1 = $(SRC_1:$(SRC_DIR_1)/%.cc=$(OBJ_DIR)/%.o)
DEP_1 = $(OBJ_1:$(OBJ_DIR)/%.o=.d)
## ======================================================================
# Create collection of all library objects
OBJ = $(OBJ_0) $(OBJ_1)
# =======================================================================
ifeq ($(ROOT),TRUE)
SRC_DIR_2 = src/Analysis
SRC_2 = $(wildcard $(SRC_DIR_2)/*.cc)
OBJ_2 = $(SRC_2:$(SRC_DIR_2)/%.cc=$(OBJ_DIR)/%.o)
DEP_2 = $(OBJ_2:$(OBJ_DIR)/%.o=.d)
endif
# =======================================================================
# =======================================================================
ifeq ($(TEST),TRUE)
SRC_DIR_3 = tests/catchlibrary
SRC_3 = $(wildcard $(SRC_DIR_3)/*.cc)
OBJ_3 = $(SRC_3:$(SRC_DIR_3)/%.cc=$(OBJ_DIR)/%.o)
DEP_3 = $(OBJ_3:$(OBJ_DIR)/%.o=.d)
endif
# =======================================================================
# -----------------------------------------------------------------------
# PROGRAM compiled against the library
# =======================================================================
SRC_DIR_PROGRAM = src/Program
SRC_PROGRAM = $(wildcard $(SRC_DIR_PROGRAM)/*.cc)
OBJ_PROGRAM = $(SRC_PROGRAM:$(SRC_DIR_PROGRAM)/%.cc=$(OBJ_DIR)/$(BIN_DIR)/%.o)
DEP_PROGRAM = $(OBJ_PROGRAM:$(OBJ_DIR)/$(BIN_DIR)/%.o=.d)
# =======================================================================
# =======================================================================
ifeq ($(ROOT),TRUE)
SRC_DIR_PROGRAM_ROOT = src/Program/Analysis
SRC_PROGRAM_ROOT = $(wildcard $(SRC_DIR_PROGRAM_ROOT)/*.cc)
OBJ_PROGRAM_ROOT = $(SRC_PROGRAM_ROOT:$(SRC_DIR_PROGRAM_ROOT)/%.cc=$(OBJ_DIR)/$(BIN_DIR)/%.o)
DEP_PROGRAM_ROOT = $(OBJ_PROGRAM_ROOT:$(OBJ_DIR)/$(BIN_DIR)/%.o=.d)
endif
# =======================================================================
# =======================================================================
ifeq ($(TEST),TRUE)
SRC_DIR_PROGRAM_TEST = tests
SRC_PROGRAM_TEST = $(wildcard $(SRC_DIR_PROGRAM_TEST)/*.cc)
OBJ_PROGRAM_TEST = $(SRC_PROGRAM_TEST:$(SRC_DIR_PROGRAM_TEST)/%.cc=$(OBJ_DIR)/$(BIN_DIR)/%.o)
DEP_PROGRAM_TEST = $(OBJ_PROGRAM_TEST:$(OBJ_DIR)/$(BIN_DIR)/%.o=.d)
endif
# =======================================================================
# -----------------------------------------------------------------------
# PROGRAM
# Directory
BIN_DIR = bin
.SUFFIXES: .o .cc
# Normal
EXE_NAMES = gr xscan minbias hepmc3tolhe data2hepmc3 pathmark pdebench sommerfeld ot
PROGRAM = $(EXE_NAMES:%=$(BIN_DIR)/%)
ifeq ($(ROOT),TRUE)
EXE_ROOT_NAMES = analyze fitsoft fitharmonic
PROGRAM_ROOT = $(EXE_ROOT_NAMES:%=$(BIN_DIR)/%)
endif
PROGRAM_TEST =
ifeq ($(TEST),TRUE)
EXE_TEST_NAMES = testbench0 testbench1 testbench2 testbench3 testbench4
PROGRAM_TEST = $(EXE_TEST_NAMES:%=$(BIN_DIR)/%)
endif
# Multicore
#export MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)"
# -----------------------------------------------------------------------
# RULES for linking
all: $(PROGRAM) $(PROGRAM_ROOT) $(PROGRAM_TEST)
@echo " "
@echo "PROGRAM:" $(PROGRAM) $(PROGRAM_ROOT) $(PROGRAM_TEST)
@echo " "
@echo "Compilation of '$@' done."
$(PROGRAM): $(OBJ) $(OBJ_PROGRAM)
$(CXX) $(OBJ_DIR)/[email protected] $(OBJ) -o $@ $(CXXFLAGS) $(LDLIBS)
$(PROGRAM_ROOT): $(OBJ) $(OBJ_2) $(OBJ_PROGRAM_ROOT)
$(CXX) $(OBJ_DIR)/[email protected] $(OBJ) $(OBJ_2) -o $@ $(CXXFLAGS) $(LDLIBS) $(ROOTlib)
# Unit tests (note, we use catchmain.o from $(OBJ_3) for linking with catch2)
$(PROGRAM_TEST): $(OBJ) $(OBJ_3) $(OBJ_PROGRAM_TEST)
$(CXX) $(OBJ_DIR)/[email protected] $(OBJ) $(OBJ_3) -o $@ $(CXXFLAGS) $(LDLIBS)
# -----------------------------------------------------------------------
# RULES to generate library dependencies and compile
# LIBRARY objects
#
# Note that for different ROOT installations, we need both:
# -I$(ROOTSYS)/include
# -I$(ROOTSYS)/include/root
# =======================================================================
$(OBJ_DIR)/%.o: $(SRC_DIR_0)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER) -c $< -o $@ $(CXXFLAGS) $(INCLUDES)
# =======================================================================
# =======================================================================
$(OBJ_DIR)/%.o: $(SRC_DIR_1)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER) -c $< -o $@ $(CXXFLAGS) $(INCLUDES)
# =======================================================================
# =======================================================================
$(OBJ_DIR)/%.o: $(SRC_DIR_2)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER_OLD) -c $< -o $@ $(CXXFLAGS) $(INCLUDES) \
-I$(ROOTSYS)/include -I$(ROOTSYS)/include/root
# =======================================================================
# =======================================================================
$(OBJ_DIR)/%.o: $(SRC_DIR_3)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER) -c $< -o $@ $(CXXFLAGS) $(INCLUDES)
# =======================================================================
# PROGRAM objects
# =======================================================================
$(OBJ_DIR)/$(BIN_DIR)/%.o: $(SRC_DIR_PROGRAM)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER) -c $< -o $@ $(CXXFLAGS) $(INCLUDES)
# =======================================================================
# =======================================================================
$(OBJ_DIR)/$(BIN_DIR)/%.o: $(SRC_DIR_PROGRAM_ROOT)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER_OLD) -c $< -o $@ $(CXXFLAGS) $(INCLUDES) \
-I$(ROOTSYS)/include -I$(ROOTSYS)/include/root
# =======================================================================
# =======================================================================
$(OBJ_DIR)/$(BIN_DIR)/%.o: $(SRC_DIR_PROGRAM_TEST)/%.cc
@echo " "
@echo "Generating dependencies and compiling $<..."
$(CXX) $(CXXVER) -c $< -o $@ $(CXXFLAGS) $(INCLUDES)
# =======================================================================
# -----------------------------------------------------------------------
# Clean up of object files
# - ignores return code error
# @ is silent
.PHONY: clean
clean:
@echo "Cleaning objects"
-@rm $(OBJ_DIR)/*.o 2>/dev/null || true
-@rm $(OBJ_DIR)/*.d 2>/dev/null || true
-@rm $(OBJ_DIR)/$(BIN_DIR)/*.o 2>/dev/null || true
-@rm $(OBJ_DIR)/$(BIN_DIR)/*.d 2>/dev/null || true
.PHONY: superclean
superclean: clean
@echo "Cleaning binaries"
-@rm $(BIN_DIR)/* 2>/dev/null || true
# -----------------------------------------------------------------------
# Dependencies listed here
-include $(DEP_0)
-include $(DEP_1)
ifeq ($(ROOT),TRUE)
-include $(DEP_2)
endif
ifeq ($(TEST),TRUE)
-include $(DEP_3)
endif
# DEBUG printing
#print_vars:
# echo FOO = ${OBJ}