| 1 | # Copyright 2026 LiveKit, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # Sourced by the build scripts. Do not run directly. |
| 16 | |
| 17 | # Usage: apply_patch <patch> [dir] |
| 18 | # |
| 19 | # Applies <patch> to the git checkout at [dir] (default: current directory). |
| 20 | # A patch that is already applied is skipped, so a build can be rerun on the |
| 21 | # same checkout. A patch that neither applies nor is already applied stops the |
| 22 | # build. Carrying on would produce a libwebrtc that silently lacks the change. |
| 23 | apply_patch() { |
| 24 | local patch="$1" |
| 25 | local dir="${2:-.}" |
| 26 | local name |
| 27 | name="$(basename "$patch")" |
| 28 | local flags=(--ignore-space-change --ignore-whitespace --whitespace=nowarn) |
| 29 | |
| 30 | if git -C "$dir" apply --reverse --check "${flags[@]}" "$patch" 2>/dev/null; then |
| 31 | echo "Patch already applied: $name" |
| 32 | return 0 |
| 33 | fi |
| 34 | |
| 35 | echo "Applying patch: $name" |
| 36 | # git apply is all or nothing, so a failure leaves the checkout untouched. |
| 37 | if ! git -C "$dir" apply -v "${flags[@]}" "$patch"; then |
| 38 | echo "Error: $name does not apply to $(cd "$dir" && pwd)." >&2 |
| 39 | echo "Regenerate the patch against the pinned WebRTC revision, or reset a" >&2 |
| 40 | echo "partially patched checkout (git checkout -- .) and rerun." >&2 |
| 41 | exit 1 |
| 42 | fi |
| 43 | } |