#hip is built with hcc which does not add a build id to objects.
%undefine _missing_build_ids_terminate_build
%global debug_package %{nil}

%global rocm_version 1.6.0

Name:		hip
Version:	1.2
Release:	8.rocm%{rocm_version}%{?dist}
Summary:	Tool for porting CUDA to Portable C++ Code

License:	MIT
URL:		https://github.com/ROCm-Developer-Tools/HIP
Source0:	https://github.com/ROCm-Developer-Tools/HIP/archive/roc-%{rocm_version}.zip
Patch0:		0001-Make-HIP_VERSION_PATCH-configurable.patch
# hipconfig defaults to using the nvcc platform if it doesn't detect an hsa
# enabled system, but we want to be able use non-HSA sytems for buliding
# hip applications (e.g. koji builders), so we make hcc the default platform.
Patch1:		0001-Make-hcc-the-default-platform.patch
Patch2:		0001-Remove-explicit-address-spaces-declarations.patch

BuildRequires:	cmake
# Perl requried for hipconfig
BuildRequires:	perl
BuildRequires:	elfutils-libelf-devel
BuildRequires:	hcc
BuildRequires:	rocm-runtime-devel

%if 0%{?fedora} < 27
BuildRequires:  perl-Getopt-Long
%endif

Requires:  libstdc++-static
Requires:  perl-Getopt-Long

%description
Tool for porting CUDA to Portable C++ Code

%prep
%autosetup -n  HIP-roc-%{rocm_version} -p1


%build
mkdir build
cd build

# FIXME: hipcc reads HCC_HOME from .hipInfo in order to find the path to
# hcc.  In order to add HCC_HOME to .hipInfo, we need to set the
# HIP_DEVELOPER environment variable when configuring the source.
# We should try to fix this.
export HIP_DEVELOPER=1

# FIXME We aren't using cmake macro here, because it adds CXXFLAGS that hcc does
# not support.
cmake .. \
	-DHCC_HOME=/usr/ \
	-DHSA_PATH=/usr/ \
	-DHIP_VERSION_PATCH=` date --utc +%y%U%w` \
	-DHIP_PLATFORM=hcc \
	-DCMAKE_INSTALL_PREFIX=/usr

%make_build


%install
# FIXME: The hip CMake file is setup to regenrate .hipInfo every time make
# is run, so we need to re-define HIP_DEVELOPERS before we do make install.
export HIP_DEVELOPER=1
cd build
%make_install

%if 0%{?__isa_bits} == 64
	mkdir %{buildroot}%{_libdir}
	mv %{buildroot}/usr/lib/* %{buildroot}%{_libdir}
	mv %{buildroot}/usr/lib/.hipInfo %{buildroot}/%{_libdir}

	# Fix paths in hipcc
	sed -i s~HIP_PATH/lib~HIP_PATH/lib64~g %{buildroot}%{_bindir}/hipcc

	# Fix paths in cmake files
	sed -i s~/usr/lib~/usr/lib64~g %{buildroot}%{_libdir}/cmake/hip/*
%endif


# We don't want to ship the source files
rm -Rf %{buildroot}/usr/src

# cmake files shouldn't go in /usr/cmake
mv %{buildroot}/usr/cmake/* %{buildroot}%{_libdir}/cmake/hip

%files
%doc
%{_includedir}/*
%{_bindir}/*
%{_bindir}/.hipVersion
%{_libdir}/*
%{_libdir}/.hipInfo
%{_libdir}/cmake/hip



%changelog
* Fri Feb 16 2018 Tom Stellard <tstellar@redhat.com> - 1.2-8.rocm1.6.0
- Package cmake files

* Thu Feb 15 2018 Tom Stellard <tstellar@redhat.com> - 1.2-7.rocm1.6.0
- Fix assertion failure during build

* Wed Dec 20 2017 Tom Stellard <tstellar@redhat.com> - 1.2-6.rocm1.6.0
- Update version to match upstream

* Sat Dec 16 2017 Tom Stellard <tstellar@redhat.com> - 1.6.0-5
- Fix library paths in cmake files

* Sat Dec 16 2017 Tom Stellard <tstellar@redhat.com> - 1.6.0-4
- Package cmake files

* Fri Dec 15 2017 Tom Stellard <tstellar@redhat.com> - 1.6.0-3
- Add Requires: perl-Getopt-Long

* Fri Dec 15 2017 Tom Stellard <tstellar@redhat.com> - 1.6.0-2
- Make hcc the default platform