%bcond_with snapshot_build

%if %{with snapshot_build}
# Unlock LLVM Snapshot LUA functions
%{llvm_sb_verbose}
%{llvm_sb}
%endif

# Opt out of https://fedoraproject.org/wiki/Changes/fno-omit-frame-pointer
# https://bugzilla.redhat.com/show_bug.cgi?id=2158587
%undefine _include_frame_pointers

%bcond_without check
%bcond_with compat_build

%global maj_ver 17
%global min_ver 0
%global patch_ver 2
#global rc_ver 4

%if %{with snapshot_build}
%undefine rc_ver
%global maj_ver %{llvm_snapshot_version_major}
%global min_ver %{llvm_snapshot_version_minor}
%global patch_ver %{llvm_snapshot_version_patch}
%endif

%global lld_version %{maj_ver}.%{min_ver}.%{patch_ver}

%global lld_srcdir lld-%{lld_version}%{?rc_ver:rc%{rc_ver}}.src

%if %{with compat_build}
%global pkg_name lld%{maj_ver}
%global install_prefix %{_libdir}/llvm%{maj_ver}
%global install_includedir %{install_prefix}/include
%global install_libdir %{install_prefix}/lib
%global install_datadir %{install_prefix}/share
%else
%global pkg_name lld
%global install_prefix /usr
%global install_includedir %{_includedir}
%global install_libdir %{_libdir}
%global install_datadir %{_datadir}
%endif

%bcond_with ld_alternative

Name:		%{pkg_name}
Version:	%{lld_version}%{?rc_ver:~rc%{rc_ver}}%{?llvm_snapshot_version_suffix:~%{llvm_snapshot_version_suffix}}
Release:	1%{?dist}
Summary:	The LLVM Linker

License:	NCSA
URL:		http://llvm.org
%if %{with snapshot_build}
Source0:	%{llvm_snapshot_source_prefix}lld-%{llvm_snapshot_yyyymmdd}.src.tar.xz
%{llvm_snapshot_extra_source_tags}
%else
Source0:	https://github.com/llvm/llvm-project/releases/download/llvmorg-%{maj_ver}.%{min_ver}.%{patch_ver}%{?rc_ver:-rc%{rc_ver}}/%{lld_srcdir}.tar.xz
Source1:	https://github.com/llvm/llvm-project/releases/download/llvmorg-%{maj_ver}.%{min_ver}.%{patch_ver}%{?rc_ver:-rc%{rc_ver}}/%{lld_srcdir}.tar.xz.sig
Source2:	release-keys.asc
%endif

ExcludeArch:	s390x

# Bundle libunwind header need during build for MachO support
Patch1:		0002-PATCH-lld-Import-compact_unwind_encoding.h-from-libu.patch


BuildRequires:	gcc
BuildRequires:	gcc-c++
BuildRequires:	cmake
BuildRequires:	ninja-build
%if %{with compat_build}
BuildRequires:	llvm%{maj_ver}-devel = %{version}
BuildRequires:	llvm%{maj_ver}-cmake-utils = %{version}
%else
BuildRequires:	llvm-devel = %{version}
BuildRequires:	llvm-cmake-utils = %{version}
BuildRequires:	llvm-test = %{version}
BuildRequires:	llvm-googletest = %{version}
%endif
BuildRequires:	ncurses-devel
BuildRequires:	zlib-devel
BuildRequires:	python3-devel

# For make check:
BuildRequires:	python3-rpm-macros
BuildRequires:	python3-lit

# For gpg source verification
BuildRequires:	gnupg2

%if %{with ld_alternative}
Requires(post): %{_sbindir}/alternatives
Requires(preun): %{_sbindir}/alternatives
%endif

Requires: %{name}-libs = %{version}-%{release}

%description
The LLVM project linker.

%package devel
Summary:	Libraries and header files for LLD
Requires: %{name}-libs%{?_isa} = %{version}-%{release}
%if %{without compat_build}
# lld tools are referenced in the cmake files, so we need to add lld as a
# dependency.
Requires: %{name}%{?_isa} = %{version}-%{release}
%endif

%description devel
This package contains library and header files needed to develop new native
programs that use the LLD infrastructure.

%package libs
Summary:	LLD shared libraries

%description libs
Shared libraries for LLD.

%prep
%if %{without snapshot_build}
#{gpgverify} --keyring='%{SOURCE2}' --signature='%{SOURCE1}' --data='%{SOURCE0}'
%endif

%autosetup -n %{lld_srcdir} -p2

%if %{with compat_build}
# For compat builds, we don't want to build the actual lld binary. While there is an
# LLD_BUILD_TOOLS cmake option, it is incomplete in various ways (e.g. still leaves install
# targets and symlinks), so instead skip the tools/lld build entirely.
# We can't simply delete the binaries after the fact, because this would leave checks for
# their existence in the cmake exports.
sed 's/add_subdirectory(tools\/lld)//' -i CMakeLists.txt
%endif

%build

%undefine __cmake_in_source_build

%cmake \
	-GNinja \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DCMAKE_INSTALL_PREFIX=%{install_prefix} \
	-DLLVM_LINK_LLVM_DYLIB:BOOL=ON \
	-DLLVM_DYLIB_COMPONENTS="all" \
	-DLLVM_COMMON_CMAKE_UTILS=%{install_datadir}/llvm/cmake \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DPYTHON_EXECUTABLE=%{__python3} \
%if %{with compat_build}
	-DLLVM_CMAKE_DIR=%{install_libdir}/cmake/llvm \
	-DLLVM_INCLUDE_TESTS=OFF \
%else
	-DLLVM_INCLUDE_TESTS=ON \
	-DLLVM_EXTERNAL_LIT=%{_bindir}/lit \
	-DLLVM_LIT_ARGS="-sv \
	--path %{_libdir}/llvm" \
%if %{with snapshot_build}
	-DLLVM_VERSION_SUFFIX="%{llvm_snapshot_version_suffix}" \
%endif
%if 0%{?__isa_bits} == 64
	-DLLVM_LIBDIR_SUFFIX=64 \
%else
	-DLLVM_LIBDIR_SUFFIX= \
%endif
%endif
	-DLLVM_MAIN_SRC_DIR=%{_datadir}/llvm/src

%cmake_build

%install

# Install libraries and binaries
%cmake_install

# This is generated by Patch1 during build and (probably) must be removed afterward
rm %{buildroot}%{install_includedir}/mach-o/compact_unwind_encoding.h

install -D -m 644 -t  %{buildroot}%{_mandir}/man1/ docs/ld.lld.1

%if %{with ld_alternative}
# Required when using update-alternatives:
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Alternatives/
touch %{buildroot}%{_bindir}/ld

%post
%{_sbindir}/update-alternatives --install %{_bindir}/ld ld %{_bindir}/ld.lld 1

%postun
if [ $1 -eq 0 ] ; then
  %{_sbindir}/update-alternatives --remove ld %{_bindir}/ld.lld
fi
%endif

%check

%if %{without compat_build}
%if %{with check}
%cmake_build --target check-lld
%endif

%ldconfig_scriptlets libs
%endif

%if %{without compat_build}
%files
%license LICENSE.TXT
%if %{with ld_alternative}
%ghost %{_bindir}/ld
%endif
%{_bindir}/lld*
%{_bindir}/ld.lld
%{_bindir}/ld64.lld
%{_bindir}/wasm-ld
%{_mandir}/man1/ld.lld.1*
%endif

%files devel
%{install_includedir}/lld
%{install_libdir}/liblld*.so
%{install_libdir}/cmake/lld/

%files libs
%{install_libdir}/liblld*.so.*

%changelog
* Wed Oct 04 2023 Nikita Popov <npopov@redhat.com> - 17.0.2-1
- Update to LLVM 17.0.2

* Fri Jun 30 2023 Tom Stellard <tstellar@redhat.com> - 16.0.6-1
- 16.0.6 Release

* Fri Apr 28 2023 Tom Stellard <tstellar@redhat.com> - 16.0.0-1
- 16.0.0 Release

* Thu Jan 19 2023 Tom Stellard <tstellar@redhat.com> - 15.0.7-1
- Update to LLVM 15.0.7

* Tue Sep 06 2022 Nikita Popov <npopov@redhat.com> - 15.0.0-1
- Update to LLVM 15.0.0

* Tue Jun 28 2022 Tom Stellard <tstellar@redhat.com> - 14.0.6-1
- 14.0.6 Release

* Fri May 06 2022 Timm Bäder <tbaeder@redhat.com> - 14.0.0-2
- Backport ignoring --no-add-needed

* Thu Apr 07 2022 Timm Bäder <tbaeder@redhat.com> - 14.0.0-1
- Update to 14.0.0

* Thu Feb 03 2022 Tom Stellard <tstellar@redhat.com> - 13.0.1-1
- 13.0.1 Release

* Thu Dec 09 2021 Tom Stellard <tstellar@redhat.com> - 13.0.0-2
- Drop lld-test package

* Fri Oct 15 2021 Tom Stellard <tstellar@redhat.com> - 13.0.0-1
- 13.0.0 Release

* Fri Jul 16 2021 sguelton@redhat.com - 12.0.1-1
- 12.0.1 release

* Thu May 6 2021 sguelton@redhat.com - 12.0.0-1
- 12.0.0 release

* Mon Nov 09 2020 sguelton@redhat.com - 11.0.0-3
- Exclude s390x, see rhbz#1894927

* Thu Oct 29 2020 sguelton@redhat.com - 11.0.0-1
- 11.0.0 final

* Fri Sep 18 2020 sguelton@redhat.com - 11.0.0-0.1.rc2
- 11.0.0-rc2 Release

* Fri Jul 24 2020 sguelton@redhat.com - 10.0.1-1
- 10.0.1 release

* Mon Jul 20 2020 sguelton@redhat.com - 10.0.0-2
- Fix arch-dependent tarball

* Thu Apr 9 2020 sguelton@redhat.com - 10.0.0-1
- 10.0.0 final

* Thu Dec 19 2019 Tom Stellard <tstellar@redhat.com> -9.0.1-1
- 9.0.1 Release

* Fri Dec 13 2019 Tom Stellard <tstellar@redhat.com> - 9.0.0-5
- Fix some rpmdiff errors

* Fri Dec 13 2019 Tom Stellard <tstellar@redhat.com> - 9.0.0-4
- Remove build artifacts installed with unittests

* Thu Dec 05 2019 Tom Stellard <tstellar@redhat.com> - 9.0.0-3
- Add lld-test package

* Thu Nov 14 2019 Tom Stellard <tstellar@redhat.com> - 9.0.0-2
- Add explicit lld-libs requires to fix rpmdiff errors

* Thu Sep 26 2019 Tom Stellard <tstellar@redhat.com> - 9.0.0-1
- 9.0.0 Release

* Thu Aug 1 2019 sguelton@redhat.com - 8.0.1-1
- 8.0.1 release

* Mon Jun 17 2019 sguelton@redhat.com - 8.0.1-0.2.rc2
- Remove unnecessary threading patch

* Thu Jun 13 2019 sguelton@redhat.com - 8.0.1-0.1.rc2
- 8.0.1rc2 Release

* Tue Apr 16 2019 sguelton@redhat.com - 8.0.0-1
- 8.0.0 Release

* Mon Jan 14 2019 sguelton@redhat.com - 7.0.1-3
- Fix lld + annobin integration & Setup basic CI tests

* Sat Dec 15 2018 Tom Stellard <tstellar@redhat.com> - 7.0.1-2
- Bump required python3-lit version

* Fri Dec 14 2018 Tom Stellard <tstellar@redhat.com> - 7.0.1-1
- 7.0.1-1 Release

* Mon Dec 10 2018 Tom Stellard <tstellar@redhat.com> - 7.0.1-0.2.rc3
- 7.0.1-rc3 Release

* Tue Nov 27 2018 Tom Stellard <tstellar@redhat.com> - 7.0.0-1
- 7.0.0 Release

* Mon Oct 01 2018 Tom Stellard <tstellar@redhat.com> - 6.0.1-2
- Drop scl macros

* Wed Jun 27 2018 Tom Stellard <tstellar@redhat.com> - 6.0.1-1
- 6.0.1 Release

* Fri May 11 2018 Tom Stellard <tstellar@redhat.com> - 6.0.1-0.1.rc1
- 6.0.1-rc1 Release

* Thu Mar 08 2018 Tom Stellard <tstellar@redhat.com> - 6.0.0-1
- 6.0.0 Release

* Tue Feb 13 2018 Tom Stellard <tstellar@redhat.com> - 6.0.0-0.3.rc2
- 6.0.0-rc2 Release

* Thu Feb 08 2018 Fedora Release Engineering <releng@fedoraproject.org> - 6.0.0-0.2.rc1
- Rebuilt for https://fedoraproject.org/wiki/Fedora_28_Mass_Rebuild

* Thu Jan 25 2018 Tom Stellard <tstellar@redhat.com> - 6.0.0-0.1.rc1
- 6.0.0-rc1 Release

* Thu Dec 21 2017 Tom Stellard <tstellar@redhat.com> - 5.0.1-1
- 5.0.1 Release

* Mon Sep 11 2017 Tom Stellard <tstellar@redhat.com> - 5.0.0-1
- 5.0.0 Release

* Thu Aug 03 2017 Fedora Release Engineering <releng@fedoraproject.org> - 4.0.1-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_27_Binutils_Mass_Rebuild

* Wed Jul 26 2017 Fedora Release Engineering <releng@fedoraproject.org> - 4.0.1-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_27_Mass_Rebuild

* Thu Jul 06 2017 Tom Stellard <tstellar@redhat.com> - 4.0.1-2
- Backport r307092

* Tue Jul 04 2017 Tom Stellard <tstellar@redhat.com> - 4.0.1-1
- 4.0.1 Release

* Tue Jul 04 2017 Tom Stellard <tstellar@redhat.com> - 4.0.0-4
- Fix build without llvm-static

* Wed May 31 2017 Tom Stellard <tstellar@redhat.com> - 4.0.0-3
- Remove llvm-static dependency

* Mon May 15 2017 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 4.0.0-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_26_27_Mass_Rebuild

* Tue Mar 14 2017 Tom Stellard <tstellar@redhat.com> - 4.0.0-1
- lld 4.0.0 Final Release
