swh:1:snp:d80eec3f654c152adbdd6e641362bcb340d39fe2
Raw File
Tip revision: 05b178901347f05a667eb2d4281f77361affade0 authored by Brian Ward on 09 May 2023, 14:39:33 UTC
Add get_param_sizes, update random_var_context
Tip revision: 05b1789
tests
##
# Build/download stanc3
##

STANC_DL_RETRY = 5
STANC_DL_DELAY = 10

ifeq ($(OS),Windows_NT)
 OS_TAG := windows
else ifeq ($(OS),Darwin)
 OS_TAG := mac
else ifeq ($(OS),Linux)
 OS_TAG := linux
endif

ifneq ($(STANC3),)

  # build stanc3 from local installation
  bin/stanc$(EXE) : $(call findfiles,$(STANC3)/src/,*.ml*) $(STANC#)
	@mkdir -p $(dir $@)
	cd $(STANC3) && echo "--- Rebuilding stanc ---\n" && dune build @install
	cp $(STANC3)/_build/default/src/stanc/stanc.exe $@

else ifneq ($(STANC3_TEST_BIN_URL),)

# download stanc3 build from specific branch
ifeq ($(OS_TAG),windows)
    bin/stanc$(EXE) :
	@mkdir -p $(dir $@)
	$(shell echo "curl -L $(STANC3_TEST_BIN_URL)/bin/$(OS_TAG)-stanc -o bin/stanc$(EXE) --retry $(STANC_DL_RETRY) --retry-delay $(STANC_DL_DELAY)")
else
    bin/stanc$(EXE) :
	@mkdir -p $(dir $@)
	curl -L $(STANC3_TEST_BIN_URL)/bin/$(OS_TAG)-stanc -o bin/stanc$(EXE) --retry $(STANC_DL_RETRY) --retry-delay $(STANC_DL_DELAY)
	chmod +x bin/stanc$(EXE)
endif # end download if/else

else ifeq ($(OS_TAG),windows)
# get latest stanc3 - Windows
    bin/stanc$(EXE) :
	@mkdir -p $(dir $@)
	$(shell echo "curl -L https://github.com/stan-dev/stanc3/releases/download/nightly/$(OS_TAG)-stanc -o bin/stanc$(EXE) --retry $(STANC_DL_RETRY) --retry-delay $(STANC_DL_DELAY)")
else
# get latest stanc3 - Linux & MacOS
    bin/stanc$(EXE) :
	@mkdir -p $(dir $@)
	curl -L https://github.com/stan-dev/stanc3/releases/download/nightly/$(OS_TAG)-stanc -o bin/stanc$(EXE) --retry $(STANC_DL_RETRY) --retry-delay $(STANC_DL_DELAY)
	chmod +x bin/stanc$(EXE)
endif

##
# Build test executables
##

test/%$(EXE) : CXXFLAGS += $(CXXFLAGS_GTEST)
test/%$(EXE) : CPPFLAGS += $(CPPFLAGS_GTEST)
test/%$(EXE) : INC_FIRST = -I $(if $(STAN),$(STAN)/src,src) -I $(if $(STAN),$(STAN),.) -I $(RAPIDJSON)
test/%$(EXE) : INC += $(INC_GTEST)
test/%$(EXE) : test/%.o $(GTEST)/src/gtest_main.cc $(GTEST)/src/gtest-all.o $(SUNDIALS_TARGETS) $(MPI_TARGETS) $(TBB_TARGETS)
	$(LINK.cpp) $(filter-out test/%.hpp %.hpp-test,$^) $(LDLIBS) $(OUTPUT_OPTION)

test/%.o : src/test/%.cpp
	@mkdir -p $(dir $@)
	$(COMPILE.cpp) $< $(OUTPUT_OPTION)

##
# Customization for generating dependencies
##
src/test/%.d : CXXFLAGS += $(CXXFLAGS_GTEST)
src/test/%.d : CPPFLAGS += $(CPPFLAGS_GTEST)
src/test/%.d : INC_FIRST = -I $(if $(STAN),$(STAN)/src,src) -I $(if $(STAN),$(STAN),.) -I $(RAPIDJSON)
src/test/%.d : INC += $(INC_GTEST)
src/test/%.d : DEPTARGETS = -MT $(patsubst src/test/%.d,test/%.o,$@) -MT $@

ifneq ($(filter test/%$(EXE),$(MAKECMDGOALS)),)
-include $(patsubst test/%$(EXE),src/test/%.d,$(filter test/%,$(MAKECMDGOALS)))
endif

############################################################
##
# Target to verify header files within Stan has
# enough include calls
##
HEADER_TESTS := $(addsuffix -test,$(call findfiles,src/stan,*.hpp))

ifeq ($(OS),Windows_NT)
  DEV_NULL = nul
else
  DEV_NULL = /dev/null
endif

%.hpp-test : %.hpp test/dummy.cpp
	$(COMPILE.cpp) -O0 -include $^ -o $(DEV_NULL)

test/dummy.cpp:
	@mkdir -p test
	@touch $@
	@echo "int main() {return 0;}" >> $@

.PHONY: test-headers
test-headers: $(HEADER_TESTS)

##
# Target to verify code generated Stan programs
# (.stan -> .hpp) are compilable
#
# Running:
# > make test/integration/compile_models
# will make sure everything in src/test/test-models/good/
# compiles
##
.PHONY: %.hpp-test

ifneq ($(OS),Windows_NT)
test/test-models/good/%.hpp-test : O = 0
else
test/test-models/good/%.hpp-test : O = 1
endif
test/test-models/good/%.hpp-test : test/test-models/good/%.hpp test/test-model-main.cpp
	$(COMPILE.cpp) -o $(DEV_NULL) -include $^

test/test-model-main.cpp:
	@mkdir -p test
	@touch $@
	@echo "int main() {" >> $@
	@echo "  stan::io::var_context *data = NULL;" >> $@
	@echo "  stan_model model(*data);" >> $@
	@echo >> $@
	@echo "  std::vector<stan::math::var> params;" >> $@
	@echo "  std::vector<double> x_r;" >> $@
	@echo "  std::vector<int> x_i;" >> $@
	@echo >> $@
	@echo "  model.log_prob<false, false>(params, x_i);" >> $@
	@echo "  model.log_prob<false, true>(params, x_i);" >> $@
	@echo "  model.log_prob<true, false>(params, x_i);" >> $@
	@echo "  model.log_prob<true, true>(params, x_i);" >> $@
	@echo "  model.log_prob<false, false>(x_r, x_i);" >> $@
	@echo "  model.log_prob<false, true>(x_r, x_i);" >> $@
	@echo "  model.log_prob<true, false>(x_r, x_i);" >> $@
	@echo "  model.log_prob<true, true>(x_r, x_i);" >> $@
	@echo >> $@
	@echo "  return 0;" >> $@
	@echo "}" >> $@
	@echo >> $@

##
# Adding a test for multiple translation units. If this fails,
# a new function is probably missing an inline.
# Templates for such test are prefixed with 'mtu_'.
##
test/integration/mtu :
	mkdir -p test/integration/mtu

test/integration/mtu/%_1.cpp : src/test/integration/mtu/%.cpp test/integration/mtu
	cp $< $@
test/integration/mtu/%_2.cpp : src/test/integration/mtu/%.cpp test/integration/mtu
	cp $< $@

ifneq ($(OS),Windows_NT)
test/integration/mtu/%.o : CXXFLAGS += -fPIC
test/integration/mtu/%.o : O = 0
else
test/integration/mtu/%.o : O = 1
endif

test/integration/mtu/%.o : %.cpp
	$(COMPILE.cpp) -pipe -o $(DEV_NULL) -include $^

test/integration/libmtu_%.so : LDFLAGS += -shared

test/integration/libmtu_%.so : test/integration/mtu/%_1.o test/integration/mtu/%_2.o $(MPI_TARGETS) $(TBB_TARGETS)
	$(LINK.cpp) $^ $(LDLIBS) $(OUTPUT_OPTION)

test/integration/multiple_translation_units_test$(EXE) : test/integration/libmtu_model.so test/integration/libmtu_mcmc.so


############################################################
##
# Use the stanc compiler to generate C++ from Stan programs
##
TEST_MODELS = $(call findfiles,src/test/test-models,*.stan)
$(patsubst src/%.stan,%.hpp,$(TEST_MODELS)) : test/test-models/%.hpp : src/test/test-models/%.stan bin/stanc$(EXE)
	@mkdir -p $(dir $@)
	$(WINE) bin/stanc$(EXE) $< --o=$@

##
# Generate C++ from Stan standalone functions
##
TEST_FUNCTIONS = $(call findfiles,src/test/test-models,*.stanfuncs)
$(patsubst src/%.stanfuncs,%.hpp,$(TEST_FUNCTIONS)) : test/test-models/%.hpp : src/test/test-models/%.stanfuncs bin/stanc$(EXE)
	@mkdir -p $(dir $@)
	$(WINE) bin/stanc$(EXE) --standalone-functions $< --o=$@

##
# Compile models depends on every model within
# src/test/test-models/good/*.stan being able to
# be code-generated to a *.hpp, then passed through
# the compiler using the -fsyntax-only flag
##
test/integration/compile_models_test$(EXE): $(patsubst src/%.stan,%.hpp-test,$(call findfiles,src/test/test-models/good,*.stan))

##
# Same trick as above for models, but for standalone functions, using
# "src/test/test-models/good-standalone-functions/*.stanfuncs"
##
test/integration/compile_standalone_functions_test$(EXE): $(patsubst src/%.stanfuncs,%.hpp-test,$(call findfiles,src/test/test-models/good-standalone-functions,*.stanfuncs))
back to top