File mingw-nss.spec of Package mingw-nss

%{?mingw_package_header}
%define _pkg_name nss

Name:		mingw-%{_pkg_name}
Version:	3.14
Release:	1%{?dist}
Summary:	MinGW build of Network Security Services

License:	MPLv1.1 or GPLv2+ or LGPLv2+
URL:		http://www.mozilla.org/projects/security/pki/nss/
Source0:	https://ftp.mozilla.org/pub/mozilla.org/security/nss/releases/NSS_3_14_RTM/src/nss-%{version}.tar.gz

Source1:        nss.pc.in
Source2:        nss-config.in
Source3:        blank-cert8.db
Source4:        blank-key3.db
Source5:        blank-secmod.db

Patch01:	nss-3.13.3-build.patch

BuildArch:	noarch

BuildRequires:	mingw32-filesystem >= 95
BuildRequires:	mingw64-filesystem >= 95
BuildRequires:	mingw32-gcc mingw64-gcc
BuildRequires:	mingw32-binutils mingw64-binutils
BuildRequires:	mingw32-nspr mingw64-nspr
BuildRequires:	mingw32-sqlite mingw64-sqlite
BuildRequires:	mingw32-zlib mingw64-zlib
BuildRequires:	mingw32-pkg-config mingw64-pkg-config

%description
Network Security Services (NSS) is a set of libraries designed to
support cross-platform development of security-enabled client and
server applications. Applications built with NSS can support SSL v2
and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509
v3 certificates, and other security standards.

%package -n mingw32-%{_pkg_name}
Summary:	%{summary}

%description -n mingw32-%{_pkg_name}
Network Security Services (NSS) is a set of libraries designed to
support cross-platform development of security-enabled client and
server applications. Applications built with NSS can support SSL v2
and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509
v3 certificates, and other security standards.

%package -n mingw64-%{_pkg_name}
Summary:	%{summary}

%description -n mingw64-%{_pkg_name}
Network Security Services (NSS) is a set of libraries designed to
support cross-platform development of security-enabled client and
server applications. Applications built with NSS can support SSL v2
and v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509
v3 certificates, and other security standards.

%{?mingw_debug_package}

%prep
%setup -q -n %{_pkg_name}-%{version}

%patch01 -p1 -b .build

# Because this is a poorly-built library
pushd mozilla

mkdir -p ../build_win32
cp -R * ../build_win32

mkdir -p ../build_win64
cp -R * ../build_win64

%build
export PKG_CONFIG_ALLOW_SYSTEM_LIBS=1
export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1

# Building for Win32
pushd build_win32/security

NSPR_INCLUDE_DIR=`%{mingw32_target}-pkg-config --cflags-only-I nspr | sed 's/-I//'`
NSPR_LIB_DIR=`%{mingw32_target}-pkg-config --libs-only-L nspr | sed 's/-L//'`

export NSPR_INCLUDE_DIR
export NSPR_LIB_DIR

MINGW32_MAKE_ARGS="OS_TARGET=WINNT OS_RELEASE=5.0 XP_WIN=1 
	NSINSTALL=$(pwd)/coreconf/nsinstall/nsinstall NS_USE_GCC=1 
	NSS_USE_SYSTEM_SQLITE=1 USE_SYSTEM_ZLIB=1 ZLIB_LIBS=%{mingw32_libdir}/libz.dll.a 
	CC=%{mingw32_cc} RC=%{mingw32_windres} RANLIB=%{mingw32_ranlib}"

(cd coreconf/nsinstall && gcc -o nsinstall nsinstall.c pathsub.c)

%mingw32_make -C dbm ${MINGW32_MAKE_ARGS}
%mingw32_make -C nss ${MINGW32_MAKE_ARGS}

popd

# Building for Win64
pushd build_win64/security

NSPR_INCLUDE_DIR=`%{mingw64_target}-pkg-config --cflags-only-I nspr | sed 's/-I//'`
NSPR_LIB_DIR=`%{mingw64_target}-pkg-config --libs-only-L nspr | sed 's/-L//'`

export NSPR_INCLUDE_DIR
export NSPR_LIB_DIR

MINGW64_MAKE_ARGS="OS_TARGET=WINNT OS_RELEASE=5.0 XP_WIN=1 
	NSINSTALL=$(pwd)/coreconf/nsinstall/nsinstall NS_USE_GCC=1 USE_64=1 
	NSS_USE_SYSTEM_SQLITE=1 USE_SYSTEM_ZLIB=1 ZLIB_LIBS=%{mingw64_libdir}/libz.dll.a 
	CC=%{mingw64_cc} RC=%{mingw64_windres} RANLIB=%{mingw64_ranlib}"

(cd coreconf/nsinstall && gcc -o nsinstall nsinstall.c pathsub.c)

%mingw64_make -C dbm ${MINGW64_MAKE_ARGS}
%mingw64_make -C nss ${MINGW64_MAKE_ARGS}

popd

%install
export PKG_CONFIG_PATH=%{mingw64_libdir}/pkgconfig

mkdir -p %{buildroot}%{mingw32_libdir}/pkgconfig
mkdir -p %{buildroot}%{mingw64_libdir}/pkgconfig

# Manually create the .pc file
cat %{SOURCE1} | sed -e "s,%%libdir%%,%{mingw64_libdir},g" \
			-e "s,%%prefix%%,%{mingw64_prefix},g" \
			-e "s,%%exec_prefix%%,%{mingw64_prefix},g" \
			-e "s,%%includedir%%,%{mingw64_includedir}/nss3,g" \
			-e "s,%%NSPR_VERSION%%,4\.9\.4,g" \
			-e "s,%%NSS_VERSION%%,%{version},g" > \
			%{buildroot}%{mingw64_libdir}/pkgconfig/nss.pc
cat %{SOURCE1} | sed -e "s,%%libdir%%,%{mingw32_libdir},g" \
			-e "s,%%prefix%%,%{mingw32_prefix},g" \
			-e "s,%%exec_prefix%%,%{mingw32_prefix},g" \
			-e "s,%%includedir%%,%{mingw32_includedir}/nss3,g" \
			-e "s,%%NSPR_VERSION%%,4\.9\.4,g" \
			-e "s,%%NSS_VERSION%%,%{version},g" > \
			%{buildroot}%{mingw32_libdir}/pkgconfig/nss.pc

NSS_VMAJOR=`cat mozilla/security/nss/lib/nss/nss.h \
	| grep "#define.*NSS_VMAJOR" \
	| awk '{print $3}'`
NSS_VMINOR=`cat mozilla/security/nss/lib/nss/nss.h \
	| grep "#define.*NSS_VMINOR" \
	| awk '{print $3}'`
NSS_VPATCH=`cat mozilla/security/nss/lib/nss/nss.h \
	| grep "#define.*NSS_VPATCH" \
	| awk '{print $3}'`


export NSS_VMAJOR 
export NSS_VMINOR 
export NSS_VPATCH

mkdir -p %{buildroot}%{mingw32_bindir}
mkdir -p %{buildroot}%{mingw64_bindir}

cat %{SOURCE2} | sed -e "s,@libdir@,%{mingw64_libdir},g" \
			-e "s,@prefix@,%{mingw64_prefix},g" \
			-e "s,@exec_prefix@,%{mingw64_prefix},g" \
			-e "s,@includedir@,%{mingw64_includedir}/nss3,g" \
			-e "s,@MOD_MAJOR_VERSION@,$NSS_VMAJOR,g" \
			-e "s,@MOD_MINOR_VERSION@,$NSS_VMINOR,g" \
			-e "s,@MOD_PATCH_VERSION@,$NSS_VPATCH,g" \
			> $RPM_BUILD_ROOT%{mingw64_bindir}/nss-config

cat %{SOURCE2} | sed -e "s,@libdir@,%{mingw32_libdir},g" \
			-e "s,@prefix@,%{mingw32_prefix},g" \
			-e "s,@exec_prefix@,%{mingw32_prefix},g" \
			-e "s,@includedir@,%{mingw32_includedir}/nss3,g" \
			-e "s,@MOD_MAJOR_VERSION@,$NSS_VMAJOR,g" \
			-e "s,@MOD_MINOR_VERSION@,$NSS_VMINOR,g" \
			-e "s,@MOD_PATCH_VERSION@,$NSS_VPATCH,g" \
			> %{buildroot}%{mingw32_bindir}/nss-config

chmod 755 %{buildroot}%{mingw64_bindir}/nss-config
chmod 755 %{buildroot}%{mingw32_bindir}/nss-config

# There is no 'make install' target, so we do it all manually
mkdir -p %{buildroot}%{mingw64_includedir}/nss3
mkdir -p %{buildroot}%{mingw64_bindir}
mkdir -p %{buildroot}%{mingw64_libdir}/nss

mkdir -p %{buildroot}%{mingw32_includedir}/nss3
mkdir -p %{buildroot}%{mingw32_bindir}
mkdir -p %{buildroot}%{mingw32_libdir}/nss

for file in softokn nss nssutil \
		ssl smime nssdbm
do
	install -m 755 build_win64/dist/*.OBJ/lib/${file}3.dll %{buildroot}%{mingw64_bindir}/
	install -m 644 build_win64/dist/*.OBJ/lib/lib${file}3.a %{buildroot}%{mingw64_libdir}/lib${file}3.dll.a

	install -m 755 build_win32/dist/*.OBJ/lib/${file}3.dll %{buildroot}%{mingw32_bindir}
	install -m 644 build_win32/dist/*.OBJ/lib/lib${file}3.a %{buildroot}%{mingw32_libdir}/lib${file}3.dll.a
done

install -m 755 build_win64/dist/*.OBJ/lib/nssckbi.dll %{buildroot}%{mingw64_bindir}/
install -m 755 build_win64/dist/*.OBJ/lib/freebl3.dll %{buildroot}%{mingw64_bindir}/

install -m 755 build_win32/dist/*.OBJ/lib/nssckbi.dll %{buildroot}%{mingw32_bindir}/
install -m 755 build_win32/dist/*.OBJ/lib/freebl3.dll %{buildroot}%{mingw32_bindir}/

# Install the empty NSS db files
mkdir -p %{buildroot}%{mingw64_sysconfdir}/pki/nssdb
mkdir -p %{buildroot}%{mingw32_sysconfdir}/pki/nssdb

install -m 644 %{SOURCE3} %{buildroot}%{mingw64_sysconfdir}/pki/nssdb/cert8.db
install -m 644 %{SOURCE4} %{buildroot}%{mingw64_sysconfdir}/pki/nssdb/key3.db
install -m 644 %{SOURCE5} %{buildroot}%{mingw64_sysconfdir}/pki/nssdb/secmod.db

install -m 644 %{SOURCE3} %{buildroot}%{mingw32_sysconfdir}/pki/nssdb/cert8.db
install -m 644 %{SOURCE4} %{buildroot}%{mingw32_sysconfdir}/pki/nssdb/key3.db
install -m 644 %{SOURCE5} %{buildroot}%{mingw32_sysconfdir}/pki/nssdb/secmod.db

# And more development files
for file in libcrmf.a libnssb.a libnssckfw.a
do
	install -m 644 build_win64/dist/*.OBJ/lib/${file} %{buildroot}%{mingw64_libdir}
	install -m 644 build_win32/dist/*.OBJ/lib/${file} %{buildroot}%{mingw32_libdir}
done

# The executable binaries
for file in certutil cmsutil crlutil modutil pk12util signtool signver ssltap
do
	install -m 755 build_win64/dist/*.OBJ/bin/${file}.exe %{buildroot}%{mingw64_bindir}
	install -m 755 build_win32/dist/*.OBJ/bin/${file}.exe %{buildroot}%{mingw32_bindir}
done

# And more, unsupported(?), binaries
for file in atob btoa derdump ocspclnt pp selfserv shlibsign strsclnt symkeyutil \
 tstclnt vfyserv vfychain
do
  install -m 755 build_win64/dist/*.OBJ/bin/${file}.exe %{buildroot}%{mingw64_libdir}/nss
  install -m 755 build_win32/dist/*.OBJ/bin/${file}.exe %{buildroot}%{mingw32_libdir}/nss
done

for file in build_win32/dist/public/nss/*.h
do
	install -m 644 ${file} %{buildroot}%{mingw32_includedir}/nss3
done
for file in build_win64/dist/public/nss/*.h
do
	install -m 644 ${file} %{buildroot}%{mingw64_includedir}/nss3
done

%files -n mingw32-%{_pkg_name}
%doc mozilla/security/nss/COPYING
%{mingw32_bindir}/*.dll
%{mingw32_bindir}/*.exe
%{mingw32_bindir}/nss-config
%{mingw32_libdir}/*.a
%{mingw32_libdir}/pkgconfig/nss.pc
%{mingw32_libdir}/nss/
%{mingw32_includedir}/nss3
%{mingw32_sysconfdir}/pki/nssdb/*.db

%files -n mingw64-%{_pkg_name}
%doc mozilla/security/nss/COPYING
%{mingw64_bindir}/*.dll
%{mingw64_bindir}/*.exe
%{mingw64_bindir}/nss-config
%{mingw64_libdir}/*.a
%{mingw64_libdir}/pkgconfig/nss.pc
%{mingw64_libdir}/nss/
%{mingw64_includedir}/nss3
%{mingw64_sysconfdir}/pki/nssdb/*.db

%changelog
* Mon Dec 11 2012 Greg Hellings <greg.hellings@gmail.com> - 3.14-1
- Initial build