%global targets_to_build "all"

%global install_prefix  %{_libdir}/llvm%{llvm_major}
%global forgeurl        https://github.com/llvm/llvm-project

# Temporarily disable dwz because to avoid some out of memory
%define _find_debuginfo_dwz_opts %{nil}

# reduce number of parallel building process for ninja
%ifarch %ix86
%global _smp_ncpus_max 1
%else
%global _smp_ncpus_max 12
%endif

%define llvm_major      19
%define llvm_minor      1
%define llvm_patch      0

%define libname         %mklibname %{name}
%define develname       %mklibname %{name} -d

# Opt out of https://fedoraproject.org/wiki/Changes/fno-omit-frame-pointer
# https://bugzilla.redhat.com/show_bug.cgi?id=2158587
%undefine _include_frame_pointers

# Components enabled if supported by target architecture:
%define gold_arches %{ix86} x86_64 %{arm} aarch64 %{power64} s390x
%ifarch %{gold_arches}
  %bcond_without gold
%else
  %bcond_with gold
%endif

# Disable LTO to speed up builds
%bcond_with lto_build

%if %{without lto_build}
%global _lto_cflags %nil
%endif

# Lower memory usage of dwz on s390x
%global _dwz_low_mem_die_limit_s390x 1
%global _dwz_max_die_limit_s390x 1000000

%ifarch %{arm32}
%global _gnu -gnueabihf
%endif

# https://fedoraproject.org/wiki/Changes/PythonSafePath#Opting_out
# Don't add -P to Python shebangs
# The executable Python scripts in /usr/share/opt-viewer/ import each other
%undefine _py3_shebang_P

Name:           llvm%{llvm_major}-suite
Version:        %{llvm_major}.%{llvm_minor}.%{llvm_patch}
Release:        %mkrel 0.5
Summary:        LLVM stack as a monolithic package
Group:          Development/Other
Url:            https://llvm.org/
License:        Apache-2.0 WITH LLVM-exception
Source0:        %{forgeurl}/archive/refs/tags/llvmorg-%{version}.tar.gz#/llvm-project-llvmorg-%{version}.tar.gz

Patch0:         llvm-19.1.0-detect-mageia.patch
Patch1:         llvm-19.1.0-python3.patch

BuildRequires:  binutils-devel
BuildRequires:  clang
BuildRequires:  lld
BuildRequires:  cmake
BuildRequires:  glibc-devel
BuildRequires:  ninja-build
BuildRequires:  python3
# for %%py3_shebang_fix
BuildRequires:	python3-devel

BuildRequires:	python3-psutil
BuildRequires:	python3-yaml
BuildRequires:	python3-psutil
BuildRequires:	python3-yaml
BuildRequires:	python3-sphinx

BuildRequires:	pkgconfig(libxml-2.0)
BuildRequires:  pkgconfig(libedit)
BuildRequires:  pkgconfig(libffi)
BuildRequires:  pkgconfig(ncurses)
BuildRequires:  pkgconfig(valgrind)
BuildRequires:	pkgconfig(zlib)

Requires:       %libname = %version-%release

Provides:       %{name}(major) = %llvm_major


%description
LLVM%llvm_major toolchain (llvm, clang, lld, compiler-rt)


%package -n %develname
Summary:	Header files for LLVM%llvm_major
Group:		Development/Other

Provides:	%{name}-devel = %version
Provides:	%{name}-devel%{?_isa} = %version
Provides:	%{name}-devel(major) = %llvm_major

Requires:	%libname = %version-%release

Requires:	%{name}-static%{?_isa} = %version-%release

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


%package -n %libname
Summary:	LLVM%llvm_major shared libraries
Group:		System/Libraries

%description -n %libname
Shared libraries for the LLVM%llvm_major compiler infrastructure.


%package static
Summary:	LLVM%llvm_major static libraries
Group:		Development/Other

Provides:	%{name}-static%{?_isa} = %version
Provides:	%{name}-static(major) = %llvm_major

%description static
Static libraries for the ROCm-LLVM compiler infrastructure.


%package analyzer
Summary:	A source code analysis framework for LLVM%llvm_major
License:	Apache-2.0 WITH LLVM-exception
BuildArch:	noarch
Requires:	%{name} = %{version}-%{release}

%description analyzer
The Clang Static Analyzer consists of both a source code analysis
framework and a standalone tool that finds bugs in C and Objective-C
programs. The standalone tool is invoked from the command-line, and is
intended to run in tandem with a build of a project or code base.


%package tools-extra
Summary:	Extra tools for clang%llvm_major
Group:		Development/Other
Requires:	%libname%{?_isa} = %{version}-%{release}

%description tools-extra
A set of extra tools built using Clang%llvm_major's tooling API.


%package polly
Summary:        LLVM Framework for High-Level Loop and Data-Locality Optimizations
Group:          System/Libraries
URL:            https://polly.llvm.org/
Provides:       %{name}-polly-provider = %{version}

%description polly
Polly is a high-level loop and data-locality optimizer and optimization
infrastructure for LLVM. It uses an abstract mathematical representation based
on integer polyhedra to analyze and optimize the memory access pattern of a
program. Polly can currently perform classical loop transformations, especially
tiling and loop fusion to improve data-locality. It can also exploit OpenMP
level parallelism and expose SIMDization opportunities.


%package polly-devel
Summary:        Development files for Polly
Group:          Development/Other
Requires:       %{name}-devel = %{version}-%{release}
Requires:       %{name}-polly = %{version}-%{release}
Provides:       %{name}-polly-devel-provider = %{version}

%description polly-devel
This package contains the development files for Polly.


%prep
%autosetup -p1 -n llvm-project-llvmorg-%{version}

#if %{mgaver} > 9
#global optflags %{optflags} -fPIC
#endif

# Require 32 GB of RAM per vCPU for debuginfo processing. 16 GB is not enough.
%{?limit_build:%global _find_debuginfo_opts %{limit_build -m 32768}}

%py3_shebang_fix \
	llvm/tools/opt-viewer/*.py \
	clang/tools/scan-view/bin/scan-view

%build
#To build with clang. GCC is way too much memory consuming.
%set_build_flags
CC=clang; export CC
CXX=clang++; export CXX

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

# Copy CFLAGS into ASMFLAGS, so -fcf-protection is used when compiling assembly files.
export ASMFLAGS="%{build_cflags}"

%cmake  -G Ninja \
        -B build \
        -S "./llvm" \
        -DLLVM_PARALLEL_LINK_JOBS=1 \
%ifarch %ix86 %arm aarch64
        -DCMAKE_C_FLAGS_RELWITHDEBINFO="%{optflags} -DNDEBUG" \
        -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="%{optflags} -DNDEBUG" \
%endif
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DCMAKE_INSTALL_PREFIX=%{install_prefix} \
        -DLLVM_HOST_TRIPLE=%{_host} \
        -DLLVM_DEFAULT_TARGET_TRIPLE=%{_host} \
        -DLLVM_ENABLE_PROJECTS='clang;compiler-rt;lld;openmp;polly;' \
        -DLLVM_TARGETS_TO_BUILD=%{targets_to_build} \
	-DCLANG_DEFAULT_LINKER=lld \
%ifarch %ix86
	-DLLVM_USE_LINKER=gold \
%else
        -DLLVM_USE_LINKER=lld \
%endif
        -DLLVM_INSTALL_UTILS=ON \
        -DLLVM_ENABLE_BINDINGS=OFF \
        -DLLVM_BUILD_LLVM_DYLIB=OFF \
        -DLLVM_LINK_LLVM_DYLIB=OFF \
        -DLLVM_ENABLE_ASSERTIONS=ON \
        -DOCAMLFIND=NO \
        -DLLVM_ENABLE_OCAMLDOC=OFF \
        -DLLVM_INCLUDE_BENCHMARKS=OFF \
        -DLLVM_BUILD_TESTS=OFF \
        -DLLVM_INCLUDE_TESTS=OFF \
%if %{with gold}
        -DLLVM_BINUTILS_INCDIR=%{_includedir} \
%endif
%if %{with lto_build}
	    -DLLVM_UNITTEST_LINK_FLAGS="-Wl,-plugin-opt=O0" \
%endif
        -DBUILD_SHARED_LIBS=OFF
# Static lib requires to build the ROCm stack
# https://github.com/RadeonOpenCompute/ROCm-CompilerSupport/issues/55#issuecomment-1821109039

%cmake_build

%install
%cmake_install

# Add symlink to lto plugin in the binutils plugin directory.
%{__install} -d -m 0755  %{buildroot}%{install_prefix}/lib/bfd-plugins
ln -s %{install_prefix}/lib/LLVMgold.so %{buildroot}%{install_prefix}/lib/bfd-plugins/LLVMgold.so

# Add symlink to a major version folder
ln -s %{install_prefix}/lib/clang/%{llvm_major}.%{llvm_minor}.%{llvm_patch} %{buildroot}%{install_prefix}/lib/clang/%{llvm_major}

# Removed in Fedora and Mageia packages
rm -vf %{buildroot}%{install_prefix}/share/clang/bash-autocomplete.sh

# remove editor integrations (bbedit, sublime, emacs, vim)
rm -vf %{buildroot}%{install_prefix}/share/clang/clang-format-bbedit.applescript
rm -vf %{buildroot}%{install_prefix}/share/clang/clang-format-sublime.py


cat > README.urpmi <<EOF

NOTE: this package lives into %{install_prefix}. You need to set env variables
and path accordingly.

For instance and to be adjusted to your needs:

PATH="%{install_prefix}/bin:\$PATH"
LLVM_DIR=%{install_prefix}/lib/cmake/llvm

EOF

%files
%license LICENSE.TXT
%doc README.md README.urpmi
%{install_prefix}/bin{,.*}
%exclude %{install_prefix}/bin/llvm-config
%exclude %{install_prefix}/bin/clang-tblgen
%exclude %{install_prefix}/share/opt-viewer{,.*}

%files -n %libname
%ifarch x86_64
%{install_prefix}/lib/clang/%{llvm_major}*/bin/hwasan_symbolize
%endif
%{install_prefix}/lib/*.so.*
%{install_prefix}/lib/clang/%{llvm_major}*/include/*
%{install_prefix}/lib/clang/%{llvm_major}*/share/*.txt
%{install_prefix}/lib/*-mageia-linux-gnu/*
%{install_prefix}/share/gdb

%files -n %{develname}
%{install_prefix}/bin/llvm-config
%{install_prefix}/bin/clang-tblgen
%{install_prefix}/include/lld{,.*}
%{install_prefix}/include/llvm{,.*}
%{install_prefix}/include/llvm-c{,.*}
%{install_prefix}/include/clang{,.*}
%{install_prefix}/include/clang-c{,.*}
%{install_prefix}/lib/cmake{,.*}
%exclude %{install_prefix}/lib/cmake/polly{,.*}
%{install_prefix}/lib/bfd-plugins/LLVMgold.so
%{install_prefix}/lib/*.so
%exclude %{install_prefix}/lib/LLVMPolly.so

%files static
%{install_prefix}/lib/*.a
%{install_prefix}/lib/clang/%{llvm_major}

%files tools-extra
%{install_prefix}/share/clang/clang-format-diff.py
%{install_prefix}/share/clang/clang-format.el
%{install_prefix}/share/clang/clang-format.py
%{install_prefix}/share/clang/clang-rename.el
%{install_prefix}/share/clang/clang-rename.py

%files analyzer
%exclude %{install_prefix}/bin/scan-view
%exclude %{install_prefix}/bin/scan-build
%exclude %{install_prefix}/bin/analyze-build
%exclude %{install_prefix}/bin/intercept-build
%exclude %{install_prefix}/bin/scan-build-py
%exclude %{install_prefix}/libexec/ccc-analyzer
%exclude %{install_prefix}/libexec/c++-analyzer
%exclude %{install_prefix}/libexec/analyze-c++
%exclude %{install_prefix}/libexec/analyze-cc
%exclude %{install_prefix}/libexec/intercept-c++
%exclude %{install_prefix}/libexec/intercept-cc
%{install_prefix}/share/scan-view{,.*}
%{install_prefix}/share/scan-build{,.*}
%{install_prefix}/lib/libear{,.*}
%{install_prefix}/lib/libscanbuild{,.*}
%{install_prefix}/share/man/man1/scan-build.1

%files polly
%license LICENSE.TXT
%{install_prefix}/lib/LLVMPolly.so

%files polly-devel
%license LICENSE.TXT
%{install_prefix}/include/polly{,.*}
%{install_prefix}/lib/cmake/polly{,.*}