%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 lldb_version %{maj_ver}.%{min_ver}.%{patch_ver}

%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}

%global install_bindir %{install_prefix}/bin
%global install_includedir %{install_prefix}/include
%if 0%{?__isa_bits} == 64
%global install_libdir %{install_prefix}/lib64
%else
%global install_libdir %{install_prefix}/lib
%endif
%global install_libexecdir %{install_prefix}/libexec
%global install_sharedir %{install_prefix}/share
%global install_docdir %{install_sharedir}/doc

Name:		%{?scl_prefix}lldb
Version:	%{lldb_version}
Release:	1
Summary:	Next generation high-performance debugger

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

BuildRequires:	gcc
BuildRequires:	gcc-c++
BuildRequires:	cmake
BuildRequires:	ninja-build
BuildRequires:  %{?scl_prefix}clang
BuildRequires:	%{?scl_prefix}llvm-devel = %{version}
BuildRequires:	%{?scl_prefix}llvm-test = %{version}
BuildRequires:	%{?scl_prefix}clang-devel = %{version}
BuildRequires:	%{?scl_prefix}clang-tools-extra = %{version}
BuildRequires:	ncurses-devel
BuildRequires:	swig
BuildRequires:	%{?scl_prefix}llvm-static = %{version}
BuildRequires:	libffi-devel
BuildRequires:	zlib-devel
BuildRequires:	libxml2-devel
BuildRequires:	libedit-devel
BuildRequires:	python3-lit
BuildRequires:	multilib-rpm-config
%if %{with toolchain_clang}
BuildRequires:	clang
%endif

Requires:	%{?scl_prefix}python3-lldb

%description
LLDB is a next generation, high-performance debugger. It is built as a set
of reusable components which highly leverage existing libraries in the
larger LLVM Project, such as the Clang expression parser and LLVM
disassembler.

%package devel
Summary:	Development header files for LLDB
Requires:	%{pkg_name}%{?_isa} = %{version}-%{release}

%description devel
The package contains header files for the LLDB debugger.

%package -n %{?scl_prefix}python3-lldb
%{?python_provide:%python_provide python3-lldb}
Summary:	Python module for LLDB
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
Requires:	python3-six
Requires:	%{pkg_name}%{?_isa} = %{version}-%{release}
BuildRequires: %{?scl_prefix}llvm-cmake-utils = %{version}

%description -n %{?scl_prefix}python3-lldb
The package contains the LLDB Python module.

%prep
%autosetup -n lldb-%{version}.src -p2

%build

# Python version detection is broken
LDFLAGS="%{__global_ldflags} -lpthread -ldl"

CFLAGS="%{optflags} -Wno-error=format-security"
CXXFLAGS="%{optflags} -Wno-error=format-security"

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_SKIP_RPATH:BOOL=ON \
	-DLLVM_LINK_LLVM_DYLIB:BOOL=ON \
	-DLLVM_CONFIG:FILEPATH=/usr/bin/llvm-config-%{__isa_bits} \
	\
	-DLLDB_DISABLE_CURSES:BOOL=OFF \
	-DLLDB_DISABLE_LIBEDIT:BOOL=OFF \
	-DLLDB_DISABLE_PYTHON:BOOL=OFF \
%if "%toolchain" == "clang"
	-DCMAKE_C_COMPILER=clang \
	-DCMAKE_CXX_COMPILER=clang++ \
%endif
%if 0%{?__isa_bits} == 64
	-DLLVM_LIBDIR_SUFFIX=64 \
%else
	-DLLVM_LIBDIR_SUFFIX= \
%endif
	\
	-DPYTHON_EXECUTABLE:STRING=%{__python3} \
	-DPYTHON_VERSION_MAJOR:STRING=$(%{__python3} -c "import sys; print(sys.version_info.major)") \
	-DPYTHON_VERSION_MINOR:STRING=$(%{__python3} -c "import sys; print(sys.version_info.minor)") \
	-DLLVM_EXTERNAL_LIT=%{_bindir}/lit \
	-DCLANG_LINK_CLANG_DYLIB=ON \
	-DLLVM_LIT_ARGS="-sv \
	--path %{install_libdir}/llvm" \

%make_build

%install
cd _build
%make_install

# remove static libraries
rm -fv %{buildroot}%{install_libdir}/*.a

# python: fix binary libraries location
liblldb=$(basename $(readlink -e %{buildroot}%{install_libdir}/liblldb.so))
mkdir -p %{buildroot}%{python3_sitearch}
mv %{buildroot}%{install_prefix}/..%{python3_sitearch}/lldb %{buildroot}%{python3_sitearch}/lldb
ln -vsf "../../../${liblldb}" %{buildroot}%{python3_sitearch}/lldb/_lldb.so
%py_byte_compile %{__python3} %{buildroot}%{python3_sitearch}/lldb

# remove bundled six.py
rm -f %{buildroot}%{install_prefix}%{python3_sitearch}/six.*

%ldconfig_scriptlets

%check


%files
%license LICENSE.TXT
%{install_bindir}/lldb*
%{install_libdir}/liblldb.so.*
%{install_libdir}/liblldbIntelFeatures.so.*

%files devel
%{install_includedir}/lldb
%{install_libdir}/*.so

%files -n %{?scl_prefix}python3-lldb
%{python3_sitearch}/lldb

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

* Wed Nov 13 2024 Funda Wang <fundawang@yeah.net> - 17.0.6-3
- adopt to new cmake macro

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

* Mon Dec 4 2023 zhoujing <zhoujing@huawei.com> 17.0.6-1
- upgrade lldb to 17.0.6

* Mon Jul 17 2023 cf-zhao <zhaochuanfeng@huawei.com> 15.0.7-1
- upgrade lldb to 15.0.7

* Thu Jan 06 2022 Chen Chen <chen_aka_jan@163.com> - 12.0.1-1
- upgrade lldb to 12.0.1

* Mon Oct 12 2020 wangxiao <wangxiao65@huawei.com> - 10.0.1-1
- upgrade lldb to 10.0.1

* Mon Dec 2 2019 likexin <likexin@huawei.com> 7.0.0-2
- Package init
