Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
if(NOT PREFERRED_LLVM_VERSION)
set(PREFERRED_LLVM_VERSION "12.0.0")
endif(NOT PREFERRED_LLVM_VERSION)
message(STATUS "Looking for LLVM version ${PREFERRED_LLVM_VERSION}")
message(STATUS "[OPENCL-CLANG] Looking for LLVM version ${PREFERRED_LLVM_VERSION}")
find_package(LLVM ${PREFERRED_LLVM_VERSION} REQUIRED)

message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "[OPENCL-CLANG] Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "[OPENCL-CLANG] Found LLVM ${LLVM_PACKAGE_VERSION}")

set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
Expand All @@ -38,12 +38,12 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
option(LLVMSPIRV_INCLUDED_IN_LLVM
"Set to ON if libLLVMSPIRVLib is linked into libLLVM" ON)
if(LLVMSPIRV_INCLUDED_IN_LLVM)
message(STATUS "Assuming that libLLVMSPIRVLib is linked into libLLVM")
message(STATUS "[OPENCL-CLANG] Assuming that libLLVMSPIRVLib is linked into libLLVM")
else(LLVMSPIRV_INCLUDED_IN_LLVM)
message(STATUS
"Assuming that libLLVMSPIRVLib is NOT linked into libLLVM")
"[OPENCL-CLANG] Assuming that libLLVMSPIRVLib is NOT linked into libLLVM")
if(NOT SPIRV_TRANSLATOR_DIR)
message(FATAL_ERROR "SPIRV_TRANSLATOR_DIR is required")
message(FATAL_ERROR "[OPENCL-CLANG] SPIRV_TRANSLATOR_DIR is required")
endif(NOT SPIRV_TRANSLATOR_DIR)
endif(LLVMSPIRV_INCLUDED_IN_LLVM)
else(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
Expand Down Expand Up @@ -85,10 +85,10 @@ if(NOT USE_PREBUILT_LLVM)
set(CLANG_SOURCE_DIR "${LLVM_EXTERNAL_CLANG_SOURCE_DIR}")
endif()
if(EXISTS ${CLANG_SOURCE_DIR})
message(STATUS "Using Clang source code direcotry: ${CLANG_SOURCE_DIR}")
message(STATUS "[OPENCL-CLANG] Using Clang source code direcotry: ${CLANG_SOURCE_DIR}")
else()
message(FATAL_ERROR
"Can't find Clang source code directory!\n"
"[OPENCL-CLANG] Can't find Clang source code directory!\n"
"If you are using LLVM monorepo:\n"
" 1. Clean CMake cache: `rm CMakeCache.txt`\n"
" 2. Enable Clang project with `-DLLVM_ENABLE_PROJECTS=\"clang\"` option\n"
Expand All @@ -103,10 +103,10 @@ if(NOT USE_PREBUILT_LLVM)
set(SPIRV_SOURCE_DIR ${LLVM_EXTERNAL_LLVM_SPIRV_SOURCE_DIR})
endif()
if(EXISTS ${SPIRV_SOURCE_DIR})
message(STATUS "Using SPIRV-LLVM-Translator source code directory: ${SPIRV_SOURCE_DIR}")
message(STATUS "[OPENCL-CLANG] Using SPIRV-LLVM-Translator source code directory: ${SPIRV_SOURCE_DIR}")
else()
message(FATAL_ERROR
"Can't find SPIRV-LLVM-Translator source code dir!\n"
"[OPENCL-CLANG] Can't find SPIRV-LLVM-Translator source code dir!\n"
"If you are using LLVM monorepo, SPIRV-LLVM-Translator should be checked out "
"at '<monorepo_root_dir>/llvm-spirv' and it should be enabled as an external LLVM "
"project using the following 2 options:\n"
Expand Down
91 changes: 84 additions & 7 deletions cmake/modules/CMakeFunctions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,51 @@ macro(use_eh val)
endif()
endmacro(use_eh)

# Reads hash-commit from backported patch
# This function assumes that each of files starts with (for example):
# From 1a400928bf8fc86fa0f062524c25d0985c94ac6f Mon Sep 17 00:00:00 2001
function(get_backport_patch_hash patch_path patch_hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use "git rev-parse " to get commit id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot see how I could read sha1 using git rev-parse from the file containing the patch before patching? Could you share the exact command line which you are thinking?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably did a wrong test, rev-parse is not working.

file(READ ${patch_path} first_line LIMIT 40 OFFSET 5)
string(STRIP ${first_line} first_line_strip)
set(patch_hash ${first_line_strip} PARENT_SCOPE)
endfunction()

# Checks if the the patch is present in current local branch
function(is_backport_patch_present patch_path repo_dir patch_in_branch)
get_backport_patch_hash(${patch_path} patch_hash)
message(STATUS "[OPENCL-CLANG] Checking if patch ${patch_hash} is present in repository")
execute_process(
COMMAND ${GIT_EXECUTABLE} merge-base --is-ancestor ${patch_hash} HEAD
WORKING_DIRECTORY ${repo_dir}
RESULT_VARIABLE patch_not_in_branches
OUTPUT_QUIET
ERROR_QUIET
)
if(patch_not_in_branches)
set(patch_in_branch False PARENT_SCOPE) # The patch is not present in local branch
else()
set(patch_in_branch True PARENT_SCOPE) # The patch is not present in local branch
endif()
endfunction()

# Validates if given SHA1/tag/branch name exists in local repo
function(is_valid_revision repo_dir revision return_val)
message(STATUS "[OPENCL-CLANG] Validating ${revision} in repository")
# Check if we have under revision existing branch/tag/SHA1 in this repo
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 ${revision}
WORKING_DIRECTORY ${repo_dir}
RESULT_VARIABLE output_var
ERROR_QUIET
OUTPUT_QUIET
)
if(${output_var} EQUAL 0)
set(${return_val} True PARENT_SCOPE) # this tag/branch/sha1 exists in repo
else()
set(${return_val} False PARENT_SCOPE) # this tag/branch/sha1 not exists in repo
endif()
endfunction()

#
# Creates `target_branch` starting at the `base_revision` in the `repo_dir`.
# Then all patches from the `patches_dir` are committed to the `target_branch`.
Expand All @@ -47,31 +92,63 @@ endmacro(use_eh)
function(apply_patches repo_dir patches_dir base_revision target_branch)
file(GLOB patches ${patches_dir}/*.patch)
if(NOT patches)
message(STATUS "No patches in ${patches_dir}")
message(STATUS "[OPENCL-CLANG] No patches in ${patches_dir}")
return()
endif()

message(STATUS "${repo_dir}:")
message(STATUS "[OPENCL-CLANG] Patching repository ${repo_dir}")
# Check if the target branch already exists
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --verify --no-revs -q ${target_branch}
WORKING_DIRECTORY ${repo_dir}
RESULT_VARIABLE patches_needed
ERROR_QUIET
OUTPUT_QUIET
)
if(patches_needed) # The target branch doesn't exist
list(SORT patches)
is_valid_revision(${repo_dir} ${base_revision} exists_base_rev)

if(NOT ${exists_base_rev})
execute_process( # take SHA1 from HEAD
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${repo_dir}
OUTPUT_VARIABLE repo_head
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
message(STATUS "[OPENCL-CLANG] ref ${base_revision} not exists in repository, using current HEAD:${repo_head}")
set(base_revision ${repo_head})
endif()
execute_process( # Create the target branch
COMMAND ${GIT_EXECUTABLE} checkout -b ${target_branch} ${base_revision}
WORKING_DIRECTORY ${repo_dir}
)
execute_process( # Apply the pathces
COMMAND ${GIT_EXECUTABLE} am --3way --ignore-whitespace ${patches}
WORKING_DIRECTORY ${repo_dir}
)
RESULT_VARIABLE ret_check_out
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE checkout_log
OUTPUT_QUIET
)
message(STATUS "[OPENCL-CLANG] ${checkout_log} which starts from ref : ${base_revision}")
foreach(patch ${patches})
is_backport_patch_present(${patch} ${repo_dir} patch_in_branch)
if(${patch_in_branch})
message(STATUS "[OPENCL-CLANG] Patch ${patch} is already in local branch - ignore patching")
else()
execute_process( # Apply the patch
COMMAND ${GIT_EXECUTABLE} am --3way --ignore-whitespace ${patch}
WORKING_DIRECTORY ${repo_dir}
OUTPUT_VARIABLE patching_log
ERROR_QUIET
)
message(STATUS "[OPENCL-CLANG] Not present - ${patching_log}")
endif()
endforeach(patch)
else() # The target branch already exists
execute_process( # Check it out
COMMAND ${GIT_EXECUTABLE} checkout ${target_branch}
WORKING_DIRECTORY ${repo_dir}
ERROR_QUIET
OUTPUT_QUIET
)
endif()
endfunction()
Expand Down