Compare commits

..

No commits in common. "7d60219c331f3ec6c36e40a0c776025460fc6599" and "023f0b1d401854b14553beacd0aa7d405d4325ac" have entirely different histories.

12 changed files with 149 additions and 222 deletions

View File

@ -1,13 +1,8 @@
# v2.0.2
+ new dependency resolution system (see **config** and `example_dependency_configs`)
+ **config**: changed description of `OBJDIR`
+ **config**: added task `rebuild_dependencies`
+ added variable `TASK_ARGS` which can be used in task scripts
# v2.0.1 # v2.0.1
+ fixed bugs in `myprint.sh`, `--new-project`, task execution
+ updated `.gitignore` + updated `.gitignore`
+ **config**: added `pwd` call to `valgrind` task + added `pwd` call to `valgrind` task in config
+ **config**: added `""` empty task check + added `""` empty task check in config
# 2.0.0 # 2.0.0
+ updated setup.sh to do system-wide installation + updated setup.sh to do system-wide installation

View File

@ -5,7 +5,7 @@ tabs 4
# exit on errors # exit on errors
set -eo pipefail set -eo pipefail
INSTALLED_CBUILD_VERSION=2.0.2 INSTALLED_CBUILD_VERSION=2.0.1
if [ -z "$CBUILD_INSTALL_DIR" ]; then if [ -z "$CBUILD_INSTALL_DIR" ]; then
CBUILD_INSTALL_DIR="/usr/local/share/cbuild" CBUILD_INSTALL_DIR="/usr/local/share/cbuild"
@ -95,10 +95,10 @@ function call_task {
local current_config_path="$1" local current_config_path="$1"
local default_config_path="$2" local default_config_path="$2"
local task="$3" local task="$3"
TASK_ARGS="$4"
print_header "${CYAN}" "─" "$PROJECT/$task" print_header "${CYAN}" "─" "$task"
load_config "$current_config_path" "$default_config_path" "$task" true load_config "$current_config_path" "$default_config_path" "$task"
resolve_dependencies "$DEPS_BASEDIR" "$DEPS"
if [ ! -z "$PRE_TASK_SCRIPT" ]; then if [ ! -z "$PRE_TASK_SCRIPT" ]; then
myprint "${BLUE}executing ${WHITE}$TASK_SCRIPT" myprint "${BLUE}executing ${WHITE}$TASK_SCRIPT"
@ -116,23 +116,27 @@ function call_task {
function call_tasks { function call_tasks {
local tasks="$@" local tasks="$@"
load_config "$current_config_path" "$default_config_path" "" false load_config "$current_config_path" "$default_config_path"
print_header "${WHITE}" "═" "$PROJECT" print_header "${WHITE}" "═" "$PROJECT"
project_dir="$(pwd)" for task in $tasks ; do
for task_str in $tasks ; do call_task "$current_config_path" "$default_config_path" "$task"
task=$(safeprint "$task_str" | sed 's/=.*//g')
local args_str=$(safeprint "$task_str" | grep -oP '(?<=\=).*' || echo "")
IFS_backup="$IFS"
IFS=',;'
args_array=($args_str)
IFS="$IFS_backup"
call_task "$current_config_path" "$default_config_path" "$task" "${args_array[@]}"
cd "$project_dir"
done done
print_hline "${WHITE}" "═"
} }
print_hline "${WHITE}" "═"
selected_tasks_count=${#selected_tasks_array[@]} selected_tasks_count=${#selected_tasks_array[@]}
if [ $selected_tasks_count -gt 0 ]; then if [ $selected_tasks_count -gt 0 ]; then
call_tasks "${selected_tasks_array[@]}" time call_tasks "${selected_tasks_array[@]}" #2>&1 | tee cbuild.log
fi
if [ -f cbuild.log ]; then
# remove terminal escape codes
sed -e 's/[^[:blank:][:print:]]//g' \
-e 's/\[0;[0-9][0-9]m//g' \
-e 's/\[0;[0-9]m//g' \
-e 's/\[[0-9][0-9]m//g' \
-e 's/\[[0-9]m//g' \
-e 's/ H //g' \
-e 's/\[3gH //g' \
-i cbuild.log
fi fi

View File

@ -4,32 +4,10 @@ include cbuild/myprint.sh
include cbuild/functions.sh include cbuild/functions.sh
include cbuild/detect_os.sh include cbuild/detect_os.sh
function myprint_quiet {
local quiet=$1
local text="$2"
if [ "$quiet" != true ]; then
myprint "$text"
fi
}
function exec_script_line {
local script="$1"
local line_num="$2"
local quiet=$3
myprint_quiet $quiet "${BLUE}reading line $line_num from $script"
local line_str="$(sed $line_num'!d' $script)"
myprint_quiet $quiet "$line_str"
eval "$line_str"
}
function load_config { function load_config {
local current_config_path="$1" local current_config_path="$1"
local default_config_path="$2" local default_config_path="$2"
TASK="$3" TASK="$3"
local quiet=$4
myprint "${BLUE}loading config current='$(realpath $current_config_path)' default='$(realpath $default_config_path)'"
if [ -z "$current_config_path" ]; then if [ -z "$current_config_path" ]; then
error "current_config_path is null" error "current_config_path is null"
fi fi
@ -38,7 +16,16 @@ function load_config {
fi fi
OS=$(detect_os) OS=$(detect_os)
myprint_quiet $quiet "${GREEN}detected OS: $OS" myprint "${GREEN}detected OS: $OS"
function exec_script_line {
local script="$1"
local line_num="$2"
myprint "${BLUE}reading line $line_num from $script"
local line_str="$(sed $line_num'!d' $script)"
myprint "$line_str"
eval "$line_str"
}
# getting version of cbuild installation # getting version of cbuild installation
if [ -z "$INSTALLED_CBUILD_VERSION" ]; then if [ -z "$INSTALLED_CBUILD_VERSION" ]; then
@ -46,7 +33,7 @@ function load_config {
fi fi
# getting version of default config # getting version of default config
exec_script_line "$default_config_path" 3 $quiet exec_script_line "$default_config_path" 3
DEFAULT_CONFIG_VERSION="$CONFIG_VERSION" DEFAULT_CONFIG_VERSION="$CONFIG_VERSION"
unset CONFIG_VERSION unset CONFIG_VERSION
@ -63,32 +50,31 @@ function load_config {
myprint "${YELLOW}Created copy (${current_config_path}) of default config (${default_config_path})" myprint "${YELLOW}Created copy (${current_config_path}) of default config (${default_config_path})"
fi fi
myprint_quiet $quiet "${BLUE}reading $current_config_path" myprint "${BLUE}reading $current_config_path"
include "$current_config_path" include "$current_config_path"
myprint_quiet $quiet "${WHITE}project: ${CYAN}$PROJECT" myprint "${WHITE}project: ${CYAN}$PROJECT"
myprint_quiet $quiet "${WHITE}${current_config_path} cbuild version: ${CYAN}$CBUILD_VERSION" myprint "${WHITE}${current_config_path} cbuild version: ${CYAN}$CBUILD_VERSION"
myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" myprint "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION"
myprint_quiet $quiet "${WHITE}${current_config_path} version: ${CYAN}$CONFIG_VERSION" myprint "${WHITE}${current_config_path} version: ${CYAN}$CONFIG_VERSION"
myprint_quiet $quiet "${WHITE}${default_config_path} version: ${CYAN}$DEFAULT_CONFIG_VERSION" myprint "${WHITE}${default_config_path} version: ${CYAN}$DEFAULT_CONFIG_VERSION"
# checking versions # checking versions
if [ "$CBUILD_VERSION" != "$INSTALLED_CBUILD_VERSION" ]; then if [ ! "$CBUILD_VERSION" -eq "$INSTALLED_CBUILD_VERSION" ]; then
error "config was created for outdated cbuild version" error "config was created for outdated cbuild version"
fi fi
if [ "$CONFIG_VERSION" != "$DEFAULT_CONFIG_VERSION" ]; then if [ ! "$CONFIG_VERSION" -eq "$DEFAULT_CONFIG_VERSION" ]; then
error "current config version doesn't match default config version" error "config version isn't correct"
fi fi
mkdir -p "$OUTDIR" mkdir -p "$OUTDIR"
mkdir -p "$OBJDIR/libs"
mkdir -p "$OBJDIR/objects" mkdir -p "$OBJDIR/objects"
mkdir -p "$OBJDIR/static_libs"
mkdir -p "$OBJDIR/dynamic_libs"
mkdir -p "$OBJDIR/profile" mkdir -p "$OBJDIR/profile"
# dont thorw error on undefined variable # dont thorw error on undefined variable
set +u set +u
myprint_quiet $quiet "${GREEN}loaded cbuild config" myprint "${GREEN}cbuild initialized!"
} }

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
CBUILD_VERSION=2.0.2 CBUILD_VERSION=2.0.1
CONFIG_VERSION=1 CONFIG_VERSION=1
PROJECT="%PROJECT_NAME%" PROJECT="%PROJECT_NAME%"
@ -14,17 +14,20 @@ SRC_CPP="$( find src -name '*.cpp')"
TESTS_C="$( find tests -name '*.c')" TESTS_C="$( find tests -name '*.c')"
TESTS_CPP="$(find tests -name '*.cpp')" TESTS_CPP="$(find tests -name '*.cpp')"
# Directory with dependency configs. # dir with dependeicy dirs
# See cbuild/example_dependency_configs DEPS_BASEDIR="."
DEPENDENCY_CONFIGS_DIR='.' # EXAMPLE: "dependency_dir='build_task out_dir lib_file'
# List of dependency config files in DEPENDENCY_CONFIGS_DIR separated by space. # other_depndency_dir=..."
ENABLED_DEPENDENCIES='' # Dependencies must be declared on separate lines
# Values can be override by resetting one of dependencies:
# DEPS="$DEPS
# dependency_dir='...'"
DEPS=""
# OBJDIR structure: # OBJDIR structure:
# ├── objects/ - Compiled object files. Cleans on each call of build task # ├── objects/ - dir where compiled *.o files are stored. cleans every call of build task
# ├── static_libs/ - Symbolic links to static libraries used by linker. Cleans on each call of build task. # ├── profile/ - dir where gcc *.gcda profiling info files stored
# ├── static_libs/ - Symbolic links to dynamic libraries used by linker. Cleans on each call of build task. # └── libs/ - there you can put static libs and linker will find them
# └── profile/ - gcc *.gcda profiling info files
OBJDIR="obj" OBJDIR="obj"
OUTDIR="bin" OUTDIR="bin"
STATIC_LIB_FILE="lib$PROJECT.a" STATIC_LIB_FILE="lib$PROJECT.a"
@ -169,12 +172,6 @@ case "$TASK" in
TASK_SCRIPT=cbuild/default_tasks/exec.sh TASK_SCRIPT=cbuild/default_tasks/exec.sh
POST_TASK_SCRIPT= POST_TASK_SCRIPT=
;; ;;
# rebuilds specified dependencies
# EXAMPLE: `cbuild rebuild_dependencies=libexample1,fonts`
# 'all' can be specified to rebuild all dependencies
rebuild_dependencies)
TASK_SCRIPT=cbuild/default_tasks/rebuild_dependencies.sh
;;
# deletes generated files # deletes generated files
clean) clean)
TASK_SCRIPT=cbuild/default_tasks/clean.sh TASK_SCRIPT=cbuild/default_tasks/clean.sh
@ -184,6 +181,6 @@ case "$TASK" in
;; ;;
# unknown task # unknown task
*) *)
error "task <$PROJECT/$TASK> not found" error "task <$TASK> not found"
;; ;;
esac esac

View File

@ -5,10 +5,21 @@ try_delete_dir_or_file "$OUTDIR"
myprint "${WHITE}deleting build logs" myprint "${WHITE}deleting build logs"
rm -rf *.log rm -rf *.log
project_dir="$(pwd)" for tmpfile in $(ls -a | grep -e '\.rebuild.*\.tmp'); do
for dep in $ENABLED_DEPENDENCIES; do try_delete_dir_or_file "$tmpfile"
load_dependency_config "$DEPENDENCY_CONFIGS_DIR/$dep.config"
cd "$DEP_WORKING_DIR"
exec_command "$DEP_CLEAN_COMMAND"
cd "$project_dir"
done done
set +e
OLDIFS="$IFS"
IFS=$'\n'
cd "$DEPS_BASEDIR"
for dep in $DEPS; do
dep_dir=$(safeprint ${dep/=*/} | tr -d '[:blank:]')
print_header "${CYAN}" "─" "$dep_dir"
cd "$dep_dir"
make clean
cd ..
done
IFS="$OLDIFS"
cd ..
set -e

View File

@ -1,11 +0,0 @@
#!/usr/bin/env bash
dependencies="$TASK_ARGS"
if [ "$dependencies" = 'all' ]; then
dependencies="$ENABLED_DEPENDENCIES"
fi
if [ ! -z "$dependencies" ]; then
myprint "${BLUE}dependencies to be rebuild: $dependencies"
build_dependencies "$dependencies" true
else
myprint "${YELLOW}no dependencies specified"
fi

View File

@ -1,10 +0,0 @@
#!/usr/bin/env bash
DEP_WORKING_DIR='depencencies/libexample1'
DEP_PRE_BUILD_COMMAND=''
DEP_BUILD_COMMAND='make libexample1.a'
DEP_POST_BUILD_COMMAND=''
DEP_CLEAN_COMMAND='make clean'
# won't be copied to project $OUTDIR
DEP_STATIC_OUT_FILES='libexample1.a libexample1_addon.a'
# will be copied tp project $OUTDIR
DEP_DYNAMIC_OUT_FILES='libexample1.config.json'

View File

@ -1,19 +0,0 @@
#!/usr/bin/env bash
DEP_WORKING_DIR='depencencies/libexample2'
DEP_PRE_BUILD_COMMAND=''
DEP_POST_BUILD_COMMAND=''
DEP_CLEAN_COMMAND='make clean'
DEP_STATIC_OUT_FILES=''
case $OS in
WINDOWS)
DEP_BUILD_COMMAND='make libexample2.dll'
DEP_DYNAMIC_OUT_FILES='libexample2.dll'
;;
LINUX)
DEP_BUILD_COMMAND='make libexample2.so'
DEP_DYNAMIC_OUT_FILES='libexample2.so'
;;
*)
error "operating system $OS has no configuration variants"
;;
esac

View File

@ -23,85 +23,7 @@ function try_delete_dir_or_file {
fi fi
} }
function exec_command {
local command="$@"
if [ ! -z "$command" ]; then
myprint "${GRAY}$command"
$command || error "command returned eror"
fi
}
function load_dependency_config {
local dependency_config_file="$1"
myprint "${BLUE}loading dependency config ${WHITE}${dependency_config_file}${BLUE}"
include "$dependency_config_file"
}
# builds a dependency when $dep_out_files dont exist or rebuild task is executed
function build_dependency {
# path to *.config file
local dependency_config_file="$1"
# true or false
local force_build="$2"
load_dependency_config "$dependency_config_file"
local proj_root_dir="$(pwd)"
myprint "${BLUE}entering dependency directory '${DEP_WORKING_DIR}'"
cd "$DEP_WORKING_DIR"
local build_needed="$force_build"
if [ "$build_needed" != true ]; then
for file in $DEP_STATIC_OUT_FILES $DEP_DYNAMIC_OUT_FILES; do
if [ ! -f "$file" ]; then
myprint "${GRAY}missing file '$file'"
local build_needed=true
fi
done
fi
if [ "$build_needed" = true ]; then
exec_command "$DEP_PRE_BUILD_COMMAND"
exec_command "$DEP_BUILD_COMMAND"
exec_command "$DEP_POST_BUILD_COMMAND"
myprint "${GRAY}dependency build finished"
else
myprint "${GRAY}dependency was built already"
fi
if [ ! -z "$DEP_DYNAMIC_OUT_FILES" ]; then
# copy each file to $OUTDIR
cp -rv $DEP_DYNAMIC_OUT_FILES "$proj_root_dir/$OUTDIR"
# symlink each file to $OBJDIR/dynamic_libs
for file in $DEP_DYNAMIC_OUT_FILES; do
ln -sfv $(realpath $file) "$proj_root_dir/$OBJDIR/dynamic_libs"
done
fi
if [ ! -z "$DEP_STATIC_OUT_FILES" ]; then
# symlink each file to $OBJDIR/static_libs
for file in $DEP_STATIC_OUT_FILES; do
ln -sfv $(realpath $file) "$proj_root_dir/$OBJDIR/static_libs"
done
fi
cd "$proj_root_dir"
}
function build_dependencies {
local dependencies="$1"
# true or false
local force_build="$2"
myprint "${BLUE}resolving dependencies"
clean_dir "$OBJDIR/static_libs"
clean_dir "$OBJDIR/dynamic_libs"
for dep in $dependencies; do
build_dependency "$DEPENDENCY_CONFIGS_DIR/$dep.config" "$force_build"
done
}
function compile { function compile {
build_dependencies "$ENABLED_DEPENDENCIES"
print_hline "${BLUE}" "─"
local cmp="$1" local cmp="$1"
myprint "${BLUE}compiler: ${GRAY}$cmp" myprint "${BLUE}compiler: ${GRAY}$cmp"
local std="$2" local std="$2"
@ -128,26 +50,25 @@ function compile {
# (args, sources) # (args, sources)
function compile_c { function compile_c {
print_header "${CYAN}" "─" "$PROJECT/$TASK/compile_c" print_header "${CYAN}" "─" "compile_c"
compile "$CMP_C" "$STD_C" "$WARN_C" "$1" "$INCLUDE" "$2" compile "$CMP_C" "$STD_C" "$WARN_C" "$1" "$INCLUDE" "$2"
} }
# (args, sources) # (args, sources)
function compile_cpp { function compile_cpp {
print_header "${CYAN}" "─" "$PROJECT/$TASK/compile_cpp" print_header "${CYAN}" "─" "compile_cpp"
compile "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$1" "$INCLUDE" "$2" compile "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$1" "$INCLUDE" "$2"
} }
# (outfile) # (outfile)
function pack_static_lib { function pack_static_lib {
print_header "${CYAN}" "─" "$PROJECT/$TASK/pack_static_lib" print_header "${CYAN}" "─" "pack_static_lib"
local outfile="$1" local outfile="$1"
myprint "${BLUE}outfile: ${GRAY}$outfile" myprint "${BLUE}outfile: ${GRAY}$outfile"
local objects="$(find $OBJDIR/objects -type f,l | tr '\n' ' ')" local objects="$(find $OBJDIR/objects -name *.o)
$(find $OBJDIR/libs -name '*.a')"
myprint "${BLUE}objects: ${GRAY}$objects" myprint "${BLUE}objects: ${GRAY}$objects"
local command="ar rcs $OUTDIR/$outfile $objects" if ar rcs "$OUTDIR/$outfile" $(safeprint "$objects" | tr '\n' ' ')
myprint "$command"
if $command
then then
myprint "${GREEN}file $CYAN$outfile ${GREEN}created" myprint "${GREEN}file $CYAN$outfile ${GREEN}created"
else else
@ -155,23 +76,65 @@ function pack_static_lib {
fi fi
} }
# if $lib_file doesn't exist or rebuild_* task was executed, builds static lib
function handle_static_dependency {
local deps_basedir="${1%/}"
local lib_project_dir="$2"
local lib_build_task="$3"
local lib_build_dir="$4"
local lib_file="$5"
if [ ! -f "$OBJDIR/libs/$lib_file" ] || [ -f .rebuild_$lib_file.tmp ]; then
[[ -z "$lib_build_task" ]] && error "lib_build_task is empty"
myprint "${BLUE}making $lib_file by task $lib_build_task"
local proj_root_dir="$(pwd)"
cd "$deps_basedir/$lib_project_dir"
if ! make "$lib_build_task"; then
exit 1
fi
cd "$proj_root_dir"
cp "$deps_basedir/$lib_project_dir/$lib_build_dir/$lib_file" "$OBJDIR/libs/"
myprint "${GREEN}copied ${CYAN}$lib_file to $OBJDIR/libs/"
rm -f .rebuild_$lib_file.tmp
fi
}
function resolve_dependencies {
deps_basedir=$1
deps=$2
[[ -z "$deps_basedir" ]] && deps_basedir="."
OLDIFS="$IFS"
IFS=$'\n'
# Evalueting dependency expressions.
# Necessary for overriding dependency configurations.
for dep in $deps; do
eval $dep
done
# handling dependencies
for dep in $deps; do
IFS="$OLDIFS"
dep_dir=$(safeprint ${dep/=*/} | tr -d '[:blank:]')
eval 'dep_params=$'$dep_dir
f_args="$deps_basedir $dep_dir $dep_params"
myprint "${BLUE}resolving dependency ${WHITE}$dep_dir${BLUE}: ${GRAY}$f_args"
handle_static_dependency $f_args
IFS=$'\n'
done
IFS="$OLDIFS"
}
function link { function link {
print_header "${CYAN}" "─" "$PROJECT/$TASK/link" print_header "${CYAN}" "─" "link"
local args="$1" local args="$1"
local outfile="$2" local outfile="$2"
myprint "${BLUE}args: ${GRAY}$args" myprint "${BLUE}args: ${GRAY}$args"
myprint "${BLUE}outfile: ${GRAY}$outfile" myprint "${BLUE}outfile: ${GRAY}$outfile"
local objects="$(find $OBJDIR/objects -type f,l | tr '\n' ' ')" local objects="$(find $OBJDIR/objects -name '*.o')
$(find $OBJDIR/libs -name '*.a')"
myprint "${BLUE}objects: ${GRAY}$objects" myprint "${BLUE}objects: ${GRAY}$objects"
local static_libs="$(find $OBJDIR/static_libs -type f,l | tr '\n' ' ')" local command="$CMP_CPP $(safeprint "$objects" | tr '\n' ' ') $args -o $OUTDIR/$outfile"
myprint "${BLUE}static libraries: ${GRAY}$static_libs"
local dynamic_libs="$(find $OBJDIR/dynamic_libs -type f,l | tr '\n' ' ')"
myprint "${BLUE}dynamic libraries: ${GRAY}$dynamic_libs"
local dynamic_libs_args="-L $OBJDIR/dynamic_libs"
for lib in $dynamic_libs; do
dynamic_libs_args="$dynamic_libs_args -l:$lib"
done
local command="$CMP_CPP $objects $static_libs $args $dynamic_libs_args -o $OUTDIR/$outfile"
myprint "$command" myprint "$command"
if $command if $command
then then

View File

@ -23,7 +23,7 @@ function myprint {
# print message and exit # print message and exit
function error { function error {
myprint "${RED}$@" myprint "$@"
exit 1 exit 1
} }

11
rebuild_dep.sh Normal file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
echo "ERROR: rebuild_dep.sh IS OBSOLETE"
exit 1
include "cbuild/config.sh"
load_config
target_file="$1"
touch ".rebuild_$target_file.tmp"
rm -fv "$OBJDIR/libs/$target_file"
myprint "${YELLOW}dependency ${WHITE}$target_file ${YELLOW}will be rebuilt during the next build task"

View File

@ -12,4 +12,4 @@ if [ "$CBUILD_INSTALL_DIR" != "." ]; then
cp -r ./ "$CBUILD_INSTALL_DIR" cp -r ./ "$CBUILD_INSTALL_DIR"
rm -rf "$CBUILD_INSTALL_DIR/.git" rm -rf "$CBUILD_INSTALL_DIR/.git"
fi fi
ln -sf "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "/usr/local/bin/cbuild" ln -sfv "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "/usr/local/bin/cbuild"