moved srcipts to include/ and changed function include()

This commit is contained in:
2025-11-09 22:20:58 +05:00
parent 05f3b9a0a0
commit 579dd5916e
9 changed files with 54 additions and 48 deletions

7
include/chmod_scripts.sh Normal file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env bash
for f in $(find ./ -name '*.sh')
do
chmod a+x "$f"
ls -lh "$f"
done

73
include/config.sh Normal file
View File

@@ -0,0 +1,73 @@
#!/usr/bin/env bash
include "@cbuild/include/myprint.sh"
include "@cbuild/include/functions.sh"
include "@cbuild/include/detect_os.sh"
function load_config {
local project_config_path="$1"
TASK="$2"
local quiet=$3
myprint "${BLUE}loading ${WHITE}'$project_config_path'"
if [ -z "$project_config_path" ]; then
error "config path is null"
fi
if [ ! -f "$project_config_path" ]; then
error "${YELLOW}$project_config_path doesn't exist"
fi
OS=$(detect_os)
ARCH=$(detect_arch)
myprint_quiet $quiet "${GREEN}detected OS: $OS"
# getting version of cbuild installation
if [ -z "$INSTALLED_CBUILD_VERSION" ]; then
error "couldnt get current cbuild installation version"
fi
# undefined task
[ -z "$TASK" ] && TASK="no_task"
# getting cbuild version from config (CBUILD_VERSION declaration is at line 2)
exec_script_line "$project_config_path" 2 $quiet
myprint_quiet $quiet "${WHITE}${project_config_path} cbuild version: ${CYAN}$CBUILD_VERSION"
myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION"
# checking versions
version_parse $INSTALLED_CBUILD_VERSION installed
version_parse $CBUILD_VERSION config
if [ "$installed_version_major.$installed_version_minor" != "$config_version_major.$config_version_minor" ]; then
error "config was created for cbuild$config_version_major.$config_version_minor, but loaded whith cbuild$installed_version_major.$installed_version_minor which is incompatible"
fi
if [[ $installed_version_patch < $config_version_patch ]]; then
myprint "${YELLOW}New patch for cbuild$installed_version_major.$installed_version_minor is abaliable."
myprint "${YELLOW}Install it to get latest bugfixes."
fi
# throw error on undefined variable usage
set -u
include "$project_config_path"
# load project user config
local project_user_config_path="$project_config_path.user"
if [ ! -f "$project_user_config_path" ]; then
myprint "${YELLOW}creating default project user config ${WHITE}$project_user_config_path"
cp "$project_user_config_path.default" "$project_user_config_path"
fi
myprint "${BLUE}loading ${WHITE}'$project_user_config_path'"
include "$project_user_config_path"
mkdir -p "$OUTDIR"
mkdir -p "$OBJDIR/objects"
mkdir -p "$OBJDIR/static_libs"
mkdir -p "$OBJDIR/dynamic_libs"
mkdir -p "$OBJDIR/profile"
# dont thorw error on undefined variable
set +u
myprint_quiet $quiet "${GREEN}config loading completed"
}

46
include/detect_os.sh Normal file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env bash
include "@cbuild/include/myprint.sh"
function detect_os {
local uname_result="$(uname -o)"
# myprint "uname result: '$uname_result'"
case "$uname_result" in
Msys | Cygwin | MS/Windows)
safeprint WINDOWS
;;
Linux | GNU/Linux | Android)
safeprint LINUX
;;
FreeBSD)
safeprint FREEBSD
;;
Darwin)
safeprint MACOS
;;
*)
error "unknown operating system: $uname_result"
;;
esac
}
function detect_arch {
local uname_result="$(uname -m)"
case "$uname_result" in
arm | arm32 | armhf | aarch32)
safeprint arm32
;;
arm64 | aarch64 | aarch64_be | armv8b | armv8l)
safeprint arm64
;;
x86 | i386 | i486 | i686)
safeprint x86
;;
x64 | x86_64 | amd64)
safeprint x64
;;
*)
error "unknown CPU architecture: $uname_result"
;;
esac
}

256
include/functions.sh Normal file
View File

@@ -0,0 +1,256 @@
#!/usr/bin/env bash
include "@cbuild/include/myprint.sh"
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)"
if [ -z "$line_str" ]; then
error "script line is empty"
fi
myprint_quiet $quiet "$line_str"
eval "$line_str"
}
function clean_dir {
local dir="$1"
myprint "${WHITE}cleaning $dir"
rm -rf "$dir"
mkdir "$dir"
}
function delete_dir {
local dir="$1"
myprint "${WHITE}deleting $dir"
rm -rf "$dir"
}
function try_delete_dir_or_file {
local path="$1"
if [ -f "$path" ] || [ -d "$path" ]; then
rm -rf "$path"
myprint "${WHITE}deleting $path"
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 $DEP_OTHER_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
# copies each file to $OUTDIR and creates symbolic link in $OBJDIR/dynamic_libs
for file in $DEP_DYNAMIC_OUT_FILES; do
# doesnt return error if called not like this
real_file=$(realpath $file)
file_dir=$(dirname $file)
if [ "$PRESERVE_OUT_DIRECTORY_STRUCTURE" = true ] && [ "$file_dir" != '.' ]; then
mkdir -p "$proj_root_dir/$OUTDIR/$file_dir"
mkdir -p "$proj_root_dir/$OBJDIR/dynamic_libs/$file_dir"
cp -v -u --preserve=timestamps "$file" "$proj_root_dir/$OUTDIR/$file"
ln -sfv "$real_file" "$proj_root_dir/$OBJDIR/dynamic_libs/$file"
else
cp -v -u --preserve=timestamps "$file" "$proj_root_dir/$OUTDIR/"
ln -sfv "$real_file" "$proj_root_dir/$OBJDIR/dynamic_libs/"
fi
done
fi
if [ ! -z "$DEP_OTHER_OUT_FILES" ]; then
# copies each file to $OUTDIR
for file in $DEP_OTHER_OUT_FILES; do
if [ "$PRESERVE_OUT_DIRECTORY_STRUCTURE" = true ]; then
file_dir=$(dirname $file)
mkdir -p "$proj_root_dir/$OUTDIR/$file_dir"
cp -v -u --preserve=timestamps "$file" "$proj_root_dir/$OUTDIR/$file"
else
cp -v -u --preserve=timestamps "$file" "$proj_root_dir/$OUTDIR/"
fi
done
fi
if [ ! -z "$DEP_STATIC_OUT_FILES" ]; then
# creates symbolic link to each file in $OBJDIR/static_libs
for file in $DEP_STATIC_OUT_FILES; do
# doesnt return error if called not like this
f=$(realpath $file)
ln -sfv $f "$proj_root_dir/$OBJDIR/static_libs"
done
fi
cd "$proj_root_dir"
# unsed all dependency config variables to not mess with next dependencies
unset DEP_WORKING_DIR
unset DEP_PRE_BUILD_COMMAND
unset DEP_BUILD_COMMAND
unset DEP_POST_BUILD_COMMAND
unset DEP_CLEAN_COMMAND
unset DEP_STATIC_OUT_FILES
unset PRESERVE_OUT_DIRECTORY_STRUCTURE
unset DEP_DYNAMIC_OUT_FILES
unset DEP_OTHER_OUT_FILES
}
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 {
build_dependencies "$ENABLED_DEPENDENCIES"
print_hline "${BLUE}" "─"
local cmp="$1"
local std="$2"
local warn="$3"
local args="$4"
local include="$5"
local sources="$6"
myprint "${BLUE}compiler: ${GRAY}$cmp"
myprint "${BLUE}standard: ${GRAY}$std"
myprint "${BLUE}warnings: ${GRAY}$warn"
myprint "${BLUE}args: ${GRAY}$args"
myprint "${BLUE}include dirs: ${GRAY}$include"
myprint "${BLUE}sources: ${GRAY}$sources"
for srcfile in $sources
do (
local object="$OBJDIR/objects/$(basename $srcfile).o"
if ! $($cmp -std=$std $warn $args $include -c -o $object $srcfile)
then
error "some error happened"
# TODO stop all threads
fi
) & done
wait
}
# (args, sources)
function compile_c {
local cmp="$1"
local std="$2"
local warn="$3"
local args="$4"
local include="$5"
local sources="$6"
print_header "${CYAN}" "─" "$PROJECT/$TASK/compile_c"
compile "$cmp" "$std" "$warn" "$args" "$include" "$sources"
}
# (args, sources)
function compile_cpp {
local cmp="$1"
local std="$2"
local warn="$3"
local args="$4"
local include="$5"
local sources="$6"
print_header "${CYAN}" "─" "$PROJECT/$TASK/compile_cpp"
compile "$cmp" "$std" "$warn" "$args" "$include" "$sources"
}
# (outfile)
function pack_static_lib {
print_header "${CYAN}" "─" "$PROJECT/$TASK/pack_static_lib"
local outfile="$1"
myprint "${BLUE}outfile: ${GRAY}$outfile"
local objects=$(find $OBJDIR/objects -type f,l | tr '\n' ' ')
myprint "${BLUE}objects: ${GRAY}$objects"
if [ -z "$objects" ]; then
error "no compiled objects found"
fi
local command="ar rcs $OUTDIR/$outfile $objects"
myprint "$command"
if $command
then
myprint "${GREEN}file $CYAN$outfile ${GREEN}created"
else
error "some error happened"
fi
clean_dir "$OBJDIR/objects"
}
function link {
print_header "${CYAN}" "─" "$PROJECT/$TASK/link"
local args="$1"
local outfile="$2"
myprint "${BLUE}args: ${GRAY}$args"
myprint "${BLUE}outfile: ${GRAY}$outfile"
local objects=$(find $OBJDIR/objects -type f,l | tr '\n' ' ')
myprint "${BLUE}objects: ${GRAY}$objects"
if [ -z "$objects" ]; then
error "no compiled objects found"
fi
local static_libs=$(find $OBJDIR/static_libs -type f,l | tr '\n' ' ')
myprint "${BLUE}static libraries: ${GRAY}$static_libs"
local dynamic_libs=$(find $OBJDIR/dynamic_libs -type f,l | tr '\n' ' '\
| sed "s,$OBJDIR/dynamic_libs/,,g")
myprint "${BLUE}dynamic libraries: ${GRAY}$dynamic_libs"
local dynamic_libs_args="-L./$OBJDIR/dynamic_libs -Wl,-Bdynamic"
for lib in $dynamic_libs; do
dynamic_libs_args="$dynamic_libs_args -Wl,-rpath=$(dirname $lib) -l:$lib"
done
local command="$CMP_CPP $objects $static_libs $args $dynamic_libs_args -o $OUTDIR/$outfile"
myprint "$command"
if $command
then
myprint "${GREEN}file $CYAN$outfile ${GREEN}created"
else
error "some error happened"
fi
clean_dir "$OBJDIR/objects"
}

96
include/myprint.sh Normal file
View File

@@ -0,0 +1,96 @@
#!/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 special characters
# use it to return string values from functions
function safeprint {
printf "%s" "$@"
}
# prints text with special characters and resets color
function myprint {
printf "${GRAY}$@${GRAY}\n"
}
function myprint_quiet {
local quiet=$1
local text="$2"
if [ "$quiet" != true ]; then
myprint "$text"
fi
}
# print message and exit
function error {
myprint "${RED}$@"
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] ]]);
}
function char_multiply {
local character="$1"
local length="$2"
i=0
while [ $i -lt $length ]; do
printf $character
i=$((i+1))
done
}
# 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
local term_width=$(tput cols)
local line_length=$((term_width - 1))
printf "${color}"
char_multiply "$character" $line_length
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 left_line_length=$(( line_characters_count / 2 ))
local right_line_length=$(( left_line_length - 1 + line_characters_count % 2 ))
printf "${color}"
char_multiply "$character" $left_line_length
printf "[${label}]${color}"
char_multiply "$character" $right_line_length
printf "${GRAY}\n"
}