swh:1:snp:d80eec3f654c152adbdd6e641362bcb340d39fe2
Raw File
Tip revision: 919402d232b6490cbe2a8257d87ff0939e11c98f authored by Hamada S. Badr on 24 February 2023, 15:48:05 UTC
Update math submodule
Tip revision: 919402d
tests
ifdef STANC2
TEST_STANC=test/test-models/stanc2$(EXE)
else
TEST_STANC=bin/stanc$(EXE)
endif

##
# 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),.)
test/%$(EXE) : INC += $(INC_GTEST)
test/%$(EXE) : test/%.o $(GTEST)/src/gtest_main.cc $(GTEST)/src/gtest-all.o $(LIBSUNDIALS) $(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),.)
src/test/%.d : INC += $(INC_GTEST)
src/test/%.d : DEPTARGETS = -MT $(patsubst src/test/%.d,test/%.o,$@) -MT $@

## this is for the $(STANC_TEMPLATE_INSTANTIATION) files
src/stan/%.d : DEPTARGETS = -MT test/test-models/stanc2$(EXE) -MT $(patsubst src/%.d,test/%.o,$@) -MT $@

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

ifneq ($(filter test/unit/lang/%$(EXE),$(MAKECMDGOALS)),)
-include src/test/test-models/stanc2.d
-include $(patsubst %.cpp,%.d,$(STANC_TEMPLATE_INSTANTIATION_CPP))
endif

ifneq ($(filter test/test-models/stanc2$(EXE) test/test-models/stanc2.o,$(MAKECMDGOALS)),)
-include src/test/test-models/stanc2.d
-include $(patsubst %.cpp,%.d,$(STANC_TEMPLATE_INSTANTIATION_CPP))
endif

ifneq ($(filter test/test-models/good/%.hpp test/test-models/good/%.hpp-test,$(MAKECMDGOALS)),)
-include src/test/test-models/stanc2.d
-include $(patsubst %.cpp,%.d,$(STANC_TEMPLATE_INSTANTIATION_CPP))
endif


##
# static rule to link in libstanc.
# needed by subset of unit tests that test stan compiler
# all these tests are under stan/test/unit/lang
##
STANC_TESTS_HEADERS := $(call findfiles,src/test/unit/lang,*_test.cpp)
STANC_TESTS_O := $(patsubst src/%.cpp,%.o,$(STANC_TESTS_HEADERS))
STANC_TESTS := $(patsubst src/%.cpp,%$(EXE),$(STANC_TESTS_HEADERS))

# add additional dependency to libstanc.a
$(STANC_TESTS_O) : test/libstanc.a
$(STANC_TESTS) : LDLIBS += $(LDLIBS_STANC)

############################################################
##
# 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/test-models/stanc2$(EXE) : O = $(O_STANC)
test/test-models/stanc2$(EXE) : test/libstanc.a
test/test-models/stanc2$(EXE) : LDLIBS += $(LDLIBS_STANC)
test/test-models/stanc2$(EXE) : %$(EXE) : %.o
	@mkdir -p $(dir $@)
	$(LINK.cpp) $< $(LDLIBS) $(OUTPUT_OPTION)

TEST_MODELS = $(call findfiles,src/test/test-models,*.stan)
$(patsubst src/%.stan,%.hpp,$(TEST_MODELS)) : test/test-models/%.hpp : src/test/test-models/%.stan $(TEST_STANC)
	@mkdir -p $(dir $@)
	$(WINE) $(TEST_STANC) $< --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 $(TEST_STANC)
	@mkdir -p $(dir $@)
	$(WINE) $(TEST_STANC) --standalone-functions $< --o=$@

##
# src/test/unit/lang/stanc_helper_test.cpp requires files to be read-only.
##
.PHONY: change-file-permissions
change-file-permissions:
ifeq ($(OS),Windows_NT)
	attrib +r src\\test\\test-models\\good\\stanc_helper.stan
	attrib +r src\\test\\test-models\\bad\\stanc_helper.stan
	attrib +r src\\test\\test-models\\bad\\read_only
else
	chmod 444 src/test/test-models/*/stanc_helper.stan
	chmod 555 src/test/test-models/bad/read_only
endif

test/unit/lang/stanc_helper_test.o: change-file-permissions

##
# Add additional dependencies for tests that should translate Stan to hpp, but
# isn't included in the source using `#include`.
##
test/unit/lang/parser_generator_test$(EXE): $(patsubst src/%.stan,%.hpp,$(call findfiles,src/test/test-models/good/parser-generator,*.stan))

##
# 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