%bcond_with test

%define build_compat32 %{?_with_compat32:1}%{!?_with_compat32:0}

%define ver_suffix %(echo "%{version}" | cut -d . -f 1)

%define _unpackaged_files_terminate_build 1

# Build options:
# Components skipped by default:
%bcond_with doxygen

# Documentation install path
%global llvmdocdir() %{_docdir}/%1-%{version}

%define _unpackaged_files_terminate_build 1

# build order of LLVM family.
# llvm
# clang
# lld
# compiler-rt
# lldb
# libomp

Summary:        The Low Level Virtual Machine
Summary(ja):    LLVM - 低レベルバーチャルマシン
Name:           llvm
Version:        15.0.2
Release:        1%{?_dist_release}
Group:          programming
Vendor:         Project Vine
Distribution:   Vine Linux

License:        NCSA
URL:            https://llvm.org/
Source0:        https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/llvm-%{version}.src.tar.xz
Source1:        https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/cmake-%{version}.src.tar.xz

# multilib fixes
Source10:        llvm-config.h

# patches

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

BuildRequires:  binutils-devel
BuildRequires:  cmake
BuildRequires:  libedit-devel
BuildRequires:  libffi-devel
BuildRequires:  ncurses-devel
BuildRequires:  ninja
BuildRequires:  python3-devel
BuildRequires:  python3-rpm-macros
BuildRequires:  valgrind-devel
BuildRequires:  zlib-devel

Requires:       llvm%{ver_suffix}-libs = %{version}-%{release}

%description
LLVM is a compiler infrastructure designed for compile-time,
link-time, runtime, and idle-time optimization of programs from
arbitrary programming languages.  The compiler infrastructure includes
mirror sets of programming tools as well as libraries with equivalent
functionality.


%package devel
Summary:        Libraries and header files for LLVM
Summary(ja):    LLVM のライブラリおよびヘッダファイル
Group:          programming
Requires:       %{name} = %{version}-%{release}
Requires:       llvm%{ver_suffix}-libs = %{version}-%{release}
Requires:       libedit-devel

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


%package doc
Summary:        Documentation for LLVM
Summary(ja):    LLVM のドキュメント
Group:          documentation
BuildArch:      noarch
Requires:       %{name} = %{version}-%{release}
# might seem redundant, but needed to kill off the old arch-ed -doc subpackage
Obsoletes:      %{name}-doc < %{version}-%{release}

%description doc
Documentation for the LLVM compiler infrastructure.


%package -n llvm%{ver_suffix}-libs
Summary:        LLVM shared libraries
Summary(ja):    LLVM 共有ライブラリ
Group:          system
Provides:       llvm-libs = %{version}-%{release}
Obsoletes:      llvm-libs <= 7.0.0

%description -n llvm%{ver_suffix}-libs
Shared libraries for the LLVM compiler infrastructure.

%description -n llvm%{ver_suffix}-libs -l ja
LLVM コンパイラ基盤の共有ライブラリです．


%package static
Summary:        LLVM static libraries
Summary(ja):    LLVM 静的ライブラリ
Group:          programming
Requires:       llvm-devel = %{version}-%{release}

%description static
Static libraries for the LLVM compiler infrastructure.

%description static -l ja
LLVM コンパイラ基盤の静的ライブラリです．


# compat32'
%package -n compat32-%{name}%{ver_suffix}-libs
Summary:        LLVM shared libraries
Summary(ja):    LLVM 共有ライブラリ
Group:          system
Provides:       compat32-llvm-libs = %{version}-%{release}
Requires:       %{name}%{ver_suffix}-libs = %{version}-%{release}

%description -n compat32-%{name}%{ver_suffix}-libs
Shared libraries for the LLVM compiler infrastructure.


%debug_package


%prep
%setup -q -n llvm-%{version}.src -a 1
%autopatch -p2

mv -f cmake-%{version}.src/Modules/* cmake/modules/

pathfix%{python3_version}.py -i %{__python3} -pn \
	test/BugPoint/compile-custom.ll.py \
	tools/opt-viewer/*.py \
	utils/update_cc_test_checks.py


%build
%global _lto_cflags %{nil}

%ifarch s390 s390x %{arm} %ix86
# Decrease debuginfo verbosity to reduce memory consumption during final library linking
%global optflags %(echo %{optflags} | sed 's/-g /-g1 /')
%endif

%cmake \
        -GNinja \
        -DBUILD_SHARED_LIBS:BOOL=OFF \
        -DLLVM_PARALLEL_LINK_JOBS=1 \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_SKIP_RPATH:BOOL=ON \
%if 0%{?__isa_bits} == 64
        -DLLVM_LIBDIR_SUFFIX=64 \
%else
        -DLLVM_LIBDIR_SUFFIX= \
%endif
        \
        -DLLVM_TARGETS_TO_BUILD=all \
        -DLLVM_ENABLE_LIBCXX:BOOL=OFF \
        -DLLVM_ENABLE_ZLIB:BOOL=ON \
        -DLLVM_ENABLE_FFI:BOOL=ON \
        -DLLVM_ENABLE_RTTI:BOOL=ON \
        -DLLVM_USE_PERF:BOOL=ON \
        -DLLVM_BINUTILS_INCDIR=%{_includedir} \
        \
        -DLLVM_BUILD_RUNTIME:BOOL=ON \
        \
        -DLLVM_INCLUDE_TOOLS:BOOL=ON \
        -DLLVM_BUILD_TOOLS:BOOL=ON \
        \
        -DLLVM_INCLUDE_TESTS:BOOL=ON \
        -DLLVM_BUILD_TESTS:BOOL=ON \
        \
        -DLLVM_INCLUDE_EXAMPLES:BOOL=ON \
        -DLLVM_BUILD_EXAMPLES:BOOL=OFF \
        \
        -DLLVM_INCLUDE_BENCHMARKS=OFF \
        \
        -DLLVM_INCLUDE_UTILS:BOOL=ON \
        -DLLVM_INSTALL_UTILS:BOOL=ON \
        -DLLVM_UTILS_INSTALL_DIR:PATH=%{_bindir} \
        -DLLVM_TOOLS_INSTALL_DIR:PATH=bin \
        \
        -DLLVM_INCLUDE_DOCS:BOOL=ON \
        -DLLVM_ENABLE_SPHINX:BOOL=OFF \
%if %{with doxygen}
        -DLLVM_ENABLE_DOXYGEN:BOOL=ON \
%else
        -DLLVM_ENABLE_DOXYGEN:BOOL=OFF \
%endif
        -DLLVM_BUILD_LLVM_DYLIB:BOOL=ON \
        -DLLVM_DYLIB_EXPORT_ALL:BOOL=ON \
        -DLLVM_LINK_LLVM_DYLIB:BOOL=ON \
        -DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL=ON \
        -DLLVM_INSTALL_TOOLCHAIN_ONLY:BOOL=OFF \
        \
        -DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON \

# Build libLLVM.so first.  This ensures that when libLLVM.so is linking, there
# are no other compile jobs running.  This will help reduce OOM errors on the
# builders without having to artificially limit the number of concurrent jobs.
%cmake_build --target LLVM
%cmake_build


%install
rm -rf %{buildroot}

%cmake_install

pushd %{_vpath_builddir}
#install -m755 ./bin/lli-child-target %{buildroot}%{_bindir}/lli-child-target
# fix multi-lib
mv -v %{buildroot}%{_bindir}/llvm-config{,-%{__isa_bits}}
ln -sf llvm-config-%{__isa_bits} %{buildroot}%{_bindir}/llvm-config
mv -v %{buildroot}%{_includedir}/llvm/Config/llvm-config{,-%{__isa_bits}}.h
install -m 0644 %{SOURCE10} %{buildroot}%{_includedir}/llvm/Config/llvm-config.h

# Add symlink to lto plugin in the binutils plugin directory.
%{__mkdir_p} %{buildroot}%{_libdir}/bfd-plugins/
ln -s ../LLVMgold.so %{buildroot}%{_libdir}/bfd-plugins/LLVMgold.so
popd


%if %{with test}
%check
ninja -C build check
%endif


%clean
rm -rf %{buildroot}


%posttrans devel
# link llvm-config to the platform-specific file;
# use ISA bits as priority so that 64-bit is preferred
# over 32-bit if both are installed
alternatives \
  --install \
  %{_bindir}/llvm-config \
  llvm-config \
  %{_bindir}/llvm-config-%{__isa_bits} \
  %{__isa_bits}

%postun devel
if [ $1 -eq 0 ]; then
  alternatives --remove llvm-config \
    %{_bindir}/llvm-config-%{__isa_bits}
fi
exit 0


%files
%defattr(-,root,root,-)
%doc CREDITS.TXT README.txt
%{_bindir}/*
%exclude %{_bindir}/llvm-config
%exclude %{_bindir}/llvm-config-%{__isa_bits}
%doc %{_mandir}/man1/*
%{_datadir}/opt-viewer

%files -n llvm%{ver_suffix}-libs
%defattr(-,root,root,-)
%license LICENSE.TXT
%{_libdir}/libLLVM-*.so
%{_libdir}/libLTO.so.*
%{_libdir}/libRemarks.so.*

%files devel
%defattr(-,root,root,-)
%{_bindir}/llvm-config
%{_bindir}/llvm-config-%{__isa_bits}
%{_includedir}/%{name}
%{_includedir}/%{name}-c
%{_libdir}/libLLVM.so
%{_libdir}/libLTO.so
%{_libdir}/libRemarks.so
%{_libdir}/LLVMgold.so
%{_libdir}/bfd-plugins/LLVMgold.so
%{_libdir}/cmake/llvm

%files static
%{_libdir}/*.a

%files doc
%defattr(-,root,root,-)
%doc docs/*

%if %{build_compat32}
%files -n compat32-%{name}%{ver_suffix}-libs
%defattr(-,root,root)
%license LICENSE.TXT
%{_libdir}/libLLVM-*.so
%{_libdir}/libRemarks.so.*
%endif


%changelog
* Wed Oct 05 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 15.0.2-1
- new upstream release.

* Wed Sep 07 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 15.0.0-1
- new upstream release.

* Wed Jun 15 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.0.5-1
- new upstream release.

* Thu Feb 03 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 13.0.1-1
- new upstream release.

* Fri Oct 22 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 13.0.0-1
- new upstream release.
- dropped Patch0.

* Wed Aug 11 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.0.1-1
- new upstream release.

* Sat Apr 17 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.0.0-1
- new upstream release.
- separated into some packages.

* Fri Oct 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.0-2
- enabled to build utils.

* Thu Oct 15 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.0-1
- new upstream release.

* Mon Aug 17 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.0.1-1
- new upstream release.

* Thu Apr 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.0.0-2
- rebuilt with libffi-3.3.

* Fri Mar 27 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.0.0-1
- new upstream release.

* Sun Dec 22 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.1-1
- new upstream release.

* Sun Sep 22 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.0-2
- dropped all patches.
- switched build-system to ninja.
- switched python to python3.

* Fri Sep 20 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.0-1
- new upstream release.
- added OpenMP.

* Sun Dec 16 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.0.1-1
- fixed %%files.

* Wed Dec 05 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.0.0-1
- new upstream release.
- dropped Patch0, 4, 5, 100 and 200.
- imported Patch3, 7, 12, 15, 100, 101, 102 and 400 from rawhide.
- renamed a subpackage "llvm-libs".
- added a subpackage "lld".

* Tue Jan 02 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.0.1-2
- added subpackages "llvm-static", "python-lldb" and "python-clang".

* Mon Jan 01 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.0.1-1
- new upstream release.
- dropped Patch1 and 2: fixed in upstream.
- imported Patch0, 3-5, 100 and 200 from rawhide.
- disabled Patch1002: no longer needed?

* Fri Aug  5 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.8.1-1
- new upstream release.
- switched to cmake.
- disable ocaml binding as default.
- updated Patch1000 and 1002.
- disabled Patch1000 as default.
- cleanup patches.

* Thu Jun 30 2016 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.6.2-2
- rebuild with gcc-5.4.0

* Sat Sep  5 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.6.2-1
- new upstream release
- added BR: ocaml-ctypes

* Thu Jun  4 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.6.1-1
- new upstream release
- dropt Patch 1, 201 and 1001
- updated Patch 1000

* Tue Mar 24 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.5.1-1
- updated to 3.5.1
- added Patch1, 2, 100, 101, 200, 201 and 202 from Fedora 
- added clang-libs, lldb, lldb-devel and compat32-llvm-libs subpackage
- obsoleted clang-doc
- built with ocaml 4.02.1

* Sat Nov 29 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.5.0-2
- fix <BTS:2832>
  - fix configure option "--with-c-include-dirs"
  - update Patch1000: clang-3.5.0-driver-ld.gold.patch
  - update Patch1001: clang-3.5.0-driver-lib64.patch
  - update Patch1002: clang-3.5.0-driver-vine.patch

* Tue Oct 28 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.5.0-1
- update to 3.5.0
- remove Patch11 (clang-hardfloat-hack.patch)

* Sun Jul 06 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3-2
- rebuild with libffi-3.0.13

* Mon Oct 28 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 3.3-1
- update to 3.3

* Mon Dec 03 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 3.1-2
- ld.gold (patch1000,1001)
- add /%{_lib} to ld search path
- add support *-vine-linux gcc (patch1002)

* Fri Nov 30 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 3.1-1
- update to 3.1

* Tue Jan 24 2012 NAKAMURA Kenta <kenta@vinelinux.org> - 3.0-1
- updated to 3.0 release

* Sun Jan 15 2012 NAKAMURA Kenta <kenta@vinelinux.org> - 2.9-3
- added patch 2-4 to support -O4 link-time optimization

* Fri Sep  9 2011 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 2.9-2
- disable -fno-var-tracking-assignments on ppc
  (seems like gcc-4.4.5 still doesn't support this)

* Thu Aug 25 2011 Daisuke SUZUKI <daisuke@linux.or.jp> 2.9-1
- new upstream release
- add BR: libffi-devel
- add R: libffi-devel to -devel

* Sun May 23 2010 Daisuke SUZUKI <daisuke@linux.or.jp> 2.7-1
- initial build for Vine Linux

* Sun May  2 2010 Michel Salim <salimma@fedoraproject.org> - 2.7-1
- Update to final 2.7 release

* Sun Mar 28 2010 Michel Salim <salimma@fedoraproject.org> - 2.7-0.1.pre1
- Update to first 2.7 pre-release

* Fri Sep 18 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.6.pre2
- Update to 2.6 pre-release2
- -devel subpackage now virtually provides -static

* Wed Sep  9 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.5.pre1
- Disable var tracking assignments on PPC

* Wed Sep  9 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.4.pre1
- Don't adjust clang include dir; files there are noarch (bz#521893)
- Enable clang unit tests
- clang and clang-analyzer renamed; no longer depend on llvm at runtime

* Mon Sep  7 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.3.pre1
- Package Clang's static analyzer tools

* Mon Sep  7 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.2.pre1
- PIC is now enabled by default; explicitly disable on %%{ix86}

* Mon Sep  7 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.1.pre1
- First 2.6 prerelease
- Enable Clang front-end
- Enable debuginfo generation

* Sat Sep  5 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-6
- Disable assertions (needed by OpenGTL, bz#521261)
- Align spec file with upstream build instructions
- Enable unit tests

* Sat Aug 22 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-5
- Only disable PIC on %%ix86; ppc actually needs it

* Sat Aug 22 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-4
- Disable use of position-independent code on 32-bit platforms
  (buggy in LLVM <= 2.5)

* Sat Jul 25 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.5-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild

* Wed Mar  4 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-2
- Remove build scripts; they require the build directory to work

* Wed Mar  4 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-1
- Update to 2.5
- Package build scripts (bug #457881)

* Tue Dec  2 2008 Michel Salim <salimma@fedoraproject.org> - 2.4-2
- Patched build process for the OCaml binding

* Tue Dec  2 2008 Michel Salim <salimma@fedoraproject.org> - 2.4-1
- Update to 2.4
- Package Ocaml binding

* Wed Jun 18 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.3-2
- Add dependency on groff

* Wed Jun 18 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.3-1
- LLVM 2.3

* Thu May 29 2008 Tom "spot" Callaway <tcallawa@redhat.com> - 2.2-4
- fix license tags

* Wed Mar  5 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.2-3
- Fix compilation problems with gcc 4.3

* Mon Feb 18 2008 Fedora Release Engineering <rel-eng@fedoraproject.org> - 2.2-2
- Autorebuild for GCC 4.3

* Sun Jan 20 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.1-2
- Fix review comments

* Sun Jan 20 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.1-1
- Initial version
