|
| 1 | +# Copyright (C) 2020-2024 CERN and UCLouvain. |
| 2 | +# Licensed under the GNU Lesser General Public License (version 3 or later). |
| 3 | +# Created by: S. Roiser (Feb 2020) for the MG5aMC CUDACPP plugin. |
| 4 | +# Further modified by: S. Hageboeck, O. Mattelaer, S. Roiser, J. Teig, A. Valassi, Z. Wettersten (2020-2024) for the MG5aMC CUDACPP plugin. |
| 5 | + |
| 6 | +#=== Determine the name of this makefile (https://ftp.gnu.org/old-gnu/Manuals/make-3.80/html_node/make_17.html) |
| 7 | +#=== NB: assume that the same name (e.g. cudacpp.mk, Makefile...) is used in the Subprocess and src directories |
| 8 | + |
| 9 | +THISMK = $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) |
| 10 | + |
| 11 | +#------------------------------------------------------------------------------- |
| 12 | + |
| 13 | +#=== Use bash in the Makefile (https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html) |
| 14 | + |
| 15 | +SHELL := /bin/bash |
| 16 | + |
| 17 | +#------------------------------------------------------------------------------- |
| 18 | + |
| 19 | +#=== Configure common compiler flags for CUDA and C++ |
| 20 | + |
| 21 | +INCFLAGS = -I. |
| 22 | + |
| 23 | +#------------------------------------------------------------------------------- |
| 24 | + |
| 25 | +#=== Configure the C++ compiler (note: CXXFLAGS has been exported from cudacpp.mk) |
| 26 | + |
| 27 | +###$(info CXXFLAGS=$(CXXFLAGS)) |
| 28 | + |
| 29 | +# Note: AR, CXX and FC are implicitly defined if not set externally |
| 30 | +# See https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html |
| 31 | +###RANLIB = ranlib |
| 32 | + |
| 33 | +# Add -mmacosx-version-min=11.3 to avoid "ld: warning: object file was built for newer macOS version than being linked" |
| 34 | +LDFLAGS = -L. -lrex -ltearex -Wl,-rpath=. |
| 35 | +ifneq ($(shell $(CXX) --version | egrep '^Apple clang'),) |
| 36 | + LDFLAGS += -mmacosx-version-min=11.3 |
| 37 | +endif |
| 38 | + |
| 39 | +#------------------------------------------------------------------------------- |
| 40 | + |
| 41 | +#=== Configure the GPU (CUDA or HIP) compiler (note: GPUCC including ccache, GPUFLAGS, GPULANGUAGE, GPUSUFFIX have been exported from cudacpp.mk) |
| 42 | + |
| 43 | +###$(info GPUCC=$(GPUCC)) |
| 44 | +###$(info GPUFLAGS=$(GPUFLAGS)) |
| 45 | +###$(info GPULANGUAGE=$(GPULANGUAGE)) |
| 46 | +###$(info GPUSUFFIX=$(GPUSUFFIX)) |
| 47 | + |
| 48 | +#------------------------------------------------------------------------------- |
| 49 | + |
| 50 | +#=== Configure ccache for C++ builds (note: GPUCC has been exported from cudacpp.mk including ccache) |
| 51 | + |
| 52 | +# Enable ccache if USECCACHE=1 |
| 53 | +ifeq ($(USECCACHE)$(shell echo $(CXX) | grep ccache),1) |
| 54 | + override CXX:=ccache $(CXX) |
| 55 | +endif |
| 56 | +#ifeq ($(USECCACHE)$(shell echo $(AR) | grep ccache),1) |
| 57 | +# override AR:=ccache $(AR) |
| 58 | +#endif |
| 59 | + |
| 60 | +#------------------------------------------------------------------------------- |
| 61 | + |
| 62 | +#=== Configure build directories and build lockfiles === |
| 63 | + |
| 64 | +# Use the build directory exported from cudacpp.mk |
| 65 | +###$(info CUDACPP_BUILDDIR=$(CUDACPP_BUILDDIR)) |
| 66 | + |
| 67 | +# Use the build lockfile "full" tag exported from cudacpp.mk |
| 68 | +###$(info TAG=$(TAG)) |
| 69 | + |
| 70 | +# Build directory: current directory by default, or build.$(DIRTAG) if USEBUILDDIR==1 |
| 71 | +###$(info Current directory is $(shell pwd)) |
| 72 | +override BUILDDIR = $(CUDACPP_BUILDDIR) |
| 73 | +ifeq ($(USEBUILDDIR),1) |
| 74 | + override LIBDIRREL = ../lib/$(BUILDDIR) |
| 75 | + ###$(info Building in BUILDDIR=$(BUILDDIR) for tag=$(TAG) (USEBUILDDIR=1 is set)) |
| 76 | +else |
| 77 | + override LIBDIRREL = ../lib |
| 78 | + ###$(info Building in BUILDDIR=$(BUILDDIR) for tag=$(TAG) (USEBUILDDIR is not set)) |
| 79 | +endif |
| 80 | +######$(info Building in BUILDDIR=$(BUILDDIR) for tag=$(TAG)) |
| 81 | + |
| 82 | +# Workaround for Mac #375 (I did not manage to fix rpath with @executable_path): use absolute paths for LIBDIR |
| 83 | +# (NB: this is quite ugly because it creates the directory if it does not exist - to avoid removing src by mistake) |
| 84 | +UNAME_S := $(shell uname -s) |
| 85 | +ifeq ($(UNAME_S),Darwin) |
| 86 | + override LIBDIR = $(shell mkdir -p $(LIBDIRREL); cd $(LIBDIRREL); pwd) |
| 87 | + ifeq ($(wildcard $(LIBDIR)),) |
| 88 | + $(error Directory LIBDIR="$(LIBDIR)" should have been created by now) |
| 89 | + endif |
| 90 | +else |
| 91 | + override LIBDIR = $(LIBDIRREL) |
| 92 | +endif |
| 93 | + |
| 94 | +#=============================================================================== |
| 95 | +#=== Makefile TARGETS and build rules below |
| 96 | +#=============================================================================== |
| 97 | + |
| 98 | +# NB1: there are no CUDA targets in src as we avoid RDC! |
| 99 | +# NB2: CUDA includes for curand.h are no longer needed in the C++ code anywhere in src! |
| 100 | + |
| 101 | +ifeq ($(GPUCC),) |
| 102 | +MG5AMC_COMMONLIB = mg5amc_common_cpp |
| 103 | +else |
| 104 | +MG5AMC_COMMONLIB = mg5amc_common_$(GPUSUFFIX) |
| 105 | +endif |
| 106 | + |
| 107 | +# Explicitly define the default goal (this is not necessary as it is the first target, which is implicitly the default goal) |
| 108 | +.DEFAULT_GOAL := all.$(TAG) |
| 109 | + |
| 110 | +# First target (default goal) |
| 111 | +all.$(TAG): $(BUILDDIR)/.build.$(TAG) $(LIBDIR)/.build.$(TAG) $(LIBDIR)/lib$(MG5AMC_COMMONLIB).so $(LIBDIR)/librex.so $(LIBDIR)/libtearex.so |
| 112 | + |
| 113 | +# Target (and build options): debug |
| 114 | +debug: all.$(TAG) |
| 115 | + |
| 116 | +# Target: tag-specific build lockfiles |
| 117 | +override oldtagsb=`if [ -d $(BUILDDIR) ]; then find $(BUILDDIR) -maxdepth 1 -name '.build.*' ! -name '.build.$(TAG)' -exec echo $(shell pwd)/{} \; ; fi` |
| 118 | +override oldtagsl=`if [ -d $(LIBDIR) ]; then find $(LIBDIR) -maxdepth 1 -name '.build.*' ! -name '.build.$(TAG)' -exec echo $(shell pwd)/{} \; ; fi` |
| 119 | + |
| 120 | +$(BUILDDIR)/.build.$(TAG): $(LIBDIR)/.build.$(TAG) |
| 121 | + |
| 122 | +$(LIBDIR)/.build.$(TAG): |
| 123 | + @if [ "$(oldtagsl)" != "" ]; then echo -e "Cannot build for tag=$(TAG) as old builds exist in $(LIBDIR) for other tags:\n$(oldtagsl)\nPlease run 'make clean' first\nIf 'make clean' is not enough: run 'make cleanall'"; exit 1; fi |
| 124 | + @if [ "$(oldtagsb)" != "" ]; then echo -e "Cannot build for tag=$(TAG) as old builds exist in $(BUILDDIR) for other tags:\n$(oldtagsb)\nPlease run 'make clean' first\nIf 'make clean' is not enough: run 'make cleanall'"; exit 1; fi |
| 125 | + @if [ ! -d $(LIBDIR) ]; then echo "mkdir -p $(LIBDIR)"; mkdir -p $(LIBDIR); fi |
| 126 | + @touch $(LIBDIR)/.build.$(TAG) |
| 127 | + @if [ ! -d $(BUILDDIR) ]; then echo "mkdir -p $(BUILDDIR)"; mkdir -p $(BUILDDIR); fi |
| 128 | + @touch $(BUILDDIR)/.build.$(TAG) |
| 129 | + |
| 130 | +#------------------------------------------------------------------------------- |
| 131 | + |
| 132 | +# Generic target and build rules: objects from C++ compilation |
| 133 | +$(BUILDDIR)/%%_cpp.o : %%.cc *.h $(BUILDDIR)/.build.$(TAG) |
| 134 | + @if [ ! -d $(BUILDDIR) ]; then echo "mkdir -p $(BUILDDIR)"; mkdir -p $(BUILDDIR); fi |
| 135 | + $(CXX) $(CPPFLAGS) $(INCFLAGS) $(CXXFLAGS) -c $< -o $@ |
| 136 | + |
| 137 | +# Generic target and build rules: objects from CUDA compilation |
| 138 | +ifneq ($(GPUCC),) |
| 139 | +$(BUILDDIR)/%%_$(GPUSUFFIX).o : %%.cc *.h $(BUILDDIR)/.build.$(TAG) |
| 140 | + @if [ ! -d $(BUILDDIR) ]; then echo "mkdir -p $(BUILDDIR)"; mkdir -p $(BUILDDIR); fi |
| 141 | + $(GPUCC) $(CPPFLAGS) $(INCFLAGS) $(GPUFLAGS) -c -x $(GPULANGUAGE) $< -o $@ |
| 142 | +endif |
| 143 | + |
| 144 | +#------------------------------------------------------------------------------- |
| 145 | + |
| 146 | +cxx_objects=$(addprefix $(BUILDDIR)/, read_slha_cpp.o) |
| 147 | +cxx_objects+=$(addprefix $(BUILDDIR)/, rwgt_instance_cpp.o) |
| 148 | +ifeq ($(GPUCC),) |
| 149 | + cxx_objects+=$(addprefix $(BUILDDIR)/, Parameters_%(model)s_cpp.o) |
| 150 | +else |
| 151 | + gpu_objects=$(addprefix $(BUILDDIR)/, Parameters_%(model)s_$(GPUSUFFIX).o) |
| 152 | +endif |
| 153 | + |
| 154 | +# Target (and build rules): common (src) library |
| 155 | +ifeq ($(GPUCC),) |
| 156 | +$(LIBDIR)/lib$(MG5AMC_COMMONLIB).so : $(cxx_objects) |
| 157 | + @if [ ! -d $(LIBDIR) ]; then echo "mkdir -p $(LIBDIR)"; mkdir -p $(LIBDIR); fi |
| 158 | + $(CXX) -shared -o $@ $(cxx_objects) $(LDFLAGS) |
| 159 | +else |
| 160 | +$(LIBDIR)/lib$(MG5AMC_COMMONLIB).so : $(cxx_objects) $(gpu_objects) |
| 161 | + @if [ ! -d $(LIBDIR) ]; then echo "mkdir -p $(LIBDIR)"; mkdir -p $(LIBDIR); fi |
| 162 | + $(GPUCC) -shared -o $@ $(cxx_objects) $(gpu_objects) $(LDFLAGS) |
| 163 | +endif |
| 164 | + |
| 165 | +#------------------------------------------------------------------------------- |
| 166 | + |
| 167 | +# Atomic copy helper macro: |
| 168 | +# 1) copy to unique temp file next to the destination |
| 169 | +# 2) atomically rename into place |
| 170 | +# This is safe under parallel make and concurrent invocations. |
| 171 | +define ATOMIC_COPY |
| 172 | + @tmp="$$(mktemp "$@.tmp.XXXXXX")"; \ |
| 173 | + cp -f "$(firstword $^)" "$$tmp"; \ |
| 174 | + mv -f "$$tmp" "$@" |
| 175 | +endef |
| 176 | + |
| 177 | +# Rex and teaRex: copy .so from src to LIBDIR atomically |
| 178 | +$(LIBDIR)/librex.so : ../src/librex.so |
| 179 | + $(ATOMIC_COPY) |
| 180 | + |
| 181 | +$(LIBDIR)/libtearex.so : ../src/libtearex.so |
| 182 | + $(ATOMIC_COPY) |
| 183 | + |
| 184 | +#------------------------------------------------------------------------------- |
| 185 | + |
| 186 | +# Target: clean the builds |
| 187 | +.PHONY: clean |
| 188 | + |
| 189 | +clean: |
| 190 | +ifeq ($(USEBUILDDIR),1) |
| 191 | + rm -rf $(LIBDIR) |
| 192 | + rm -rf $(BUILDDIR) |
| 193 | +else |
| 194 | + rm -f $(LIBDIR)/.build.* $(LIBDIR)/lib$(MG5AMC_COMMONLIB).so |
| 195 | + rm -f $(BUILDDIR)/.build.* $(BUILDDIR)/*.o $(BUILDDIR)/*.exe |
| 196 | +endif |
| 197 | + |
| 198 | +cleanall: |
| 199 | + @echo |
| 200 | + $(MAKE) clean -f $(THISMK) |
| 201 | + @echo |
| 202 | + rm -rf $(LIBDIR)/build.* |
| 203 | + rm -rf build.* |
| 204 | + |
| 205 | +#------------------------------------------------------------------------------- |
0 commit comments