# main Makefile template for Linux and MacOS, populated by configure

defaultMakefileTarget = Makefile.unix

host=@host@
host_short=@host_short@
CC=@CC@
CXX=@CXX@
CPPFLAGS=@CPPFLAGS@
CFLAGS=@CFLAGS@
CXXFLAGS=@CXXFLAGS@
LDFLAGS=@LDFLAGS@

CMAKE = @CMAKE@
# Use the system's default cmake if nothing is passed to configure script, i.e., no CMAKE=<some_path> is specified
ifeq ($(CMAKE),)
	CMAKE=cmake
endif

$(info "Selected CMAKE from configure (OpenModelica): $(CMAKE)")

all: @ALL_TARGETS@

.PRECIOUS: Makefile
.PHONY: omc omlibrary omplot omedit omedit-testsuite omparser omnotebook omoptim omshell omc-diff omsimulator ReferenceFiles libs-for-testing test fast-test testsuite-depends omsens_qt

omc: omc.skip
omc.skip:
	$(MAKE) -C OMCompiler @OMC_TARGET@
omlibrary:
	$(MAKE) -C libraries lib-for-installing
omplot: omplot.skip
omplot.skip: omc
	test -f $@ || $(MAKE) -C OMPlot
omedit: omedit.skip
omedit.skip: omc omplot omsimulator omparser
	test -f $@ || $(MAKE) -f $(defaultMakefileTarget) -C OMEdit
omedit-testsuite: omedit-testsuite.skip
omedit-testsuite.skip: omedit.skip testsuite-depends
	$(MAKE) -f $(defaultMakefileTarget) -C OMEdit/Testsuite
omparser: omparser.skip
omparser.skip:
	test -f $@ || $(MAKE) -C OMParser OMBUILDDIR=@OMBUILDDIR@ "host_short=@host_short@" CC="@CC@" CXX="@CXX@" CFLAGS="@CFLAGS@" CPPFLAGS="@CPPFLAGS@" CXXFLAGS="@CXXFLAGS@" CMAKE=$(CMAKE)
omsimulator: omsimulator.skip
omsimulator.skip:
	mkdir -p OMSimulator/build
	cd OMSimulator/build && $(CMAKE) .. \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DCMAKE_C_COMPILER="@CC@" \
		-DCMAKE_CXX_COMPILER="@CXX@" \
		-DCMAKE_C_FLAGS="@CFLAGS@" \
		-DCMAKE_CXX_FLAGS="@CXXFLAGS@" \
		-DCMAKE_INSTALL_PREFIX=../install
	$(MAKE) -C OMSimulator/build/ install
	cp -vpPR OMSimulator/install/include/OMSimulator/ @OMBUILDDIR@/include/omc
	cp -vpPR OMSimulator/install/bin/OMSimulator* @OMBUILDDIR@/bin
	cp -vpPR OMSimulator/install/share/OMSimulator/ @OMBUILDDIR@/share
#	copy the libraries from lib/@host_short@/ OR lib64/. Fails if it can not find at least one of them.
#	This is needed because the autoconf configuration adds the '@host_short@' (e.g. 'x86_64-linux-gnu') even
#	on systems that do not use it to signify library architecture differences. For example, on fedora
#	linux one is supposed to use 'lib64/' instead of 'lib/x86_64-linux-gnu/'. The OMSimulator CMake build will
#	use the lib64 but OpenModelica wants to install to lib/x86_64-linux-gnu/. So try both locations and take
#	the one that exists. If we find other systems with different setups this will need to be extended.
	cp -vpPR OMSimulator/install/lib/@host_short@/* @OMBUILDDIR@/lib/@host_short@/omc/ || \
	cp -vpPR OMSimulator/install/lib64/* @OMBUILDDIR@/lib/@host_short@/omc/
omnotebook: omnotebook.skip
omnotebook.skip: omc omplot
	$(MAKE) -C OMNotebook
omoptim: omoptim.skip
omoptim.skip: omc omplot
	$(MAKE) -C OMOptim
omshell: omshell.skip
omshell.skip: omc
	$(MAKE) -C OMShell
omc-diff: omc-diff.skip
omc-diff.skip:
	$(MAKE) -C testsuite/difftool "OMBUILDDIR=@OMBUILDDIR@" CC="@CC@" CFLAGS="@CFLAGS@"
ReferenceFiles: ReferenceFiles.skip
ReferenceFiles.skip:
	$(MAKE) -C testsuite/ReferenceFiles
libs-for-testing: libs-for-testing.skip
libs-for-testing.skip: omc
	$(MAKE) -C libraries
ffi-test-lib: ffi-test-lib.skip
ffi-test-lib.skip:
	$(MAKE) -C testsuite/flattening/modelica/ffi/FFITest/Resources/BuildProjects/gcc
testsuite-depends: omc-diff ReferenceFiles libs-for-testing omsimulator ffi-test-lib
test: testsuite-depends
	$(MAKE) omc-diff
	cd testsuite/partest && echo "Running all $(./runtests.pl -counttests) tests"
	cd testsuite/partest && ./runtests.pl -with-txt
fast-test: testsuite-depends
	$(MAKE) omc-diff
	cd testsuite/partest && echo "Running $(./runtests.pl -f -counttests) fast tests"
	cd testsuite/partest && ./runtests.pl -f -with-txt
omsens_qt: omsens_qt.skip
omsens_qt.skip:
	$(MAKE) -f $(defaultMakefileTarget) -C OMSens_Qt || true

SOURCE_DIRS_UTF8=OMEdit OMShell/OMShell OMNotebook/OMNotebook OMOptim/OMOptim OMPlot/OMPlot OMCompiler/Compiler/ OMCompiler/SimulationRuntime/ `bash -c echo testsuite/flattening/libraries/3rdParty/{PlanarMechanics,siemens,SiemensPower,ThermoSysPro}` testsuite/openmodelica/modelicaML testsuite/AVM testsuite/simulation
SOURCE_DIRS=$(SOURCE_DIRS_UTF8) # testsuite/flattening/libraries/3rdParty/HumMod

bom-error:
	rm -f bom-error.log bom-error.sh
	echo "#!/bin/sh\ntest \"\`head -c3 \\\"\$$1\\\"\`\" = \"\`/bin/echo -ne \"\\xef\\xbb\\xbf\"\`\" && (echo \$$1 contains BOM >> bom-error.log)" > bom-error.sh
	find $(SOURCE_DIRS) -type f \( ! -path '*/.svn/*' -or -path '*/.git/*' -prune \) \( ! -path 'libraries/Modelica 3.2.1/*' -prune \) -exec sh bom-error.sh "{}" ';'
	@if test -e bom-error.log; then cat bom-error.log; fi
	@test ! -e bom-error.log
	rm -f bom-error.log bom-error.sh

trailing-whitespace-error:
	! find $(SOURCE_DIRS) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec echo -n "{} " ';' -exec grep -c " $$" '{}' ';' | grep -v " 0\$$" | egrep -v '/GenTest/|/antlr-3.2/|qjson-0.8.1|ParadisEO-2.0.1|OMPlot/qwt'

tab-error:
	! find $(SOURCE_DIRS) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec echo -n "{} " ';' -exec grep -c "	" '{}' ';' | grep -v " 0\$$" | egrep -v '/GenTest/|/antlr-3.2/|Parser/MetaModelica_|Parser/ParModelica_|Parser/Modelica_3_|Parser/ModelicaParser'
# Trims trailing whitespace and replaces tabs with spaces
fix-whitespace:
	find . -type f \( ! -path '*/.svn/*' -or -path '*/.git/*' -prune \) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec sh -c 'sed -i -e "s/	/  /g" -e "s/ *\$$//" "{}"' ";"

spellcheck:
	grep -oE 'gettext[(]["]([^"]|([\\]["]))*["]' `ls OMCompiler/Compiler/*/*.mo | grep -v "Flags.*mo"` | sed "s/[\\%]./ /g" | aspell -p ./.openmodelica.aspell --mode=ccpp --lang=en_US list | sort -u | sed 's/^/aspell: /' | tee aspell.log
	@test ! -s aspell.log
thumbsdb-error:
	! find . -name "Thumbs.db" | grep Thumbs.db

utf8-error:
	rm -f utf8-error.log*
	find $(SOURCE_DIRS_UTF8) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec bash -c "iconv -f UTF-8 -t UTF-8 '{}' -o /dev/null 2>utf8-error.log2 || (echo -n '{}: ' && head -n1 utf8-error.log2)"  ';' 2>&1 > utf8-error.log
	# Also detect some valid UTF-8 that were obviously fracked up by Eclipse on Windows.
	find $(SOURCE_DIRS_UTF8) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec bash -c "grep 'Link[^A-Za-z0-9_,.;&-]*ping' '{}' | grep -v Linköping && (echo '{}: Failed Linköping test')"  ';' 2>&1 >> utf8-error.log
	@cat utf8-error.log
	@test ! -s utf8-error.log
	rm -f utf8-error.log*

utf8-iconv:
	rm -f tmp
	find $(SOURCE_DIRS) -regextype posix-egrep -regex '.*\.(cpp|c|h|mo|tpl)$$' -exec bash -c "iconv -f UTF-8 -t UTF-8 '{}' -o /dev/null 2>tmp || (rm -f tmp && cp '{}' tmp && iconv -f ISO-8859-1 -t UTF-8 tmp -o '{}' && echo Converted {} to UTF-8)" ';'
	rm -f tmp

clean:
	test ! -d build || rm -r build/
	for d in @subdirs@ testsuite/difftool libraries; do $(MAKE) -C $$d clean; done

gitclean:
	git submodule foreach --recursive 'git clean -fdx -e /git -e /svn'
	git clean -fdx -e OpenModelicaSetup -e OMSetup -e OMEncryption -e .project -e *.launch -e .vscode

prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
libdir = @libdir@
includedir = @includedir@
docdir = @docdir@
INSTALL_APPDIR     = ${DESTDIR}/Applications/MacPorts/
INSTALL_BINDIR     = ${DESTDIR}${bindir}
INSTALL_LIBDIR     = ${DESTDIR}${libdir}
INSTALL_INCLUDEDIR = ${DESTDIR}${includedir}
INSTALL_DATADIR    = ${DESTDIR}${datadir}
INSTALL_DOCDIR     = ${DESTDIR}${docdir}
INSTALL_SHAREDIR   = ${DESTDIR}${datadir}
INSTALL_MANDIR     = ${DESTDIR}${datadir}/man/
INSTALL_JAVADIR    = ${DESTDIR}${datadir}/omc/java
INSTALL_LOCALEDIR  = ${DESTDIR}${datadir}/locale

install-dirs:
	@test ! "${DESTDIR}/@prefix@" -ef "@OMBUILDDIR@" || (echo Error: Install and build dirs are the same && false)
	if [ "@APP@" = ".app" ]; then mkdir -p ${INSTALL_APPDIR}; fi
	mkdir -p ${INSTALL_BINDIR}
	mkdir -p ${INSTALL_LIBDIR}
	mkdir -p ${INSTALL_INCLUDEDIR}/omc
	mkdir -p ${INSTALL_INCLUDEDIR}/@host_short@/omc/antlr3
	mkdir -p ${INSTALL_DOCDIR}/omc/testmodels
	mkdir -p ${INSTALL_MANDIR}/man1/
	mkdir -p ${INSTALL_LOCALEDIR}
	test ! -d "@OMBUILDDIR@"/share/omnotebook/nls/ || mkdir -p ${INSTALL_SHAREDIR}/omnotebook/nls/
	test ! -d "@OMBUILDDIR@"/share/omshell/nls/ || mkdir -p ${INSTALL_SHAREDIR}/omshell/nls/
	test ! -d "@OMBUILDDIR@"/share/omedit/nls/ || mkdir -p ${INSTALL_SHAREDIR}/omedit/nls/
	mkdir -p ${INSTALL_SHAREDIR}/omc/scripts ${INSTALL_JAVADIR}

install: install-dirs
	echo Installing OpenModelica...
	# Application directory (OSX)
	if [ "@APP@" = ".app" ]; then cp -a "@OMBUILDDIR@"/Applications/* $(INSTALL_APPDIR); fi
	# Binaries
	cp -a "@OMBUILDDIR@"/bin/* ${INSTALL_BINDIR}
	# Libraries
	cp -a "@OMBUILDDIR@"/lib/* ${INSTALL_LIBDIR}
	# Includes
	cp -a "@OMBUILDDIR@"/include/* ${INSTALL_INCLUDEDIR}
	# License
	cp -p OMCompiler/COPYING ${INSTALL_DOCDIR}/omc/
	# Documents
	test ! -e "doc" || cp -p doc/*.pdf ${INSTALL_DOCDIR}/omc/
	cp -p "@OMBUILDDIR@"/share/doc/omc/testmodels/* ${INSTALL_DOCDIR}/omc/testmodels/
	# man pages
	# cp -p "@OMBUILDDIR@"/share/man/man1/*.gz ${INSTALL_MANDIR}/man1/
	# Shared data
	cp -a "@OMBUILDDIR@"/share/omc/*.* "@OMBUILDDIR@"/share/omc/runtime ${INSTALL_SHAREDIR}/omc/
	test ! -d "@OMBUILDDIR@"/share/omnotebook/ || cp -rp "@OMBUILDDIR@"/share/omnotebook/* ${INSTALL_SHAREDIR}/omnotebook/
	test ! -d "@OMBUILDDIR@"/share/omshell/ || cp -p "@OMBUILDDIR@"/share/omshell/*.xml ${INSTALL_SHAREDIR}/omshell/
	test ! -d "@OMBUILDDIR@"/share/omshell/nls/ || cp -p "@OMBUILDDIR@"/share/omshell/nls/*.qm ${INSTALL_SHAREDIR}/omshell/nls/
	test ! -d "@OMBUILDDIR@"/share/omedit/nls/ || cp -p "@OMBUILDDIR@"/share/omedit/nls/*.qm ${INSTALL_SHAREDIR}/omedit/nls/
	test ! -d "@OMBUILDDIR@"/share/omnotebook/nls/ || cp -p "@OMBUILDDIR@"/share/omnotebook/nls/*.qm ${INSTALL_SHAREDIR}/omnotebook/nls/
	test ! -d "@OMBUILDDIR@"/share/OMSimulator/ || cp -rp "@OMBUILDDIR@"/share/OMSimulator ${INSTALL_SHAREDIR}/
	# Scripts
	cp -rp "@OMBUILDDIR@"/share/omc/scripts/*.* ${INSTALL_SHAREDIR}/omc/scripts
	# Java
	cp -p "@OMBUILDDIR@"/share/omc/java/* ${INSTALL_JAVADIR}
	# Translations
	# cp -rp "@OMBUILDDIR@"/share/locale/* ${INSTALL_LOCALEDIR}/
	test ! -d "@OMBUILDDIR@"/share/omlibrary || cp -rp "@OMBUILDDIR@"/share/omlibrary "${INSTALL_SHAREDIR}/"

Makefile: Makefile.in config.status
	./config.status
config.status: configure
	./config.status -recheck
configure: configure.ac
	autoreconf --install
