From 45e8115889cf0d196f0e45f55e05adf1fc32ba04 Mon Sep 17 00:00:00 2001
From: Tom Rix <trix@redhat.com>
Date: Fri, 3 May 2024 07:02:05 -0600
Subject: [PATCH] Add compat logic

Signed-off-by: Tom Rix <trix@redhat.com>
---
 mlir.spec => mlir17.spec | 104 +++++++++++++++++++++++++++------------
 1 file changed, 73 insertions(+), 31 deletions(-)
 rename mlir.spec => mlir17.spec (87%)

diff --git a/mlir.spec b/mlir17.spec
similarity index 87%
rename from mlir.spec
rename to mlir17.spec
index 54157c5a2d2f..16f2e7944c81 100644
--- a/mlir.spec
+++ b/mlir17.spec
@@ -10,9 +10,28 @@
 # https://bugzilla.redhat.com/show_bug.cgi?id=2158587
 %undefine _include_frame_pointers
 
-Name: mlir
+%bcond_without check
+%bcond_without compat_build
+
+%if %{with compat_build}
+%global pkg_name mlir%{maj_ver}
+%global install_prefix %{_libdir}/llvm%{maj_ver}
+%global install_includedir %{install_prefix}/include
+%global install_libdir %{install_prefix}/lib
+%global install_datadir %{install_prefix}/share
+%global install_bindir %{install_prefix}/bin
+%else
+%global pkg_name mlir
+%global install_prefix /usr
+%global install_includedir %{_includedir}
+%global install_libdir %{_libdir}
+%global install_datadir %{_datadir}
+%global install_bindir %{_bindir}
+%endif
+
+Name:    %{pkg_name}
 Version: %{mlir_version}%{?rc_ver:~rc%{rc_ver}}
-Release: 3%{?dist}
+Release: 4%{?dist}
 Summary: Multi-Level Intermediate Representation Overview
 
 License: Apache-2.0 WITH LLVM-exception
@@ -71,10 +90,15 @@ BuildRequires: clang
 BuildRequires: cmake
 BuildRequires: ninja-build
 BuildRequires: zlib-devel
+%if %{with compat_build}
+BuildRequires:	llvm%{maj_ver}-devel = %{version}
+BuildRequires:	llvm%{maj_ver}-cmake-utils = %{version}
+%else
 BuildRequires: llvm-devel = %{version}
 BuildRequires: llvm-cmake-utils = %{version}
 BuildRequires: llvm-googletest = %{version}
 BuildRequires: llvm-test = %{version}
+%endif
 BuildRequires: python3-lit
 BuildRequires: python3-devel
 BuildRequires: python3-numpy
@@ -106,6 +130,7 @@ Requires: %{name}-static%{?_isa} = %{version}-%{release}
 %description devel
 MLIR development files.
 
+%if %{without compat_build}
 %package -n python3-%{name}
 Summary: MLIR python bindings
 Requires: python3
@@ -113,6 +138,7 @@ Requires: python3-numpy
 
 %description -n python3-%{name}
 %{summary}
+%endif
 
 %prep
 %{gpgverify} --keyring='%{SOURCE2}' --signature='%{SOURCE1}' --data='%{SOURCE0}'
@@ -144,10 +170,11 @@ Requires: python3-numpy
         -DCMAKE_SKIP_RPATH=ON \
         -DLLVM_LINK_LLVM_DYLIB:BOOL=ON \
         -DLLVM_BUILD_LLVM_DYLIB=ON \
-        -DCMAKE_PREFIX_PATH=%{_libdir}/cmake/llvm/ \
+	-DCMAKE_INSTALL_PREFIX=%{install_prefix} \
+        -DCMAKE_PREFIX_PATH=%{install_datadir}/cmake/llvm/ \
         -DLLVM_EXTERNAL_LIT=%{_bindir}/lit \
-        -DLLVM_THIRD_PARTY_DIR=%{_datadir}/llvm/src/utils \
-        -DLLVM_COMMON_CMAKE_UTILS=%{_datadir}/llvm/cmake \
+        -DLLVM_THIRD_PARTY_DIR=%{install_datadir}/llvm/src/utils \
+        -DLLVM_COMMON_CMAKE_UTILS=%{install_datadir}/llvm/cmake \
         -DLLVM_BUILD_TOOLS:BOOL=ON \
         -DLLVM_BUILD_UTILS:BOOL=ON \
         -DLLVM_LIBRARY_OUTPUT_INTDIR="." \
@@ -164,24 +191,30 @@ Requires: python3-numpy
 %ifarch %ix86
         -DMLIR_RUN_X86VECTOR_TESTS:BOOL=OFF \
 %endif
+%if %{with compat_build}
+	-DLLVM_CMAKE_DIR=%{install_libdir}/cmake/llvm \
+%else
         -DMLIR_ENABLE_BINDINGS_PYTHON:BOOL=ON \
 %if 0%{?__isa_bits} == 64
         -DLLVM_LIBDIR_SUFFIX=64
 %else
         -DLLVM_LIBDIR_SUFFIX=
 %endif
+%endif
 # build process .exe tools normally use rpath or static linkage
 export LD_LIBRARY_PATH=%{_builddir}/%{mlir_srcdir}/%{name}/%{_build}/%{_lib}
 %cmake_build
 
 %install
 %cmake_install
+%if %{without compat_build}
 mkdir -p %{buildroot}/%{python3_sitearch}
 mv %{buildroot}/usr/python_packages/mlir_core/mlir %{buildroot}/%{python3_sitearch}
 # These directories should be empty now.
 rmdir %{buildroot}/usr/python_packages/mlir_core %{buildroot}/usr/python_packages
 # Unneeded files.
 rm -rf %{buildroot}/usr/src/python
+%endif
 
 %check
 # Remove tablegen tests, as they rely on includes from llvm/.
@@ -231,46 +264,55 @@ rm -rf test/mlir-pdll-lsp-server/textdocument-didchange.test
 rm -rf test/mlir-pdll-lsp-server/view-output.test
 %endif
 
+%if %{without compat_build}
 # Test execution normally relies on RPATH, so set LD_LIBRARY_PATH instead.
-export LD_LIBRARY_PATH=%{buildroot}/%{_libdir}:%{buildroot}/%{python3_sitearch}/mlir/_mlir_libs
+export LD_LIBRARY_PATH=%{buildroot}/%{install_libdir}:%{buildroot}/%{python3_sitearch}/mlir/_mlir_libs
 export PYTHONPATH=%{buildroot}/%{python3_sitearch}
+%else
+export LD_LIBRARY_PATH=%{buildroot}/%{install_libdir}
+%endif
 %cmake_build --target check-mlir
 
 %files
 %license LICENSE.TXT
-%{_libdir}/libMLIR*.so.%{maj_ver}*
-%{_libdir}/libmlir_async_runtime.so.%{maj_ver}*
-%{_libdir}/libmlir_c_runner_utils.so.%{maj_ver}*
-%{_libdir}/libmlir_float16_utils.so.%{maj_ver}*
-%{_libdir}/libmlir_runner_utils.so.%{maj_ver}*
+%{install_libdir}/libMLIR*.so.%{maj_ver}*
+%{install_libdir}/libmlir_async_runtime.so.%{maj_ver}*
+%{install_libdir}/libmlir_c_runner_utils.so.%{maj_ver}*
+%{install_libdir}/libmlir_float16_utils.so.%{maj_ver}*
+%{install_libdir}/libmlir_runner_utils.so.%{maj_ver}*
 
 %files static
-%{_libdir}/libMLIR*.a
+%{install_libdir}/libMLIR*.a
 
 %files devel
-%{_bindir}/mlir-cpu-runner
-%{_bindir}/mlir-linalg-ods-yaml-gen
-%{_bindir}/mlir-lsp-server
-%{_bindir}/mlir-opt
-%{_bindir}/mlir-pdll
-%{_bindir}/mlir-pdll-lsp-server
-%{_bindir}/mlir-reduce
-%{_bindir}/mlir-tblgen
-%{_bindir}/mlir-translate
-%{_bindir}/tblgen-lsp-server
-%{_libdir}/libMLIR*.so
-%{_libdir}/libmlir_async_runtime.so
-%{_libdir}/libmlir_c_runner_utils.so
-%{_libdir}/libmlir_float16_utils.so
-%{_libdir}/libmlir_runner_utils.so
-%{_includedir}/mlir
-%{_includedir}/mlir-c
-%{_libdir}/cmake/mlir
-
+%{install_bindir}/mlir-cpu-runner
+%{install_bindir}/mlir-linalg-ods-yaml-gen
+%{install_bindir}/mlir-lsp-server
+%{install_bindir}/mlir-opt
+%{install_bindir}/mlir-pdll
+%{install_bindir}/mlir-pdll-lsp-server
+%{install_bindir}/mlir-reduce
+%{install_bindir}/mlir-tblgen
+%{install_bindir}/mlir-translate
+%{install_bindir}/tblgen-lsp-server
+%{install_libdir}/libMLIR*.so
+%{install_libdir}/libmlir_async_runtime.so
+%{install_libdir}/libmlir_c_runner_utils.so
+%{install_libdir}/libmlir_float16_utils.so
+%{install_libdir}/libmlir_runner_utils.so
+%{install_includedir}/mlir
+%{install_includedir}/mlir-c
+%{install_libdir}/cmake/mlir
+
+%if %{without compat_build}
 %files -n python3-%{name}
 %{python3_sitearch}/mlir/
+%endif
 
 %changelog
+* Wed May 1 2024 Tom Rix <trix@redhat.com> - 17.0.6-4
+- Add compat_build logic
+
 * Thu Jan 25 2024 Fedora Release Engineering <releng@fedoraproject.org> - 17.0.6-3
 - Rebuilt for https://fedoraproject.org/wiki/Fedora_40_Mass_Rebuild
 
-- 
2.44.0

