From 9b1bbffbc43931bd19692c1524f3d41ad7fcd125 Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 12 Jul 2024 01:55:58 +0300 Subject: [PATCH] v2.0.0 --- CHANGELOG.md | 18 +++- call_task.sh | 26 ------ cbuild.sh | 134 ++++++++++++++++++++++++++++++ chmod_scripts.sh | 10 +-- colors.sh | 11 --- config.sh | 74 +++++++++++++++++ default.Makefile | 95 --------------------- default.config | 6 +- default_tasks/build_exec.sh | 2 +- default_tasks/build_shared_lib.sh | 2 +- default_tasks/build_static_lib.sh | 2 +- default_tasks/callgrind.sh | 2 +- default_tasks/clean.sh | 6 +- default_tasks/exec.sh | 2 +- default_tasks/gprof.sh | 2 +- default_tasks/profile.sh | 2 +- default_tasks/valgrind.sh | 2 +- detect_os.sh | 47 +++++------ functions.sh | 28 ++----- init.sh | 78 ----------------- myprint.sh | 82 ++++++++++++++++++ rebuild_dep.sh | 10 ++- setup.sh | 35 ++------ 23 files changed, 372 insertions(+), 304 deletions(-) delete mode 100755 call_task.sh create mode 100644 cbuild.sh delete mode 100755 colors.sh create mode 100644 config.sh delete mode 100644 default.Makefile delete mode 100755 init.sh create mode 100644 myprint.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f04fe4..9e0afe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,16 @@ -# v7 +# 2.0.0 ++ updated setup.sh to do system-wide installation ++ deleted makefile (call `./cbuild.sh` or installed `cbuild`) ++ added command line arguments: + + `--help` + + `--current-config` + + `--default-config` + + `--new-project` ++ all shebang changed to `#!/usr/bin/env bash` ++ `init.sh` became `config.sh` with function `load_config` ++ moved color variables and print functions to `myprint.sh` + +# v1.7.0 + added function `resolve_dependencies` to `link` + added variables `DEPS_BASEDIR` and `DEPS` to config + added script `rebuild_dep.sh` which can be called through `Makefile` @@ -7,7 +19,7 @@ + added task `no_task` which is been set in `init.sh` when `TASK` is empty + now `STATIC_LIB_FILE` starts with "lib" -# v6 +# v1.6.0 + `build_profile` task was split to `profile` and `gprof` + added task `sanitize` + default C++ standard set to `c++11` @@ -16,7 +28,7 @@ + added function `try_delete_dir_or_file` for `clean` task + dead code removal in `build_exec` and `build_static_lib` -# v5 +# v1.5.0 + added task `clean` + added task `exec_dbg` + added task `build_profile` diff --git a/call_task.sh b/call_task.sh deleted file mode 100755 index 0618ac0..0000000 --- a/call_task.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -function exec_script { - myprint "${BLUE}executing $1" - source "$1" -} - -function try_exec_script { - if [ -f "$1" ]; then - exec_script "$1" - fi -} - -function call_task { - TASK="$1" - source cbuild/init.sh - myprint "${CYAN}===========[$TASK]===========" - myprint "${WHITE}project: ${CYAN}$PROJECT" - try_exec_script "$PRE_TASK_SCRIPT" - exec_script "$TASK_SCRIPT" - try_exec_script "$POST_TASK_SCRIPT" -} - -time call_task "$1" -# new line -echo diff --git a/cbuild.sh b/cbuild.sh new file mode 100644 index 0000000..7d0aa87 --- /dev/null +++ b/cbuild.sh @@ -0,0 +1,134 @@ +#!/usr/bin/env bash + +tabs 4 + +# exit on errors +set -eo pipefail + +INSTALLED_CBUILD_VERSION=2.0.0 + +if [ -z "$CBUILD_INSTALL_DIR" ]; then + CBUILD_INSTALL_DIR="/usr/local/share/cbuild" +fi + +function include { + local script_path="$1" + if [[ "$script_path" == cbuild/* ]]; then + script_path="$CBUILD_INSTALL_DIR/$(safeprint $script_path | sed 's,^cbuild/,,')" + fi + # echp "including script $script_path" + . "$script_path" +} + +function include_if_not_null { + if [ ! -z "$1" ]; then + include "$1" + fi +} + +include "cbuild/myprint.sh" +include "cbuild/functions.sh" +include "cbuild/config.sh" + +function print_help_and_exit { + myprint "Usage: cbuild [OPTIONS] [TASK]" + myprint " -h, --help Show this message" + myprint " -n, --new-project [PROJ_DIR] Initialize new cbuild project directory (default=./)" + myprint " -c, --current-config FILE Set current config file path (default=./current.config)" + myprint " -d, --default-config FILE Set default config file path (default=./current.config)" + exit 0 +} + +current_config_path="./current.config" +default_config_path="./default.config" +args=($@) +args_len=${#args[@]} +selected_tasks_array=() +i=0 +if [ $args_len -eq 0 ]; then + print_help_and_exit +fi + +function get_next_arg { + i=$((i+1)) + safeprint "${args[i]}" +} + +while [ $i -lt $args_len ] +do + case "${args[i]}" in + '-h' | '--help') + print_help_and_exit + ;; + '-c' | '--current-config') + i=$((i+1)) + current_config_path="${args[i]}" + myprint "<$current_config_path>" + exit + ;; + '-d' | '--default-config') + default_config_path="$(get_next_arg)" + ;; + '-n' | '--new-project') + new_project_dir="$(get_next_arg)" + if [ -z "$new_project_dir" ]; then + new_project_dir="./" + fi + + if ask_yn "create default.config?"; then + cp "$CBUILD_INSTALL_DIR/default.config" "$new_project_dir" + project_name="$(ask 'Enter project name')" + sed -i "s,\%PROJECT_NAME\%,$project_name,g" + fi + if ask_yn "Copy default .gitignore?"; then + cp "$CBUILD_INSTALL_DIR/.gitignore" "$new_project_dir" + fi + ;; + *) + selected_tasks_array+=(${args[i]}) + ;; + esac + i=$((i+1)) +done + +function call_task { + local current_config_path="$1" + local default_config_path="$2" + local task="$3" + + print_header "${CYAN}" "─" "$task" + load_config "$task" "$current_config_path" "$default_config_path" + resolve_dependencies "$DEPS_BASEDIR" "$DEPS" + + myprint "${BLUE}executing $PRE_TASK_SCRIPT" + # include_if_not_null "$PRE_TASK_SCRIPT" + + myprint "${BLUE}executing $TASK_SCRIPT" + # include "$TASK_SCRIPT" + + myprint "${BLUE}executing $POST_TASK_SCRIPT" + # include_if_not_null "$POST_TASK_SCRIPT" + myprint "error" +} + +function call_tasks { + local tasks="$@" + myprint "$tasks" + + print_header "${WHITE}" "═" "$PROJECT" + for task in $tasks ; do + call_task "$task" + done + print_hline "${WHITE}" "═" +} + +time call_tasks "${selected_tasks_array[@]}" #2>&1 | tee cbuild.log +# 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 diff --git a/chmod_scripts.sh b/chmod_scripts.sh index 3d54f1c..ba91258 100755 --- a/chmod_scripts.sh +++ b/chmod_scripts.sh @@ -1,7 +1,7 @@ -#!/bin/bash -SCRIPTS="$(find ./ -name '*.sh')" -for F in $SCRIPTS +#!/usr/bin/env bash + +for f in $(find ./ -name '*.sh') do - echo "$F" - chmod +x "$F" + chmod a+x "$f" + ls -lh "$f" done diff --git a/colors.sh b/colors.sh deleted file mode 100755 index e3e8363..0000000 --- a/colors.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -BLACK='\033[0;30m' -GRAY='\033[0;37m' -WHITE='\033[0;97m' -RED='\033[0;91m' -GREEN='\033[0;92m' -YELLOW='\033[0;93m' -BLUE='\033[0;94m' -PURPLE='\033[0;95m' -CYAN='\033[0;96m' diff --git a/config.sh b/config.sh new file mode 100644 index 0000000..32d1775 --- /dev/null +++ b/config.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +include cbuild/myprint.sh +include cbuild/functions.sh +include cbuild/detect_os.sh + +function load_config { + TASK="$1" + local current_config_path="$2" + local default_config_path="$3" + + OS=$(detect_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 + if [ -z "$INSTALLED_CBUILD_VERSION" ]; then + error "couldnt get current cbuild installation version" + fi + + # getting version of default config + exec_script_line "$default_config_path" 3 + DEFAULT_CONFIG_VERSION="$CONFIG_VERSION" + unset CONFIG_VERSION + + # undefined task + [ -z "$TASK" ] && TASK="no_task" + + # error on undefined + set -u + + # reading current config or creating default + if [ ! -f "$current_config_path" ]; then + myprint "${YELLOW}$current_config_path doesn't exist" + cp "$$default_config_path" "$current_config_path" + myprint "${YELLOW}Created copy (${current_config_path}) of default config (${default_config_path})" + fi + + myprint "${BLUE}reading $current_config_path" + include "$current_config_path" + + myprint "${WHITE}project: ${CYAN}$PROJECT" + + myprint "${WHITE}${current_config_path} cbuild version: ${CYAN}$CBUILD_VERSION" + myprint "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" + myprint "${WHITE}${current_config_path} version: ${CYAN}$CONFIG_VERSION" + myprint "${WHITE}${default_config_path} version: ${CYAN}$DEFAULT_CONFIG_VERSION" + + # checking versions + if [ ! "$CBUILD_VERSION" -eq "$INSTALLED_CBUILD_VERSION" ]; then + error "config was created for outdated cbuild version" + fi + if [ ! "$CONFIG_VERSION" -eq "$DEFAULT_CONFIG_VERSION" ]; then + error "config version isn't correct" + fi + + mkdir -p "$OUTDIR" + mkdir -p "$OBJDIR/libs" + mkdir -p "$OBJDIR/objects" + mkdir -p "$OBJDIR/profile" + + # dont thorw error on undefined variable + set +u + + myprint "${GREEN}cbuild initialized!" +} \ No newline at end of file diff --git a/default.Makefile b/default.Makefile deleted file mode 100644 index ce0b08f..0000000 --- a/default.Makefile +++ /dev/null @@ -1,95 +0,0 @@ -###################################### -###### Build tasks ####### -###################################### - -all: build_exec_dbg - -# creates executable using profiling info generated by profile -build_exec: profile - @cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log - -# creates executable with debug info and no optimizations -build_exec_dbg: - @cbuild/call_task.sh build_exec_dbg 2>&1 | tee make_raw.log - -# creates shared library -build_shared_lib: - @cbuild/call_task.sh build_shared_lib 2>&1 | tee make_raw.log - -# creates shared library with debug symbols and no optimizations -build_shared_lib_dbg: - @cbuild/call_task.sh build_shared_lib_dbg 2>&1 | tee make_raw.log - -# creates static library -build_static_lib: - @cbuild/call_task.sh build_static_lib 2>&1 | tee make_raw.log - -# creates static library with debug symbols and no optimizations -build_static_lib_dbg: - @cbuild/call_task.sh build_static_lib_dbg 2>&1 | tee make_raw.log - -###################################### -###### Rebuild dependencies ####### -###################################### - -# recompile libsome.a in the next build task -#rebuild_some_dep: -# @cbuild/rebuild_dep.sh libsome.a 2>&1 | tee make_raw.log - -#rebuild_all: rebuild_some_dep - -###################################### -###### Launch tasks ####### -###################################### - -# executes $EXEC_FILE -exec: build_exec - @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log - -# executes $EXEC_FILE -exec_dbg: build_exec_dbg - @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log - -# executes $EXEC_FILE with valgrind memory checker -valgrind: build_exec_dbg - @cbuild/call_task.sh valgrind 2>&1 | tee -a make_raw.log - -# generates profiling info -profile: - @cbuild/call_task.sh profile 2>&1 | tee make_raw.log - -# compiles program with -pg and runs it with gprof -# uses gprof2dot python script to generate function call tree (pip install gprof2dot) -# requires graphviz (https://www.graphviz.org/download/source/) -gprof: - @cbuild/call_task.sh gprof 2>&1 | tee make_raw.log - -# compiles program and runs it with callgrind (part of valgrind) -# uses gprof2dot python script to generate function call tree (pip install gprof2dot) -# requires graphviz (https://www.graphviz.org/download/source/) -# P.S. detailed results can be viewed in KCacheGrind -callgrind: - @cbuild/call_task.sh callgrind 2>&1 | tee make_raw.log - -# compiles executable with sanitizers and executes it to find errors and warnings -sanitize: - @cbuild/call_task.sh sanitize 2>&1 | tee make_raw.log - -###################################### -###### Other tasks ####### -###################################### - -# deletes generated files -clean: - @cbuild/call_task.sh clean 2>&1 | tee make_raw.log - -# removes all unreadable characters copied from stdio -fix_log: - sed 's/[^[:blank:][:print:]]//g' make_raw.log \ - | sed 's/\[0;[0-9][0-9]m//g' \ - | sed 's/\[0;[0-9]m//g' \ - | sed 's/\[[0-9][0-9]m//g' \ - | sed 's/\[[0-9]m//g' \ - | sed 's/ H //g' \ - | sed 's/\[3gH //g' \ - > make_fixed.log diff --git a/default.config b/default.config index 99378eb..24c23db 100644 --- a/default.config +++ b/default.config @@ -1,8 +1,8 @@ -#!/bin/bash -CBUILD_VERSION=7 +#!/usr/bin/env bash +CBUILD_VERSION=2.0.0 CONFIG_VERSION=1 -PROJECT="NULL" +PROJECT="%PROJECT_NAME%" CMP_C="gcc" CMP_CPP="g++" STD_C="c11" diff --git a/default_tasks/build_exec.sh b/default_tasks/build_exec.sh index 8908ab3..321d5fa 100755 --- a/default_tasks/build_exec.sh +++ b/default_tasks/build_exec.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # delete old objects clean_dir "$OBJDIR/objects" diff --git a/default_tasks/build_shared_lib.sh b/default_tasks/build_shared_lib.sh index c358f5a..45d0574 100755 --- a/default_tasks/build_shared_lib.sh +++ b/default_tasks/build_shared_lib.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # delete old objects clean_dir "$OBJDIR/objects" diff --git a/default_tasks/build_static_lib.sh b/default_tasks/build_static_lib.sh index 442d9aa..e2db349 100755 --- a/default_tasks/build_static_lib.sh +++ b/default_tasks/build_static_lib.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # delete old objects clean_dir "$OBJDIR/objects" diff --git a/default_tasks/callgrind.sh b/default_tasks/callgrind.sh index 9e9d070..bb43dcb 100644 --- a/default_tasks/callgrind.sh +++ b/default_tasks/callgrind.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cd "$OUTDIR" diff --git a/default_tasks/clean.sh b/default_tasks/clean.sh index 4f4a2fb..336af1f 100644 --- a/default_tasks/clean.sh +++ b/default_tasks/clean.sh @@ -1,4 +1,4 @@ -#!/usr/bin/bash +#!/usr/bin/env bash try_delete_dir_or_file "$OBJDIR" try_delete_dir_or_file "$OUTDIR" @@ -14,8 +14,8 @@ OLDIFS="$IFS" IFS=$'\n' cd "$DEPS_BASEDIR" for dep in $DEPS; do - dep_dir=$(echo ${dep/=*/} | tr -d '[:blank:]') - myprint "${CYAN}--------------[$dep_dir]--------------" + dep_dir=$(safeprint ${dep/=*/} | tr -d '[:blank:]') + print_header "${CYAN}" "─" "$dep_dir" cd "$dep_dir" make clean cd .. diff --git a/default_tasks/exec.sh b/default_tasks/exec.sh index a7bfe7e..af5188d 100755 --- a/default_tasks/exec.sh +++ b/default_tasks/exec.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cd "$OUTDIR" ./$EXEC_FILE diff --git a/default_tasks/gprof.sh b/default_tasks/gprof.sh index edcc59e..5e1e803 100644 --- a/default_tasks/gprof.sh +++ b/default_tasks/gprof.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cd "$OUTDIR" diff --git a/default_tasks/profile.sh b/default_tasks/profile.sh index 39be355..5dce3da 100644 --- a/default_tasks/profile.sh +++ b/default_tasks/profile.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cd "$OUTDIR" diff --git a/default_tasks/valgrind.sh b/default_tasks/valgrind.sh index 57c6892..86d02ca 100755 --- a/default_tasks/valgrind.sh +++ b/default_tasks/valgrind.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cd "$OUTDIR" rm -f "valgrind.log" diff --git a/detect_os.sh b/detect_os.sh index 9bbbfd8..b69e5d4 100644 --- a/detect_os.sh +++ b/detect_os.sh @@ -1,26 +1,25 @@ -#!/bin/bash +#!/usr/bin/env bash -source cbuild/colors.sh +include "cbuild/myprint.sh" -uname_result="$(uname -o)" -myprint "${GRAY}uname result: '$uname_result'" - -case "$uname_result" in - Msys | Cygwin | "MS/Windows") - OS=WINDOWS - ;; - Linux | GNU/Linux | Android) - OS=LINUX - ;; - FreeBSD) - OS=FREEBSD - ;; - Darwin) - OS=MACOS - ;; - *) - error "unknown operating system: $uname_result" - ;; -esac - -myprint "${GREEN}detected OS: $OS" +function detect_os { + local uname_rezult="$(uname -o)" + # myprint "uname rezult: '$uname_rezult'" + case "$uname_rezult" in + Msys | Cygwin | MS/Windows) + safeprint WINDOWS + ;; + Linux | GNU/Linux | Android) + safeprint LINUX + ;; + FreeBSD) + safeprint FREEBSD + ;; + Darwin) + safeprint MACOS + ;; + *) + error "unknown operating system: $uname_rezult" + ;; + esac +} diff --git a/functions.sh b/functions.sh index 14d6dea..17d4f73 100755 --- a/functions.sh +++ b/functions.sh @@ -1,14 +1,6 @@ -#!/bin/bash +#!/usr/bin/env bash -function myprint { - # first gray is needed to print string trarting with - - printf "${GRAY}$@${GRAY}\n" -} - -function error { - myprint "${RED}$1" - exit 1 -} +include "cbuild/myprint.sh" function clean_dir { local dir="$1" @@ -31,7 +23,6 @@ function try_delete_dir_or_file { fi } - function compile { local cmp="$1" myprint "${BLUE}compiler: ${GRAY}$cmp" @@ -59,25 +50,25 @@ function compile { # (args, sources) function compile_c { - myprint "${CYAN}-------------[compile_c]--------------" + print_header "${CYAN}" "─" "compile_c" compile "$CMP_C" "$STD_C" "$WARN_C" "$1" "$INCLUDE" "$2" } # (args, sources) function compile_cpp { - myprint "${CYAN}------------[compile_cpp]-------------" + print_header "${CYAN}" "─" "compile_cpp" compile "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$1" "$INCLUDE" "$2" } # (outfile) function pack_static_lib { - myprint "${CYAN}----------[pack_static_lib]-----------" + print_header "${CYAN}" "─" "pack_static_lib" local outfile="$1" myprint "${BLUE}outfile: ${GRAY}$outfile" local objects="$(find $OBJDIR/objects -name *.o) $(find $OBJDIR/libs -name '*.a')" myprint "${BLUE}objects: ${GRAY}$objects" - if ar rcs "$OUTDIR/$outfile" $(echo "$objects" | tr '\n' ' ') + if ar rcs "$OUTDIR/$outfile" $(safeprint "$objects" | tr '\n' ' ') then myprint "${GREEN}file $CYAN$outfile ${GREEN}created" else @@ -124,7 +115,7 @@ function resolve_dependencies { # handling dependencies for dep in $deps; do IFS="$OLDIFS" - dep_dir=$(echo ${dep/=*/} | tr -d '[:blank:]') + 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" @@ -135,16 +126,15 @@ function resolve_dependencies { } function link { - myprint "${CYAN}----------------[link]----------------" + print_header "${CYAN}" "─" "link" local args="$1" local outfile="$2" - resolve_dependencies "$DEPS_BASEDIR" "$DEPS" myprint "${BLUE}args: ${GRAY}$args" myprint "${BLUE}outfile: ${GRAY}$outfile" local objects="$(find $OBJDIR/objects -name '*.o') $(find $OBJDIR/libs -name '*.a')" myprint "${BLUE}objects: ${GRAY}$objects" - local command="$CMP_CPP $(echo "$objects" | tr '\n' ' ') $args -o $OUTDIR/$outfile" + local command="$CMP_CPP $(safeprint "$objects" | tr '\n' ' ') $args -o $OUTDIR/$outfile" myprint "$command" if $command then diff --git a/init.sh b/init.sh deleted file mode 100755 index ce9c336..0000000 --- a/init.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash - -tabs 4 - -# exit on errors -set -eo pipefail - -source cbuild/colors.sh -source cbuild/functions.sh -source cbuild/detect_os.sh - - -# copying default config from cbuild if it not exists -if [ ! -f default.config ]; then - cp cbuild/default.config default.config - myprint "${YELLOW}Default config didn't exist, copied from cbuild." -fi - -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 -exec_script_line cbuild/default.config 2 -INSTALLED_CBUILD_VERSION="$CBUILD_VERSION" -unset CBUILD_VERSION - -# getting version of default config -exec_script_line default.config 3 -DEFAULT_CONFIG_VERSION="$CONFIG_VERSION" -unset CONFIG_VERSION - -# undefined task -[ -z "$TASK" ] && TASK="no_task" - -# error on undefined -set -u - -# reading current config or creating default -if [ ! -f current.config ]; then - myprint "${YELLOW}./current.config doesn't exist" - cp default.config current.config - myprint "${YELLOW}New config created from the default.\nEdit it." - exit -fi - -myprint "${BLUE}reading ./current.config" -source current.config - -myprint "${WHITE}project: ${CYAN}$PROJECT" - -myprint "${WHITE}current.config cbuild version: ${CYAN}$CBUILD_VERSION" -myprint "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" -myprint "${WHITE}current.config version: ${CYAN}$CONFIG_VERSION" -myprint "${WHITE}default.config version: ${CYAN}$DEFAULT_CONFIG_VERSION" - -# checking versions -if [ ! "$CBUILD_VERSION" -eq "$INSTALLED_CBUILD_VERSION" ]; then - error "config was created for outdated cbuild version" -fi -if [ ! "$CONFIG_VERSION" -eq "$DEFAULT_CONFIG_VERSION" ]; then - error "config version isn't correct" -fi - -mkdir -p "$OUTDIR" -mkdir -p "$OBJDIR/libs" -mkdir -p "$OBJDIR/objects" -mkdir -p "$OBJDIR/profile" - -# dont thorw error on undefined variable -set +u - -myprint "${GREEN}cbuild initialized!" diff --git a/myprint.sh b/myprint.sh new file mode 100644 index 0000000..c64c2db --- /dev/null +++ b/myprint.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +BLACK='\033[0;30m' +GRAY='\033[0;37m' +WHITE='\033[0;97m' +RED='\033[0;91m' +GREEN='\033[0;92m' +YELLOW='\033[0;93m' +BLUE='\033[0;94m' +PURPLE='\033[0;95m' +CYAN='\033[0;96m' + +# prints text without new line +# use it to return string values from functions +function safeprint { + printf "%s" "$@" +} + +# prints text with new line and resets color +function myprint { + safeprint "$@${GRAY}\n" +} + +# print message and exit +function error { + myprint "$@" + exit 1 +} + +# asks a question with two options and returns 0 or 1 +# usage: `if ask_yn "do something?"` +function ask_yn { + local answ="" + myprint "$@ [y/n]" + read -r answ + return $([[ "$answ" = [Yy] ]]); +} + +# asks a question and prints answer +function ask { + local answ="" + myprint "$@: " + read -r answ + safeprint "$answ" +} + +# prints horizontal line occupying whole terminal row +# https://en.wikipedia.org/wiki/Box-drawing_characters +function print_hline { + local color="$1" + local character="$2" + if [ -z "$color" ]; then + color="${GRAY}" + fi + if [ -z "$character" ]; then + character="-"; + fi + printf "${color}%.s${character}" $(seq 2 $(tput cols)) + printf "${GRAY}\n" +} + +# prints horizontal line occupying whole terminal row with a given label +function print_header { + local color="$1" + local character="$2" + local label="$3" + if [ -z "$color" ]; then + color="${GRAY}" + fi + if [ -z "$character" ]; then + character="-"; + fi + local term_width=$(tput cols) + local label_length=${#label} + local line_characters_count=$((term_width - label_length - 2)) + local letf_line_length=$(( line_characters_count / 2 )) + local right_line_length=$(( letf_line_length + line_characters_count % 2 )) + printf "${color}%.s${character}" $(seq 1 $letf_line_length) + printf "[${label}]" + printf "${color}%.s${character}" $(seq 2 $right_line_length) + printf "${GRAY}\n" +} diff --git a/rebuild_dep.sh b/rebuild_dep.sh index d0303cb..12a97b0 100644 --- a/rebuild_dep.sh +++ b/rebuild_dep.sh @@ -1,7 +1,11 @@ -#!/bin/bash -source "cbuild/init.sh" +#!/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 with the next build task" +myprint "${YELLOW}dependency ${WHITE}$target_file ${YELLOW}will be rebuilt during the next build task" diff --git a/setup.sh b/setup.sh index 457ebac..902759c 100644 --- a/setup.sh +++ b/setup.sh @@ -1,32 +1,15 @@ -#!/bin/bash +#!/usr/bin/env bash # exit on errors set -eo pipefail +set -x -# help -if [ $# -eq 0 ] || [ "$1" = "h" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "/?" ]; then - echo "usage: setup.sh [ submodule | standalone ]" - echo " submodule - add to existing git repo as submodule" - echo " standalone - keep independent git repo" +if [ -z "$CBUILD_INSTALL_DIR" ]; then + CBUILD_INSTALL_DIR="/usr/local/share/cbuild" fi -case "$1" in - submodule) - echo "mode - $1" - git submodule add ./cbuild - ;; - standalone) - echo "mode - $1" - ;; - *) - echo "invalid argument: $1" - exit -1 - ;; -esac - -cp cbuild/default.Makefile Makefile -cp cbuild/default.config ./ - -echo "copy default .gitignore? [y/any]" -read answ -[[ "$answ"="y" ]] && cp cbuild/.gitignore ./ +if [ "$CBUILD_INSTALL_DIR" != "." ]; then + cp -r ./ "$CBUILD_INSTALL_DIR" + rm -rf "$CBUILD_INSTALL_DIR/.git" +fi +ln -sfv "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "/usr/local/bin/cbuild"