# The naming scheme in non-Mandriva-based distros
%if 0%{!?mklibname:1}
%define mklibname(ds)  lib%{1}%{?2:%{2}}%{-s:-static}%{-d:-devel}
%endif

%define major 21
%define x509major 7
%define cryptomajor 16
%define libname %mklibname %{name} %{major}
%define libx509 %mklibname mbedx590_ %{x509major}
%define libcrypto %mklibname mbedcrypto %{cryptomajor}
%define libeverest %mklibname everest
%define libp256m %mklibname p256m
%define develname %mklibname %{name} -d

Name:           mbedtls
# Please stay on LTS branch 3.6.x:
# https://www.trustedfirmware.org/blog/mbed-tls-3-6-0-lts
Version:        3.6.1
Release:        1%{?dist}
Summary:        Light-weight cryptographic and SSL/TLS library
Group:          System/Libraries
License:        ASL 2.0
URL:            https://www.trustedfirmware.org/projects/mbed-tls/
Source0:        https://github.com/Mbed-TLS/mbedtls/releases/download/v%{version}/%{name}-%{version}.tar.bz2
Source1:        https://github.com/Mbed-TLS/mbedtls/releases/download/%{name}-%{version}/%{name}-%{version}-sha256sum.txt

%if 0%{?suse_version}
%bcond_with apidocs
%else
%bcond_without apidocs
%endif

BuildRequires:  gcc
BuildRequires:  cmake
BuildRequires:  coreutils
# Check:
BuildRequires:  pkgconfig(python3)
BuildRequires:  perl
# Documentation:
%if %{with apidocs}
BuildRequires:  doxygen
BuildRequires:  graphviz
#BuildRequires:  python3dist(breathe)
# Required to convert Markdown to HTML; it could probably be patched to use cmark instead.
#BuildRequires:  pandoc
%endif


%description
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains mbed TLS programs.

%package -n %{libname}
Summary:        mbed TLS library
Group:          System/Libraries

%description -n	%{libname}
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains the library itself.

%package -n %{libx509}
Summary:        mbed TLS X509 library
Group:          System/Libraries

%description -n %{libx509}
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains mbed X509 library.

%package -n %{libcrypto}
Summary:        mbed crypto library
Group:          System/Libraries

%description -n %{libcrypto}
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains mbed mbedcrypto library.

%package -n %{libeverest}
Summary:        mbed everest library
Group:          System/Libraries

%description -n %{libeverest}
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains mbed everest library.

%package -n %{libp256m}
Summary:        mbed p256m library
Group:          System/Libraries

%description -n %{libp256m}
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains mbed p256m library.

%package -n %{develname}
Summary:        mbed TLS development files
Group:          Development/C
Requires:       %{libname} = %{version}-%{release}
Requires:       %{libx509} = %{version}-%{release}
Requires:       %{libcrypto} = %{version}-%{release}
Provides:       %{name}-devel = %{version}-%{release}

%description -n %{develname}
mbed TLS (formerly known as PolarSSL) is an SSL library written in ANSI C.
mbed TLS makes it easy for developers to include cryptographic and SSL/TLS
capabilities in their (embedded) products with as little hassle as possible.
It is designed to be readable, documented, tested, loosely coupled and
portable.

This package contains development files.

%prep
%autosetup -p1
echo "$(cat %{SOURCE1})  %{SOURCE0}" | sha256sum -c

# Enable MBEDTLS_THREADING_PTHREAD and MBEDTLS_THREADING_C flags
# https://bugs.mageia.org/show_bug.cgi?id=15391#c4
sed -i -e 's|.*\(#define MBEDTLS_THREADING_C\)|\1|' \
    -e 's|.*\(#define MBEDTLS_THREADING_PTHREAD\)|\1|' \
    include/mbedtls/mbedtls_config.h

%build
%cmake \
    -DUSE_SHARED_MBEDTLS_LIBRARY=ON \
    -DMBEDTLS_FATAL_WARNINGS=OFF

%cmake_build

%if %{with apidocs}
# Use doxygen to generate API documentation.
%make_build apidoc && rm -rf apidoc/xml && find apidoc -name '*.md5' -exec rm -f '{}' \;
%define develdocname apidoc
# Alternatively, use breathe_apidoc to make readthedocs-style documentation (still requires doxygen to generate the XML feels needed by breathe).
#cd docs && %%make breathe_apidoc && mv _build/html . && rm -rf ../apidoc api requirements* _build && find . -path '*/*/Makefile' -exec sh -c 'cd $(dirname "{}") && %%make_build html && rm -f Makefile *.md' \; || true
#%%define develdocname docs
%endif

%check
# One test fails. Ah, build the RPMs anyway.
%ctest || true

%install
%cmake_install

# prevent possible file clashes
pushd %{buildroot}%{_bindir}
    for i in *; do mv ${i} %{name}-${i}; done
popd

rm -f %{buildroot}%{_libdir}/*.a

%files
%doc ChangeLog README*
%{_bindir}/%{name}-*

%files -n %{libname}
%license LICENSE
%{_libdir}/lib%{name}.so.%{major}
%{_libdir}/lib%{name}.so.%{version}

%files -n %{libx509}
%{_libdir}/libmbedx509.so.%{x509major}
%{_libdir}/libmbedx509.so.%{version}

%files -n %{libcrypto}
%{_libdir}/libmbedcrypto.so.%{cryptomajor}
%{_libdir}/libmbedcrypto.so.%{version}

%files -n %{libeverest}
%{_libdir}/libeverest.so

%files -n %{libp256m}
%{_libdir}/libp256m.so

%files -n %{develname}
%{_includedir}/%{name}/
%{_includedir}/psa/
%{_includedir}/everest/
%{_libdir}/pkgconfig/mbed*.pc
%{_libdir}/libmbed*.so
%{_libdir}/cmake/MbedTLS/*.cmake
%if %{with apidocs}
%doc %{develdocname}
%endif