Skip to content

Commit

Permalink
src: build: Add --from-sha <SHA> option
Browse files Browse the repository at this point in the history
Add --from-sha <SHA> option that builds every commit from <SHA> to actual commit

Signed-off-by: Marcelo Mendes Spessoto Junior <[email protected]>
  • Loading branch information
MarceloSpessoto committed Apr 26, 2024
1 parent 9e40555 commit 784b6b3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
5 changes: 5 additions & 0 deletions documentation/man/features/kw-build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SYNOPSIS
| *kw* (*b* | *build*) [(-c | \--clean)] [\--alert=(s | v | (sv | vs) | n)]
| *kw* (*b* | *build*) [(-f | \--full-cleanup)] [\--alert=(s | v | (sv | vs) | n)]
| *kw* (*b* | *build*) [\--cflags]
| *kw* (*b* | *build*) [\--from-sha <SHA>]
| *kw* (*b* | *build*) [\--verbose]
DESCRIPTION
Expand Down Expand Up @@ -102,6 +103,10 @@ OPTIONS
| **sv** or **vs** enables both.
| **n** (or any other option) disables notifications (this is the default).
\--from-sha:
Build every commit from <SHA> to branch head. Useful for testing if all patches in
patchset compiles.

EXAMPLES
========
For these examples, we suppose the fields in your **kworkflow.config** file are
Expand Down
9 changes: 9 additions & 0 deletions documentation/tutorials/buildlinux.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ Well, that's it. kw will automatically infer the number of job slots for
compiling based on the number of cores of your machine (i.e. when running make
``-j<number>``, *<number>* is an integer that specifies the number of processes
that will run at the same time), and compilation will begin!

Compiling patchsets
-------------------
You may want to try compiling every patch in your patchset to test if everything is alright.
You can do this by using the "from-sha" flag::

kw build --from-sha SHA

This will compile every path from the commit with given SHA to the branch HEAD.
2 changes: 1 addition & 1 deletion src/bash_autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function _kw_autocomplete()
kw_options['init']='--arch --remote --target --force --template --verbose'

kw_options['build']='--help --info --menu --cpu-scaling --ccache --llvm --clean
--full-cleanup --verbose --doc --warnings --save-log-to --cflags'
--full-cleanup --verbose --doc --warnings --save-log-to --cflags --from-sha'

kw_options['b']="${kw_options['build']}"

Expand Down
66 changes: 64 additions & 2 deletions src/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function build_kernel_main()
local clean
local output_kbuild_flag=''
local cflags
local from_sha_arg

parse_build_options "$@"

Expand All @@ -59,6 +60,7 @@ function build_kernel_main()
clean=${options_values['CLEAN']}
full_cleanup=${options_values['FULL_CLEANUP']}
cflags=${options_values['CFLAGS']}
from_sha_arg=${options_values['FROM_SHA_ARG']}

[[ -n "${options_values['VERBOSE']}" ]] && flag='VERBOSE'
flag=${flag:-'SILENT'}
Expand Down Expand Up @@ -124,6 +126,35 @@ function build_kernel_main()
return "$?"
fi

if [[ -n "$from_sha_arg" ]]; then
# Check if there is a rebase in process.
printf '%s\n' "Verifying if there is a rebase in process..."
if [[ -e .git/rebase-merge/done ]]; then
printf '%s\n' "ERROR: Abort the repository rebase before continuing with build from sha (use 'git rebase --abort')!"
return 22
fi

# Check if given SHA represents real commit
eval "git cat-file -e ${from_sha_arg}^{commit} 2> /dev/null"
if [[ "$?" != 0 ]]; then
printf '%s\n' "ERROR: The given SHA doesn't represent a real commit in this repository."
return 22
fi

# Check if given SHA is in working tree.
local sha_base
local merge_base
sha_base=$(git rev-parse --verify "$from_sha_arg")
merge_base=$(git merge-base "$from_sha_arg" HEAD)
if [[ "$sha_base" != "$merge_base" ]]; then
printf '%s\n' "ERROR: Given SHA is invalid. Check if it is on the working tree."
return 22
fi

build_from_sha "$flag" "$from_sha_arg"
return "$?"
fi

command="make ${optimizations} ${llvm}ARCH=${platform_ops}${warnings}"

if [[ -n "$cflags" ]]; then
Expand Down Expand Up @@ -247,9 +278,34 @@ function full_cleanup()
cmd_manager "$flag" "$cmd"
}

# This functions uses iteractive 'git rebase' with '--exec' flag under the hood
# to apply a 'kw build' over each commit from SHA to branch head.
#
# @flag How to display a command, see `src/lib/kwlib.sh` function `cmd_manager`.
# @sha The SHA from the first commit to be compiled until the branch head.
#
# Return:
# 0 if successfully compiled patchset, 1 otherwise.
function build_from_sha()
{
local flag="$1"
local sha="$2"
local cmd

flag=${flag:-'SILENT'}
cmd="git rebase ${sha} --exec 'kw build'"
cmd_manager "$flag" "$cmd"

if [[ "$?" != 0 ]]; then
echo "kw build failed during the compilation of a patch! Aborting rebase..."
git rebase --abort
return 1
fi
}

function parse_build_options()
{
local long_options='help,info,menu,doc,ccache,cpu-scaling:,warnings::,save-log-to:,llvm,clean,full-cleanup,verbose,cflags:'
local long_options='help,info,menu,doc,ccache,cpu-scaling:,warnings::,save-log-to:,llvm,clean,full-cleanup,verbose,cflags:,from-sha:'
local short_options='h,i,n,d,S:,w::,s:,c,f'
local doc_type
local file_name_size
Expand Down Expand Up @@ -279,6 +335,7 @@ function parse_build_options()
options_values['FULL_CLEANUP']=''
options_values['VERBOSE']=''
options_values['CFLAGS']="${build_config[cflags]}"
options_values['FROM_SHA_ARG']=''

# Check llvm option
if [[ ${options_values['USE_LLVM_TOOLCHAIN']} =~ 'yes' ]]; then
Expand Down Expand Up @@ -362,6 +419,10 @@ function parse_build_options()
options_values['LOG_PATH']="$2"
shift 2
;;
--from-sha)
options_values['FROM_SHA_ARG']="$2"
shift 2
;;
--)
shift
;;
Expand Down Expand Up @@ -394,7 +455,8 @@ function build_help()
' build (-c | --clean) - Clean option integrated into env' \
' build (-f | --full-cleanup) - Reset the kernel tree to its default option integrated into env' \
' build (--cflags) - Customize kernel compilation with specific flags' \
' build (--verbose) - Show a detailed output'
' build (--verbose) - Show a detailed output' \
' build (--from-sha <SHA>) - Build all commits from <SHA> to actual commit'
}

# Every time build.sh is loaded its proper configuration has to be loaded as well
Expand Down

0 comments on commit 784b6b3

Please sign in to comment.