%global toolchain clang

%global maj_ver 12
%global libomp_version %{maj_ver}.0.1
%global libomp_srcdir openmp-%{libomp_version}.src

%ifarch ppc64le
%global libomp_arch ppc64
%else
%global libomp_arch %{_arch}
%endif

Name: libomp
Version: %{libomp_version}
Release: 2
Summary: OpenMP runtime for clang

License: Apache-2.0 WITH LLVM-exception OR NCSA
URL: http://openmp.llvm.org
Source0: https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/%{libomp_srcdir}.tar.xz
Patch0:  0001-libomp-add-support-for-loongarch64.patch

BuildRequires: clang
# For clang-offload-packager
BuildRequires: clang-tools-extra
BuildRequires: cmake
BuildRequires: elfutils-libelf-devel
BuildRequires: perl
BuildRequires: perl-Data-Dumper
BuildRequires: perl-Encode
BuildRequires: libffi-devel
#BuildRequires: ninja-build

# libomptarget needs the llvm cmake files
BuildRequires: llvm-devel

Requires: elfutils-libelf%{?isa}

%description
OpenMP runtime for clang.

%package devel
Summary: OpenMP header files
Requires: %{name}%{?isa} = %{version}-%{release}
Requires: clang-resource-filesystem%{?isa} = %{version}

%description devel
OpenMP header files.

%prep
%autosetup -n %{libomp_srcdir} -p1

%build
mkdir build && cd build
# TODO: LIBOMP_HAVE_VERSION_SCRIPT_FLAG should be set automatically.
%cmake	\
	-DOPENMP_ENABLE_LIBOMPTARGET:BOOL=ON \
	-DLIBOMP_INSTALL_ALIASES=OFF \
	-DCMAKE_MODULE_PATH=%{_libdir}/cmake/llvm \
	-DLLVM_DIR=%{_libdir}/cmake/llvm \
	-DLIBOMPTARGET_LLVM_INCLUDE_DIRS=%{_includedir}/llvm \
	-DCMAKE_INSTALL_PREFIX="/usr" \
	-DCMAKE_INSTALL_DATADIR="%{_libdir}" \
%if 0%{?__isa_bits} == 64
	-DOPENMP_LIBDIR_SUFFIX=64 \
%else
	-DOPENMP_LIBDIR_SUFFIX= \
%endif
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DLIBOMP_HAVE_VERSION_SCRIPT_FLAG:BOOL=ON \
	..

%make_build


%install
%make_install -C build
mkdir -p %{buildroot}%{_datadir}/licenses/libomp/
cp LICENSE.txt %{buildroot}%{_datadir}/licenses/libomp/

%check
%make_build -C build check-openmp || echo "CHECK FAIL"

%files
%license LICENSE.txt
%{_libdir}/*.so

%files devel
%{_includedir}/omp.h
%{_includedir}/omp-tools.h
%{_includedir}/ompt.h
%{_includedir}/ompt-multiplex.h
%{_libdir}/*.a

%changelog
* Thu Apr 18 2024 Pengda Dou <doupengda@loongson.cn> - 12.0.1-2
- add support for loongarch64

* Tue May 9 2023 will_niutao <niutao2@huawei.com> - 12.0.1-1
- Init for openEuler

