From ce5497d9c84a856c5ae9d29f827fa16badeae422 Mon Sep 17 00:00:00 2001 From: "Yang, Haonan" Date: Mon, 26 Aug 2024 13:27:39 +0800 Subject: [PATCH] [Backport] Do not apply patches if sub non-git repo is located in a git repo. --- CMakeLists.txt | 33 +++++++++++++++++++----------- cmake/modules/CMakeFunctions.cmake | 20 ++++++++++-------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab055f0..e88d32c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,10 @@ if(CMAKE_CROSSCOMPILING AND OPENCL_CLANG_BUILD_EXTERNAL) llvm_create_cross_target(${PROJECT_NAME} NATIVE "" Release) endif() +option(LLVMSPIRV_INCLUDED_IN_LLVM + "Set to ON if libLLVMSPIRVLib is linked into libLLVM" ON) +option(APPLY_PATCHES "Apply local patches" ON) + if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(USE_PREBUILT_LLVM ON) @@ -59,8 +63,6 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) - option(LLVMSPIRV_INCLUDED_IN_LLVM - "Set to ON if libLLVMSPIRVLib is linked into libLLVM" ON) if(LLVMSPIRV_INCLUDED_IN_LLVM) message(STATUS "[OPENCL-CLANG] Assuming that libLLVMSPIRVLib is linked into libLLVM") else(LLVMSPIRV_INCLUDED_IN_LLVM) @@ -157,18 +159,25 @@ if(NOT USE_PREBUILT_LLVM) ) endif() - set(CLANG_BASE_REVISION release/18.x) + set(LLVM_BASE_REVISION release/18.x) set(SPIRV_BASE_REVISION llvm_release_180) set(TARGET_BRANCH "ocl-open-180") - - apply_patches(${CLANG_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/patches/clang - ${CLANG_BASE_REVISION} - ${TARGET_BRANCH}) - apply_patches(${SPIRV_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/patches/spirv - ${SPIRV_BASE_REVISION} - ${TARGET_BRANCH}) + get_filename_component(LLVM_MONOREPO_DIR ${LLVM_SOURCE_DIR} DIRECTORY) + set(LLVM_PATCHES_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/patches/llvm + ${CMAKE_CURRENT_SOURCE_DIR}/patches/clang) + if(APPLY_PATCHES) + message(STATUS "APPLY_PATCHES is enabled.") + apply_patches(${LLVM_MONOREPO_DIR} + "${LLVM_PATCHES_DIRS}" + ${LLVM_BASE_REVISION} + ${TARGET_BRANCH}) + apply_patches(${SPIRV_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/patches/spirv + ${SPIRV_BASE_REVISION} + ${TARGET_BRANCH}) + else() + message(STATUS "APPLY_PATCHES is disabled, skip patch apply process.") + endif() endif(NOT USE_PREBUILT_LLVM) # diff --git a/cmake/modules/CMakeFunctions.cmake b/cmake/modules/CMakeFunctions.cmake index 3323089..158d8e1 100644 --- a/cmake/modules/CMakeFunctions.cmake +++ b/cmake/modules/CMakeFunctions.cmake @@ -93,7 +93,13 @@ function(apply_patches repo_dir patches_dir base_revision target_branch) return() endif() - message(STATUS "[OPENCL-CLANG] Patching repository ${repo_dir}") + # Check if it's a git repo + if(EXISTS "${repo_dir}/.git") + message(STATUS "[OPENCL-CLANG] Patching repository ${repo_dir}") + else() + message(STATUS "[OPENCL-CLANG][Warning] ${repo_dir} is not a git repository, therefore, local patches are not applied") + return() + endif() # Check if the target branch already exists execute_process( COMMAND ${GIT_EXECUTABLE} rev-parse --verify --no-revs -q ${target_branch} @@ -101,11 +107,7 @@ function(apply_patches repo_dir patches_dir base_revision target_branch) RESULT_VARIABLE patches_needed OUTPUT_QUIET ) - if(patches_needed EQUAL 128) # not a git repo - message(STATUS "[OPENCL-CLANG] ${repo_dir} is not a git repository, therefore, local patches are not applied") - return() - endif() - if(patches_needed EQUAL 1) # The target branch doesn't exist + if(patches_needed EQUAL 1) # The target branch doesn't exist list(SORT patches) is_valid_revision(${repo_dir} ${base_revision} exists_base_rev) @@ -148,8 +150,10 @@ function(apply_patches repo_dir patches_dir base_revision target_branch) OUTPUT_QUIET ) endif() - if (ret_check_out OR ret_apply_patch) - message(FATAL_ERROR "[OPENCL-CLANG] Failed to apply patch!") + if (NOT (ret_check_out OR ret_apply_patch)) + message(STATUS "[OPENCL-CLANG] Applied patch successfully!") + else() + message(FATAL_ERROR "[OPENCL-CLANG] Failed to apply patch!") endif() endfunction()