%global  originalname mbedtls
%if 0%{?fedora} || 0%{?rhel} >= 7
%global _docdir_fmt %{originalname}
%endif

Name: mbedtls3
Version: 3.3.0
Release: 1%{?dist}
Summary: Light-weight cryptographic and SSL/TLS library
License: Apache-2.0
URL:     https://www.trustedfirmware.org/projects/mbed-tls
Source0: https://github.com/Mbed-TLS/%{originalname}/archive/refs/tags/v%{version}.tar.gz

BuildRequires: make
BuildRequires: gcc
BuildRequires: cmake
BuildRequires: doxygen
BuildRequires: graphviz
BuildRequires: perl-interpreter
BuildRequires: python3

%description
Mbed TLS is a light-weight open source cryptographic and SSL/TLS
library written in C. Mbed TLS makes it easy for developers to include
cryptographic and SSL/TLS capabilities in their (embedded)
applications with as little hassle as possible.

%package        devel
Summary:        Development files for %{name}
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description    devel
The %{name}-devel package contains libraries and header files for
developing applications that use %{name}.

%package        doc
Summary:        Documentation files for %{name}
BuildArch:      noarch

%description    doc
The %{name}-doc package contains documentation.

%prep
%autosetup -n mbedtls-%{version}

%build
export CFLAGS="%{optflags} -Wno-stringop-overflow -Wno-maybe-uninitialized"
export CXXLAGS="%{optflags} -Wno-stringop-overflow -Wno-maybe-uninitialized"

%cmake \
	-DCMAKE_BUILD_TYPE=Release \
	-DLINK_WITH_PTHREAD=ON \
	-DINSTALL_MBEDTLS_HEADERS=ON \
	-DENABLE_PROGRAMS=OFF \
	-DUSE_SHARED_MBEDTLS_LIBRARY=ON \
	-DUSE_STATIC_MBEDTLS_LIBRARY=OFF \
	-DGEN_FILES=OFF

%cmake_build
make apidoc

%install
%cmake_install

# Move cmake files to appropriate location
mkdir -p %{buildroot}/%{_datadir}/cmake/%{name}
mv %{buildroot}/usr/cmake/*.cmake %{buildroot}/%{_datadir}/cmake/%{name}/
rm -r %{buildroot}/usr/cmake

# Library files aren't supposed to be executable, but RPM requires this historically
# for automatic per-file level automatic dependency generation at ELF binaries; see:
# - https://github.com/ARMmbed/mbedtls/commit/280165c9b39091c7c7ffe031430c7cf93ebc4dec
# - https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/PDD6RNQMII472HXM4XAUUWWZKKBGHPTO/
chmod 755 %{buildroot}%{_libdir}/*.so.*

%check
%ctest --output-on-failure --force-new-ctest-process --parallel 1

%ldconfig_scriptlets

%files
%doc ChangeLog
%{!?_licensedir:%global license %%doc}
%license LICENSE
%{_libdir}/*.so.*

%files devel
%{_includedir}/mbedtls/*.h
%{_includedir}/psa/*.h
%{_libdir}/*.so
%dir %{_datadir}/cmake/%{name}
%{_datadir}/cmake/%{name}/*.cmake

%files doc
%doc apidoc/*

%changelog
* Fri Dec 16 2022 Benson Muite <benson_muite@emailplus.org> - 3.3.0-1
- Initial packaging
- Modified from spec file for mbedtls-2.28.1-1.fc38
- Available from https://src.fedoraproject.org/rpms/mbedtls