-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
makefile
349 lines (315 loc) · 11.5 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
##
# CmdStan users: if you need to customize make options,
# you should add variables to a new file called
# make/local (no file extension)
#
# A typical option might be:
# CXX = clang++
#
# Users should only need to set these variables:
# - CXX: The compiler to use. Expecting g++ or clang++.
# - O: Optimization level. Valid values are {s, 0, 1, 2, 3}.
# Default is 3.
# - STANCFLAGS: Extra options for calling stanc
##
# The default target of this Makefile is...
help:
-include make/local # user-defined variables
STAN ?= stan/
MATH ?= $(STAN)lib/stan_math/
RAPIDJSON ?= $(STAN)lib/rapidjson_1.1.0/
CLI11 ?= lib/CLI11-1.9.1/
INC_FIRST ?= -I src -I $(STAN)src -I $(RAPIDJSON) -I $(CLI11)
## Detect operating system
ifneq ($(OS),Windows_NT)
OS := $(shell uname -s)
endif
## Set default compiler
ifeq (default,$(origin CXX))
ifeq ($(OS),Darwin) ## Darwin is Mac OS X
CXX := clang++
endif
ifeq ($(OS),Linux)
CXX := g++
endif
ifeq ($(OS),Windows_NT)
CXX := g++
endif
endif
# Detect compiler type
# - CXX_TYPE: {gcc, clang, mingw32-gcc, other}
# - CXX_MAJOR: major version of CXX
# - CXX_MINOR: minor version of CXX
ifneq (,$(findstring clang,$(CXX)))
CXX_TYPE ?= clang
endif
ifneq (,$(findstring mingw32-g,$(CXX)))
CXX_TYPE ?= mingw32-gcc
endif
ifneq (,$(findstring gcc,$(CXX)))
CXX_TYPE ?= gcc
endif
ifneq (,$(findstring g++,$(CXX)))
CXX_TYPE ?= gcc
endif
CXX_TYPE ?= other
CXX_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f1)
CXX_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d'.' -f2)
ifdef STAN_CPP_OPTIMS
ifeq (clang,$(CXX_TYPE))
CXXFLAGS_OPTIM ?= -fvectorize -ftree-vectorize -fslp-vectorize -ftree-slp-vectorize -fno-standalone-debug -fstrict-return -funroll-loops
ifeq ($(shell expr $(CXX_MAJOR) \>= 5), 1)
CXXFLAGS_FLTO ?= -flto=full -fwhole-program-vtables -fstrict-vtable-pointers -fforce-emit-vtables
endif
endif
ifeq (mingw32-g,$(CXX_TYPE))
else ifeq (gcc,$(CXX_TYPE))
CXXFLAGS_OPTIM_SUNDIALS ?= -fweb -fivopts -ftree-loop-linear
CPPFLAGS_OPTIM_SUNDIALS ?= $(CXXFLAGS_OPTIM_SUNDIALS)
# temp to contro for compiler versions while letting user override
# CXXFLAGS_OPTIM
CXXFLAGS_VERSION_OPTIM ?= -fweb -fivopts -ftree-loop-linear -floop-strip-mine -floop-block -floop-nest-optimize -ftree-vectorize -ftree-loop-distribution -funroll-loops
ifeq ($(shell expr $(CXX_MAJOR) \>= 5), 1)
CXXFLAGS_VERSION_OPTIM += -floop-unroll-and-jam
endif
ifeq ($(shell expr $(CXX_MAJOR) \>= 7), 1)
CXXFLAGS_VERSION_OPTIM += -fsplit-loops
ifneq ($(OS),Windows_NT)
CXXFLAGS_FLTO ?= -flto -fuse-linker-plugin -fdevirtualize-at-ltrans
endif
endif
ifndef STAN_MPI
CXXFLAGS_VISIBILITY ?= -fvisibility=hidden -fvisibility-inlines-hidden
endif
CXXFLAGS_OPTIM ?= $(CXXFLAGS_VERSION_OPTIM) $(CXXFLAGS_VISIBILITY)
LDFLAGS_FLTO ?= $(CXXFLAGS_FLTO)
endif
endif
ifdef STAN_THREADS
STAN_FLAG_THREADS=_threads
else
STAN_FLAG_THREADS=
endif
ifdef STAN_MPI
STAN_FLAG_MPI=_mpi
else
STAN_FLAG_MPI=
endif
ifdef STAN_OPENCL
STAN_FLAG_OPENCL=_opencl
else
STAN_FLAG_OPENCL=
endif
ifdef STAN_NO_RANGE_CHECKS
STAN_FLAG_NO_RANGE_CHECKS=_nochecks
else
STAN_FLAG_NO_RANGE_CHECKS=
endif
STAN_FLAGS=$(STAN_FLAG_THREADS)$(STAN_FLAG_MPI)$(STAN_FLAG_OPENCL)$(STAN_FLAG_NO_RANGE_CHECKS)
ifeq ($(OS),Windows_NT)
ifeq (clang,$(CXX_TYPE))
PRECOMPILED_HEADERS ?= false
else
ifeq ($(shell expr $(CXX_MAJOR) \>= 8), 1)
PRECOMPILED_HEADERS ?= true
else
PRECOMPILED_HEADERS ?= false
endif
endif
else
PRECOMPILED_HEADERS ?= true
endif
ifeq ($(PRECOMPILED_HEADERS),true)
PRECOMPILED_MODEL_HEADER=$(STAN)src/stan/model/model_header.hpp.gch/model_header$(STAN_FLAGS)_$(CXX_MAJOR)_$(CXX_MINOR).hpp.gch
ifeq ($(CXX_TYPE),gcc)
CXXFLAGS_PROGRAM+= -Wno-ignored-attributes $(CXXFLAGS_OPTIM) $(CXXFLAGS_FLTO)
endif
else
PRECOMPILED_MODEL_HEADER=
endif
include $(MATH)make/compiler_flags
include $(MATH)make/dependencies
include $(MATH)make/libraries
include make/stanc
include make/program
include make/tests
include make/command
CMDSTAN_VERSION := 2.36.0
.PHONY: help
help:
@echo '--------------------------------------------------------------------------------'
@echo 'CmdStan v$(CMDSTAN_VERSION) help'
@echo ''
@echo ' Build CmdStan utilities:'
@echo ' > $(MAKE) build'
@echo ''
@echo ' This target will:'
@echo ' 1. Install the Stan compiler bin/stanc$(EXE) from stanc3 binaries.'
@echo ' 2. Build the print utility bin/print$(EXE) (deprecated; will be removed in v3.0)'
@echo ' 3. Build the stansummary utility bin/stansummary$(EXE)'
@echo ' 4. Build the diagnose utility bin/diagnose$(EXE)'
@echo ' 5. Build all libraries and object files compile and link an executable Stan program'
@echo ''
@echo ' Note: to build using multiple cores, use the -j option to make, e.g., '
@echo ' for 4 cores:'
@echo ' > $(MAKE) build -j4'
@echo ''
ifeq ($(OS),Windows_NT)
@echo ' On Windows it is recommended to include with the PATH environment'
@echo ' variable the directory of the Intel TBB library.'
@echo ' This can be setup permanently for the user with'
@echo ' > make install-tbb'
endif
@echo ''
@echo ' Build a Stan program:'
@echo ''
@echo ' Given a Stan program at foo/bar.stan, build an executable by typing:'
@echo ' > make foo/bar$(EXE)'
@echo ''
@echo ' This target will:'
@echo ' 1. Install the Stan compiler (bin/stanc), as needed.'
@echo ' 2. Use the Stan compiler to generate C++ code, foo/bar.hpp.'
@echo ' 3. Compile the C++ code using $(CC) $(CC_MAJOR).$(CC_MINOR) to generate foo/bar$(EXE)'
@echo ''
@echo ' Additional make options:'
@echo ' STANCFLAGS: defaults to "". These are extra options passed to bin/stanc$(EXE)'
@echo ' when generating C++ code. If you want to allow undefined functions in the'
@echo ' Stan program, either add this to make/local or the command line:'
@echo ' STANCFLAGS = --allow_undefined'
@echo ' USER_HEADER: when STANCFLAGS has --allow_undefined, this is the name of the'
@echo ' header file that is included. This defaults to "user_header.hpp" in the'
@echo ' directory of the Stan program.'
@echo ' STANC3_VERSION: When set, uses that tagged version specified; otherwise, downloads'
@echo ' the nightly version.'
@echo ' STAN_CPP_OPTIMS: Turns on additonal compiler flags for performance.'
@echo ' STAN_NO_RANGE_CHECKS: Removes the range checks from the model for performance.'
@echo ' STAN_THREADS: Enable multi-threaded execution of the Stan model.'
@echo ''
@echo ''
@echo ' Example - bernoulli model: examples/bernoulli/bernoulli.stan'
@echo ''
@echo ' 1. Build the model:'
@echo ' > $(MAKE) examples/bernoulli/bernoulli$(EXE)'
@echo ' 2. Run the model:'
@echo ' > examples/bernoulli/bernoulli$(EXE) sample data file=examples/bernoulli/bernoulli.data.R'
@echo ' 3. Look at the samples:'
@echo ' > bin/stansummary$(EXE) output.csv'
@echo ''
@echo ''
@echo ' Clean CmdStan:'
@echo ''
@echo ' Remove the built CmdStan tools:'
@echo ' > make clean-all'
@echo ''
@echo '--------------------------------------------------------------------------------'
.PHONY: help-dev
help-dev:
@echo '--------------------------------------------------------------------------------'
@echo 'CmdStan help for developers:'
@$(MAKE) print-compiler-flags
@echo ''
@echo ' If this copy of CmdStan has been cloned using git,'
@echo ' before building CmdStan utilities the first time you need'
@echo ' to initialize the Stan repository with:'
@echo ' make stan-update'
@echo ''
@echo ''
@echo 'Developer relevant targets:'
@echo ' Stan management targets:'
@echo ' - stan-update : Initializes and updates the Stan repository'
@echo ' - stan-update/* : Updates the Stan repository to the specified'
@echo ' branch or commit hash.'
@echo ' - stan-revert : Reverts changes made to Stan library back to'
@echo ' what is in the repository.'
@echo ''
@echo 'Model related:'
@echo '- bin/stanc$(EXE): Download the Stan compiler binary.'
@echo '- bin/print$(EXE): Build the print utility. (deprecated)'
@echo '- bin/stansummary$(EXE): Build the stansummary utility.'
@echo '- bin/diagnose$(EXE): Build the diagnose utility.'
@echo ''
@echo '- *$(EXE) : If a Stan model exists at *.stan, this target will build'
@echo ' the Stan model as an executable.'
@echo '- compile_info : prints compiler flags for compiling a CmdStan executable.'
@echo '--------------------------------------------------------------------------------'
.PHONY: build-mpi
build-mpi: $(MPI_TARGETS)
@echo ''
@echo '--- boost mpi bindings built ---'
.PHONY: build
build: bin/stanc$(EXE) $(SUNDIALS_TARGETS) $(MPI_TARGETS) $(TBB_TARGETS) $(CMDSTAN_MAIN_O) $(PRECOMPILED_MODEL_HEADER) bin/stansummary$(EXE) bin/print$(EXE) bin/diagnose$(EXE)
@echo ''
ifeq ($(OS),Windows_NT)
@echo 'NOTE: Please add $(TBB_BIN_ABSOLUTE_PATH) to your PATH variable.'
@echo 'You may call'
@echo ''
@echo '$(MAKE) install-tbb'
@echo ''
@echo 'to automatically update your user configuration.'
endif
@echo '--- CmdStan v$(CMDSTAN_VERSION) built ---'
.PHONY: install-tbb
install-tbb: $(TBB_TARGETS)
ifeq ($(OS),Windows_NT)
$(shell echo "cmd.exe /C install-tbb.bat")
endif
##
# Clean up.
##
.PHONY: clean clean-deps clean-all
clean: clean-tests
@echo ' removing built CmdStan utilities'
$(RM) bin/stanc$(EXE) bin/stansummary$(EXE) bin/print$(EXE) bin/diagnose$(EXE)
$(RM) -r bin/cmdstan
@echo ' removing cached compiler objects'
$(RM) $(wildcard src/cmdstan/main*.o)
$(RM) -r $(wildcard $(STAN)src/stan/model/model_header*.hpp.gch)
@echo ' removing built example model'
$(RM) examples/bernoulli/bernoulli$(EXE) examples/bernoulli/bernoulli.o examples/bernoulli/bernoulli.d examples/bernoulli/bernoulli.hpp $(wildcard examples/bernoulli/*.csv)
clean-deps:
@echo ' removing dependency files'
$(RM) $(call findfiles,src,*.d) $(call findfiles,src/stan,*.d) $(call findfiles,$(MATH)/stan,*.d) $(call findfiles,$(STAN)/src/stan/,*.d)
$(RM) $(call findfiles,src,*.d.*) $(call findfiles,src/stan,*.d.*) $(call findfiles,$(MATH)/stan,*.d.*)
$(RM) $(call findfiles,src,*.dSYM) $(call findfiles,src/stan,*.dSYM) $(call findfiles,$(MATH)/stan,*.dSYM)
clean-all: clean clean-deps clean-libraries
##
# Submodule related tasks
##
.PHONY: stan-update
stan-update :
git submodule update --init --recursive
stan-update/%: stan-update
cd stan && git fetch --all && git checkout $* && git pull
stan-pr/%: stan-update
cd stan && git reset --hard origin/develop && git checkout $* && git checkout develop && git merge $* --ff --no-edit --strategy=ours
.PHONY: stan-revert
stan-revert:
git submodule update --init --recursive
##
# Debug target that prints compile command for CmdStan executable
##
.PHONY: compile_info
compile_info:
@echo '$(LINK.cpp) $(CXXFLAGS_PROGRAM) $(CMDSTAN_MAIN_O) $(LDLIBS) $(SUNDIALS_TARGETS) $(MPI_TARGETS) $(TBB_TARGETS)'
##
# Debug target that allows you to print a variable
##
.PHONY: print-%
print-% : ; @echo $* = $($*)
.PHONY: clean-build
clean-build: clean-all build
##
# This is only run if the `include` statements earlier fail to find a file.
# We assume that means the submodule is missing
##
$(MATH)make/% :
@echo 'ERROR: Missing Stan submodules.'
@echo 'We tried to find the Stan Math submodule at:'
@echo ' $(MATH)'
@echo ''
@echo 'The most likely source of the problem is CmdStan was cloned without'
@echo 'the --recursive flag. To fix this, run the following command:'
@echo ' git submodule update --init --recursive'
@echo ''
@echo 'And try building again'
@exit 1