diff --git a/.gitignore b/.gitignore index f72a415..70cf9a8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,12 +2,11 @@ bin/ obj/ -# IDE files +# user files +.old*/ .vs/ .vscode/ .vshistory/ .editorconfig *.user - -#backups -.old*/ +.config diff --git a/Makefile b/Makefile index 90c1221..c5b6c64 100644 --- a/Makefile +++ b/Makefile @@ -1,113 +1,15 @@ -###################################### -###### Config ####### -###################################### -OUTDIR:=bin -OBJDIR:=obj -TEST_FILE:=kerep.com -LIB_FILE:=kerep.so -DLL_FILE:=kerep.dll -CMP_C:=gcc -CMP_CPP:=g++ -STD_C:=c11 -STD_CPP:=c++17 -WARN_C:=-Wall -Wno-discarded-qualifiers #-Wextra -WARN_CPP:=-Wall #-Wextra -SRC_C:= $(wildcard src/*.c ) $(wildcard src/**/*.c ) -SRC_CPP:= $(wildcard src/*.cpp ) $(wildcard src/**/*.cpp ) -TESTS_C:= $(wildcard tests/*c ) $(wildcard tests/**/*.c ) -TESTS_CPP:=$(wildcard tests/*cpp) $(wildcard tests/**/*.cpp) - - -###################################### -###### Small tasks ####### -###################################### - -all: clear_c clear_bin build_test - -clear_c: - clear - -clear_bin: - @echo -e '\e[96m-------------[clear_bin]--------------\e[0m' - rm -rf $(OUTDIR) - mkdir $(OUTDIR) - -clear_obj: - @echo -e '\e[96m-------------[clear_obj]--------------\e[0m' - rm -rf $(OBJDIR) - mkdir $(OBJDIR) - - -###################################### ###### Build tasks ####### -###################################### +build_test: + @build_scripts/build_test.sh -# set it in build targets -SOURCES_C:= -SOURCES_CPP:= -CMPARGS:= -LNKARGS:= -OUTFILE:= +build_lib: + @build_scripts/build_lib.sh -compile_c_file=$(CMP_C) -std=$(STD_C) $(WARN_C) $(CMPARGS) -c ../$(SRCFILE) -__compile_c: - @echo -e '\e[96m-------------[compile_c]--------------\e[0m' - $(eval commands:=cd $(OBJDIR) $(foreach SRCFILE, $(SOURCES_C), ; $(compile_c_file))) - $(commands) - -compile_cpp_file=$(CMP_CPP) -std=$(STD_CPP) $(WARN_CPP) $(CMPARGS) -c ../$(SRCFILE) -__compile_cpp: - @echo -e '\e[96m------------[compile_cpp]-------------\e[0m' - $(eval commands:=cd $(OBJDIR) $(foreach SRCFILE, $(SOURCES_CPP), ; $(compile_cpp_file))) - $(commands) - -__compile: __compile_c #__compile_cpp - -__link: - @echo -e '\e[96m----------------[link]----------------\e[0m' - $(CMP_C) $(LNKARGS) -o $(OUTDIR)/$(OUTFILE) $(wildcard $(OBJDIR)/*.o) - -###### Build test ####### -__build_test_pre: - @echo -e '\e[96m===========[build_test_dbg]===========\e[0m' -build_test: OUTFILE=$(TEST_FILE) -build_test: SOURCES_C=$(SRC_C) $(TESTS_C) -build_test: SOURCES_CPP=$(SRC_CPP) $(TESTS_CPP) -build_test: CMPARGS=-O0 -g -build_test: LNKARGS= -build_test: clear_c __build_test_pre clear_obj __compile __link - -###### Build lib ####### -__build_lib_pre: - @echo -e '\e[96m=============[build_lib]==============\e[0m' -build_lib: OUTFILE=$(LIB_FILE) -build_lib: SOURCES_C=$(SRC_C) tests/test_marshalling.c -build_lib: SOURCES_CPP=$(SRC_CPP) -build_lib: CMPARGS=-O2 -fpic -shared -build_lib: LNKARGS=-flto -Wl,-soname,$(LIB_FILE) -build_lib: clear_c __build_lib_pre clear_obj __compile __link - -###### Build dll ####### -__build_dll_pre: - @echo -e '\e[96m=============[build_dll]==============\e[0m' -build_dll: OUTFILE=$(DLL_FILE) -build_dll: SOURCES_C=$(SRC_C) tests/test_marshalling.c -build_dll: SOURCES_CPP=$(SRC_CPP) -build_dll: CMPARGS=-O2 -fpic -shared -build_dll: LNKARGS=-static-libgcc -static-libstdc++ -build_dll: clear_c __build_dll_pre clear_obj __compile __link - - -###################################### ###### Testing tasks ####### -###################################### -test: build_test - @echo -e '\e[96m================[test]================\e[0m' - @tabs 4 - cd $(OUTDIR); ./$(TEST_FILE) +test: + @build_scripts/test.sh -valgrind: build_test - @echo -e '\e[96m==============[valgrind]==============\e[0m' - @tabs 4 - cd $(OUTDIR); valgrind -s --read-var-info=yes --track-origins=yes --fullpath-after=kerep/ \ - --leak-check=full --show-leak-kinds=all $(TEST_FILE).dbg +test_valgrind: + @build_scripts/test_valgrind.sh + +all: build_test test diff --git a/build_scripts/build_lib.sh b/build_scripts/build_lib.sh new file mode 100644 index 0000000..20604be --- /dev/null +++ b/build_scripts/build_lib.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +source build_scripts/init.sh + +print "${CYAN}=============[build_lib]==============\n" +clear_dir bin +clear_dir obj +compile_c "-O2 -fpic -shared" "$SRC_C tests/test_marshalling.c" +compile_cpp "-flto -Wl,-soname,$LIB_FILE" "$SRC_CPP" +link " " $LIB_FILE +rm -rf $OBJDIR diff --git a/build_scripts/build_test.sh b/build_scripts/build_test.sh new file mode 100644 index 0000000..18bee46 --- /dev/null +++ b/build_scripts/build_test.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +source build_scripts/init.sh + +print "${CYAN}=============[build_test]=============\n" +clear_dir bin +clear_dir obj +compile_c "-O0 -g" "$SRC_C $TESTS_C" +compile_cpp "-O0 -g" "$SRC_CPP $TESTS_CPP" +link " " $TEST_FILE +rm -rf $OBJDIR diff --git a/build_scripts/colors.sh b/build_scripts/colors.sh new file mode 100644 index 0000000..e3e8363 --- /dev/null +++ b/build_scripts/colors.sh @@ -0,0 +1,11 @@ +#!/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/build_scripts/default.config.sh b/build_scripts/default.config.sh new file mode 100644 index 0000000..740a22e --- /dev/null +++ b/build_scripts/default.config.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +OUTDIR=bin +OBJDIR=obj +TEST_FILE=kerep.com +LIB_FILE=kerep.so +DLL_FILE=kerep.dll +CMP_C=gcc +CMP_CPP=g++ +STD_C=c11 +STD_CPP=c++17 +WARN_C="-Wall -Wno-discarded-qualifiers" #-Wextra +WARN_CPP="-Wall -Wconversion-null -Wno-unused-variable -Wno-return-type" #-Wextra +SRC_C="$( find src -name *.c)" +SRC_CPP="$( find src -name *.cpp)" +TESTS_C="$( find tests -name *.c)" +TESTS_CPP="$(find tests -name *.cpp)" +VALGRIND_ARGS=-s --read-var-info=yes --track-origins=yes --fullpath-after=kerep/ \ + --leak-check=full --show-leak-kinds=all diff --git a/build_scripts/functions.sh b/build_scripts/functions.sh new file mode 100644 index 0000000..3aacfd1 --- /dev/null +++ b/build_scripts/functions.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +function print { + printf "$1$GRAY" +} + +function clear_dir { + print "${BLUE}clearing $1\n" + rm -rf $1 + mkdir $1 +} + +function compile { + local cmp=$1 + print "${BLUE}compiler: ${GRAY}$cmp\n" + local std=$2 + print "${BLUE}standard: ${GRAY}$std\n" + local warn=$3 + print "${BLUE}warnings: ${GRAY}$warn\n" + local args=$4 + print "${BLUE}args: ${GRAY}$args\n" + local sources=$5 + print "${BLUE}sources: ${GRAY}$sources\n" + for SRCFILE in $sources + do + $cmp -std=$std $warn $args -c -o "$OBJDIR/$(basename $SRCFILE).o" $SRCFILE + done +} + +# (args, sources) +function compile_c { + print "${CYAN}-------------[compile_c]--------------\n" + compile $CMP_C $STD_C "$WARN_C " "$1" "$2" +} + +# (args, sources) +function compile_cpp { + print "${CYAN}------------[compile_cpp]-------------\n" + compile $CMP_CPP $STD_CPP "$WARN_CPP" "$1" "$2" +} + +# (args, outfile) +function link { + print "${CYAN}----------------[link]----------------\n" + local args=$1 + print "${BLUE}args: ${GRAY}$args\n" + local outfile=$OUTDIR/$2 + print "${BLUE}outfile: ${GRAY}$outfile\n" + local objects="$(find $OBJDIR -name *.o)" + print "${BLUE}objects: ${GRAY}$objects\n" + $CMP_C $args -o $outfile $(echo $objects | tr '\n' ' ') + print "${GREEN}file $CYAN$outfile ${GREEN}created\n" +} diff --git a/build_scripts/init.sh b/build_scripts/init.sh new file mode 100644 index 0000000..c249b95 --- /dev/null +++ b/build_scripts/init.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +reset +tabs 4 + +source build_scripts/colors.sh +source build_scripts/functions.sh + +if [ ! -f ".config" ]; then + print "${YELLOW}./.config doesn't exists\n" + cp build_scripts/default.config.sh .config + print "${YELLOW}default config created\n" + while true; do + print "${WHITE}continue? (y/n) " + read answ + case $answ in + [Yy] ) break;; + [Nn] ) exit;; + * ) print "${RED}incorrect answer\n";; + esac + done +fi +source .config diff --git a/build_scripts/test.sh b/build_scripts/test.sh new file mode 100644 index 0000000..9e836f8 --- /dev/null +++ b/build_scripts/test.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +source build_scripts/init.sh + +print "${CYAN}================[test]================\n" +cd $OUTDIR; +./$TEST_FILE +cd .. diff --git a/build_scripts/test_valgrind.sh b/build_scripts/test_valgrind.sh new file mode 100644 index 0000000..1fd944f --- /dev/null +++ b/build_scripts/test_valgrind.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +source build_scripts/init.sh + +print "${CYAN}===========[test_valgrind]============\n" +cd $OUTDIR; +valgrind $VALGRIND_ARGS $TEST_FILE +cd ..