Skip to content

Commit

Permalink
?build.sh: -Og, SusuwuUnitTestsBitmask, cleanup
Browse files Browse the repository at this point in the history
	: `-0g` for `--debug` (doubles `sha2` throughput, increases _aarch64_'s `a.out` from 3.1 megabytes to 4.2),
	return value was ${SusuwuUnitTestsBitmask}, but now `stdout` says so (as assignment, so it doesn't look as if "SusuwuUnitTestsBitmask" is the return value),

	Cleanup:
	Remove unused `#INCLUDES="${sSRC}"\n#export CXXFLAGS="-I${INCLUDES}"`.
	Use '\n' to produce logical blocks of code.
	Replace text+URLs with clickable _Markdown_.
	`:%s/$\(\w\+\)/${\1}/`: good practice to wrap all `sh` vars.
	`:%s/-c .\+$\/"\0"/`: source code files wrapped with quotes.
	`:%s/\w\+\.o\/"\0"/`: source objects wrapped with quotes.
	`:%s/\(SUSUWU_PRINT.*[^\.]\)"$/\1."/`: terminate all `stdout` messages with '.'.
	`:%s/_FLAGS/FLAGS/`: most projects do not include those underscores.

	Misc:
	?`${CCFLAGS}`, ?`${CXX}`, ?`${CXXFLAGS}`, ?`${LDFLAGS}`: use our preferences but inherit from environment.
	-`${sSrc}`: split into +`${C_SOURCE_PATH}`, +`${CXX_SOURCE_PATH}` (this mirrors common envars: `C_INCLUDE_PATH`, `CXX_INCLUDE_PATH`).
	?`clang++`: +#` -Xclang -analysis` (with a note to uncomment once know how to use this; it does not give extra outputs as is).

?`cxx/ClassSha2.cxx`, ?`posts/VirusAnalysis.md`: ?`classSha2Tests`: `%s/--release\` has 4x this throughput/--release\` has 2x this throughput/` (since `--debug` now has 2x `--debug`'s old throughput; 4/2==2).

Has to do with issues #3 (support more languages), #16 (support [more operating systems such as] Windows)
  • Loading branch information
SwuduSusuwu committed Nov 14, 2024
1 parent 2e01c08 commit 4fef864
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
105 changes: 58 additions & 47 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,85 +1,96 @@
#!/bin/sh
. ./Macros.sh
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "Dual licenses: choose \"Creative Commons\" or \"Apache 2\" (allows all uses)"
#cd build
sSRC="./cxx/"
#INCLUDES="${sSRC}"
#export CXX_FLAGS="-I${INCLUDES}"
CXX_FLAGS_ANALYSIS="-Wall -Wno-unused -Wno-unused-function -Wextra -Wno-unused-parameter -Wno-ignored-qualifiers" #TODO: `-Werror` without `-Wno-*`
CXX_FLAGS_RELEASE="${CXX_FLAGS_ANALYSIS} -fomit-frame-pointer -DNDEBUG -O2"
CXX_FLAGS_FSAN="-fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment"
#CXX_FLAGS_FSAN="${CXX_FLAGS_FSAN} -fsanitize=undefined" #/* causes 'cannot locate symbol "__ubsan_handle_function_type_mismatch_abort"' */
CXX_FLAGS_DEBUG="${CXX_FLAGS_ANALYSIS} -fno-omit-frame-pointer" #/* thus optimization won't remove stacktraces: https://stackoverflow.com/questions/48234575/g-will-fno-omit-frame-pointer-be-effective-if-specified-before-o2-or-o3 https://clang.llvm.org/docs/MemorySanitizer.html */
CXX_FLAGS_DEBUG="${CXX_FLAGS_DEBUG} -g" #/* extra symbols (line numbers + arguments) to stacktraces */
#CXX_FLAGS_DEBUG="${CXX_FLAGS_DEBUG} -fno-optimize-sibling-calls" #/* Don't inline functions. Does extra stacktraces. */
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "Dual licenses: choose \"Creative Commons\" or \"Apache 2\" (allows all uses)."

if command -v ctags > /dev/null; then
ctags -R
fi

CXXFLAGS_ANALYSIS="-Wall -Wno-unused -Wno-unused-function -Wextra -Wno-unused-parameter -Wno-ignored-qualifiers" #TODO: -`-Wno-*, +`-Wpedantic`, +`-Werror``
CXXFLAGS_RELEASE="-fomit-frame-pointer -DNDEBUG -O2" #/* without frame pointer (pointer used for stacktraces), without `assert(...)`/`SUSUWU_DEBUG(...)`/`SUSUWU_NOTICE(...)`, with optimization level 2 */
CXXFLAGS_DEBUG="-g -Og" #/* in MSVC is `/Zi /Od`: symbols for `gdb`/`lldb` use, optimizations compatible with `-g`/`-fsan*` */
CXXFLAGS_DEBUG="${CXXFLAGS_DEBUG} -fno-omit-frame-pointer" #/* thus optimization won't remove stacktraces: https://stackoverflow.com/questions/48234575/g-will-fno-omit-frame-pointer-be-effective-if-specified-before-o2-or-o3 https://clang.llvm.org/docs/MemorySanitizer.html */
#CXXFLAGS_DEBUG="${CXXFLAGS_DEBUG} -fno-optimize-sibling-calls" #/* Don't inline functions. Does extra stacktraces. */
CXXFLAGS_FSAN="-fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment"
#CXXFLAGS_FSAN="${CXXFLAGS_FSAN} -fsanitize=undefined" #/* causes 'cannot locate symbol "__ubsan_handle_function_type_mismatch_abort"' */

CROSS_COMP=""
if [ "--mingw" = "$1" ] || [ "--mingw" = "$2" ]; then
if [ "--mingw" = "${1}" ] || [ "--mingw" = "${2}" ]; then
CROSS_COMP=" --mingw"
LD_FLAGS="${LD_FLAGS} -static-libgcc -static-libstdc++"
LDFLAGS="${LDFLAGS} -static-libgcc -static-libstdc++"
if command -v x86_64-w64-mingw32-clang++ > /dev/null; then
CXX="x86_64-w64-mingw32-clang++"
CXX_FLAGS_DEBUG="${CXX_FLAGS_DEBUG} ${CXX_FLAGS_FSAN}" #/* Supports `x86_64-w64-mingw32-clang++`: https://github.com/SwuduSusuwu/SubStack/issues/16 */
CXXFLAGS_DEBUG="${CXXFLAGS_DEBUG} ${CXXFLAGS_FSAN}" #/* `-fsan*` [supports `x86_64-w64-mingw32-clang++`](https://github.com/SwuduSusuwu/SubStack/issues/16) */
elif command -v x86_64-w64-mingw32-g++ > /dev/null; then
CXX="x86_64-w64-mingw32-g++"
# CXX_FLAGS_DEBUG="${CXX_FLAGS_DEBUG} ${CXX_FLAGS_FSAN}" #/* TODO: support `x86_64-w64-mingw32-g++` ( https://www.mingw-w64.org/contribute/#thorough-status-report-for-sanitizers-asan-tsan-usan ) */
# CXXFLAGS_DEBUG="${CXXFLAGS_DEBUG} ${CXXFLAGS_FSAN}" #/* `TODO: `-fsan*` for `x86_64-w64-mingw32-g++`](https://www.mingw-w64.org/contribute/#thorough-status-report-for-sanitizers-asan-tsan-usan) */
else
SUSUWU_PRINT "${SUSUWU_SH_ERROR}" "\`x86_64-w64-mingw32-clang++ not found\`, \`x86_64-w64-mingw32-g++ not found\`. Do \`apt install llvm-mingw-w64\` or \`apt install mingw-w64\`"
SUSUWU_PRINT "${SUSUWU_SH_ERROR}" "\`x86_64-w64-mingw32-clang++ not found\`, \`x86_64-w64-mingw32-g++ not found\`. Do \`apt install llvm-mingw-w64\` or \`apt install mingw-w64\`."
exit 1
fi
elif command -v clang++ > /dev/null; then
CXX="clang++"
CXX_FLAGS_DEBUG="${CXX_FLAGS_DEBUG} ${CXX_FLAGS_FSAN}" #/* Supports `g++`/`clang++`: https://developers.redhat.com/blog/2021/05/05/memory-error-checking-in-c-and-c-comparing-sanitizers-and-valgrind#tldr */
CXX="clang++" #/* TODO: +` -Xclang -analyze -Xclang -analyzer-output=text` (got no extra outputs from this) */
CXXFLAGS_DEBUG="${CXXFLAGS_DEBUG} ${CXXFLAGS_FSAN}" #/* [`-fsan*` supports `g++`/`clang++`](https://developers.redhat.com/blog/2021/05/05/memory-error-checking-in-c-and-c-comparing-sanitizers-and-valgrind#tldr) */
elif command -v g++ > /dev/null; then
CXX="g++"
CXX_FLAGS_DEBUG="${CXX_FLAGS_DEBUG} ${CXX_FLAGS_FSAN}"
CXXFLAGS_DEBUG="${CXXFLAGS_DEBUG} ${CXXFLAGS_FSAN}"
elif command -v "${CXX}" > /dev/null; then
SUSUWU_PRINT "${SUSUWU_SH_INFO}" "\`clang++ not found\`, \`g++ not found\`. \`\${CXX}\` (\"${CXX}\") found, will use this."
else
SUSUWU_PRINT "${SUSUWU_SH_ERROR}" "\`clang++ not found\`, \`g++ not found\`. Do \`apt install clang\` or \`apt install gcc\`"
SUSUWU_PRINT "${SUSUWU_SH_ERROR}" "\`clang++ not found\`, \`g++ not found\`. \`\${CXX}\` (\"${CXX}\") not found. Do \`apt install clang\` or \`apt install gcc\`."
exit 1
fi
if [ "--release" = "$1" ] || [ "--release" = "$2" ]; then
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "\`${0}${CROSS_COMP} --release\` does not support profilers/debuggers (use \`$0${CROSS_COMP} --debug\` for this)"
CXX_FLAGS="${CXX_FLAGS} ${CXX_FLAGS_RELEASE}"

if [ "--release" = "${1}" ] || [ "--release" = "${2}" ]; then
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "\`${0}${CROSS_COMP} --release\` does not support profilers/debuggers (use \`${0}${CROSS_COMP} --debug\` for this)."
CXXFLAGS="${CXXFLAGS} ${CXXFLAGS_RELEASE}"
else
if [ "--debug" != "$1" ] && [ "--debug" != "$2" ]; then
if [ "--debug" != "${1}" ] && [ "--debug" != "${2}" ]; then
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "\`${0}${CROSS_COMP}\` defaults to \`${0}${CROSS_COMP} --debug\`."
fi
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "Use \`${0}${CROSS_COMP} --release\` to improve how fast this executes"
CXX_FLAGS="${CXX_FLAGS} ${CXX_FLAGS_DEBUG}"
SUSUWU_PRINT "${SUSUWU_SH_NOTICE}" "Use \`${0}${CROSS_COMP} --release\` to improve how fast this executes."
CXXFLAGS="${CXXFLAGS} ${CXXFLAGS_DEBUG}"
export ASAN_OPTIONS=abort_on_error=1:fast_unwind_on_malloc=0:detect_leaks=0 UBSAN_OPTIONS=print_stacktrace=1 #/* "For LLDB/GDB and to prevent very short stack traces and usually false leaks detection" */
fi
CXX="${CXX} ${CXX_FLAGS}"

LD="${CXX}"
CC="${CXX} -x c"
LDFLAGS="${LDFLAGS} ${CXXFLAGS}"
CXXFLAGS="${CXXFLAGS} ${CXXFLAGS_ANALYSIS}"
CCFLAGS="${CCFLAGS} ${CXXFLAGS}"
C_SOURCE_PATH="./c/"
CXX_SOURCE_PATH="./cxx/"
set -x
$CXX -x c -c ${sSRC}/../c/rfc6234/sha1.c
$CXX -x c -c ${sSRC}/../c/rfc6234/sha224-256.c
$CXX -x c -c ${sSRC}/../c/rfc6234/sha384-512.c
$CXX -c ${sSRC}/Macros.cxx
$CXX -c ${sSRC}/ClassSha2.cxx
$CXX -c ${sSRC}/ClassResultList.cxx
$CXX -c ${sSRC}/ClassSys.cxx
$CXX -c ${sSRC}/ClassCns.cxx
$CXX -c ${sSRC}/VirusAnalysis.cxx
$CXX -c ${sSRC}/AssistantCns.cxx
$CXX -c ${sSRC}/main.cxx
$CXX ${LD_FLAGS} sha1.o sha224-256.o sha384-512.o Macros.o ClassSha2.o ClassResultList.o ClassSys.o ClassCns.o VirusAnalysis.o AssistantCns.o main.o
${CC} ${CCFLAGS} -c "${C_SOURCE_PATH}rfc6234/sha1.c"
${CC} ${CCFLAGS} -c "${C_SOURCE_PATH}rfc6234/sha224-256.c"
${CC} ${CCFLAGS} -c "${C_SOURCE_PATH}rfc6234/sha384-512.c"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}Macros.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}ClassSha2.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}ClassResultList.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}ClassSys.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}ClassCns.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}VirusAnalysis.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}AssistantCns.cxx"
${CXX} ${CXXFLAGS} -c "${CXX_SOURCE_PATH}main.cxx"
#/* Order is 2 fold: language of code, plus which source the most `#include` (most include `Macros.hxx`). */
${LD} ${LDFLAGS} "sha1.o" "sha224-256.o" "sha384-512.o" "Macros.o" "ClassSha2.o" "ClassResultList.o" "ClassSys.o" "ClassCns.o" "VirusAnalysis.o" "AssistantCns.o" "main.o"
STATUS=$?
set +x
if [ 0 -eq $STATUS ]; then
if [ -z $CROSS_COMP ]; then

if [ 0 -eq ${STATUS} ]; then
if [ -z ${CROSS_COMP} ]; then
FILE_OUT="a.out"
else
FILE_OUT="a.exe"
fi
SUSUWU_PRINT "${SUSUWU_SH_SUCCESS}" "produced \`${FILE_OUT}\` (`stat -c%s ${FILE_OUT}` bytes)"
SUSUWU_PRINT "${SUSUWU_SH_SUCCESS}" "produced \`${FILE_OUT}\` (`stat -c%s ${FILE_OUT}` bytes)."
./${FILE_OUT}
STATUS=$?
if [ 0 -eq $STATUS ]; then
SUSUWU_PRINT "${SUSUWU_SH_SUCCESS}" "./${FILE_OUT} returned status ${STATUS}."
if [ 0 -eq ${STATUS} ]; then
SUSUWU_PRINT "${SUSUWU_SH_SUCCESS}" "\`./${FILE_OUT}\` returned status SusuwuUnitTestsBitmask(${STATUS})."
else
SUSUWU_PRINT "${SUSUWU_SH_ERROR}" "./${FILE_OUT} returned status ${STATUS}."
SUSUWU_PRINT "${SUSUWU_SH_ERROR}" "\`./${FILE_OUT}\` returned status SusuwuUnitTestsBitmask(${STATUS})."
fi
fi
return $STATUS
return ${STATUS}

2 changes: 1 addition & 1 deletion cxx/ClassSha2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const bool classSha2Tests() { /* is just to test glue code (which wraps rfc6234)
} else {
SUSUWU_INFO(std::to_string((ts2 - ts) / 1000) + " ms (" + std::to_string(ts2 - ts) + " μs) to compute `sha2(std::string(nulls, &nulls[65536])) == " + hashStrCompute + "` = " + std::to_string(float(65536) * CHAR_BIT /* to bits */ / (ts2 - ts) /* to bpμs */ * 1000000 /* to bps */ / (1 << 20) /* to mbps */) + "mbps");
}
SUSUWU_NOTICE("This `sha2()` is from `./build.sh --debug`: `./build.sh --release` has 4x this throughput");
SUSUWU_NOTICE("This `sha2()` is from `./build.sh --debug`: `./build.sh --release` has 2x this throughput");
if(0 == hash.size()) {
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "`0 == sha2(std::string()).size();"));
} else if(hashStrTrue.size() != hashStrCompute.size() && sha256 == sha2) {
Expand Down
2 changes: 1 addition & 1 deletion posts/VirusAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ const bool classSha2Tests() { /* is just to test glue code (which wraps rfc6234)
} else {
SUSUWU_INFO(std::to_string((ts2 - ts) / 1000) + " ms (" + std::to_string(ts2 - ts) + " μs) to compute `sha2(std::string(nulls, &nulls[65536])) == " + hashStrCompute + "` = " + std::to_string(float(65536) * CHAR_BIT /* to bits */ / (ts2 - ts) /* to bpμs */ * 1000000 /* to bps */ / (1 << 20) /* to mbps */) + "mbps");
}
SUSUWU_NOTICE("This `sha2()` is from `./build.sh --debug`: `./build.sh --release` has 4x this throughput");
SUSUWU_NOTICE("This `sha2()` is from `./build.sh --debug`: `./build.sh --release` has 2x this throughput");
if(0 == hash.size()) {
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "`0 == sha2(std::string()).size();"));
} else if(hashStrTrue.size() != hashStrCompute.size() && sha256 == sha2) {
Expand Down

0 comments on commit 4fef864

Please sign in to comment.