%global rocm_release 5.6
%global rocm_patch 0
%global rocm_version %{rocm_release}.%{rocm_patch}

Name:           hipcc
Version:        %{rocm_version}
Release:        3%{?dist}
Summary:        HIP compiler driver
Url:            https://github.com/ROCm-Developer-Tools/HIPCC
License:        MIT
Source0:        https://github.com/ROCm-Developer-Tools/HIPCC/archive/refs/tags/rocm-%{version}.tar.gz#/%{name}-%{version}.tar.gz

Patch0:         https://github.com/ROCm-Developer-Tools/HIPCC/commit/a11397e769f71227cfc44353842ea90707610236.patch
Patch1:         https://github.com/ROCm-Developer-Tools/HIPCC/commit/560bee0df3b95d9cf18d5e52d7fa99ffe6b0f488.patch

BuildRequires:  cmake
BuildRequires:  fdupes
BuildRequires:  gcc-c++
BuildRequires:  perl
BuildRequires:  perl-generators
BuildRequires:  rocminfo

Requires:       rocminfo >= %{rocm_release}
Requires:       clang
# 16.2 has an important fix for hipcc to work out of the box:
Requires:       rocm-device-libs >= 16.2
#HIPCC requires a backend, recommend rocm-hip as it's in Fedora:
Recommends:     rocm-hip

# HIPCC used to live in the dropped "hip" package
Provides:       hip = %{version}-%{release}
Obsoletes:      hip < 5.6.0

# This package depends on "hip" which is only built on these arches right now:
ExclusiveArch:  x86_64 aarch64 ppc64le

%description
hipcc is a compiler driver utility that will call clang or nvcc, depending on
target, and pass the appropriate include and library options for the target
compiler and HIP infrastructure.

hipcc will pass-through options to the target compiler. The tools calling hipcc
must ensure the compiler options are appropriate for the target compiler.

%prep
%autosetup -p1 -n HIPCC-rocm-%{version}

# Fix script shebang (Fedora doesn't allow using "env"):
sed -i 's|\(/usr/bin/\)env perl|\1perl|' bin/hipcc.pl
# ROCm upstream uses /opt/rocm/hsa for rocm-runtime, but Fedora uses /usr
# Don't include it since /usr/include is already included:
sed -i '/" -isystem " + hsaPath + "\/include"/d' src/hipBin_amd.h
# Same thing for hipcc.pl:
sed -i '/^# Add paths to common HIP includes:/,/^$HIPCFLAGS/d' bin/hipcc.pl

%build
%cmake -DHIPCC_BACKWARD_COMPATIBILITY=OFF
%cmake_build

%install
%cmake_install

# Fix perl module files installation:
mkdir -p %{buildroot}%{perl_vendorlib}
mv %{buildroot}%{_bindir}/hip*.pm %{buildroot}%{perl_vendorlib}
# Eventually upstream plans to deprecate Perl usage, see README.md

%fdupes %{buildroot}%{_prefix}

%files
%license %{_docdir}/hipcc/LICENSE.txt
%doc README.md
%{_bindir}/hipcc{,.pl,.bin}
%{_bindir}/hipconfig{,.pl,.bin}
%{perl_vendorlib}/hip*.pm

%changelog
* Tue Jul 04 2023 Jeremy Newton <alexjnewt at hotmail dot com> - 5.6.0-3
- Align everything to replace the hipcc subpackage in rocclr
- Bump the release to 3 to make sure this is an upgrade of rocclr's subpackage

* Thu Jun 29 2023 Jeremy Newton <alexjnewt at hotmail dot com> - 5.6.0-1
- Update to 5.6

* Wed Jun 07 2023 Jeremy Newton <alexjnewt at hotmail dot com> - 5.5.1-1
- Initial package