Skip to content

Commit f572998

Browse files
authored
Merge pull request #1953 from tlaurion/patch_revert_if_apply_fails-canary_wipe_rebuild_improvements
Revert previous applied patches in reverse order if applying patches fails and reapply patches + canary wipe dev helper improvements
2 parents 1e74f45 + 46e6efe commit f572998

File tree

2 files changed

+106
-48
lines changed

2 files changed

+106
-48
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
- run:
6969
name: Creating coreboot (and associated patches) and musl-cross-make modules digest (musl-cross-make and coreboot cache digest)
7070
command: |
71-
find .circleci/config.yml ./Makefile ./flake.lock ./modules/coreboot ./modules/musl-cross-make* ./patches/coreboot* -type f | sort -h | xargs sha256sum > ./tmpDir/coreboot_musl-cross-make.sha256sums
71+
find .circleci/config.yml ./flake.lock ./modules/coreboot ./modules/musl-cross-make* ./patches/coreboot* -type f | sort -h | xargs sha256sum > ./tmpDir/coreboot_musl-cross-make.sha256sums
7272
- run:
7373
name: Creating musl-cross-make and musl-cross-make patches digest (musl-cross-make cache digest)
7474
command: |

Makefile

Lines changed: 105 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -400,47 +400,51 @@ define define_module =
400400
# module-specific cleanup action to get rid of it.
401401
$(build)/$($1_base_dir)/.canary: FORCE
402402
if [ ! -e "$$@" ]; then \
403-
echo "INFO: .canary file not found. Cloning repository $($1_repo) into $(build)/$($1_base_dir)"; \
404-
git clone $($1_repo) "$(build)/$($1_base_dir)"; \
405-
echo "INFO: Resetting repository to commit $($1_commit_hash)"; \
406-
git -C "$(build)/$($1_base_dir)" reset --hard $($1_commit_hash); \
407-
echo "INFO: Creating .canary file with repo and commit hash"; \
408-
echo -n '$($1_repo)|$($1_commit_hash)' > "$$@"; \
403+
echo "INFO: .canary file not found. Cloning repository $($1_repo) into $(build)/$($1_base_dir)" && \
404+
git clone $($1_repo) "$(build)/$($1_base_dir)" && \
405+
echo "INFO: Resetting repository to commit $($1_commit_hash)" && \
406+
git -C "$(build)/$($1_base_dir)" reset --hard $($1_commit_hash) && \
407+
echo "INFO: Creating .canary file with repo and commit hash" && \
408+
echo -n '$($1_repo)|$($1_commit_hash)' > "$$@" ; \
409409
elif [ "$$$$(cat "$$@")" != '$($1_repo)|$($1_commit_hash)' ]; then \
410-
echo "INFO: Canary file differs. Switching $1 to $($1_repo) at $($1_commit_hash)"; \
410+
echo "INFO: Canary file differs. Switching $1 to $($1_repo) at $($1_commit_hash)" && \
411411
git -C "$(build)/$($1_base_dir)" reset --hard HEAD^ && \
412-
echo "INFO: Fetching commit $($1_commit_hash) from $($1_repo) (without recursing submodules)"; \
412+
echo "INFO: Fetching commit $($1_commit_hash) from $($1_repo) (without recursing submodules)" && \
413413
git -C "$(build)/$($1_base_dir)" fetch $($1_repo) $($1_commit_hash) --recurse-submodules=no && \
414-
echo "INFO: Resetting repository to commit $($1_commit_hash)"; \
415-
git -C "$(build)/$($1_base_dir)" reset --hard $($1_commit_hash); \
416-
echo "INFO: Cleaning repository directory (including payloads and util/cbmem)"; \
414+
echo "INFO: Resetting repository to commit $($1_commit_hash)" && \
415+
git -C "$(build)/$($1_base_dir)" reset --hard $($1_commit_hash) && \
416+
echo "INFO: Cleaning repository directory (including payloads and util/cbmem)" && \
417417
git -C "$(build)/$($1_base_dir)" clean -df && \
418418
git -C "$(build)/$($1_base_dir)" clean -dffx payloads util/cbmem && \
419-
echo "INFO: Synchronizing submodules"; \
419+
echo "INFO: Synchronizing submodules" && \
420420
git -C "$(build)/$($1_base_dir)" submodule sync && \
421-
echo "INFO: Updating submodules (init and checkout)"; \
421+
echo "INFO: Updating submodules (init and checkout)" && \
422422
git -C "$(build)/$($1_base_dir)" submodule update --init --checkout && \
423-
echo "INFO: Updating .canary file with new repo info"; \
424-
echo -n '$($1_repo)|$($1_commit_hash)' > "$$@"; \
423+
echo "INFO: Updating .canary file with new repo info" && \
424+
echo -n '$($1_repo)|$($1_commit_hash)' > "$$@" ; \
425425
fi
426426
if [ ! -e "$(build)/$($1_base_dir)/.patched" ]; then \
427-
echo "INFO: .patched file not found. Beginning patch application for $1"; \
427+
echo "INFO: .patched file not found. Beginning patch application for $1" && \
428428
if [ -r patches/$($1_patch_name).patch ]; then \
429-
echo "INFO: Found patch file patches/$($1_patch_name).patch. Applying patch..."; \
430-
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) ) \
431-
< patches/$($1_patch_name).patch \
432-
|| exit 1 ; \
433-
fi && \
434-
if [ -d patches/$($1_patch_name) ] && \
435-
[ -r patches/$($1_patch_name) ] ; then \
436-
for patch in patches/$($1_patch_name)/*.patch ; do \
437-
echo "Applying patch file : $$$$patch " ; \
438-
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) ) \
439-
< $$$$patch \
440-
|| exit 1 ; \
441-
done ; \
442-
fi && \
443-
echo "INFO: Patches applied successfully. Creating .patched file"; \
429+
echo "INFO: Applying single patch file: patches/$($1_patch_name).patch" && \
430+
if ! git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch; then \
431+
echo "ERROR: Failed to apply patch: patches/$($1_patch_name).patch. Reversing and reapplying." && \
432+
git apply --reverse --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch || true && \
433+
git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch || exit 1; \
434+
fi; \
435+
fi; \
436+
if [ -d patches/$($1_patch_name) ]; then \
437+
echo "INFO: Applying multiple patch files from directory: patches/$($1_patch_name)" && \
438+
for patch in patches/$($1_patch_name)/*.patch; do \
439+
echo "INFO: Applying patch file: $$$$patch" && \
440+
if ! git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < "$$$$patch"; then \
441+
echo "ERROR: Failed to apply patch: $$$$patch. Reversing and reapplying." && \
442+
git apply --reverse --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < "$$$$patch" || true && \
443+
git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < "$$$$patch" || exit 1; \
444+
fi; \
445+
done; \
446+
fi; \
447+
echo "INFO: Patches applied successfully. Creating .patched file." && \
444448
touch "$(build)/$($1_base_dir)/.patched"; \
445449
fi
446450
else
@@ -465,20 +469,45 @@ define define_module =
465469
mkdir -p "$$(dir $$@)"
466470
tar -xf "$(packages)/$($1_tar)" $(or $($1_tar_opt),--strip 1) -C "$$(dir $$@)"
467471
if [ -r patches/$($1_patch_name).patch ]; then \
468-
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) ) \
469-
< patches/$($1_patch_name).patch \
470-
|| exit 1 ; \
471-
fi
472-
if [ -d patches/$($1_patch_name) ] && \
473-
[ -r patches/$($1_patch_name) ] ; then \
474-
for patch in patches/$($1_patch_name)/*.patch ; do \
475-
echo "Applying patch file : $$$$patch " ; \
476-
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) ) \
477-
< $$$$patch \
478-
|| exit 1 ; \
472+
echo "INFO: Applying single patch file: patches/$($1_patch_name).patch" && \
473+
if ! ( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch ); then \
474+
echo "ERROR: Failed to apply patch: patches/$($1_patch_name).patch. Reversing and reapplying." && \
475+
( git apply --reverse --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch ) || true && \
476+
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch ) || exit 1 ; \
477+
fi ; \
478+
fi ; \
479+
if [ -d patches/$($1_patch_name) ]; then \
480+
echo "INFO: Applying multiple patch files from directory: patches/$($1_patch_name)" && \
481+
for patch in patches/$($1_patch_name)/*.patch; do \
482+
echo "INFO: Applying patch file: $$$$patch" && \
483+
if ! ( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < $$$$patch ); then \
484+
echo "ERROR: Failed to apply patch: $$$$patch. Reversing and reapplying." && \
485+
( git apply --reverse --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < $$$$patch ) || true && \
486+
echo "INFO: Reapplying patch file: $$$$patch" && \
487+
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < $$$$patch ) || exit 1 ; \
488+
fi ; \
479489
done ; \
480490
fi
481-
@touch "$$@"
491+
if [ ! -e "$$@" ] && [ -e "$(build)/$($1_base_dir)/.patched" ]; then \
492+
echo "INFO: .canary file not found but .patched exists. Reversing and reapplying patches for $1" && \
493+
if [ -r patches/$($1_patch_name).patch ]; then \
494+
echo "INFO: Reversing single patch file: patches/$($1_patch_name).patch." && \
495+
( git apply --reverse --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch ) || true && \
496+
echo "INFO: Reapplying single patch file: patches/$($1_patch_name).patch." && \
497+
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < patches/$($1_patch_name).patch ) || exit 1 ; \
498+
fi ; \
499+
if [ -d patches/$($1_patch_name) ]; then \
500+
echo "INFO: Reversing and reapplying multiple patch files from directory: patches/$($1_patch_name)" && \
501+
for patch in patches/$($1_patch_name)/*.patch; do \
502+
echo "INFO: Reversing patch file: $$$$patch" && \
503+
( git apply --reverse --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < $$$$patch ) || true && \
504+
echo "INFO: Reapplying patch file: $$$$patch" && \
505+
( git apply --verbose --reject --binary --directory build/$(CONFIG_TARGET_ARCH)/$($1_base_dir) < $$$$patch ) || exit 1 ; \
506+
done ; \
507+
fi ; \
508+
rm -f "$(build)/$($1_base_dir)/.patched" ; \
509+
fi
510+
touch "$$@"
482511
endif
483512

484513
# Allow the module to override the destination configuration file
@@ -894,26 +923,47 @@ $(board_build)/$(CB_OUTPUT_BASENAME)-gpg-injected.rom: $(board_build)/$(CB_OUTPU
894923
"$(board_build)/$(CB_OUTPUT_FILE_GPG_INJ)" "$(PUBKEY_ASC)"
895924

896925

926+
897927
#Dev cycles helpers:
928+
929+
# Helper function to overwrite coreboot git repo's .canary file with a bogus commit (.canary checked for matching commit on build)
930+
# TODO: Implement a cleaner solution to ensure files created by patches are properly deleted instead of requiring manual intervention.
931+
define overwrite_canary_if_coreboot_git
932+
@echo "Checking for coreboot directory: build/${CONFIG_TARGET_ARCH}/coreboot-$(CONFIG_COREBOOT_VERSION)"
933+
if [ -d "build/${CONFIG_TARGET_ARCH}/coreboot-$(CONFIG_COREBOOT_VERSION)" ] && \
934+
[ -d "build/${CONFIG_TARGET_ARCH}/coreboot-$(CONFIG_COREBOOT_VERSION)/.git" ]; then \
935+
echo "INFO: Recreating .canary file for 'build/${CONFIG_TARGET_ARCH}/coreboot-$(CONFIG_COREBOOT_VERSION)' with placeholder." && \
936+
echo BOGUS_COMMIT_ID > "build/${CONFIG_TARGET_ARCH}/coreboot-$(CONFIG_COREBOOT_VERSION)/.canary" && \
937+
echo "INFO: .canary file has been recreated." ; \
938+
else \
939+
echo "INFO: Coreboot directory or .git not found, skipping .canary overwrite." ; \
940+
fi
941+
endef
942+
898943
real.clean:
944+
@echo "Cleaning build artifacts and install directories, leaving crossgcc intact."
899945
for dir in \
900946
$(module_dirs) \
901947
$(kernel_headers) \
902948
; do \
903949
if [ ! -z "$$dir" ]; then \
904-
rm -rf "build/${CONFIG_TARGET_ARCH}/$$dir"; \
905-
fi; \
950+
rm -rf "build/${CONFIG_TARGET_ARCH}/$$dir" ; \
951+
fi ; \
906952
done
907953
cd install && rm -rf -- *
954+
$(call overwrite_canary_if_coreboot_git)
955+
908956
real.gitclean:
909957
@echo "Cleaning the repository using Git ignore file as a base..."
910958
@echo "This will wipe everything not in the Git tree, but keep downloaded coreboot forks (detected as Git repos)."
911959
git clean -fxd
960+
$(call overwrite_canary_if_coreboot_git)
912961

913962
real.gitclean_keep_packages:
914963
@echo "Cleaning the repository using Git ignore file as a base..."
915964
@echo "This will wipe everything not in the Git tree, but keep the 'packages' directory."
916965
git clean -fxd -e "packages"
966+
$(call overwrite_canary_if_coreboot_git)
917967

918968
real.remove_canary_files-extract_patch_rebuild_what_changed:
919969
@echo "Removing 'canary' files to force Heads to restart building board configurations..."
@@ -924,10 +974,18 @@ real.remove_canary_files-extract_patch_rebuild_what_changed:
924974
@echo "This approach economizes time since most build artifacts do not need to be rebuilt, as the file dates should be the same as when you originally built them."
925975
@echo "Only a minimal time is needed for rebuilding, which is also good for your SSD."
926976
@echo "*** USE THIS APPROACH FIRST ***"
927-
find ./build/ -type f -name ".canary" -print -delete
928-
find ./install/*/* -print -exec rm -rf {} +
977+
@echo "Removing ./build .canary files..."
978+
@find ./build/ -type f -name ".canary" -print -delete || true
979+
@echo "Removing install/*/* content..."
980+
@find ./install/*/* -print -exec rm -rf {} + 2>/dev/null || true
981+
@echo "Removing coreboot board related artifact directory: $(build)/$(coreboot_dir)"
982+
rm -rf "$(build)/$(coreboot_dir)"
983+
@echo "Removing coreboot board related artifacts directory $(board_build)"
984+
rm -rf "$(board_build)"
985+
$(call overwrite_canary_if_coreboot_git)
929986

930987
real.gitclean_keep_packages_and_build:
931988
@echo "Cleaning the repository using Git ignore file as a base..."
932989
@echo "This will wipe everything not in the Git tree, but keep the 'packages' and 'build' directories."
933990
git clean -fxd -e "packages" -e "build"
991+
$(call overwrite_canary_if_coreboot_git)

0 commit comments

Comments
 (0)