%global maj_ver 7
%global min_ver 1
%global patch_ver 0

# Components enabled if supported by target architecture:
%ifarch x86_64 ppc64le ppc64 s390x
  %bcond_without gold
%else
  %bcond_with gold
%endif

%global _smp_mflags -j8

%ifarch s390x
%global host_target SystemZ
%endif
%ifarch ppc64 ppc64le
%global host_target PowerPC
%endif
%ifarch x86_64
%global host_target X86
%endif
%ifarch aarch64
%global host_target AArch64
%endif
%ifnarch s390x
%global amdgpu ;AMDGPU
%endif

%global llvm_lib_suffix pel

Name:		llvm
Version:	%{maj_ver}.%{min_ver}.%{patch_ver}
Release:	1%{?dist}
Summary:	llvm engine for Mesa

Group:          System Environment/Libraries
License:	NCSA
URL:		http://llvm.org
Source0:	https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/llvm-%{version}.src.tar.xz
Source2:	https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/cfe-%{version}.src.tar.xz
Source100:	llvm-config.h
Source101:	clang-config.h

Patch0:	0001-Don-t-set-rpath-when-installing.patch
Patch1: 0001-Fix-CMake-include-patch.patch

BuildRequires:	cmake3
BuildRequires:	zlib-devel
%if %{with gold}
BuildRequires:  binutils-devel
%endif
BuildRequires:  python

%description
This package contains the LLVM-based runtime support for Mesa.  It is not a
fully-featured build of LLVM, and use by any package other than Mesa is not
supported.

%package devel
Summary:	Libraries and header files for LLVM
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description devel
This package contains library and header files needed to build the LLVM
support in Mesa.

%prep
%setup -T -q -b 2 -n cfe-%{version}.src

%setup -q -n llvm-%{version}.src

%patch0 -p1 -b .rpath
%patch1 -p1 -b .fixinc


%build
#sed -i 's|ActiveIncludeDir = ActivePrefix + "/include|&/llvm-private|g' tools/llvm-config/llvm-config.cpp

mkdir -p _build
cd _build

export PATH=$BUILD_DIR/bin:$PATH
%cmake3 .. \
	-DINCLUDE_INSTALL_DIR=%{_includedir}/ \
	-DLLVM_VERSION_SUFFIX="-%{llvm_lib_suffix}" \
	-DBUILD_SHARED_LIBS:BOOL=OFF \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
%if 0%{?__isa_bits} == 64
	-DLLVM_LIBDIR_SUFFIX=64 \
%else
	-DLLVM_LIBDIR_SUFFIX= \
%endif
	\
	-DLLVM_TARGETS_TO_BUILD="%{host_target}%{?amdgpu};BPF" \
	-DLLVM_ENABLE_LIBCXX:BOOL=OFF \
	-DLLVM_ENABLE_ZLIB:BOOL=ON \
	-DLLVM_ENABLE_FFI:BOOL=OFF \
	-DLLVM_ENABLE_RTTI:BOOL=OFF \
%if %{with gold}
	-DLLVM_BINUTILS_INCDIR=%{_includedir} \
%endif
	\
	-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=OFF \
	-DLLVM_BUILD_EXAMPLES:BOOL=OFF \
	\
	-DLLVM_INCLUDE_UTILS:BOOL=ON \
	-DLLVM_INSTALL_UTILS:BOOL=OFF \
	\
	-DLLVM_INCLUDE_DOCS:BOOL=OFF \
	-DLLVM_BUILD_DOCS:BOOL=OFF \
	-DLLVM_ENABLE_SPHINX:BOOL=OFF \
	-DLLVM_ENABLE_DOXYGEN:BOOL=OFF \
	\
	-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_ENABLE_RTTI:BOOT=ON \
	-DLLVM_INSTALL_TOOLCHAIN_ONLY:BOOL=OFF \
        -DLLLVM_OPTIMIZED_TABLEGEN:BOOL=ON

make %{?_smp_mflags} VERBOSE=1 LLVM llvm-config FileCheck not count gtest gtest_main

# Build clang separately, because we need to build with
# -DBUILD_SHARED_LIBS:BOOL=ON for clang, but we don't want
# this for LLVM.

cd ../../cfe-%{version}%{?rc_ver:rc%{rc_ver}}.src
mkdir -p _build
cd _build
%cmake3 ..  \
        -DLLVM_LINK_LLVM_DYLIB:BOOL=ON \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DLLVM_CONFIG:FILEPATH=%{_builddir}/llvm-%{version}%{?rc_ver:rc%{rc_ver}}.src/_build/bin/llvm-config \
        -DBUILD_SHARED_LIBS:BOOL=ON \
        \
        -DCLANG_ENABLE_ARCMT:BOOL=ON \
        -DCLANG_ENABLE_STATIC_ANALYZER:BOOL=ON \
        -DCLANG_INCLUDE_DOCS:BOOL=OFF \
        -DCLANG_INCLUDE_TESTS:BOOL=ON \
        -DCLANG_PLUGIN_SUPPORT:BOOL=ON \
        -DENABLE_LINKER_BUILD_ID:BOOL=ON \
        -DLLLVM_OPTIIMIZED_TABLEGEN:BOOL=ON \
        \
        -DCLANG_BUILD_EXAMPLES:BOOL=OFF \
%if 0%{?__isa_bits} == 64
        -DLLVM_LIBDIR_SUFFIX=64 \
%else
        -DLLVM_LIBDIR_SUFFIX= \
%endif
        -DLIB_SUFFIX=

make %{?_smp_mflags}

%install

# Install LLVM
cd _build
make install-LLVM install-llvm-config install-llvm-headers install-cmake-exports DESTDIR=%{buildroot}

cd ../../cfe-%{version}%{?rc_ver:rc%{rc_ver}}.src/_build
make install DESTDIR=%{buildroot}


# fix multi-lib
mv -v %{buildroot}%{_bindir}/llvm-config %{buildroot}%{_bindir}/%{name}-config-%{__isa_bits}
ln -s %{_bindir}/%{name}-config-%{__isa_bits} %{buildroot}%{_bindir}/%{name}-config
mv -v %{buildroot}%{_includedir}/llvm/Config/llvm-config{,-%{__isa_bits}}.h
install -m 0644 %{SOURCE100} %{buildroot}%{_includedir}/llvm/Config/llvm-config.h
mv -v %{buildroot}%{_includedir}/clang/Config/config{,-%{__isa_bits}}.h
install -m 0644 %{SOURCE101} %{buildroot}%{_includedir}/clang/Config/config.h

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

rm -f %{buildroot}%{_libdir}/libLLVM.so

# remove documentation makefiles:
# they require the build directory to work
find examples -name 'Makefile' | xargs -0r rm -f

# RHEL: strip out most binaries, most libs, and man pages
ls %{buildroot}%{_bindir}/* | grep -v bin/llvm | xargs rm -f
ls %{buildroot}%{_libdir}/* | grep -v libLLVM | grep -v libclang | xargs rm -f
rm -rf %{buildroot}%{_mandir}/man1

# RHEL: Strip out some headers Mesa doesn't need
rm -rf %{buildroot}%{_includedir}/llvm/{Assembly}
rm -rf %{buildroot}%{_includedir}/llvm/TableGen
rm -rf %{buildroot}%{_includedir}/llvm-c/lto.h

# RHEL: Strip out cmake build foo
rm -rf %{buildroot}%{_datadir}/llvm/cmake
rm -rf %{buildroot}%{_libdir}/cmake/llvm/LLVMExports-*
rm -rf %{buildroot}%{_libdir}/cmake/clang

# RHEL: Strip out eveything in _datadir and _libexedir
rm -rf %{buildroot}%{_datadir}/*
rm -rf %{buildroot}%{_libexecdir}/*

# clang seems to ignore INCLUDE_INSTALL_DIR
#mv %{buildroot}%{_includedir}/{,llvm}/clang
#mv %{buildroot}%{_includedir}/{,llvm}/clang-c

# Move clang libraries:
#mkdir %{buildroot}%{_libdir}/clang
#for f in `find %{buildroot}%{_libdir} -iname 'libclang*' `; do mv $f %{buildroot}%{_libdir}/clang; done

%check
cd _build
make %{?_smp_mflags} check-all || :

cd ../../cfe-%{version}%{?rc_ver:rc%{rc_ver}}.src/_build
make %{?_smp_mflags} check-all

%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig

%files
%doc LICENSE.TXT
%{_libdir}/libLLVM-%{maj_ver}*-%{llvm_lib_suffix}.so
%{_libdir}/libclang*.so*

%files devel
%{_bindir}/llvm*
%{_includedir}/llvm
%{_includedir}/llvm-c
%{_libdir}/cmake/llvm/
%{_includedir}/clang
%{_includedir}/clang-c
%{_libdir}/clang/%{version}/include


%changelog
