cmake_minimum_required(VERSION 2.8.3)
project(hip_rocclr)

if(@BUILD_SHARED_LIBS@)
    install(FILES @PROJECT_BINARY_DIR@/lib/libamdhip64.so DESTINATION lib)
    install(FILES @PROJECT_BINARY_DIR@/lib/libamdhip64.so.@HIP_LIB_VERSION_MAJOR@ DESTINATION lib)
    install(FILES @PROJECT_BINARY_DIR@/lib/libamdhip64.so.@HIP_LIB_VERSION_STRING@ DESTINATION lib)
else()
    install(FILES @PROJECT_BINARY_DIR@/lib/libamdhip64.a DESTINATION lib)
endif()

install(FILES @PROJECT_BINARY_DIR@/.hipInfo DESTINATION lib)
install(FILES @PROJECT_BINARY_DIR@/hip-config.cmake @PROJECT_BINARY_DIR@/hip-config-version.cmake DESTINATION lib/cmake/hip)

#############################
# Rocclr install
#############################

set(ROCclr_BUILD_DIR "@ROCclr_DIR@/../../..")
set(ROCclr_LIB_DIR "@ROCM_PATH@/rocclr/lib")
set(ROCclr_CMAKE_DDIR "@ROCM_PATH@/rocclr/lib/cmake/rocclr")
set(ROCclr_CMAKE_SDIR "${ROCclr_BUILD_DIR}/CMakeFiles/Export/lib/cmake/rocclr")

install(FILES ${ROCclr_BUILD_DIR}/libamdrocclr_static.a DESTINATION ${ROCclr_LIB_DIR})
install(FILES @ROCclr_DIR@/ROCclrConfig.cmake DESTINATION ${ROCclr_CMAKE_DDIR})
install(FILES @ROCclr_DIR@/rocclr-config-version.cmake DESTINATION ${ROCclr_CMAKE_DDIR})
file(GLOB _rocclr_target_files ${ROCclr_CMAKE_SDIR}/rocclr-targets*.cmake)
foreach(_rocclr_target_file ${_rocclr_target_files})
  message(STATUS "_rocclr_target_file: ${_rocclr_target_file}")
endforeach()
install(FILES ${_rocclr_target_files} DESTINATION ${ROCclr_CMAKE_DDIR})

#############################
# Packaging steps
#############################
set(CPACK_SET_DESTDIR TRUE)
set(CPACK_INSTALL_PREFIX @CPACK_INSTALL_PREFIX@)

## cmake generated target files contains IMPORTED_LOCATION_RELEASE etc. which
## is installation path when building the project, which may be different from
## the intallation path for packaging. These paths have to be replaced by
## the package installation path, otherwise apps using pkg-config will fail.
file(GLOB _target_files @CONFIG_PACKAGE_INSTALL_DIR@/hip-targets*.cmake)
foreach(_target_file ${_target_files})
  execute_process(COMMAND sed -i s:@CMAKE_INSTALL_PREFIX@:${CPACK_INSTALL_PREFIX}:g ${_target_file})
endforeach()
install(FILES ${_target_files} DESTINATION lib/cmake/hip)

set(CPACK_PACKAGE_NAME "hip-rocclr")
set(HCC_PACKAGE_NAME "rocclr")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "HIP: Heterogenous-computing Interface for Portability [ROCClr]")
set(CPACK_PACKAGE_VENDOR "Advanced Micro Devices, Inc.")
set(CPACK_PACKAGE_CONTACT "HIP Support <hip.support@amd.com>")
set(CPACK_PACKAGE_VERSION_MAJOR @HIP_VERSION_MAJOR@)
set(CPACK_PACKAGE_VERSION_MINOR @HIP_VERSION_MINOR@)
set(CPACK_PACKAGE_VERSION_PATCH @HIP_VERSION_PATCH@)
set(CPACK_PACKAGE_VERSION @HIP_VERSION_MAJOR@.@HIP_VERSION_MINOR@.@HIP_PACKAGING_VERSION_PATCH@)
set(CPACK_GENERATOR "TGZ;DEB;RPM")

set(CPACK_BINARY_DEB "ON")
set(CPACK_DEBIAN_PACKAGE_RELEASE @CPACK_DEBIAN_PACKAGE_RELEASE@)
set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_BINARY_DIR}/postinst;${PROJECT_BINARY_DIR}/prerm")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "hsa-rocr-dev, rocminfo, hip-base (= ${CPACK_PACKAGE_VERSION}-${CPACK_DEBIAN_PACKAGE_RELEASE}),  comgr (>= 1.1), llvm-amdgpu, libc6")
set(CPACK_DEBIAN_PACKAGE_PROVIDES "hip-hcc (= ${CPACK_PACKAGE_VERSION})")

set(CPACK_BINARY_RPM "ON")
set(CPACK_RPM_PACKAGE_RELEASE @CPACK_RPM_PACKAGE_RELEASE@)
set(CPACK_RPM_FILE_NAME "RPM-DEFAULT")
set(CPACK_RPM_PACKAGE_ARCHITECTURE "${CMAKE_SYSTEM_PROCESSOR}")
set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${PROJECT_BINARY_DIR}/postinst")
set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${PROJECT_BINARY_DIR}/prerm")
set(CPACK_RPM_PACKAGE_AUTOREQPROV " no")
string(REPLACE "-" "_" HIP_BASE_VERSION ${CPACK_PACKAGE_VERSION})
set(CPACK_RPM_PACKAGE_REQUIRES "hsa-rocr-dev, rocminfo, hip-base = ${HIP_BASE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE},  comgr >= 1.1, llvm-amdgpu")
set(CPACK_RPM_PACKAGE_PROVIDES "hip-hcc = ${HIP_BASE_VERSION}")
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/opt")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)
