buildSettings.mk 9.52 KB
Newer Older
wangkx1's avatar
change  
wangkx1 committed
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
# This file defines standard commands and compiler flags. It is included
# before vars.mk, which uses the variables defined here to construct the
# final compiler flag variables that are used in C/C++ compilation.
#
# Final values for CFLAGS, CPPFLAGS, CXXFLAGS, LDFLAGS, NVCCFLAGS, and
# NVCCLDFLAGS (including include/library search paths) are constructed in
# vars.mk,


# get system type (Darwin, Linux)
SYSTYPE ?= $(shell uname -s)


#####################################################################
#
# Standard commands
#
#####################################################################
SHELL   ?= /bin/sh
RM      ?= /bin/rm
CP      ?= /bin/cp
MV      ?= /bin/mv
CHMOD   ?= /bin/chmod
MKDIR   ?= /bin/mkdir
INSTALL ?= install -p
TCLSH   ?= ${FSLDIR}/bin/fsltclsh

#####################################################################
#
# Architecture-independent compiler/linker flags
#
#####################################################################

# Platform independent compiler/linker options may be added to
# the following ARCH* variables. These variables are *always*
# added to compiler calls, for all platforms.
#
# The ARCH* variables here are added to the end of the final
# FLAGS variables, which means that any options specified here
# will typically take precedence over options provided by the
# environment.

# Compiler flags for C projects.
#  - C99 as minimum source compatibility standard
#  - Position independent code essential for
#    compiling/using shared libraries
ARCHCFLAGS = -std=c99 -fPIC

# Compiler flags for C++ projects.
#  - C++17 as minimum source compatibility standard
#  - Position independent code essential for
#    compiling/using shared libraries
ARCHCXXFLAGS = -std=c++17 -fPIC -g -O0

# Preprocesor flags for C/C++ projects.
ARCHCPPFLAGS = -g -O0

# Linker flags for all projects
wangkx1's avatar
wangkx1 committed
59
ARCHLDFLAGS = 
wangkx1's avatar
change  
wangkx1 committed
60
61

# Libraries available for linking by all projects.
wangkx1's avatar
wangkx1 committed
62
ARCHLIBS = 
wangkx1's avatar
change  
wangkx1 committed
63
64
65
66
67
68
69
70
71
72
73
74
75

# Compiler flags for CUDA projects.
#  - Define ARMA_ALLOW_FAKE_GCC, otherwise nvcc-compiled
#    armadillo structures may have different byte-alignment
#    to equivalent g++-compiled structures (see
#    include/armadillo_bits/compiler_setup.hpp)
#
#  - Set -std=c++14. This is the newest C++ standard
#    supported by nvcc for CUDA<11.0.
# ARCHNVCCFLAGS = -std=c++14 -fPIC
ARCHNVCCFLAGS = -DARMA_ALLOW_FAKE_GCC -std=c++17 -fPIC -g -O0

# Linker flags for CUDA projects.
wangkx1's avatar
wangkx1 committed
76
ARCHNVCCLDFLAGS = -std=c++17 -fPIC  
wangkx1's avatar
change  
wangkx1 committed
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

#####################################################################
#
# Default compiler commands, flags and basic external libraries for
# macOS.
#
#####################################################################

# Drop -rpath flags from LDFLAGS if it is set in
# the environment - we explicitly add them below
# such that executables will be able to find libs
# in $(pwd), $FSLDEVDIR/lib, and $FSLDIR/lib *in
# that order*. This is so that executables
# located in the project source dir can be
# executed in place with sensible behaviour (i.e.
# any shared libs located in the same dir will
# take precedence over libs of the same name in
# $FSLDIR/lib/).
#
# Fortunately -Wl,-rpath is used for both g++ and
# clang, so we can cover both with one substitution.
COMMA    := ,
_LDFLAGS := $(patsubst -Wl${COMMA}-rpath%,,${LDFLAGS})
LDFLAGS   = ${_LDFLAGS}

# Also drop -L options from LDFLAGS if set in the
# environment, as we want to set our own
# precedence rules as above ($FSLDEVDIR > $FSLDIR)
# - this is done in vars.mk
_LDFLAGS := $(patsubst -L%,,${_LDFLAGS})
LDFLAGS   = ${_LDFLAGS}

# Construct all rpath options, and add them to
# LDFLAGS.  We keep them in a separate variable
# so they can be added to NVCCLDFLAGS as well.
RPATHS    =

ifeq ($(SYSTYPE), Darwin)

  # CC, CXX, CFLAGS, CXXFLAGS, and LDFLAGS
  # may be overridden by the environment.
  CC       ?= clang
  CXX      ?= clang++
  CFLAGS   ?= -arch x86_64 -Wall -pedantic
  CXXFLAGS ?= -arch x86_64 -Wall -pedantic
  RPATHS   += -Wl,-rpath,"@executable_path/"
  # Only add FSLDEVDIR if different to FSLDIR
  ifneq (${FSLDEVDIR}, ${FSLDIR})
    # RPATHS += -Wl,-rpath,"${FSLDEVDIR}/lib"
  endif
  # RPATHS   += -Wl,-rpath,"${FSLDIR}/lib"
  ARCHLIBS +=  -llapack -lblas -lz -lm
  # -L /usr/local/lib/python3.10/dist-packages/ray/core  -ljemalloc 

  # Old versions of libxml++ (which is used by
  # ciftilib) use std::auto_ptr, which has been
  # removed from the llvm C++17 implementation.
  # Adding this flag is a hack which allows us
  # to continue to use std::auto_ptr.
  ARCHCPPFLAGS += -D_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR

  # Some standard C++ features may be hidden when
  # being compiled on a macOS version which does
  # not support those features. But we use the
  # conda provided libc++.
  # https://conda-forge.org/docs/maintainer/knowledge_base.html#newer-c-features-with-old-skd
  ARCHCPPFLAGS += -D_LIBCPP_DISABLE_AVAILABILITY

  # On macOS, we need to change the install
  # name of shared libs so that they can be
  # looked up according to the rpath of
  # executables that depend on them. Note
  # that we give shared libs a suffix of
  # .so (not .dylib) on both linux and
  # macOS.
  %.so : RPATHS += -Wl,-install_name,@rpath/$@

  ARCHLDFLAGS += ${RPATHS}
endif

#####################################################################
#
# Default compiler commands, flags and basic external libraries for
# Linux.
#
#####################################################################
ifeq ($(SYSTYPE), Linux)

  # CC, CXX, CFLAGS, CXXFLAGS, and LDFLAGS
  # may be overridden by the environment.
  CC       ?= gcc
  CXX      ?= g++
  CFLAGS   ?= -fexpensive-optimizations \
              -Wall -pedantic -Wno-long-long
  CXXFLAGS ?= -fexpensive-optimizations \
              -Wall -pedantic -Wno-long-long
  # RPATHS   += -Wl,-rpath,'$$ORIGIN' \
  #             -Wl,-rpath-link,'$$ORIGIN'
  
  # Only add FSLDEVDIR if different to FSLDIR
  ifneq (${FSLDEVDIR}, ${FSLDIR})
    # RPATHS  += -Wl,-rpath,'${FSLDEVDIR}/lib' \
    #            -Wl,-rpath-link,'${FSLDEVDIR}/lib'
  endif
  # RPATHS    += -Wl,-rpath,'${FSLDIR}/lib' \
  #              -Wl,-rpath-link,'${FSLDIR}/lib'
  LDFLAGS   += ${RPATHS}

  # Libraries which are implicitly available
  # to all projects for linking
  ARCHCXXFLAGS += -pthread
  ARCHLIBS     += -llapack -lblas -lz -lm
  # -L /usr/local/lib/python3.10/dist-packages/ray/core  -ljemalloc 
  # -L /usr/local/lib/python3.10/dist-packages/ray/core  -ljemalloc 
endif

#####################################################################
#
# Default compiler commands, flags, and basic external libraries for
# CUDA projects. nvcc must be on the $PATH, or $NVCC must be set.
#
#####################################################################
NVCC ?= $(shell which nvcc 2> /dev/null)
ifneq (${NVCC}, )

  # NVCC, GENCODEFLAGS, NVCCFLAGS, NVCCLDFLAGS,
  # and CUDA_DYNAMIC may be specified/overridden
  # by the environment.
  CUDA_HOME     = $(realpath $(dir ${NVCC})/../)
  CUDA_VER      = $(shell ${NVCC} --version | grep release | cut -d ' ' -f 5 | tr -d ',')
  GENCODEFLAGS ?= $(shell ${FSLCONFDIR}/supportedGencodes.sh ${CUDA_VER})

  # We use nvcc to compile .cu files, g++ to
  # compile .cc/.cpp files, and nvcc to perform
  # the final linking. So we need to manage
  # compiler/linker flags carefully.

  # NVCCFLAGS is used for compiling .cu files
  # (and final linking) - it contains nvcc-
  # specific options.
  #
  # See vars.mk for the final construction of
  # these variables

  # We specify the compiler to use with -ccbin, as
  # nvcc might otherwise naively call "g++", rather
  # than honouring $(CXX). We specifically use
  # "-ccbin" rather than "--compiler-bindir" here,
  # because the conda-forge nvcc wrapper checks for
  # -ccbin, and adds its own if not present.
  NVCCFLAGS    ?= ${GENCODEFLAGS} \
                  -ccbin $(CXX)
  NVCCLDFLAGS  ?= -L${CUDA_HOME}/lib         \
                  -L${CUDA_HOME}/lib64       \
                  -L${CUDA_HOME}/lib/stubs   \
                  -L${CUDA_HOME}/lib64/stubs \
                  -L/usr/local/cuda/lib      \
                  $(subst -Wl${COMMA},--linker-options ,${RPATHS})

  # Link CUDA libraries dynamically, if compilation
  # was invoked with "make CUDA_DYNAMIC=1". The
  # default behaviour is to link statically.
  # Compiler / linker options vary depending on the
  # CUDA version
  _CUDALIBS = -lcuda
  ifndef CUDA_DYNAMIC
    # The cuda/cudart/cudadevrt libs are handled by nvcc.
    # Other components of the CUDA toolkit are provided
    # as both dynamic and static libraries. "_CUDALIBS"
    # is what is used to construct the final NVCCLDFLAGS
    # (see vars.mk).
    #
    # --cudadevrt was added in CUDA/nvcc 10.*
    #
    # In CUDA 10.* and newer, cublas_static requires
    # cublas_Lt_static, in addition to culibos
    ifeq ($(patsubst 9.%,,${CUDA_VER}),)
      # CUBLAS_STATIC = -lcublas -lculibos
      CUBLAS_STATIC = -lcublas -lcublasLt -l cudart -l cudadevrt 

      # -L /usr/local/lib/python3.10/dist-packages/ray/core  -ljemalloc
      # NVCCLDFLAGS  += --cudart=static
    else
      # CUBLAS_STATIC = -lcublas -lcublasLt -lculibos
      CUBLAS_STATIC = -lcublas -lcublasLt -l cudart -l cudadevrt 
      # -L /usr/local/lib/python3.10/dist-packages/ray/core  -ljemalloc
      # NVCCLDFLAGS  += --cudart=static --cudadevrt=static
	endif

    # Other CUDA toolkit components will
    # be added here on an as-needed basis.
    _CUDALIBS += $(subst -lcublas,${CUBLAS_STATIC}, \
                 $(subst -lcurand,-lcurand_static,${CUDALIBS}))
  else
    ifeq ($(patsubst 9.%,,${CUDA_VER}),)
      NVCCLDFLAGS += --cudart=shared
    else
      NVCCLDFLAGS += --cudart=shared --cudadevrt=static
    endif
    _CUDALIBS += ${CUDALIBS}
  endif
endif