Skip to content

Instantly share code, notes, and snippets.

@bojidar-bg
Last active November 17, 2016 15:27
Show Gist options
  • Save bojidar-bg/dd177bfde1e0d15af3440a3f2c145fa7 to your computer and use it in GitHub Desktop.
Save bojidar-bg/dd177bfde1e0d15af3440a3f2c145fa7 to your computer and use it in GitHub Desktop.
Godot-buildscripts -- a few buildscripts useful for developing godot
#!/bin/bash
## Helps build/debug/test a Godot (usable for non-godot things as well)
##################
#-----Common-----#
##################
function start {
start=1$(date -u +"%s")
#echo -en "\e[3A"
}
function run {
print_fake_prompt "\e[7m$*\e[0m"
if [ -z $dry_run ]; then
eval "$@";
if [ $? -ne 0 ]; then
exit $?;
fi
fi
}
function finish {
end=1$(date -u +"%s")
seconds=$(($end - $start))
print_fake_prompt "Finished in $(($seconds/60)) minutes, $(($seconds % 60)) seconds\n";
}
function print_fake_prompt {
echo -e "\n\n\n[\e[32m$(date +%H:%M:%S.%3N)\e[39m] \e[33m$(whoami)@$(hostname)\e[35m$(pwd) \e[91m\$ \e[39m$1"
}
function ask {
echo -en "$1 (timeouts in 3 seconds)? [Y/n/d] "
read -t 3 answer
if [ -z $answer ] || [[ $answer =~ ^[Dd](efault)?$ ]]; then
[ -n $answer ] || echo ""
answer=$2
fi
[[ $answer =~ ^([Yy](es)?|[Tt](rue)?)$ ]]
}
##################
#----Arguments---#
##################
_arguments=()
_arguments_long=()
_argument_variables=()
_argument_descriptions=()
_additional_argument_names=()
additional_arguments=()
function register_argument {
_arguments+=("$1")
_arguments_long+=("$2")
_argument_variables+=("$3")
_argument_descriptions+=("$4")
}
function register_additional_argument {
_additional_argument_names+=("$1")
}
function parse_arguments {
local _reached_additional
for argument in $@; do
if [ -z $_reached_additional ]; then
if [ "$argument" = "--" ]; then
local _reached_additional=1
else
local _matched=""
for i in ${!_arguments[@]}; do # http://stackoverflow.com/a/37073155/4168713
local _argument_stripped=${argument#${_arguments[$i]}}
local _argument_long_stripped=${argument#${_arguments_long[$i]}}
if [ "$_argument_long_stripped" ]; then # If it is empty, then it is boolan, otherwise, parse the =
local _argument_long_stripped=${argument#${_arguments_long[$i]}=}
fi
if [ "$_argument_stripped" != "$argument" ]; then
local _matched=${_argument_stripped:-1}
break
elif [ "$_argument_long_stripped" != "$argument" ]; then
local _matched=${_argument_long_stripped:-1}
break
fi
done
if [ "$_matched" ]; then
readonly "${_argument_variables[$i]}=$_matched"
else
additional_arguments+=("$argument")
fi
fi
else
additional_arguments+=("$argument")
fi
done
if [ "$show_help" ]; then
show_help
exit
fi
}
function show_help {
local additionals="[--]"
for name in ${_additional_argument_names[@]}; do
additionals+=" [$name]"
done
printf 'Usage:\n\t%s [options] %s\n' "$(basename "$0")" "$additionals"
local arguments=()
for i in ${!_arguments[@]}; do
arguments+=("${_arguments[$i]}" "${_arguments_long[$i]}" "${_argument_descriptions[$i]}")
done
printf 'Available arguments:\n'
printf '\t\e[36m%-8s%-20s\e[0m %s\n' "${arguments[@]}"
}
register_argument -h --help show_help "Display help and exit"
register_argument -z --dry-run dry_run "Do not invoke actual commands"
##################
#-Build-Sections-#
##################
_section_commands=()
_section_defaults=()
_section_selection=()
_section_names=()
function register_section {
_section_names+=("$1")
_section_defaults+=("$2")
_section_commands+=("$3")
}
function select_sections {
for i in ${!_section_commands[@]}; do
if ask "Do you want to ${_section_names[$i]}" ${_section_defaults[$i]}; then
_section_selection+=("$i")
fi
done;
}
function run_sections {
for i in ${_section_selection[@]}; do
print_fake_prompt "Running section '${_section_names[$i]}'..."
eval ${_section_commands[$i]}
done;
}
#!/bin/bash
## Builds a Godot, works with worktrees
. build-helpers.sh
register_argument -r --release release "Build only release"
register_argument -d --debug debug "Build only debug"
register_additional_argument build-dir
parse_arguments "$@"
register_section "pull from git" True pull_git;
function pull_git {
run git pull origin master;
}
register_section "build for linux" True build_linux;
function build_linux {
if [ -z "$release" ] && [ -z "$debug" ]; then
release=1
debug=1
fi
if [ "$release" ]; then
run scons platform=x11 target=debug tools=yes -j $(nproc);
fi
if [ "$debug" ]; then
run scons platform=x11 target=release_debug tools=yes -j $(nproc);
fi
}
cdir=$(pwd)
build_dir=${additional_arguments[0]}
if [ -z build_dir ]; then
if [ -f SConstruct ]; then
build_dir=.
else
build_dir=godot
fi
fi
if ! cd "$build_dir" && [ -f SConstruct ]; then
echo "Bad build-dir argument"
exit 1
fi
select_sections
start
run_sections
run cp ./bin/godot.x11.opt.tools.64 ../bin/godot
run cp ./bin/godot.x11.tools.64 ../bin/godot.dbg
finish
#!/bin/bash
## Debugs (builds + runs) a PR-ed Godot, requires worktree support
## It is advisable to remove line 63 if you don't have a buildcache.
. build-helpers.sh
register_argument -f --force force "Fully reclone the repo"
register_additional_argument pull-id
register_additional_argument repo-root
register_additional_argument "db.sh params.."
parse_arguments "$@"
#####################
###----Process----###
#####################
cdir=$(pwd)
build_dir=${additional_arguments[1]}
if [ -z $build_dir ]; then
if [ -f SConstruct ]; then
build_dir=.
else
build_dir=godot
fi
fi
if ! cd "$build_dir" && [ -f SConstruct ]; then
echo "Bad build-dir argument"
exit 1
fi
start;
python_command=`cat <<PY
import json, sys
obj = json.load(sys.stdin)
print(obj["head"]["repo"]["clone_url"])
print(obj["head"]["ref"])
print(obj["head"]["label"])
PY`
pull_details=($(curl -s https://api.github.com/repos/godotengine/godot/pulls/${additional_arguments[0]} | python -c "$python_command"))
[ $? -ne 0 ] && exit $?
clone_url=${pull_details[0]}
clone_branch=${pull_details[1]}
name=${pull_details[2]//:/--}
path="projects/pr_testing/$name" # misusing nice .gitignored dir
if [ ! -z $force ]; then
run rm $path -rf
run git worktree prune
run git branch -D $name
fi
if [ ! -d $path ]; then
run git worktree add $path -b $name master
run cd $path
run git pull $clone_url $clone_branch --no-commit
run 'sed -i.bak "4i\\
CacheDir('"'$cdir/.buildcache'"');\\
" SConstruct'
else
run cd $path
run git pull $clone_url $clone_branch
fi
finish;
cd $cdir
run ./db.sh ${additional_arguments[@]:2}
#!/bin/bash
## Debugs (builds + runs) a Godot, works with worktrees
. build-helpers.sh
register_argument -r --release release "Build release"
register_argument -s --script run_script "Execute script instead of editing project"
register_argument -n --no-compile skip_compile "Do not run compilation"
register_additional_argument build-dir
register_additional_argument project-path
register_additional_argument script-path
parse_arguments "$@"
cdir=$(pwd)
build_dir=${additional_arguments[0]}
if [ -z $build_dir ]; then
if [ -f SConstruct ]; then
build_dir=.
else
build_dir=godot
fi
fi
if ! cd "$build_dir" && [ -f SConstruct ]; then
echo "Bad build-dir argument"
exit 1
fi
start;
if [ -z "$skip_compile" ]; then
if [ -z "$release" ]; then
run scons platform=x11 target=debug tools=yes -j $(nproc)
else
run scons platform=x11 target=release_debug tools=yes -j $(nproc)
fi
fi
cd $cdir # Run godot from current dir, to be able to use relative paths in project path
if [ -z "$release" ]; then
godot=$build_dir/bin/godot.x11.tools.64
else
godot=$build_dir/bin/godot.x11.opt.tools.64
fi
project_path=${additional_arguments[1]:-godot-test_project/} # Preferably change
script_path=${additional_arguments[2]:-test_script.gd}
if [ -z "$run_script" ]; then
run "$godot" -e -path "$project_path"
else
run "$godot" -path "$project_path" -s "res://$script_path"
fi
finish;
#!/bin/bash
## Proxies a script (in the parent directory). Useful when one has many, many worktrees
cdir=$(pwd)
cd ../
./$(basename "$0") $cdir $@
cd $cdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment