#!/usr/bin/env bash include "cbuild/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; 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 { build_dependencies "$ENABLED_DEPENDENCIES" print_hline "${BLUE}" "─" local cmp="$1" myprint "${BLUE}compiler: ${GRAY}$cmp" local std="$2" myprint "${BLUE}standard: ${GRAY}$std" local warn="$3" myprint "${BLUE}warnings: ${GRAY}$warn" local args="$4" myprint "${BLUE}args: ${GRAY}$args" local include="$5" myprint "${BLUE}include dirs: ${GRAY}$include" local sources="$6" 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 { print_header "${CYAN}" "─" "$PROJECT/$TASK/compile_c" compile "$CMP_C" "$STD_C" "$WARN_C" "$1" "$INCLUDE" "$2" } # (args, sources) function compile_cpp { print_header "${CYAN}" "─" "$PROJECT/$TASK/compile_cpp" compile "$CMP_CPP" "$STD_CPP" "$WARN_CPP" "$1" "$INCLUDE" "$2" } # (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" 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 } 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" 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' ' ')" 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" if $command then myprint "${GREEN}file $CYAN$outfile ${GREEN}created" else error "some error happened" fi }