This is an automated email from the ASF dual-hosted git repository.

lxy-9602 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new f450d0a5 build(deps): validate shared third-party patch state (#570)
f450d0a5 is described below

commit f450d0a560cb901ad97716dc95b7fd1b5ab89ce1
Author: Zhang Jiawei <[email protected]>
AuthorDate: Thu Nov 19 18:40:52 2026 +0800

    build(deps): validate shared third-party patch state (#570)
---
 .devcontainer/Dockerfile.template        |   1 +
 .devcontainer/centos7/Dockerfile         |   1 +
 .github/workflows/build_and_test.yaml    |   2 +-
 .github/workflows/gcc8_test.yaml         |   2 +-
 .github/workflows/release_candidate.yaml |   4 +-
 CMakeLists.txt                           |   6 -
 cmake_modules/ApplyThirdpartyPatch.cmake | 373 +++++++++++++++++++++++++++++++
 cmake_modules/BuildUtils.cmake           |   4 +-
 cmake_modules/ThirdpartyPatch.cmake      | 225 +++++++++++++++++++
 cmake_modules/ThirdpartyToolchain.cmake  | 150 +++++++++----
 crates/vortex_callback_io/README.md      |   6 +-
 docs/source/building.rst                 |  19 +-
 12 files changed, 737 insertions(+), 56 deletions(-)

diff --git a/.devcontainer/Dockerfile.template 
b/.devcontainer/Dockerfile.template
index 0d2e3816..c9352963 100644
--- a/.devcontainer/Dockerfile.template
+++ b/.devcontainer/Dockerfile.template
@@ -65,6 +65,7 @@ RUN apt update && \
     lsb-release \
     meson \
     ninja-build \
+    patchutils \
     pkg-config \
     python3 \
     python3-pip \
diff --git a/.devcontainer/centos7/Dockerfile b/.devcontainer/centos7/Dockerfile
index fa0f9c1c..6119a358 100644
--- a/.devcontainer/centos7/Dockerfile
+++ b/.devcontainer/centos7/Dockerfile
@@ -133,6 +133,7 @@ enabled=1\n' > /etc/yum.repos.d/CentOS-SCLo-scl.repo \
         ninja-build \
         make \
         patch \
+        patchutils \
         curl \
         wget \
         unzip \
diff --git a/.github/workflows/build_and_test.yaml 
b/.github/workflows/build_and_test.yaml
index 7e32fffe..4a4770ef 100644
--- a/.github/workflows/build_and_test.yaml
+++ b/.github/workflows/build_and_test.yaml
@@ -126,7 +126,7 @@ jobs:
       - name: Install build and debugging dependencies
         run: |
           sudo apt-get update
-          sudo apt-get install -y gdb libcurl4-openssl-dev libssl-dev
+          sudo apt-get install -y gdb libcurl4-openssl-dev libssl-dev 
patchutils
       - name: Install and verify regression compiler
         if: matrix.gcc_version
         shell: bash
diff --git a/.github/workflows/gcc8_test.yaml b/.github/workflows/gcc8_test.yaml
index 045c4366..ca18360e 100644
--- a/.github/workflows/gcc8_test.yaml
+++ b/.github/workflows/gcc8_test.yaml
@@ -52,7 +52,7 @@ jobs:
           printf '%s\n' 'Acquire::https::CaInfo 
"/etc/apt/runner-ca-certificates.crt";' \
               'Acquire::Retries "3";' > /etc/apt/apt.conf.d/80-ci-downloads
           apt-get update
-          DEBIAN_FRONTEND=noninteractive apt-get install -y gcc-8 g++-8 
ninja-build git tar curl libcurl4-openssl-dev libssl-dev tzdata zip unzip 
pkg-config build-essential python3-dev gdb sudo
+          DEBIAN_FRONTEND=noninteractive apt-get install -y gcc-8 g++-8 
ninja-build git tar curl libcurl4-openssl-dev libssl-dev tzdata zip unzip 
pkg-config build-essential python3-dev gdb sudo patchutils
           curl -L -O 
https://github.com/Kitware/CMake/releases/download/v3.28.3/cmake-3.28.3-linux-x86_64.tar.gz
           tar -zxvf cmake-3.28.3-linux-x86_64.tar.gz -C /usr/local 
--strip-components=1
           rm cmake-3.28.3-linux-x86_64.tar.gz
diff --git a/.github/workflows/release_candidate.yaml 
b/.github/workflows/release_candidate.yaml
index f5ec2a57..84c7b4c5 100644
--- a/.github/workflows/release_candidate.yaml
+++ b/.github/workflows/release_candidate.yaml
@@ -140,10 +140,10 @@ jobs:
         shell: bash
         run: ci/scripts/setup_flatc.sh
 
-      - name: Install HTTP and TLS development dependencies
+      - name: Install build dependencies
         run: |
           sudo apt-get update
-          sudo apt-get install -y libcurl4-openssl-dev libssl-dev
+          sudo apt-get install -y libcurl4-openssl-dev libssl-dev patchutils
 
       - name: Verify, build, test, and install
         shell: bash
diff --git a/CMakeLists.txt b/CMakeLists.txt
index df1c51ea..149d689a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -149,12 +149,6 @@ include(GNUInstallDirs)
 set(PAIMON_CMAKE_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/Paimon")
 include(DefineOptions)
 
-if(PAIMON_OPTIONAL_INSTALL)
-    # Don't make the "install" target depend on the "all" target
-    set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
-    set(INSTALL_IS_OPTIONAL OPTIONAL)
-endif()
-
 if(PAIMON_EXTRA_ERROR_CONTEXT)
     add_definitions(-DPAIMON_EXTRA_ERROR_CONTEXT)
 endif()
diff --git a/cmake_modules/ApplyThirdpartyPatch.cmake 
b/cmake_modules/ApplyThirdpartyPatch.cmake
new file mode 100644
index 00000000..4666f461
--- /dev/null
+++ b/cmake_modules/ApplyThirdpartyPatch.cmake
@@ -0,0 +1,373 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+function(paimon_patch_error)
+    string(CONCAT reason ${ARGV})
+    set(cleanup "")
+    foreach(path IN LISTS PP_CLEAN)
+        string(APPEND cleanup "\n  ${path}")
+    endforeach()
+    message(FATAL_ERROR "${PP_NAME}: ${reason}\n"
+                        "Do not remove only .patched or a patch stamp. Remove 
these "
+                        "build-owned paths, then re-run CMake and 
build:${cleanup}\n"
+                        "Verified source archives may be retained. No files 
were deleted automatically."
+    )
+endfunction()
+
+function(paimon_patch_fingerprint output)
+    foreach(url IN LISTS PP_URLS)
+        if(IS_DIRECTORY "${url}")
+            paimon_patch_error("Local source directories are not supported; 
use a SHA256-verified archive: ${url}"
+            )
+        endif()
+    endforeach()
+    # The same ordered list is used here and by the application loop below.
+    set(identity
+        
"paimon-patch-v1\n${PP_NAME}\n${PP_VERSION}\n${PP_ARCHIVE}\n${PP_URLS}\n${PP_SOURCE}\n${PP_BINARY}\n${PP_INSTALL}\n${PP_CONTEXT}\n"
+    )
+    file(SHA256 "${CMAKE_CURRENT_FUNCTION_LIST_FILE}" runner_hash)
+    string(APPEND identity
+           
"runner=${runner_hash}\npatch-options=-p1,--batch,--forward,--fuzz=0\n")
+    set(index 0)
+    foreach(input IN LISTS PP_PATCHES PP_INPUTS)
+        if(NOT EXISTS "${input}")
+            paimon_patch_error("Required patch/build input is missing: 
${input}")
+        endif()
+        file(SHA256 "${input}" hash)
+        get_filename_component(name "${input}" NAME)
+        string(APPEND identity "${index}:${name}:${hash}\n")
+        math(EXPR index "${index} + 1")
+    endforeach()
+    list(LENGTH PP_PATCHES count)
+    string(APPEND identity "patch-count=${count}\n")
+    if(PP_VERSIONS_FILE)
+        if(NOT EXISTS "${PP_VERSIONS_FILE}")
+            paimon_patch_error("Version file is missing: ${PP_VERSIONS_FILE}")
+        endif()
+        foreach(variable IN LISTS PP_VERSION_VARIABLES)
+            file(STRINGS "${PP_VERSIONS_FILE}" entries REGEX "^${variable}=")
+            list(LENGTH entries count)
+            if(NOT count EQUAL 1)
+                paimon_patch_error("Expected one ${variable} entry in 
${PP_VERSIONS_FILE}"
+                )
+            endif()
+            string(APPEND identity "${entries}\n")
+        endforeach()
+    endif()
+    string(SHA256 fingerprint "${identity}")
+    set(${output}
+        "${fingerprint}"
+        PARENT_SCOPE)
+endfunction()
+
+function(paimon_patch_write name content)
+    file(WRITE "${PP_STATE}/${name}.tmp" "${content}")
+    file(RENAME "${PP_STATE}/${name}.tmp" "${PP_STATE}/${name}")
+endfunction()
+
+function(paimon_patch_check_paths)
+    file(REAL_PATH "${PP_ROOT}" root_real)
+    foreach(path IN LISTS PP_CLEAN)
+        file(REAL_PATH "${path}" path_real)
+        cmake_path(IS_PREFIX
+                   root_real
+                   "${path_real}"
+                   NORMALIZE
+                   owned)
+        if(NOT owned OR path_real STREQUAL root_real)
+            # Do not print removal advice for a path redirected outside the 
build.
+            message(FATAL_ERROR "${PP_NAME}: ${path} resolves outside its 
build-owned location. "
+                                "Restore the directory layout; do not delete 
the external source."
+            )
+        endif()
+    endforeach()
+endfunction()
+
+function(paimon_patch_assert_empty directory)
+    file(GLOB_RECURSE contents
+         LIST_DIRECTORIES FALSE
+         "${directory}/*")
+    foreach(path IN LISTS contents)
+        cmake_path(IS_PREFIX
+                   PP_STAMP
+                   "${path}"
+                   NORMALIZE
+                   in_stamp)
+        cmake_path(IS_PREFIX
+                   PP_TMP
+                   "${path}"
+                   NORMALIZE
+                   in_tmp)
+        if(NOT in_stamp AND NOT in_tmp)
+            paimon_patch_error("Unrecognized existing source/build/install 
files in ${directory}"
+            )
+        endif()
+    endforeach()
+endfunction()
+
+function(paimon_patch_check_postimage)
+    if(NOT EXISTS "${PP_SOURCE}/.paimon-source-id" OR NOT EXISTS 
"${PP_STATE}/postimage")
+        paimon_patch_error("Patched source or its verification record is 
missing")
+    endif()
+    file(READ "${PP_SOURCE}/.paimon-source-id" source_id)
+    if(NOT source_id STREQUAL PP_EXPECTED)
+        paimon_patch_error("Source identity does not match its build state")
+    endif()
+    file(SHA256 "${PP_STATE}/postimage" postimage_hash)
+    file(READ "${PP_STATE}/patched" patched)
+    if(NOT patched STREQUAL "${PP_EXPECTED}\n${postimage_hash}\n")
+        paimon_patch_error("Patch completion or source verification record is 
inconsistent"
+        )
+    endif()
+    file(STRINGS "${PP_STATE}/postimage" entries)
+    foreach(entry IN LISTS entries)
+        if(NOT entry MATCHES "^([^ ]+) (.+)$")
+            paimon_patch_error("Invalid source verification record")
+        endif()
+        set(expected "${CMAKE_MATCH_1}")
+        set(relative "${CMAKE_MATCH_2}")
+        set(path "${PP_SOURCE}/${relative}")
+        if(expected STREQUAL "absent")
+            if(EXISTS "${path}" OR IS_SYMLINK "${path}")
+                paimon_patch_error("Deleted patch target reappeared: ${path}")
+            endif()
+        else()
+            if(NOT EXISTS "${path}"
+               OR IS_DIRECTORY "${path}"
+               OR IS_SYMLINK "${path}")
+                paimon_patch_error("Patch target is missing or replaced: 
${path}")
+            endif()
+            file(SHA256 "${path}" actual)
+            if(NOT actual STREQUAL expected)
+                paimon_patch_error("Patched source was modified: ${path}")
+            endif()
+        endif()
+    endforeach()
+endfunction()
+
+function(paimon_patch_guard)
+    paimon_patch_check_paths()
+    paimon_patch_fingerprint(actual)
+    if(NOT actual STREQUAL PP_EXPECTED)
+        paimon_patch_error("Inputs changed since configuration; re-run CMake. 
Configured "
+                           "identity ${PP_EXPECTED}, current identity 
${actual}")
+    endif()
+    if(EXISTS "${PP_STATE}/identity")
+        file(READ "${PP_STATE}/identity" previous)
+        if(NOT previous STREQUAL PP_EXPECTED)
+            paimon_patch_error("Source version, patches/order, or build inputs 
changed. "
+                               "Previous identity ${previous}, requested 
identity ${PP_EXPECTED}"
+            )
+        endif()
+    else()
+        # Generated EP helper scripts and empty include directories are 
harmless;
+        # an old source, install, compilation cache or success stamp is not.
+        paimon_patch_assert_empty("${PP_SOURCE}")
+        paimon_patch_assert_empty("${PP_BINARY}")
+        paimon_patch_assert_empty("${PP_INSTALL}")
+        foreach(step
+                download
+                update
+                update_disconnected
+                patch
+                patch_disconnected
+                configure
+                build
+                install
+                done
+                paimon-verify)
+            if(EXISTS "${PP_STAMP}/${PP_NAME}-${step}")
+                paimon_patch_error("Legacy ExternalProject stamp has no 
verified identity: ${step}"
+                )
+            endif()
+        endforeach()
+        if(EXISTS "${PP_COMPLETE}")
+            paimon_patch_error("Legacy ExternalProject completion record has 
no verified identity"
+            )
+        endif()
+        paimon_patch_write(identity "${PP_EXPECTED}")
+    endif()
+    if(EXISTS "${PP_STATE}/applying")
+        paimon_patch_error("Patch application failed or was interrupted; the 
source may be partially patched"
+        )
+    endif()
+    if(EXISTS "${PP_STATE}/patched")
+        # Clean targets remove ExternalProject stamps but retain the source and
+        # our records. Stop before download can overwrite that patched source.
+        if(NOT EXISTS "${PP_STAMP}/${PP_NAME}-download")
+            paimon_patch_error("Download completion record is missing; 
refusing to overwrite patched source"
+            )
+        endif()
+        paimon_patch_check_postimage()
+        # Re-extraction of the same archive can preserve timestamps of its 
files.
+        # Never reuse the old binary cache after a repeated download/update 
step.
+        foreach(step download update update_disconnected)
+            if(EXISTS "${PP_STAMP}/${PP_NAME}-${step}"
+               AND "${PP_STAMP}/${PP_NAME}-${step}" IS_NEWER_THAN 
"${PP_STATE}/patched")
+                paimon_patch_error("Source download/update ran after patch 
completion")
+            endif()
+        endforeach()
+    elseif(EXISTS "${PP_STAMP}/${PP_NAME}-patch"
+           OR EXISTS "${PP_STAMP}/${PP_NAME}-patch_disconnected"
+           OR EXISTS "${PP_STATE}/installed")
+        paimon_patch_error("A build stamp or install exists without verified 
patch completion"
+        )
+    endif()
+    if(EXISTS "${PP_STATE}/installed")
+        file(READ "${PP_STATE}/installed" installed)
+        if(NOT installed STREQUAL PP_EXPECTED)
+            paimon_patch_error("Installation belongs to a different 
source/build identity"
+            )
+        endif()
+    elseif(EXISTS "${PP_COMPLETE}")
+        paimon_patch_error("ExternalProject completed without a verified 
installation")
+    endif()
+endfunction()
+
+function(paimon_patch_apply)
+    if(EXISTS "${PP_STATE}/patched")
+        return()
+    endif()
+    if(NOT EXISTS "${PP_STAMP}/${PP_NAME}-download")
+        paimon_patch_error("Cannot patch without a completed, 
checksum-verified download")
+    endif()
+    if(EXISTS "${PP_SOURCE}/.patched" OR EXISTS 
"${PP_SOURCE}/.paimon-vortex-patches")
+        paimon_patch_error("Legacy patch marker found in downloaded source")
+    endif()
+    # Reject unsupported patch paths before modifying anything. This mechanism
+    # handles textual -p1 diffs, including additions/deletions, not binary 
patches,
+    # renames, mode changes or paths through symlinks.
+    set(targets "")
+    foreach(patch IN LISTS PP_PATCHES)
+        file(READ "${patch}" contents)
+        if(contents MATCHES
+           "(^|\n)(GIT binary patch|rename from |rename to |old mode |new mode 
)")
+            paimon_patch_error("Unsupported non-text patch operation in 
${patch}")
+        endif()
+        execute_process(COMMAND "${PP_LSDIFF_EXECUTABLE}" -- "${patch}"
+                        RESULT_VARIABLE result
+                        OUTPUT_VARIABLE paths
+                        ERROR_VARIABLE error)
+        if(NOT result STREQUAL "0")
+            paimon_patch_error("Cannot list patch targets in ${patch}: 
${error}")
+        endif()
+        if(paths STREQUAL "")
+            paimon_patch_error("No text patch targets found in ${patch}")
+        endif()
+        # Only convert the filename output to a list, never the diff body.
+        # Reject filenames that cannot be represented by our verification 
record.
+        if(paths MATCHES "[;\\\t]|\\[|\\]")
+            paimon_patch_error("Unsafe patch path in ${patch}: ${paths}")
+        endif()
+        string(REGEX REPLACE "\n+$" "" paths "${paths}")
+        string(REPLACE "\n" ";" paths "${paths}")
+        foreach(path IN LISTS paths)
+            if(NOT path MATCHES "^[ab]/(.+)$")
+                paimon_patch_error("Unsupported patch path in ${patch}: 
${path}")
+            endif()
+            set(relative "${CMAKE_MATCH_1}")
+            if(relative MATCHES "(^|/)\\.\\.(/|$)" OR IS_ABSOLUTE 
"${relative}")
+                paimon_patch_error("Unsafe patch path in ${patch}: 
${relative}")
+            endif()
+            file(REAL_PATH "${PP_SOURCE}" source_real)
+            file(REAL_PATH "${PP_SOURCE}/${relative}" target_real)
+            cmake_path(IS_PREFIX
+                       source_real
+                       "${target_real}"
+                       NORMALIZE
+                       contained)
+            if(NOT contained OR IS_SYMLINK "${PP_SOURCE}/${relative}")
+                paimon_patch_error("Patch target escapes its source tree: 
${relative}")
+            endif()
+            list(APPEND targets "${relative}")
+        endforeach()
+    endforeach()
+    paimon_patch_write(applying "${PP_EXPECTED}")
+    foreach(patch IN LISTS PP_PATCHES)
+        # Check each patch against the result of its predecessors. A failure
+        # leaves 'applying' in place; never guess which hunks succeeded.
+        foreach(check --dry-run apply)
+            set(options
+                --batch
+                --forward
+                --fuzz=0
+                -p1
+                -i
+                "${patch}")
+            if(check STREQUAL "--dry-run")
+                list(APPEND options --dry-run)
+            endif()
+            execute_process(COMMAND "${PP_PATCH_EXECUTABLE}" ${options}
+                            WORKING_DIRECTORY "${PP_SOURCE}"
+                            RESULT_VARIABLE result
+                            OUTPUT_VARIABLE log
+                            ERROR_VARIABLE error)
+            if(NOT result EQUAL 0)
+                paimon_patch_error("Cannot apply ${patch} (${check}): 
${log}${error}")
+            endif()
+        endforeach()
+    endforeach()
+    list(REMOVE_DUPLICATES targets)
+    set(postimage "")
+    foreach(relative IN LISTS targets)
+        if(EXISTS "${PP_SOURCE}/${relative}")
+            file(SHA256 "${PP_SOURCE}/${relative}" hash)
+        else()
+            set(hash absent)
+        endif()
+        string(APPEND postimage "${hash} ${relative}\n")
+    endforeach()
+    paimon_patch_write(postimage "${postimage}")
+    file(WRITE "${PP_SOURCE}/.paimon-source-id" "${PP_EXPECTED}")
+    string(SHA256 postimage_hash "${postimage}")
+    paimon_patch_write(patched "${PP_EXPECTED}\n${postimage_hash}\n")
+    # A crash between these operations is conservatively treated as 
interrupted.
+    file(REMOVE "${PP_STATE}/applying")
+endfunction()
+
+if(CMAKE_SCRIPT_MODE_FILE STREQUAL CMAKE_CURRENT_LIST_FILE)
+    cmake_minimum_required(VERSION 3.22)
+    if(NOT PAIMON_PATCH_CONFIG)
+        message(FATAL_ERROR "PAIMON_PATCH_CONFIG is required")
+    endif()
+    include("${PAIMON_PATCH_CONFIG}")
+    file(LOCK "${PP_STATE}/lock"
+         GUARD PROCESS
+         TIMEOUT 0
+         RESULT_VARIABLE locked)
+    if(NOT locked STREQUAL "0")
+        message(FATAL_ERROR "${PP_NAME}: another process is validating or 
patching this source. Retry after it finishes."
+        )
+    endif()
+    paimon_patch_guard()
+    if(PAIMON_PATCH_MODE STREQUAL "apply")
+        paimon_patch_apply()
+    elseif(PAIMON_PATCH_MODE STREQUAL "complete" OR PAIMON_PATCH_MODE STREQUAL 
"consume")
+        if(NOT EXISTS "${PP_STATE}/patched")
+            paimon_patch_error("Cannot use outputs before patch completion")
+        endif()
+        if(PAIMON_PATCH_MODE STREQUAL "complete")
+            # This step runs only after ExternalProject's install command 
succeeds.
+            # Record that success for this identity, without sampling output 
files.
+            paimon_patch_write(installed "${PP_EXPECTED}")
+        elseif(NOT EXISTS "${PP_STATE}/installed")
+            paimon_patch_error("Build and installation have not completed")
+        endif()
+    elseif(PAIMON_PATCH_MODE)
+        message(FATAL_ERROR "Unknown patch mode: ${PAIMON_PATCH_MODE}")
+    endif()
+endif()
diff --git a/cmake_modules/BuildUtils.cmake b/cmake_modules/BuildUtils.cmake
index c0a9e14f..a7d207a6 100644
--- a/cmake_modules/BuildUtils.cmake
+++ b/cmake_modules/BuildUtils.cmake
@@ -198,7 +198,7 @@ function(add_paimon_lib LIB_NAME)
             target_link_options(${LIB_NAME}_shared PRIVATE 
${SHARED_LINK_OPTIONS})
         endif()
 
-        install(TARGETS ${LIB_NAME}_shared ${INSTALL_IS_OPTIONAL}
+        install(TARGETS ${LIB_NAME}_shared
                 EXPORT PaimonTargets
                 RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
                 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -245,7 +245,7 @@ function(add_paimon_lib LIB_NAME)
         target_link_libraries(${LIB_NAME}_static
                               PUBLIC 
"$<BUILD_INTERFACE:paimon_sanitizer_flags>")
 
-        install(TARGETS ${LIB_NAME}_static ${INSTALL_IS_OPTIONAL}
+        install(TARGETS ${LIB_NAME}_static
                 EXPORT PaimonTargets
                 RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
                 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
diff --git a/cmake_modules/ThirdpartyPatch.cmake 
b/cmake_modules/ThirdpartyPatch.cmake
new file mode 100644
index 00000000..01ff9454
--- /dev/null
+++ b/cmake_modules/ThirdpartyPatch.cmake
@@ -0,0 +1,225 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+include_guard(GLOBAL)
+include(ExternalProject)
+
+# Call before ExternalProject_Add, then register the project after creating it.
+function(paimon_patch_command name output)
+    set(${output}
+        "${CMAKE_COMMAND}"
+        "-DPAIMON_PATCH_MODE=apply"
+        
"-DPAIMON_PATCH_CONFIG=${CMAKE_CURRENT_BINARY_DIR}/paimon-thirdparty/${name}/request.cmake"
+        -P
+        "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/ApplyThirdpartyPatch.cmake"
+        PARENT_SCOPE)
+endfunction()
+
+# Read the previous generated request in a separate scope. Directory migrations
+# must report old caches as well as the newly configured paths on failure.
+function(paimon_patch_previous_cleanup config output)
+    set(PP_CLEAN "")
+    if(EXISTS "${config}")
+        include("${config}")
+    endif()
+    set(${output}
+        "${PP_CLEAN}"
+        PARENT_SCOPE)
+endfunction()
+
+# Only SHA256-verified archives in build-owned directories are supported. 
PATCHES
+# is ordered; an empty list still registers source/build identity validation.
+function(paimon_register_patched_project name)
+    cmake_parse_arguments(P
+                          ""
+                          "VERSION;INSTALL_DIR"
+                          "PATCHES;INPUTS;VERSION_VARIABLES"
+                          ${ARGN})
+    if(P_UNPARSED_ARGUMENTS
+       OR NOT P_VERSION
+       OR NOT P_INSTALL_DIR)
+        message(FATAL_ERROR "Invalid patch registration for ${name}")
+    endif()
+    if(CMAKE_CONFIGURATION_TYPES)
+        message(FATAL_ERROR "${name}: patch state validation requires a 
single-configuration generator"
+        )
+    endif()
+    externalproject_get_property(${name}
+                                 SOURCE_DIR
+                                 BINARY_DIR
+                                 STAMP_DIR
+                                 TMP_DIR)
+    get_property(archive_hash
+                 TARGET ${name}
+                 PROPERTY _EP_URL_HASH)
+    if(NOT archive_hash MATCHES "^SHA256=[0-9a-fA-F]+$")
+        message(FATAL_ERROR "${name}: patched dependencies require a 
SHA256-verified archive"
+        )
+    endif()
+    string(LENGTH "${archive_hash}" hash_length)
+    if(NOT hash_length EQUAL 71)
+        message(FATAL_ERROR "${name}: invalid archive SHA256")
+    endif()
+    get_property(urls
+                 TARGET ${name}
+                 PROPERTY _EP_URL)
+    set(PP_URLS "")
+    foreach(url IN LISTS urls)
+        # Match ExternalProject's local file URL handling and resolve relative
+        # paths now, before the guard runs from a different working directory.
+        string(REGEX REPLACE "^file://" "" url "${url}")
+        if(NOT url MATCHES "^[a-zA-Z][a-zA-Z0-9+.-]*://")
+            get_filename_component(url
+                                   "${url}"
+                                   ABSOLUTE
+                                   BASE_DIR
+                                   "${CMAKE_CURRENT_SOURCE_DIR}")
+        endif()
+        list(APPEND PP_URLS "${url}")
+    endforeach()
+
+    # Validate ownership even though this module never deletes directories.
+    # In particular, do not generate cleanup advice for a user checkout.
+    set(PP_ROOT "${CMAKE_BINARY_DIR}")
+    set(PP_NAME "${name}")
+    set(PP_SOURCE "${SOURCE_DIR}")
+    set(PP_BINARY "${BINARY_DIR}")
+    set(PP_STAMP "${STAMP_DIR}")
+    set(PP_TMP "${TMP_DIR}")
+    set(PP_INSTALL "${P_INSTALL_DIR}")
+    set(PP_STATE "${CMAKE_CURRENT_BINARY_DIR}/paimon-thirdparty/${name}")
+    set(PP_COMPLETE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${name}-complete")
+    paimon_patch_previous_cleanup("${PP_STATE}/request.cmake" previous_cleanup)
+    set(PP_CLEAN
+        
"${SOURCE_DIR};${BINARY_DIR};${STAMP_DIR};${TMP_DIR};${P_INSTALL_DIR};${PP_COMPLETE};${PP_STATE};${previous_cleanup}"
+    )
+    list(FILTER
+         PP_CLEAN
+         EXCLUDE
+         REGEX
+         "^$")
+    list(REMOVE_DUPLICATES PP_CLEAN)
+    file(REAL_PATH "${PP_ROOT}" root_real)
+    foreach(path IN LISTS PP_CLEAN)
+        file(REAL_PATH "${path}" path_real)
+        cmake_path(IS_PREFIX
+                   root_real
+                   "${path_real}"
+                   NORMALIZE
+                   owned)
+        if(NOT owned OR path_real STREQUAL root_real)
+            message(FATAL_ERROR "${name}: ${path} is not a build-owned 
directory. "
+                                "Local source overrides are not supported by 
the patch mechanism."
+            )
+        endif()
+    endforeach()
+
+    set(PP_VERSION "${P_VERSION}")
+    string(TOLOWER "${archive_hash}" PP_ARCHIVE)
+    set(PP_PATCHES "${P_PATCHES}")
+    set(PP_INPUTS "${P_INPUTS}")
+    # Resolve relative to this module, including when Paimon is a subproject.
+    get_filename_component(PP_VERSIONS_FILE
+                           
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../third_party/versions.txt"
+                           ABSOLUTE)
+    set(PP_VERSION_VARIABLES "${P_VERSION_VARIABLES}")
+    set(PP_CONTEXT
+        
"${CMAKE_GENERATOR}|${CMAKE_BUILD_TYPE}|${CMAKE_C_COMPILER}|${CMAKE_CXX_COMPILER}"
+    )
+    foreach(property CMAKE_ARGS CONFIGURE_COMMAND BUILD_COMMAND 
INSTALL_COMMAND)
+        get_property(value
+                     TARGET ${name}
+                     PROPERTY _EP_${property})
+        string(APPEND PP_CONTEXT "\n${property}=${value}")
+    endforeach()
+    set(PP_RUNNER 
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/ApplyThirdpartyPatch.cmake")
+    find_program(PP_PATCH_EXECUTABLE patch REQUIRED)
+    find_program(PP_LSDIFF_EXECUTABLE lsdiff)
+    if(NOT PP_LSDIFF_EXECUTABLE)
+        message(FATAL_ERROR "${name}: lsdiff is required to inspect dependency 
patches. Install patchutils and re-run CMake."
+        )
+    endif()
+
+    set_property(DIRECTORY
+                 APPEND
+                 PROPERTY CMAKE_CONFIGURE_DEPENDS
+                          ${PP_PATCHES}
+                          ${PP_INPUTS}
+                          ${PP_VERSIONS_FILE}
+                          "${PP_RUNNER}")
+    # Including the runner only defines helpers; script mode executes them.
+    include("${PP_RUNNER}")
+    paimon_patch_fingerprint(PP_EXPECTED)
+    file(MAKE_DIRECTORY "${PP_STATE}")
+    set(request "")
+    foreach(variable
+            ROOT
+            NAME
+            SOURCE
+            BINARY
+            STAMP
+            TMP
+            INSTALL
+            STATE
+            COMPLETE
+            CLEAN
+            VERSION
+            ARCHIVE
+            URLS
+            PATCHES
+            INPUTS
+            VERSIONS_FILE
+            VERSION_VARIABLES
+            CONTEXT
+            PATCH_EXECUTABLE
+            LSDIFF_EXECUTABLE
+            EXPECTED)
+        # Bracket arguments preserve spaces, semicolons, dollar signs and 
backslashes.
+        set(delimiter "=")
+        while("${PP_${variable}}" MATCHES "]${delimiter}]")
+            string(APPEND delimiter "=")
+        endwhile()
+        string(APPEND request
+               "set(PP_${variable} 
[${delimiter}[${PP_${variable}}]${delimiter}])\n")
+    endforeach()
+    # Do not update timestamps on ordinary reconfiguration.
+    set(config "${PP_STATE}/request.cmake")
+    file(WRITE "${config}.tmp" "${request}")
+    configure_file("${config}.tmp" "${config}" COPYONLY)
+    file(REMOVE "${config}.tmp")
+    set(command "${CMAKE_COMMAND}" "-DPAIMON_PATCH_CONFIG=${config}" -P 
"${PP_RUNNER}")
+    execute_process(COMMAND ${command} RESULT_VARIABLE result)
+    if(NOT result EQUAL 0)
+        message(FATAL_ERROR "${name}: third-party patch state validation 
failed")
+    endif()
+
+    add_custom_target(${name}-patch-guard
+                      COMMAND ${command}
+                      VERBATIM)
+    # A target dependency orders validation without making the download stamp
+    # perpetually out of date, unlike an ALWAYS step in the EP step chain.
+    externalproject_add_stepdependencies(${name} download ${name}-patch-guard)
+    externalproject_add_step(${name} paimon-verify
+                             COMMAND "${CMAKE_COMMAND}" 
"-DPAIMON_PATCH_MODE=complete"
+                                     "-DPAIMON_PATCH_CONFIG=${config}" -P 
"${PP_RUNNER}"
+                             DEPENDEES install) # codespell:ignore dependees
+    # cmake --install does not build first. Refuse to export stale artifacts.
+    install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" 
\"-DPAIMON_PATCH_MODE=consume\" \"-DPAIMON_PATCH_CONFIG=${config}\" -P 
\"${PP_RUNNER}\" RESULT_VARIABLE patch_result)
+if(NOT patch_result EQUAL 0)
+    message(FATAL_ERROR \"${name}: build the dependency successfully before 
installing Paimon\")
+endif()")
+endfunction()
diff --git a/cmake_modules/ThirdpartyToolchain.cmake 
b/cmake_modules/ThirdpartyToolchain.cmake
index dffebe65..4b78d0e7 100644
--- a/cmake_modules/ThirdpartyToolchain.cmake
+++ b/cmake_modules/ThirdpartyToolchain.cmake
@@ -15,6 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
+include(ThirdpartyPatch)
+
 set(THIRDPARTY_LOG_OPTIONS
     LOG_CONFIGURE
     1
@@ -43,6 +45,9 @@ endmacro()
 
 set(THIRDPARTY_DIR "${CMAKE_SOURCE_DIR}/third_party")
 # Read toolchain versions from third_party/versions.txt
+set_property(DIRECTORY
+             APPEND
+             PROPERTY CMAKE_CONFIGURE_DEPENDS "${THIRDPARTY_DIR}/versions.txt")
 file(STRINGS "${THIRDPARTY_DIR}/versions.txt" TOOLCHAIN_VERSIONS_TXT)
 foreach(_VERSION_ENTRY ${TOOLCHAIN_VERSIONS_TXT})
     # Exclude comments
@@ -946,18 +951,31 @@ macro(build_jieba)
         "-DCMAKE_INSTALL_PREFIX=${JIEBA_INSTALL}")
 
     set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/jieba.diff")
+    paimon_patch_command(jieba_ep PATCH_COMMAND)
     externalproject_add(jieba_ep
                         ${EP_COMMON_OPTIONS}
                         URL ${JIEBA_SOURCE_URL}
                         URL_HASH "SHA256=${PAIMON_JIEBA_BUILD_SHA256_CHECKSUM}"
                         CMAKE_ARGS ${JIEBA_CMAKE_ARGS}
                         LOG_PATCH ON
-                        PATCH_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> 
bash -c
-                                      "[ -f .patched ] && echo '<SOURCE_DIR> 
patch already applied, ignore...' || patch -s -N -p1 -i '${PATCH_FILE}' && 
touch .patched"
+                        PATCH_COMMAND ${PATCH_COMMAND}
                         INSTALL_COMMAND bash -c
                                         "cp -r 
${JIEBA_PREFIX}/src/jieba_ep/include/* ${JIEBA_INSTALL}/include/ && cp -r 
${JIEBA_PREFIX}/src/jieba_ep/dict/* ${JIEBA_INSTALL}/dict/ && cp -r 
${LIMONP_PREFIX}/src/limonp_ep/include/* ${JIEBA_INSTALL}/include/"
     )
 
+    paimon_register_patched_project(jieba_ep
+                                    VERSION
+                                    "${PAIMON_JIEBA_BUILD_VERSION}"
+                                    PATCHES
+                                    "${PATCH_FILE}"
+                                    INSTALL_DIR
+                                    "${JIEBA_INSTALL}"
+                                    VERSION_VARIABLES
+                                    PAIMON_JIEBA_BUILD_VERSION
+                                    PAIMON_JIEBA_BUILD_SHA256_CHECKSUM
+                                    PAIMON_LIMONP_BUILD_VERSION
+                                    PAIMON_LIMONP_BUILD_SHA256_CHECKSUM)
+
     # The include directory must exist before it is referenced by a target.
     include_directories(SYSTEM ${JIEBA_INCLUDE_DIR} ${JIEBA_DICT_DIR})
     add_library(jieba INTERFACE IMPORTED)
@@ -1397,7 +1415,8 @@ macro(build_mosaic)
     set(MOSAIC_DYNAMIC_LIB
         
"${MOSAIC_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}paimon_mosaic_ffi${CMAKE_SHARED_LIBRARY_SUFFIX}"
     )
-    set(MOSAIC_CARGO_TARGET_DIR "${CMAKE_CURRENT_BINARY_DIR}/mosaic_ep-cargo")
+    set(MOSAIC_CARGO_TARGET_DIR
+        "${CMAKE_CURRENT_BINARY_DIR}/mosaic_ep-prefix/src/mosaic_ep-build")
     set(MOSAIC_CARGO_DYNAMIC_LIB
         
"${MOSAIC_CARGO_TARGET_DIR}/release/${CMAKE_SHARED_LIBRARY_PREFIX}paimon_mosaic_ffi${CMAKE_SHARED_LIBRARY_SUFFIX}"
     )
@@ -1441,7 +1460,8 @@ macro(build_full_text)
     set(FULL_TEXT_DYNAMIC_LIB
         
"${FULL_TEXT_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}paimon_ftindex_ffi${CMAKE_SHARED_LIBRARY_SUFFIX}"
     )
-    set(FULL_TEXT_CARGO_TARGET_DIR 
"${CMAKE_CURRENT_BINARY_DIR}/full_text_ep-cargo")
+    set(FULL_TEXT_CARGO_TARGET_DIR
+        
"${CMAKE_CURRENT_BINARY_DIR}/full_text_ep-prefix/src/full_text_ep-build")
     set(FULL_TEXT_CARGO_DYNAMIC_LIB
         
"${FULL_TEXT_CARGO_TARGET_DIR}/release/${CMAKE_SHARED_LIBRARY_PREFIX}paimon_ftindex_ffi${CMAKE_SHARED_LIBRARY_SUFFIX}"
     )
@@ -1495,7 +1515,8 @@ macro(build_vortex)
     set(VORTEX_STATIC_LIB
         
"${VORTEX_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}vortex_ffi${CMAKE_STATIC_LIBRARY_SUFFIX}"
     )
-    set(VORTEX_CARGO_TARGET_DIR "${CMAKE_CURRENT_BINARY_DIR}/vortex_ep-cargo")
+    set(VORTEX_CARGO_TARGET_DIR
+        "${CMAKE_CURRENT_BINARY_DIR}/vortex_ep-prefix/src/vortex_ep-build")
     set(VORTEX_CARGO_STATIC_LIB
         
"${VORTEX_CARGO_TARGET_DIR}/release/${CMAKE_STATIC_LIBRARY_PREFIX}vortex_ffi${CMAKE_STATIC_LIBRARY_SUFFIX}"
     )
@@ -1504,42 +1525,29 @@ macro(build_vortex)
     # backed by host callbacks; it is maintained in this repository and 
injected into the Vortex
     # source tree. The patch declares the module.
     set(VORTEX_PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/vortex.diff")
+    paimon_patch_command(vortex_ep PATCH_COMMAND)
     set(VORTEX_CALLBACK_IO_SRC
         "${CMAKE_SOURCE_DIR}/crates/vortex_callback_io/callback_io.rs")
 
     file(MAKE_DIRECTORY "${VORTEX_INCLUDE_DIR}")
     file(MAKE_DIRECTORY "${VORTEX_LIB_DIR}")
 
-    # A local checkout (PAIMON_VORTEX_SOURCE_DIR) avoids re-downloading the 
full cargo workspace;
-    # otherwise download the pinned source archive (with SHA256 when 
versions.txt provides one).
-    if(DEFINED ENV{PAIMON_VORTEX_SOURCE_DIR})
-        set(VORTEX_EP_DOWNLOAD SOURCE_DIR "$ENV{PAIMON_VORTEX_SOURCE_DIR}"
-                               DOWNLOAD_COMMAND "")
-    elseif(PAIMON_VORTEX_BUILD_SHA256_CHECKSUM)
-        set(VORTEX_EP_DOWNLOAD URL ${VORTEX_SOURCE_URL} URL_HASH
-                               "SHA256=${PAIMON_VORTEX_BUILD_SHA256_CHECKSUM}")
-    else()
-        set(VORTEX_EP_DOWNLOAD URL ${VORTEX_SOURCE_URL})
-    endif()
-
     externalproject_add(vortex_ep
-                        ${VORTEX_EP_DOWNLOAD} ${THIRDPARTY_LOG_OPTIONS}
-                        BUILD_IN_SOURCE TRUE
+                        URL ${VORTEX_SOURCE_URL}
+                        URL_HASH 
"SHA256=${PAIMON_VORTEX_BUILD_SHA256_CHECKSUM}"
+                        ${THIRDPARTY_LOG_OPTIONS}
+                        BINARY_DIR "${VORTEX_CARGO_TARGET_DIR}"
                         CONFIGURE_COMMAND ""
                         LOG_PATCH ON
-                        PATCH_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> 
bash -c
-                                      "[ -f .patched ] && echo '<SOURCE_DIR> 
patch already applied, ignore...' || patch -s -N -p1 -i '${VORTEX_PATCH_FILE}' 
&& touch .patched"
-                        # Rebuild every time so edits to callback_io.rs are 
picked up; cargo
-                        # returns immediately when nothing changed, and 
copy_if_different keeps
-                        # the archive's timestamp stable so dependents do not 
relink.
-                        BUILD_ALWAYS TRUE
+                        PATCH_COMMAND ${PATCH_COMMAND}
+                        # The patch guard tracks callback_io.rs as a build 
input.
                         BUILD_COMMAND ${CMAKE_COMMAND} -E copy_if_different
                                       "${VORTEX_CALLBACK_IO_SRC}"
                                       
"<SOURCE_DIR>/vortex-ffi/src/callback_io.rs"
-                        COMMAND ${CMAKE_COMMAND} -E env "FLATC=${VORTEX_FLATC}"
-                                "RUSTUP_TOOLCHAIN=stable" 
"CC=${CMAKE_C_COMPILER}"
-                                "CXX=${CMAKE_CXX_COMPILER}"
-                                "CARGO_TARGET_DIR=${VORTEX_CARGO_TARGET_DIR}"
+                        COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> 
${CMAKE_COMMAND} -E
+                                env "FLATC=${VORTEX_FLATC}" 
"RUSTUP_TOOLCHAIN=stable"
+                                "CC=${CMAKE_C_COMPILER}" 
"CXX=${CMAKE_CXX_COMPILER}"
+                                "CARGO_TARGET_DIR=<BINARY_DIR>"
                                 "RUSTFLAGS=-Crelocation-model=pic"
                                 ${PAIMON_CARGO_EXECUTABLE} rustc --locked 
--package
                                 vortex-ffi --lib --crate-type=staticlib 
--release
@@ -1551,6 +1559,19 @@ macro(build_vortex)
                         INSTALL_COMMAND ""
                         BUILD_BYPRODUCTS "${VORTEX_STATIC_LIB}")
 
+    paimon_register_patched_project(vortex_ep
+                                    VERSION
+                                    "${PAIMON_VORTEX_BUILD_VERSION}"
+                                    PATCHES
+                                    "${VORTEX_PATCH_FILE}"
+                                    INPUTS
+                                    "${VORTEX_CALLBACK_IO_SRC}"
+                                    INSTALL_DIR
+                                    "${VORTEX_PREFIX}"
+                                    VERSION_VARIABLES
+                                    PAIMON_VORTEX_BUILD_VERSION
+                                    PAIMON_VORTEX_BUILD_SHA256_CHECKSUM)
+
     if(NOT TARGET Threads::Threads)
         set(THREADS_PREFER_PTHREAD_FLAG TRUE)
         find_package(Threads REQUIRED)
@@ -1686,16 +1707,27 @@ macro(build_oss_sdk_v2)
     # Avoid copying the metadata variant, which triggers GCC 12's
     # -Wmaybe-uninitialized warning in optimized builds.
     set(OSS_SDK_V2_PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/oss_sdk_v2.diff")
+    paimon_patch_command(oss_sdk_v2_ep PATCH_COMMAND)
     externalproject_add(oss_sdk_v2_ep
                         ${EP_COMMON_OPTIONS}
                         URL ${OSS_SDK_V2_SOURCE_URL}
                         URL_HASH 
"SHA256=${PAIMON_OSS_SDK_V2_BUILD_SHA256_CHECKSUM}"
                         LOG_PATCH ON
-                        PATCH_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> 
bash -c
-                                      "[ -f .patched ] && echo '<SOURCE_DIR> 
patch already applied, ignore...' || patch -s -N -p1 -i 
'${OSS_SDK_V2_PATCH_FILE}' && touch .patched"
+                        PATCH_COMMAND ${PATCH_COMMAND}
                         CMAKE_ARGS ${OSS_SDK_V2_CMAKE_ARGS} 
${THIRDPARTY_LOG_OPTIONS}
                         BUILD_BYPRODUCTS "${OSS_SDK_V2_STATIC_LIB}")
 
+    paimon_register_patched_project(oss_sdk_v2_ep
+                                    VERSION
+                                    "${PAIMON_OSS_SDK_V2_BUILD_VERSION}"
+                                    PATCHES
+                                    "${OSS_SDK_V2_PATCH_FILE}"
+                                    INSTALL_DIR
+                                    "${OSS_SDK_V2_PREFIX}"
+                                    VERSION_VARIABLES
+                                    PAIMON_OSS_SDK_V2_BUILD_VERSION
+                                    PAIMON_OSS_SDK_V2_BUILD_SHA256_CHECKSUM)
+
     file(MAKE_DIRECTORY "${OSS_SDK_V2_INCLUDE_DIR}")
     file(MAKE_DIRECTORY "${OSS_SDK_V2_LIB_DIR}")
 
@@ -1873,10 +1905,8 @@ macro(build_orc)
              "-DCMAKE_MODULE_LINKER_FLAGS=-Wl,-rpath=${ORC_RPATH}")
     endif()
 
-    set(ORC_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/orc_ep-prefix")
+    set(ORC_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/orc_ep-install")
     set(ORC_INCLUDE_DIR "${ORC_PREFIX}/include")
-    set(ORC_SOURCE_DIR "${ORC_PREFIX}/cpp")
-    set(ORC_BUILD_DIR "${CMAKE_BINARY_DIR}/build/orc")
 
     set(ORC_STATIC_LIB "${ORC_PREFIX}/lib/liborc.a")
 
@@ -1896,15 +1926,13 @@ macro(build_orc)
         -DBUILD_CPP_ENABLE_METRICS=ON)
 
     set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/orc.diff")
+    paimon_patch_command(orc_ep PATCH_COMMAND)
     externalproject_add(orc_ep
                         URL ${ORC_SOURCE_URL}
                         URL_HASH "SHA256=${PAIMON_ORC_BUILD_SHA256_CHECKSUM}"
-                        SOURCE_DIR ${ORC_SOURCE_DIR}
-                        BINARY_DIR ${ORC_BUILD_DIR}
                         CMAKE_ARGS ${ORC_CMAKE_ARGS}
                         LOG_PATCH ON
-                        PATCH_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> 
bash -c
-                                      "[ -f .patched ] && echo '<SOURCE_DIR> 
patch already applied, ignore...' || patch -s -N -p1 -i '${PATCH_FILE}' && 
touch .patched"
+                        PATCH_COMMAND ${PATCH_COMMAND}
                         UPDATE_DISCONNECTED 1
                         BUILD_BYPRODUCTS ${ORC_STATIC_LIB}
                         DEPENDS zstd
@@ -1913,6 +1941,27 @@ macro(build_orc)
                                 zlib
                                 libprotobuf)
 
+    paimon_register_patched_project(orc_ep
+                                    VERSION
+                                    "${PAIMON_ORC_BUILD_VERSION}"
+                                    PATCHES
+                                    "${PATCH_FILE}"
+                                    INSTALL_DIR
+                                    "${ORC_PREFIX}"
+                                    VERSION_VARIABLES
+                                    PAIMON_ORC_BUILD_VERSION
+                                    PAIMON_ORC_BUILD_SHA256_CHECKSUM
+                                    PAIMON_PROTOBUF_BUILD_VERSION
+                                    PAIMON_PROTOBUF_BUILD_SHA256_CHECKSUM
+                                    PAIMON_ZSTD_BUILD_VERSION
+                                    PAIMON_ZSTD_BUILD_SHA256_CHECKSUM
+                                    PAIMON_SNAPPY_BUILD_VERSION
+                                    PAIMON_SNAPPY_BUILD_SHA256_CHECKSUM
+                                    PAIMON_LZ4_BUILD_VERSION
+                                    PAIMON_LZ4_BUILD_SHA256_CHECKSUM
+                                    PAIMON_ZLIB_BUILD_VERSION
+                                    PAIMON_ZLIB_BUILD_SHA256_CHECKSUM)
+
     # The include directory must exist before it is referenced by a target.
     file(MAKE_DIRECTORY "${ORC_INCLUDE_DIR}")
 
@@ -2022,12 +2071,12 @@ macro(build_arrow)
 
     set(ARROW_CONFIGURE SOURCE_SUBDIR "cpp" CMAKE_ARGS ${ARROW_CMAKE_ARGS})
     set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/arrow.diff")
+    paimon_patch_command(arrow_ep PATCH_COMMAND)
     externalproject_add(arrow_ep
                         URL ${ARROW_SOURCE_URL}
                         URL_HASH "SHA256=${PAIMON_ARROW_BUILD_SHA256_CHECKSUM}"
                         LOG_PATCH ON
-                        PATCH_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR> 
bash -c
-                                      "[ -f .patched ] && echo '<SOURCE_DIR> 
patch already applied, ignore...' || patch -s -N -p1 -i '${PATCH_FILE}' && 
touch .patched"
+                        PATCH_COMMAND ${PATCH_COMMAND}
                         GIT_SUBMODULES "" GIT_SUBMODULES_RECURSE FALSE 
${ARROW_CONFIGURE}
                         UPDATE_DISCONNECTED 1
                         BUILD_BYPRODUCTS "${ARROW_STATIC_LIB}"
@@ -2041,6 +2090,27 @@ macro(build_arrow)
                                 zlib
                                 re2::re2)
 
+    paimon_register_patched_project(arrow_ep
+                                    VERSION
+                                    "${PAIMON_ARROW_BUILD_VERSION}"
+                                    PATCHES
+                                    "${PATCH_FILE}"
+                                    INSTALL_DIR
+                                    "${ARROW_PREFIX}"
+                                    VERSION_VARIABLES
+                                    PAIMON_ARROW_BUILD_VERSION
+                                    PAIMON_ARROW_BUILD_SHA256_CHECKSUM
+                                    PAIMON_ZSTD_BUILD_VERSION
+                                    PAIMON_ZSTD_BUILD_SHA256_CHECKSUM
+                                    PAIMON_SNAPPY_BUILD_VERSION
+                                    PAIMON_SNAPPY_BUILD_SHA256_CHECKSUM
+                                    PAIMON_LZ4_BUILD_VERSION
+                                    PAIMON_LZ4_BUILD_SHA256_CHECKSUM
+                                    PAIMON_ZLIB_BUILD_VERSION
+                                    PAIMON_ZLIB_BUILD_SHA256_CHECKSUM
+                                    PAIMON_RE2_BUILD_VERSION
+                                    PAIMON_RE2_BUILD_SHA256_CHECKSUM)
+
     add_library(arrow STATIC IMPORTED)
     set_target_properties(arrow
                           PROPERTIES IMPORTED_LOCATION 
"${ARROW_PREFIX}/lib/libarrow.a"
diff --git a/crates/vortex_callback_io/README.md 
b/crates/vortex_callback_io/README.md
index 2e92064b..9a07c2eb 100644
--- a/crates/vortex_callback_io/README.md
+++ b/crates/vortex_callback_io/README.md
@@ -55,9 +55,9 @@ build directory (`build-clang` below), the source directory 
and cargo cache are
 cmake --build build-clang --target vortex_ep
 
 # Run this module's Rust tests directly.
-cd "$(sed -n 's/^source_dir=//p' \
-  
build-clang/vortex_ep-prefix/src/vortex_ep-stamp/vortex_ep-source_dirinfo.txt)"
-FLATC=<path to flatc> CARGO_TARGET_DIR=<repo>/build-clang/vortex_ep-cargo \
+cd build-clang/vortex_ep-prefix/src/vortex_ep
+FLATC=<path to flatc> \
+  CARGO_TARGET_DIR=<repo>/build-clang/vortex_ep-prefix/src/vortex_ep-build \
   cargo test --locked --release -p vortex-ffi callback_io
 ```
 
diff --git a/docs/source/building.rst b/docs/source/building.rst
index 14973d13..0cdd8bc2 100644
--- a/docs/source/building.rst
+++ b/docs/source/building.rst
@@ -34,6 +34,7 @@ Building requires:
 
 * A C++17-enabled compiler. On Linux, gcc 8 and higher should be sufficient.
   Windows is not supported for now.
+* ``patch`` and ``lsdiff`` (provided by ``patchutils``) for patched 
dependencies.
 * At least 2GB of RAM for a minimal build, 8GB for a minimal
   debug build with tests and 16GB for a full build.
 
@@ -43,7 +44,9 @@ On Ubuntu/Debian you can install the requirements with:
 
    sudo apt-get install \
         build-essential \
-        cmake
+        cmake \
+        patch \
+        patchutils
 
 We also provide a docker template to help you get started quickly. See in
 ``.devcontainer`` folder for more details.
@@ -262,6 +265,20 @@ All other enabled dependencies must be available as system 
packages or under
 the specified prefix. When ORC support is disabled, the ``ORC_SOURCE`` override
 can be omitted.
 
+Some third-party dependencies require project-specific patches.
+Repeated builds reuse these dependencies when their versions, patches, and 
build
+inputs are unchanged.
+
+If an existing build cannot be safely reused after an update, cleanup, or 
interrupted
+patching, the build stops and lists the paths to clean. Remove all listed 
paths,
+then re-run CMake and build. No files are deleted automatically. Older build
+directories may also require this cleanup when first using the patch checks.
+
+Compilation or installation failures after successful patching can be retried
+without cleanup if the inputs are unchanged. Avoid manually modifying 
dependency
+sources or installed files in the build directory; clean and rebuild the 
affected
+dependency if its files are damaged.
+
 Package-manager-specific modes are intentionally out of scope for this first
 dependency source interface. They can still be used through standard CMake
 mechanisms such as ``CMAKE_PREFIX_PATH`` or ``CMAKE_TOOLCHAIN_FILE``, while

Reply via email to