13 Commits

23 changed files with 101 additions and 47 deletions

View File

@@ -1 +1 @@
2.3.0 2.3.3

View File

@@ -1,9 +1,24 @@
## 2.3.3
+ now all default build dasks delete out file before building new one
## 2.3.2
+ fixed symlink creation in `OBJDIR/static_libs`
## 2.3.1
+ user config loads before project config
## 2.3.0 ## 2.3.0
+ Added ***project user config***! Read more in `./project.config.user.default` + **CONFIG:** Added new file `./project.config.user.default`.
+ **CONFIG:** Changed `cbuild/default_tasks` to `@cbuild/default_tasks`.
+ **CONFIG:** Updated `gprof` task.
+ Changed `include` function: replaced prefix `cbuild/` with `@cbuild/`. + Changed `include` function: replaced prefix `cbuild/` with `@cbuild/`.
+ **CONFIG:** Changed `cbuild/default_tasks` to `@cbuild/default_tasks` + Moved most scripts to `include/`.
+ Moved most scripts to `include/` + Renamed default config to `./project.config.default`.
+ Renamed default config to `./project.config.default` + Added functions:
+ `file_copy_default_if_not_present()`
+ `replace_var_value_in_script()`
+ Added task scripts:
+ `strip_exec.sh` - enabled in default config in task `build_exec`
## 2.2.4 ## 2.2.4
+ *default config*: C standard changed to C99 + *default config*: C standard changed to C99

0
bootstrap.sh Normal file → Executable file
View File

6
cbuild.sh Normal file → Executable file
View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
INSTALLED_CBUILD_VERSION=2.3.0 INSTALLED_CBUILD_VERSION=2.3.3
# set \t size to 4 spaces # set \t size to 4 spaces
tabs 4 tabs 4
@@ -45,7 +45,7 @@ include "@cbuild/include/functions.sh"
include "@cbuild/include/config.sh" include "@cbuild/include/config.sh"
function print_help { function print_help {
myprint "cbuild v$INSTALLED_CBUILD_VERSION" myprint "${GRAY}cbuild v$INSTALLED_CBUILD_VERSION"
myprint "C/C++ project build system written in bash." myprint "C/C++ project build system written in bash."
myprint "Usage: cbuild [OPTIONS] [TASKS]" myprint "Usage: cbuild [OPTIONS] [TASKS]"
myprint "Options:" myprint "Options:"
@@ -74,7 +74,7 @@ do
exit 0 exit 0
;; ;;
'-v' | '--version') '-v' | '--version')
myprint "cbuild v$INSTALLED_CBUILD_VERSION" myprint "${GRAY}cbuild v$INSTALLED_CBUILD_VERSION"
exit 0 exit 0
;; ;;
'-c' | '--config') '-c' | '--config')

0
include/chmod_scripts.sh → chmod_scripts.sh Normal file → Executable file
View File

View File

@@ -2,6 +2,7 @@
# delete old objects # delete old objects
clean_dir "$OBJDIR/objects" clean_dir "$OBJDIR/objects"
rm -f "$EXEC_FILE"
# copy profiling info # copy profiling info
prof_files=$(find "$OBJDIR/profile/" -name '*.gcda') prof_files=$(find "$OBJDIR/profile/" -name '*.gcda')

View File

@@ -2,6 +2,8 @@
# delete old objects # delete old objects
clean_dir "$OBJDIR/objects" clean_dir "$OBJDIR/objects"
rm -f "$SHARED_LIB_FILE"
[ ! -z "$SRC_C" ] && compile_c "$CMP_C" "$STD_C" "$WARN_C" "$C_ARGS" "$INCLUDE" "$SRC_C" [ ! -z "$SRC_C" ] && compile_c "$CMP_C" "$STD_C" "$WARN_C" "$C_ARGS" "$INCLUDE" "$SRC_C"
[ ! -z "$SRC_CPP" ] && compile_cpp "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$CPP_ARGS" "$INCLUDE" "$SRC_CPP" [ ! -z "$SRC_CPP" ] && compile_cpp "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$CPP_ARGS" "$INCLUDE" "$SRC_CPP"
link "$LINKER_ARGS" "$SHARED_LIB_FILE" link "$LINKER_ARGS" "$SHARED_LIB_FILE"

View File

@@ -2,6 +2,8 @@
# delete old objects # delete old objects
clean_dir "$OBJDIR/objects" clean_dir "$OBJDIR/objects"
rm -f "$STATIC_LIB_FILE"
[ ! -z "$SRC_C" ] && compile_c "$CMP_C" "$STD_C" "$WARN_C" "$C_ARGS" "$INCLUDE" "$SRC_C" [ ! -z "$SRC_C" ] && compile_c "$CMP_C" "$STD_C" "$WARN_C" "$C_ARGS" "$INCLUDE" "$SRC_C"
[ ! -z "$SRC_CPP" ] && compile_cpp "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$CPP_ARGS" "$INCLUDE" "$SRC_CPP" [ ! -z "$SRC_CPP" ] && compile_cpp "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$CPP_ARGS" "$INCLUDE" "$SRC_CPP"
pack_static_lib "$STATIC_LIB_FILE" pack_static_lib "$STATIC_LIB_FILE"

0
default_tasks/callgrind.sh Normal file → Executable file
View File

0
default_tasks/clean.sh Normal file → Executable file
View File

0
default_tasks/gprof.sh Normal file → Executable file
View File

0
default_tasks/profile.sh Normal file → Executable file
View File

0
default_tasks/rebuild_dependencies.sh Normal file → Executable file
View File

4
default_tasks/strip_exec.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env bash
exe_path="$OUTDIR/$EXEC_FILE"
myprint "${BLUE}stripping symbols from ${WHITE}$exe_path"
strip -s "$exe_path"

0
example_dependency_configs/libexample1.config Normal file → Executable file
View File

0
example_dependency_configs/libexample2.config Normal file → Executable file
View File

40
include/config.sh Normal file → Executable file
View File

@@ -5,19 +5,30 @@ include "@cbuild/include/functions.sh"
include "@cbuild/include/detect_os.sh" include "@cbuild/include/detect_os.sh"
function load_config { function load_config {
local project_config_path="$1" local proj_conf_file="$1"
TASK="$2" TASK="$2"
#true or false
local quiet=$3 local quiet=$3
myprint "${BLUE}loading ${WHITE}'$project_config_path'" if [ -z "$proj_conf_file" ]; then
if [ -z "$project_config_path" ]; then
error "config path is null" error "config path is null"
fi fi
if [ ! -f "$project_config_path" ]; then if [ ! -f "$proj_conf_file" ]; then
error "${YELLOW}$project_config_path doesn't exist" error "${YELLOW}$proj_conf_file doesn't exist"
fi fi
# load project user config
local proj_conf_user_file="$proj_conf_file.user"
file_copy_default_if_not_present "$proj_conf_user_file" "$proj_conf_user_file.default"
myprint "${BLUE}loading ${WHITE}'$proj_conf_user_file'"
# throw error on undefined variable usage
set -u
include "$proj_conf_user_file"
# don't throw error on undefined variable usage
set +u
myprint "${BLUE}loading ${WHITE}'$proj_conf_file'"
OS=$(detect_os) OS=$(detect_os)
ARCH=$(detect_arch) ARCH=$(detect_arch)
myprint_quiet $quiet "${GREEN}detected OS: $OS" myprint_quiet $quiet "${GREEN}detected OS: $OS"
@@ -31,9 +42,9 @@ function load_config {
[ -z "$TASK" ] && TASK="no_task" [ -z "$TASK" ] && TASK="no_task"
# getting cbuild version from config (CBUILD_VERSION declaration is at line 2) # getting cbuild version from config (CBUILD_VERSION declaration is at line 2)
exec_script_line "$project_config_path" 2 $quiet exec_script_line "$proj_conf_file" 2 $quiet
myprint_quiet $quiet "${WHITE}${project_config_path} cbuild version: ${CYAN}$CBUILD_VERSION" myprint_quiet $quiet "${WHITE}'${proj_conf_file}' cbuild version: ${CYAN}$CBUILD_VERSION"
myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION"
# checking versions # checking versions
@@ -47,18 +58,8 @@ function load_config {
myprint "${YELLOW}Install it to get latest bugfixes." myprint "${YELLOW}Install it to get latest bugfixes."
fi fi
# throw error on undefined variable usage
set -u set -u
include "$project_config_path" include "$proj_conf_file"
# 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 "$OUTDIR"
mkdir -p "$OBJDIR/objects" mkdir -p "$OBJDIR/objects"
@@ -66,7 +67,6 @@ function load_config {
mkdir -p "$OBJDIR/dynamic_libs" mkdir -p "$OBJDIR/dynamic_libs"
mkdir -p "$OBJDIR/profile" mkdir -p "$OBJDIR/profile"
# dont thorw error on undefined variable
set +u set +u
myprint_quiet $quiet "${GREEN}config loading completed" myprint_quiet $quiet "${GREEN}config loading completed"

1
include/detect_os.sh Normal file → Executable file
View File

@@ -4,7 +4,6 @@ include "@cbuild/include/myprint.sh"
function detect_os { function detect_os {
local uname_result="$(uname -o)" local uname_result="$(uname -o)"
# myprint "uname result: '$uname_result'"
case "$uname_result" in case "$uname_result" in
Msys | Cygwin | MS/Windows) Msys | Cygwin | MS/Windows)
safeprint WINDOWS safeprint WINDOWS

51
include/functions.sh Normal file → Executable file
View File

@@ -5,7 +5,9 @@ include "@cbuild/include/myprint.sh"
function exec_script_line { function exec_script_line {
local script="$1" local script="$1"
local line_num="$2" local line_num="$2"
#true or false
local quiet=$3 local quiet=$3
myprint_quiet $quiet "${BLUE}reading line $line_num from $script" myprint_quiet $quiet "${BLUE}reading line $line_num from $script"
local line_str="$(sed $line_num'!d' $script)" local line_str="$(sed $line_num'!d' $script)"
if [ -z "$line_str" ]; then if [ -z "$line_str" ]; then
@@ -15,16 +17,28 @@ function exec_script_line {
eval "$line_str" eval "$line_str"
} }
function replace_var_value_in_script(){
local script="$1"
local var_name="$2"
local new_value="$3"
myprint "${BLUE}setting $var_name to ${CYAN}'$new_value' in '$script'"
cp "$script" "$script.tmp"
sed "s,$var_name=\".*\",$var_name=\"$new_value\",g" \
"$script.tmp" > "$script"
rm "$script.tmp"
}
function clean_dir { function clean_dir {
local dir="$1" local dir="$1"
myprint "${WHITE}cleaning $dir" myprint "${BLUE}cleaning ${WHITE}'$dir'"
rm -rf "$dir" rm -rf "$dir"
mkdir "$dir" mkdir "$dir"
} }
function delete_dir { function delete_dir {
local dir="$1" local dir="$1"
myprint "${WHITE}deleting $dir" myprint "${BLUE}deleting ${WHITE}'$dir'"
rm -rf "$dir" rm -rf "$dir"
} }
@@ -32,7 +46,16 @@ function try_delete_dir_or_file {
local path="$1" local path="$1"
if [ -f "$path" ] || [ -d "$path" ]; then if [ -f "$path" ] || [ -d "$path" ]; then
rm -rf "$path" rm -rf "$path"
myprint "${WHITE}deleting $path" myprint "${BLUE}deleting ${WHITE}'$path'"
fi
}
file_copy_default_if_not_present(){
local file="$1"
local file_default="$2"
if [ ! -f "$file" ]; then
myprint "${YELLOW}creating default ${WHITE}'$file'"
cp -r "$file_default" "$file"
fi fi
} }
@@ -45,18 +68,18 @@ function exec_command {
} }
function load_dependency_config { function load_dependency_config {
local dependency_config_file="$1" local dep_conf_file="$1"
myprint "${BLUE}loading dependency config ${WHITE}${dependency_config_file}${BLUE}" myprint "${BLUE}loading dependency config ${WHITE}'${dep_conf_file}'"
include "$dependency_config_file" include "$dep_conf_file"
} }
# builds a dependency when $dep_out_files dont exist or rebuild task is executed # builds a dependency when $dep_out_files dont exist or rebuild task is executed
function build_dependency { function build_dependency {
# path to *.config file # path to *.config file
local dependency_config_file="$1" local dep_conf_file="$1"
# true or false # true or false
local force_build="$2" local force_build="$2"
load_dependency_config "$dependency_config_file" load_dependency_config "$dep_conf_file"
local proj_root_dir="$(pwd)" local proj_root_dir="$(pwd)"
myprint "${BLUE}entering dependency directory '${DEP_WORKING_DIR}'" myprint "${BLUE}entering dependency directory '${DEP_WORKING_DIR}'"
@@ -114,8 +137,8 @@ function build_dependency {
# creates symbolic link to each file in $OBJDIR/static_libs # creates symbolic link to each file in $OBJDIR/static_libs
for file in $DEP_STATIC_OUT_FILES; do for file in $DEP_STATIC_OUT_FILES; do
# doesnt return error if called not like this # doesnt return error if called not like this
f=$(realpath $file) real_file=$(realpath $file)
ln -sfv $f "$proj_root_dir/$OBJDIR/static_libs" ln -sfv "$real_file" "$proj_root_dir/$OBJDIR/static_libs/"
done done
fi fi
@@ -210,7 +233,9 @@ function pack_static_lib {
fi fi
local command="ar rcs $OUTDIR/$outfile $objects" local command="ar rcs $OUTDIR/$outfile $objects"
myprint "$command" print_hline "${GRAY}" "─"
myprint "${GRAY}$command"
print_hline "${GRAY}" "─"
if $command if $command
then then
myprint "${GREEN}file $CYAN$outfile ${GREEN}created" myprint "${GREEN}file $CYAN$outfile ${GREEN}created"
@@ -245,7 +270,9 @@ function link {
done done
local command="$CMP_CPP $objects $static_libs $args $dynamic_libs_args -o $OUTDIR/$outfile" local command="$CMP_CPP $objects $static_libs $args $dynamic_libs_args -o $OUTDIR/$outfile"
myprint "$command" print_hline "${GRAY}" "─"
myprint "${GRAY}$command"
print_hline "${GRAY}" "─"
if $command if $command
then then
myprint "${GREEN}file $CYAN$outfile ${GREEN}created" myprint "${GREEN}file $CYAN$outfile ${GREEN}created"

3
include/myprint.sh Normal file → Executable file
View File

@@ -18,10 +18,11 @@ function safeprint {
# prints text with special characters and resets color # prints text with special characters and resets color
function myprint { function myprint {
printf "${GRAY}$@${GRAY}\n" printf "$@${GRAY}\n"
} }
function myprint_quiet { function myprint_quiet {
#true or false
local quiet=$1 local quiet=$1
local text="$2" local text="$2"
if [ "$quiet" != true ]; then if [ "$quiet" != true ]; then

13
project.config.default Normal file → Executable file
View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
CBUILD_VERSION=2.3.0 CBUILD_VERSION=2.3.3
PROJECT="%PROJECT_NAME%" PROJECT="%PROJECT_NAME%"
CMP_C="gcc" CMP_C="gcc"
@@ -35,7 +35,7 @@ OBJDIR="obj"
OUTDIR="bin" OUTDIR="bin"
STATIC_LIB_FILE="$PROJECT.a" STATIC_LIB_FILE="$PROJECT.a"
# example: "-I./include" # example: "-I./include -I$DEPENDENCIES_DIR/libexample"
INCLUDE="" INCLUDE=""
# OS-specific options # OS-specific options
@@ -73,7 +73,7 @@ case "$TASK" in
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS" LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT="" PRE_TASK_SCRIPT=""
TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh" TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"
POST_TASK_SCRIPT="" POST_TASK_SCRIPT="@cbuild/default_tasks/strip_exec.sh"
;; ;;
# creates executable with debug info and no optimizations # creates executable with debug info and no optimizations
build_exec_dbg) build_exec_dbg)
@@ -148,8 +148,11 @@ case "$TASK" in
# requires graphviz (https://www.graphviz.org/download/source/) # requires graphviz (https://www.graphviz.org/download/source/)
gprof) gprof)
OUTDIR="$OUTDIR/gprof" OUTDIR="$OUTDIR/gprof"
# -pg adds code to executable, that generates file containing function call info (gmon.out) # arguments that emit some call counter code and disable optimizations to see function names
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg" # https://github.com/msys2/MINGW-packages/issues/8503#issuecomment-1365475205
C_ARGS="-O0 -g -pg -no-pie -fno-omit-frame-pointer
-fno-inline-functions -fno-inline-functions-called-once
-fno-optimize-sibling-calls -fopenmp"
CPP_ARGS="$C_ARGS" CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS" LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh" PRE_TASK_SCRIPT="@cbuild/default_tasks/build_exec.sh"

0
project.config.user.default Normal file → Executable file
View File

0
setup.sh Normal file → Executable file
View File