# Copyright (c) Advanced Micro Devices, Inc.
# SPDX-License-Identifier:  MIT

cmake_minimum_required(VERSION 3.21 FATAL_ERROR)

# This example requires hip and rocjpeg.
find_package(HIP QUIET)

if(NOT HIP_FOUND)
    message(WARNING "hip is not found. Skip jpegdecode example.")
    return()
endif()

# Set AMD Clang as default compiler
if(NOT DEFINED CMAKE_CXX_COMPILER)
    set(CMAKE_C_COMPILER ${ROCmVersion_DIR}/bin/amdclang)
    set(CMAKE_CXX_COMPILER ${ROCmVersion_DIR}/bin/amdclang++)
endif()

project(rocprofiler-systems-jpegdecode-example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED On)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../cmake)
list(APPEND CMAKE_PREFIX_PATH ${ROCmVersion_DIR}/lib/cmake ${ROCmVersion_DIR})
list(APPEND CMAKE_PREFIX_PATH ${ROCmVersion_DIR}/hip ${ROCmVersion_DIR})
list(APPEND CMAKE_MODULE_PATH ${ROCmVersion_DIR}/share/rocjpeg/cmake)

set(CMAKE_BUILD_TYPE "RelWithDebInfo")
string(REPLACE " " ";" _FLAGS "${CMAKE_CXX_FLAGS_DEBUG}")

if(ROCPROFSYS_DISABLE_EXAMPLES)
    get_filename_component(_DIR ${CMAKE_CURRENT_LIST_DIR} NAME)

    if(
        ${PROJECT_NAME} IN_LIST ROCPROFSYS_DISABLE_EXAMPLES
        OR ${_DIR} IN_LIST ROCPROFSYS_DISABLE_EXAMPLES
    )
        return()
    endif()
endif()

# find rocJPEG - library and headers
find_path(
    rocjpeg_ROOT_DIR
    NAMES include/rocjpeg/rocjpeg.h
    HINTS ${ROCmVersion_DIR} ${ROCM_PATH}
    PATHS ${ROCmVersion_DIR} ${ROCM_PATH}
)

mark_as_advanced(rocjpeg_ROOT_DIR)

find_path(
    rocjpeg_INCLUDE_DIR
    NAMES rocjpeg/rocjpeg.h
    HINTS ${rocjpeg_ROOT_DIR}
    PATHS ${rocjpeg_ROOT_DIR}
    PATH_SUFFIXES include
)

find_library(
    rocjpeg_LIBRARY
    NAMES rocjpeg
    HINTS ${rocjpeg_ROOT_DIR}
    PATHS ${rocjpeg_ROOT_DIR}
    PATH_SUFFIXES lib
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
    rocjpeg
    FOUND_VAR rocjpeg_FOUND
    REQUIRED_VARS rocjpeg_INCLUDE_DIR rocjpeg_LIBRARY
)

if(rocjpeg_FOUND)
    if(NOT TARGET rocjpeg::rocjpeg)
        add_library(rocjpeg::rocjpeg INTERFACE IMPORTED)
        target_link_libraries(rocjpeg::rocjpeg INTERFACE ${rocjpeg_LIBRARY})
        target_include_directories(rocjpeg::rocjpeg INTERFACE ${rocjpeg_INCLUDE_DIR})
    endif()
endif()

find_package(rocprofiler-register QUIET)

# Copy image files to build directory
function(copy_image_files_and_make_copies)
    if(EXISTS "${ROCmVersion_DIR}/share/rocjpeg/images")
        if(NOT EXISTS "${CMAKE_BINARY_DIR}/images")
            file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/images")
        endif()

        file(GLOB_RECURSE image_files "${ROCmVersion_DIR}/share/rocjpeg/images/*")
        file(COPY ${image_files} DESTINATION ${CMAKE_BINARY_DIR}/images)
        set(NUM_COPIES 20)

        # Loop over each file and make additional copies
        foreach(file ${image_files})
            get_filename_component(filename ${file} NAME)
            foreach(i RANGE 1 ${NUM_COPIES})
                file(
                    COPY ${file}
                    DESTINATION ${CMAKE_BINARY_DIR}/images/${filename}_copy${i}.jpg
                )
            endforeach()
        endforeach()
    else()
        message(
            AUTHOR_WARNING
            "Source directory ${ROCmVersion_DIR}/share/rocjpeg/images does not exist"
        )
    endif()
endfunction()

# threads
find_package(Threads REQUIRED)

if(HIP_FOUND AND rocjpeg_FOUND AND Threads_FOUND AND rocprofiler-register_FOUND)
    # HIP
    set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} hip::host)
    # threads
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} Threads::Threads)
    # std filesystem
    set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} stdc++fs)
    # rocprofiler-register
    set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} rocprofiler-register::rocprofiler-register)

    # rocJPEG
    message(STATUS "RocJPEG library found: ${rocjpeg_LIBRARY}")
    include_directories(${rocjpeg_INCLUDE_DIR})
    set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} rocjpeg::rocjpeg)
    list(APPEND SOURCES ${PROJECT_SOURCE_DIR} jpegdecodeperf.cpp)
    add_executable(jpegdecode ${SOURCES})
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17")
    target_link_libraries(jpegdecode ${LINK_LIBRARY_LIST})
    target_compile_options(jpegdecode PRIVATE ${_FLAGS})
    copy_image_files_and_make_copies()

    if(ROCPROFSYS_INSTALL_EXAMPLES AND TARGET jpegdecode)
        install(
            TARGETS jpegdecode
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/rocprofiler-systems/examples
            COMPONENT rocprofiler-systems-examples
        )
        install(
            DIRECTORY ${CMAKE_BINARY_DIR}/images/
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/rocprofiler-systems/examples/images
            COMPONENT rocprofiler-systems-examples
        )
    endif()
else()
    message(
        "-- ERROR!: ${PROJECT_NAME} excluded! please install all the dependencies and try again!"
    )
    if(NOT HIP_FOUND)
        message(FATAL_ERROR "-- ERROR!: HIP Not Found! - please install ROCm and HIP!")
    endif()
    if(NOT rocjpeg_FOUND)
        message(WARNING "-- ERROR!: rocJPEG Not Found! - please install rocJPEG!")
    endif()
    if(NOT Threads_FOUND)
        message(WARNING "-- ERROR!: Threads Not Found! - please install Threads!")
    endif()
    if(NOT rocprofiler-register_FOUND)
        message(
            WARNING
            "-- ERROR!: rocprofiler-register Not Found! - please install rocprofiler-register!"
        )
    endif()
endif()
