%bcond_without check
%bcond_without toolchain_clang

%if %{with toolchain_clang}
%global toolchain clang
%endif

%global maj_ver 18
%global min_ver 1
%global patch_ver 8
%global crt_version %{maj_ver}.%{min_ver}.%{patch_ver}
%global crt_srcdir compiler-rt-%{version}%{?rc_ver:rc%{rc_ver}}.src
%global optflags %(echo %{optflags} -D_DEFAULT_SOURCE)
%global optflags %(echo %{optflags} -Dasm=__asm__)

%global _scl_prefix /opt/openEuler
%{?scl:%scl_package %scl}
%{!?scl:%global scl_prefix llvm-toolset-%{maj_ver}-}
%{!?scl:%global pkg_name %{name}}
%global install_prefix %{!?scl:%{_scl_prefix}/llvm-toolset-%{maj_ver}/root}%{_prefix}
%global install_datadir %{!?scl:%{_scl_prefix}/llvm-toolset-%{maj_ver}/root}%{_datadir}

%if 0%{?__isa_bits} == 64
%global install_libdir %{install_prefix}/lib64
%else
%global install_libdir %{install_prefix}/lib
%endif

Name:		%{?scl_prefix}compiler-rt
Version:	%{crt_version}
Release:	1
Summary:	LLVM "compiler-rt" runtime libraries

License:	NCSA or MIT
URL:		http://llvm.org
Source0:	https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/%{crt_srcdir}.tar.xz
Source1:	https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/%{crt_srcdir}.tar.xz.sig

BuildRequires:	gcc
BuildRequires:	gcc-c++
BuildRequires:	cmake
BuildRequires:	ninja-build
BuildRequires:	python3
# We need python3-devel for pathfix.py.
BuildRequires:	python3-devel
%if %{with toolchain_clang}
BuildRequires:	clang
%endif

BuildRequires:	%{?scl_prefix}llvm-devel = %{version}
BuildRequires:	%{?scl_prefix}llvm-test = %{version}
BuildRequires:  %{?scl_prefix}llvm-cmake-utils = %{version}

%description
The compiler-rt project is a part of the LLVM project. It provides
implementation of the low-level target-specific hooks required by
code generation, sanitizer runtimes and profiling library for code
instrumentation, and Blocks C language extension.

%prep
%autosetup -n %{crt_srcdir} -p2
# compiler-rt does not allow configuring LLVM_COMMON_CMAKE_UTILS.
ln -s %{install_datadir}/llvm/cmake ../cmake

pathfix.py -i %{__python3} -pn lib/hwasan/scripts/hwasan_symbolize

%build
# Copy CFLAGS into ASMFLAGS, so -fcf-protection is used when compiling assembly files.
export ASMFLAGS=$CFLAGS
mkdir -p _build
cd _build
%cmake  .. \
    -DCMAKE_INSTALL_PREFIX=%{install_prefix} \
    -DLLVM_COMMON_CMAKE_UTILS=%{install_datadir}/llvm/cmake \
    -DLLVM_MAIN_SRC_DIR=%{install_prefix}/src \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DCMAKE_MODULE_PATH=%{install_libdir}/cmake/llvm \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DCOMPILER_RT_INSTALL_PATH=%{install_libdir}/clang/%{maj_ver} \
	-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \
%if 0%{?__isa_bits} == 64
	-DLLVM_LIBDIR_SUFFIX=64 \
%else
	-DLLVM_LIBDIR_SUFFIX= \
%endif
%if "%{toolchain}" == "clang"
	-DCMAKE_C_COMPILER=clang \
	-DCMAKE_CXX_COMPILER=clang++ \
%endif
	-DCOMPILER_RT_INCLUDE_TESTS:BOOL=OFF # could be on?

%make_build

%install
cd _build
%make_install

%check

#%%cmake_build --target check-compiler-rt

%files
%license LICENSE.TXT
%ifarch x86_64 aarch64 riscv64
%{install_libdir}/clang/%{maj_ver}/bin/*
%endif
%{install_libdir}/clang/%{maj_ver}/include/*
%{install_libdir}/clang/%{maj_ver}/lib/*
%{install_libdir}/clang/%{maj_ver}/share/*
%ifarch x86_64 aarch64 riscv64
%{install_libdir}/clang/%{maj_ver}/bin/hwasan_symbolize
%endif

%changelog
* Fri Dec 6 2024 liyunfei <liyunfei33@huawei.com> - 18.1.8-1
- init for Multi-Version LLVM-18.1.8

* Thu Nov 14 2024 Funda Wang <fundawang@yeah.net> - 17.0.6-6
- adopt to new cmake macro

* Tue Aug 20 2024 liyunfei <liyunfei33@huawei.com> - 17.0.6-5
- Add BiSheng Autotuner support.

* Fri Jul 5 2024 liyunfei <liyunfei33@huawei.com> - 17.0.6-4
- Add toolchain_clang build support

* Wed Apr 03 2024 zhanglimin <zhanglimin@loongson.cn> - 17.0.6-3
- Improve the support for compiler-rt on LoongArch

* Fri Feb 23 2024 jchzhou <zhoujiacheng@iscas.ac.cn> - 17.0.6-2
- Fix an unpackaged file issue on riscv64

* Tue Dec 5 2023 zhoujing <zhoujing106@huawei.com> - 17.0.6-1
- Update to 17.0.6

* Tue Dec 20 2022 eastb233 <xiezhiheng@huawei.com> - 12.0.1-2
- Delete run path in DSO

* Mon Dec 27 2021 Chen Chen <chen_aka_jan@163.com> - 12.0.1-1
- Update to 12.0.1

* Fri Sep 25 2020 Guoshuai Sun <sunguoshuai@huawei.com> - 10.0.1-3
- hwasan_symbolize should run in python2 and python3, and python3 is default now

* Sat Sep 19 2020 Guoshuai Sun <sunguoshuai@huawei.com> - 10.0.1-2
- Keep "/usr/bin/env python" instead of /usr/bin/python3 in hwasan_symbolize

* Thu Jul 30 2020 Guoshuai Sun <sunguoshuai@huawei.com> - 10.0.1-1
- Update to 10.0.1

* Thu Dec 5 2019 openEuler Buildteam <buildteam@openeuler.org> - 7.0.0-2
- Package init
