<commit_before>cmake_minimum_required(VERSION 2.8.3)
project(hip)

#############################
# Setup config generation
#############################
string(TIMESTAMP _timestamp UTC)
set(_versionInfo "# Auto-generated by cmake\n")
set(_buildInfo "# Auto-generated by cmake on ${_timestamp} UTC\n")
macro(add_to_config _configfile _variable)
    set(${_configfile} "${${_configfile}}${_variable}=${${_variable}}\n")
endmacro()

#############################
# Setup version information
#############################
# Determine HIP_BASE_VERSION
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bin/hipconfig --version
                OUTPUT_VARIABLE HIP_BASE_VERSION
                OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "." ";" VERSION_LIST ${HIP_BASE_VERSION})
list(GET VERSION_LIST 0 HIP_VERSION_MAJOR)
list(GET VERSION_LIST 1 HIP_VERSION_MINOR)

# get date information based on UTC
# use the last two digits of year + week number + day in the week as HIP_VERSION_PATCH
# use the commit date, instead of build date
# add xargs to remove strange trailing newline character
execute_process(COMMAND git show -s --format=@%ct
                COMMAND xargs
                COMMAND date -f - --utc +%y%U%w
                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
                OUTPUT_VARIABLE HIP_VERSION_PATCH
                OUTPUT_STRIP_TRAILING_WHITESPACE)

set(HIP_VERSION $HIP_VERSION_MAJOR.$HIP_VERSION_MINOR.$HIP_VERSION_PATCH)
add_to_config(_versionInfo HIP_VERSION_MAJOR)
add_to_config(_versionInfo HIP_VERSION_MINOR)
add_to_config(_versionInfo HIP_VERSION_PATCH)

#############################
# Configure variables
#############################
# Determine HIP_PLATFORM
if(NOT DEFINED HIP_PLATFORM)
    if(NOT DEFINED ENV{HIP_PLATFORM})
        execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bin/hipconfig --platform
                        OUTPUT_VARIABLE HIP_PLATFORM
                        OUTPUT_STRIP_TRAILING_WHITESPACE)
    else()
        set(HIP_PLATFORM $ENV{HIP_PLATFORM} CACHE STRING "HIP Platform")
    endif()
endif()
message(STATUS "HIP Platform: " ${HIP_PLATFORM})

# If HIP_PLATFORM is hcc, we need HCC_HOME and HSA_PATH to be defined
if(HIP_PLATFORM STREQUAL "hcc")
    # Determine HCC_HOME
    if(NOT DEFINED HCC_HOME)
        if(NOT DEFINED ENV{HCC_HOME})
            set(HCC_HOME "/opt/rocm/hcc" CACHE PATH "Path to which HCC has been installed")
        else()
            set(HCC_HOME $ENV{HCC_HOME} CACHE PATH "Path to which HCC has been installed")
        endif()
    endif()
    if(DEFINED ENV{HIP_DEVELOPER})
        add_to_config(_buildInfo HCC_HOME)
    endif()
    if(IS_ABSOLUTE ${HCC_HOME} AND EXISTS ${HCC_HOME} AND IS_DIRECTORY ${HCC_HOME})
        execute_process(COMMAND ${HCC_HOME}/bin/hcc --version
                        COMMAND cut -d\  -f9
                        OUTPUT_VARIABLE HCC_VERSION
                        OUTPUT_STRIP_TRAILING_WHITESPACE)
        message(STATUS "Looking for HCC in: " ${HCC_HOME} ". Found version: " ${HCC_VERSION})
    else()
        message(FATAL_ERROR "Don't know where to find HCC. Please specify abolute path using -DHCC_HOME")
    endif()
    add_to_config(_buildInfo HCC_VERSION)

    # Determine HSA_PATH
    if(NOT DEFINED HSA_PATH)
        if(NOT DEFINED ENV{HSA_PATH})
            set(HSA_PATH "/opt/rocm/hsa" CACHE PATH "Path to which HSA runtime has been installed")
        else()
            set(HSA_PATH $ENV{HSA_PATH} CACHE PATH "Path to which HSA runtime has been installed")
        endif()
    endif()
    if(IS_ABSOLUTE ${HSA_PATH} AND EXISTS ${HSA_PATH} AND IS_DIRECTORY ${HSA_PATH})
        message(STATUS "Looking for HSA runtime in: " ${HSA_PATH})
    else()
        message(FATAL_ERROR "Don't know where to find HSA runtime. Please specify absolute path using -DHSA_PATH")
    endif()
endif()

# Set default build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release")
endif()

# Determine HIP install path
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND CMAKE_INSTALL_PREFIX MATCHES "/usr/local")
    if(CMAKE_BUILD_TYPE MATCHES Debug)
            set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "Installation path for HIP" FORCE)
    elseif(CMAKE_BUILD_TYPE MATCHES Release)
            set(CMAKE_INSTALL_PREFIX "/opt/rocm/hip" CACHE PATH "Installation path for HIP" FORCE)
    else()
        message(FATAL_ERROR "Invalid CMAKE_BUILD_TYPE specified. Valid values are Debug and Release")
    endif()
endif()
if(IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX})
    message(STATUS "HIP will be installed in: " ${CMAKE_INSTALL_PREFIX})
else()
    message(FATAL_ERROR "Don't know where to install HIP. Please specify absolute path using -DCMAKE_INSTALL_PREFIX")
endif()

# Set if we need to build shared or static library
if(NOT DEFINED HIP_LIB_TYPE)
    if(NOT DEFINED ENV{HIP_LIB_TYPE})
        set(HIP_LIB_TYPE 1)
    else()
        set(HIP_LIB_TYPE $ENV{HIP_LIB_TYPE})
    endif()
endif()
add_to_config(_buildInfo HIP_LIB_TYPE)

# Check if we need to build clang hipify
if(NOT DEFINED BUILD_CLANG_HIPIFY)
    if(NOT DEFINED ENV{BUILD_CLANG_HIPIFY})
        set(BUILD_CLANG_HIPIFY 0)
    else()
        set(BUILD_CLANG_HIPIFY $ENV{BUILD_CLANG_HIPIFY})
    endif()
endif()

# Check if we need to enable ATP marker
if(NOT DEFINED COMPILE_HIP_ATP_MARKER)
    if(NOT DEFINED ENV{COMPILE_HIP_ATP_MARKER})
        set(COMPILE_HIP_ATP_MARKER 0)
    else()
        set(COMPILE_HIP_ATP_MARKER $ENV{COMPILE_HIP_ATP_MARKER})
    endif()
endif()
add_to_config(_buildInfo COMPILE_HIP_ATP_MARKER)

#############################
# Build steps
#############################
# Build clang hipify if enabled
if(BUILD_CLANG_HIPIFY)
    add_subdirectory(clang-hipify)
endif()

# Build hip_hcc if platform is hcc
if(HIP_PLATFORM STREQUAL "hcc")
    include_directories(${PROJECT_SOURCE_DIR}/include)
    if(COMPILE_HIP_ATP_MARKER)
        include_directories(/opt/rocm/profiler/CXLActivityLogger/include)
        set(CMAKE_CXX_FLAGS " -DCOMPILE_HIP_ATP_MARKER=1 ${CMAKE_CXX_FLAGS}")
        set(CMAKE_C_FLAGS   " -DCOMPILE_HIP_ATP_MARKER=1 ${CMAKE_C_FLAGS}")
    endif()

    set(CMAKE_CXX_COMPILER "${HCC_HOME}/bin/hcc")
    set(CMAKE_C_COMPILER   "${HCC_HOME}/bin/hcc")

    # Add HIP_VERSION to CMAKE_<LANG>_FLAGS
    set(CMAKE_CXX_FLAGS " -DHIP_VERSION_MAJOR=${HIP_VERSION_MAJOR} -DHIP_VERSION_MINOR=${HIP_VERSION_MINOR} -DHIP_VERSION_PATCH=${HIP_VERSION_PATCH} ${CMAKE_CXX_FLAGS}")
    set(CMAKE_C_FLAGS " -DHIP_VERSION_MAJOR=${HIP_VERSION_MAJOR} -DHIP_VERSION_MINOR=${HIP_VERSION_MINOR} -DHIP_VERSION_PATCH=${HIP_VERSION_PATCH} ${CMAKE_C_FLAGS}")

    # Set HIP_HCC so we know this is HIP compile, some files are shared with HCC (staging_buffer).
    set(CMAKE_CXX_FLAGS " -fPIC -hc -I${HCC_HOME}/include -I${HSA_PATH}/include -I/opt/rocm/libhsakmt/include/libhsakmt -stdlib=libc++ -DHIP_HCC ${CMAKE_CXX_FLAGS}")
    set(CMAKE_C_FLAGS   " -fPIC -hc -I${HCC_HOME}/include -I${HSA_PATH}/include -I/opt/rocm/libhsakmt/include/libhsakmt -stdlib=libc++ -DHIP_HCC ${CMAKE_C_FLAGS}")

    set(SOURCE_FILES src/device_util.cpp
                     src/hip_hcc.cpp
                     src/hip_context.cpp
                     src/hip_device.cpp
                     src/hip_error.cpp
                     src/hip_event.cpp
                     src/hip_ldg.cpp
                     src/hip_memory.cpp
                     src/hip_peer.cpp
                     src/hip_stream.cpp
                     src/hip_fp16.cpp
                     src/hip_module.cpp)

    if(${HIP_LIB_TYPE} EQUAL 0)
        add_library(hip_hcc OBJECT ${SOURCE_FILES})
    elseif(${HIP_LIB_TYPE} EQUAL 1)
        add_library(hip_hcc STATIC ${SOURCE_FILES})
    else()
        add_library(hip_hcc SHARED ${SOURCE_FILES})
    endif()

    # Generate .buildInfo
    file(WRITE "${PROJECT_BINARY_DIR}/.buildInfo" ${_buildInfo})
endif()

# Generate .version
file(WRITE "${PROJECT_BINARY_DIR}/.version" ${_versionInfo})
<commit_msg>CMakeLists.txt: No need to define HIP_HCC<commit_after>cmake_minimum_required(VERSION 2.8.3)
project(hip)

#############################
# Setup config generation
#############################
string(TIMESTAMP _timestamp UTC)
set(_versionInfo "# Auto-generated by cmake\n")
set(_buildInfo "# Auto-generated by cmake on ${_timestamp} UTC\n")
macro(add_to_config _configfile _variable)
    set(${_configfile} "${${_configfile}}${_variable}=${${_variable}}\n")
endmacro()

#############################
# Setup version information
#############################
# Determine HIP_BASE_VERSION
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bin/hipconfig --version
                OUTPUT_VARIABLE HIP_BASE_VERSION
                OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "." ";" VERSION_LIST ${HIP_BASE_VERSION})
list(GET VERSION_LIST 0 HIP_VERSION_MAJOR)
list(GET VERSION_LIST 1 HIP_VERSION_MINOR)

# get date information based on UTC
# use the last two digits of year + week number + day in the week as HIP_VERSION_PATCH
# use the commit date, instead of build date
# add xargs to remove strange trailing newline character
execute_process(COMMAND git show -s --format=@%ct
                COMMAND xargs
                COMMAND date -f - --utc +%y%U%w
                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
                OUTPUT_VARIABLE HIP_VERSION_PATCH
                OUTPUT_STRIP_TRAILING_WHITESPACE)

set(HIP_VERSION $HIP_VERSION_MAJOR.$HIP_VERSION_MINOR.$HIP_VERSION_PATCH)
add_to_config(_versionInfo HIP_VERSION_MAJOR)
add_to_config(_versionInfo HIP_VERSION_MINOR)
add_to_config(_versionInfo HIP_VERSION_PATCH)

#############################
# Configure variables
#############################
# Determine HIP_PLATFORM
if(NOT DEFINED HIP_PLATFORM)
    if(NOT DEFINED ENV{HIP_PLATFORM})
        execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bin/hipconfig --platform
                        OUTPUT_VARIABLE HIP_PLATFORM
                        OUTPUT_STRIP_TRAILING_WHITESPACE)
    else()
        set(HIP_PLATFORM $ENV{HIP_PLATFORM} CACHE STRING "HIP Platform")
    endif()
endif()
message(STATUS "HIP Platform: " ${HIP_PLATFORM})

# If HIP_PLATFORM is hcc, we need HCC_HOME and HSA_PATH to be defined
if(HIP_PLATFORM STREQUAL "hcc")
    # Determine HCC_HOME
    if(NOT DEFINED HCC_HOME)
        if(NOT DEFINED ENV{HCC_HOME})
            set(HCC_HOME "/opt/rocm/hcc" CACHE PATH "Path to which HCC has been installed")
        else()
            set(HCC_HOME $ENV{HCC_HOME} CACHE PATH "Path to which HCC has been installed")
        endif()
    endif()
    if(DEFINED ENV{HIP_DEVELOPER})
        add_to_config(_buildInfo HCC_HOME)
    endif()
    if(IS_ABSOLUTE ${HCC_HOME} AND EXISTS ${HCC_HOME} AND IS_DIRECTORY ${HCC_HOME})
        execute_process(COMMAND ${HCC_HOME}/bin/hcc --version
                        COMMAND cut -d\  -f9
                        OUTPUT_VARIABLE HCC_VERSION
                        OUTPUT_STRIP_TRAILING_WHITESPACE)
        message(STATUS "Looking for HCC in: " ${HCC_HOME} ". Found version: " ${HCC_VERSION})
    else()
        message(FATAL_ERROR "Don't know where to find HCC. Please specify abolute path using -DHCC_HOME")
    endif()
    add_to_config(_buildInfo HCC_VERSION)

    # Determine HSA_PATH
    if(NOT DEFINED HSA_PATH)
        if(NOT DEFINED ENV{HSA_PATH})
            set(HSA_PATH "/opt/rocm/hsa" CACHE PATH "Path to which HSA runtime has been installed")
        else()
            set(HSA_PATH $ENV{HSA_PATH} CACHE PATH "Path to which HSA runtime has been installed")
        endif()
    endif()
    if(IS_ABSOLUTE ${HSA_PATH} AND EXISTS ${HSA_PATH} AND IS_DIRECTORY ${HSA_PATH})
        message(STATUS "Looking for HSA runtime in: " ${HSA_PATH})
    else()
        message(FATAL_ERROR "Don't know where to find HSA runtime. Please specify absolute path using -DHSA_PATH")
    endif()
endif()

# Set default build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release")
endif()

# Determine HIP install path
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND CMAKE_INSTALL_PREFIX MATCHES "/usr/local")
    if(CMAKE_BUILD_TYPE MATCHES Debug)
            set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "Installation path for HIP" FORCE)
    elseif(CMAKE_BUILD_TYPE MATCHES Release)
            set(CMAKE_INSTALL_PREFIX "/opt/rocm/hip" CACHE PATH "Installation path for HIP" FORCE)
    else()
        message(FATAL_ERROR "Invalid CMAKE_BUILD_TYPE specified. Valid values are Debug and Release")
    endif()
endif()
if(IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX})
    message(STATUS "HIP will be installed in: " ${CMAKE_INSTALL_PREFIX})
else()
    message(FATAL_ERROR "Don't know where to install HIP. Please specify absolute path using -DCMAKE_INSTALL_PREFIX")
endif()

# Set if we need to build shared or static library
if(NOT DEFINED HIP_LIB_TYPE)
    if(NOT DEFINED ENV{HIP_LIB_TYPE})
        set(HIP_LIB_TYPE 1)
    else()
        set(HIP_LIB_TYPE $ENV{HIP_LIB_TYPE})
    endif()
endif()
add_to_config(_buildInfo HIP_LIB_TYPE)

# Check if we need to build clang hipify
if(NOT DEFINED BUILD_CLANG_HIPIFY)
    if(NOT DEFINED ENV{BUILD_CLANG_HIPIFY})
        set(BUILD_CLANG_HIPIFY 0)
    else()
        set(BUILD_CLANG_HIPIFY $ENV{BUILD_CLANG_HIPIFY})
    endif()
endif()

# Check if we need to enable ATP marker
if(NOT DEFINED COMPILE_HIP_ATP_MARKER)
    if(NOT DEFINED ENV{COMPILE_HIP_ATP_MARKER})
        set(COMPILE_HIP_ATP_MARKER 0)
    else()
        set(COMPILE_HIP_ATP_MARKER $ENV{COMPILE_HIP_ATP_MARKER})
    endif()
endif()
add_to_config(_buildInfo COMPILE_HIP_ATP_MARKER)

#############################
# Build steps
#############################
# Build clang hipify if enabled
if(BUILD_CLANG_HIPIFY)
    add_subdirectory(clang-hipify)
endif()

# Build hip_hcc if platform is hcc
if(HIP_PLATFORM STREQUAL "hcc")
    include_directories(${PROJECT_SOURCE_DIR}/include)
    set(HIP_HCC_BUILD_FLAGS)
    if(COMPILE_HIP_ATP_MARKER)
        include_directories(/opt/rocm/profiler/CXLActivityLogger/include)
        set(HIP_HCC_BUILD_FLAGS "${HIP_HCC_BUILD_FLAGS} -DCOMPILE_HIP_ATP_MARKER=1")
    endif()

    # Add HIP_VERSION to CMAKE_<LANG>_FLAGS
    set(HIP_HCC_BUILD_FLAGS "${HIP_HCC_BUILD_FLAGS} -DHIP_VERSION_MAJOR=${HIP_VERSION_MAJOR} -DHIP_VERSION_MINOR=${HIP_VERSION_MINOR} -DHIP_VERSION_PATCH=${HIP_VERSION_PATCH}")

    # Add remaining flags
    set(HIP_HCC_BUILD_FLAGS "${HIP_HCC_BUILD_FLAGS} -fPIC -hc -I${HCC_HOME}/include -I${HSA_PATH}/include -I/opt/rocm/libhsakmt/include/libhsakmt -stdlib=libc++")

    # Set compiler and compiler flags
    set(CMAKE_CXX_COMPILER "${HCC_HOME}/bin/hcc")
    set(CMAKE_C_COMPILER   "${HCC_HOME}/bin/hcc")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${HIP_HCC_BUILD_FLAGS}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${HIP_HCC_BUILD_FLAGS}")

    set(SOURCE_FILES src/device_util.cpp
                     src/hip_hcc.cpp
                     src/hip_context.cpp
                     src/hip_device.cpp
                     src/hip_error.cpp
                     src/hip_event.cpp
                     src/hip_ldg.cpp
                     src/hip_memory.cpp
                     src/hip_peer.cpp
                     src/hip_stream.cpp
                     src/hip_fp16.cpp
                     src/hip_module.cpp)

    if(${HIP_LIB_TYPE} EQUAL 0)
        add_library(hip_hcc OBJECT ${SOURCE_FILES})
    elseif(${HIP_LIB_TYPE} EQUAL 1)
        add_library(hip_hcc STATIC ${SOURCE_FILES})
    else()
        add_library(hip_hcc SHARED ${SOURCE_FILES})
    endif()

    # Generate .buildInfo
    file(WRITE "${PROJECT_BINARY_DIR}/.buildInfo" ${_buildInfo})
endif()

# Generate .version
file(WRITE "${PROJECT_BINARY_DIR}/.version" ${_versionInfo})

{'fraction_non_alphanumeric': 0.11861582826017301, 'fraction_numerical': 0.0018583787247677026, 'mean_word_length': 3.516931982633864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3252126', 'n_tokens_mistral': 5654, 'n_tokens_neox': 5325, 'n_words': 1132}