#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2024 Marcel Telka
#

%patch% 01-compiler_def_static_linker.patch
%patch% 02-fix-tests-rpath.patch
%patch% 05-FILE-stdlib.patch
%patch% 06-system-curses.patch
%patch% 07-test-skip-gir.patch
%patch% 08-h5c++-show-c.patch
%patch% 09-libboost_log.patch
%patch% 10-ld-version-script.patch
%patch% 11-gtk-doc-devhelp2.patch

%include-2%
# Manifest options for developer/build/meson package
COMPONENT_FMRI.meson-meta =		developer/build/meson
COMPONENT_CLASSIFICATION.meson-meta =	Development/Distribution Tools

TEST_STYLE = custom
%include-3%
# We have no polkit
COMPONENT_POST_INSTALL_ACTION += $(RM) -r $(PROTOUSRSHAREDIR)/polkit-1 ;

# Rename section 1 man page(s) to contain version number
COMPONENT_POST_INSTALL_ACTION += \
	for f in $(PROTOUSRSHAREMAN1DIR)/*.1 ; do \
		[[ -f $$f ]] || continue ; \
		for v in $(PYTHON_VERSIONS) ; do \
			[[ "$$f" == "$${f%%$$v.1}" ]] || continue 2 ; \
		done ; \
		$(MV) $$f $${f%%.1}-$(PYTHON_VERSION).1 ; \
	done ;

# Custom test style
# See also: https://mesonbuild.com/MesonCI.html
COMPONENT_TEST_CMD = $(PYTHON) run_tests.py
COMPONENT_TEST_ARGS =
COMPONENT_TEST_ARGS += -v		# verbose
COMPONENT_TEST_ARGS += --failfast
COMPONENT_TEST_TARGETS =
COMPONENT_TEST_ENV += PYTEST_ADDOPTS="$(PYTEST_ADDOPTS)"
# Testing expects GNU msgfmt and GNU make
COMPONENT_TEST_ENV += PATH="$(PATH.gnu)"
# Testing needs to find Qt5/Qt6 libraries
COMPONENT_TEST_ENV += LD_LIBRARY_PATH=$(QT5_LIBDIR):$(QT6_LIBDIR)

# Exhausts memory
PYTEST_ADDOPTS += --deselect unittests/linuxliketests.py::LinuxlikeTests::test_generate_gir_with_address_sanitizer
# Uses valgrind and fails (needs further investigation)
PYTEST_ADDOPTS += --deselect unittests/allplatformstests.py::AllPlatformTests::test_testsetups
# Python 2 test that fails in our environment
PYTEST_ADDOPTS += --deselect unittests/machinefiletests.py::NativeFileTests::test_python_module

# Remove timing
COMPONENT_TEST_TRANSFORMS += "-e '/^Total .*time:/d'"

# This project does not support tox so we need to provide test requirements
# manually.
TEST_REQUIRED_PACKAGES.python += library/python/pytest-xdist

# Manually added test dependencies to make sure we test as much as possible and
# to get consistent test results
TEST_REQUIRED_PACKAGES += developer/documentation-tool/gtk-doc
TEST_REQUIRED_PACKAGES += library/c++/hdf5
TEST_REQUIRED_PACKAGES += library/qt5
TEST_REQUIRED_PACKAGES += library/qt6
%hook-manifest%
# Create mediated symlinks
printf '<transform file path=(usr/share/man/man1/)([^/]+)-(3\\.\\d+)\\.1$ -> emit link path=%%<1>%%<2>.1 target=%%<2>-%%<3>.1 mediator=python mediator-version=%%<3> >\n' >> "$DISTRIBUTION-PYVER.p5m"

# Create manifest for developer/build/meson package
cat "$WS_TOP/transforms/manifest-metadata-template" \
	| sed -e 's/^#.*Copyright.*<contributor>.*$/# This file was automatically generated using '"$THIS"'/g' \
	> "$DISTRIBUTION-meta.p5m"
cat <<-"EOF" >> "$DISTRIBUTION-meta.p5m"
depend type=require fmri=library/python/meson
EOF
MANIFEST="$MANIFEST $DISTRIBUTION-meta.p5m"