diff --git a/scripts/build.sh b/scripts/build.sh index 643be55..c6bde3a 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -15,20 +15,7 @@ # version 3 along with this program. # If not, see https://www.gnu.org/licenses/agpl-3.0.en.html -if [ "$(tr '[:upper:]' '[:lower:]' <<< $SINGLE)" = "yes" ]; then - echo "Running build script in single-threaded mode." - WAIT_ANYWAY="wait" -else - WAIT_ANYWAY="" -fi - -if [ -z "$CXX" ]; then - CXX="c++" -fi -if [ -z "$CXXFLAGS" ]; then - CXXFLAGS="-std=c++20 -Wall -Wextra" -fi -CXX_WITH_FLAGS="$CXX $CXXFLAGS" +source scripts/lib.sh # if unknown, figure out endianness if [ -f .endianness ]; then @@ -47,7 +34,7 @@ fi # `.cpp` files in src/lib will be automatically picked up and compiled into # dynamically linked libraries. echo "Building libs..." -mkdir -pv bin/lib +create_directory bin/lib for lib in $(find ./src/lib -name "*.cpp"); do COMPILE_COMMAND="$CXX_WITH_FLAGS -I ./include -fPIC -shared -o $(sed -e 's/^.\/src/.\/bin/;s/cpp$/so/' <<< $lib) $lib" echo $COMPILE_COMMAND @@ -69,7 +56,7 @@ wait # when running a program # Example: LD_LIBRARY_PATH=bin/lib bin/tools/dumpnbt echo "Building tools..." -mkdir -pv bin/tools +create_directory bin/tools # add compile commands to this array COMPILE_COMMANDS=( "$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -I./include -Lbin/lib -l:nbt.so -l:javacompat.so -o bin/tools/dumpnbt" diff --git a/scripts/clean.sh b/scripts/clean.sh index 594b300..88b386f 100755 --- a/scripts/clean.sh +++ b/scripts/clean.sh @@ -15,13 +15,15 @@ # version 3 along with this program. # If not, see https://www.gnu.org/licenses/agpl-3.0.en.html -rm -rv ./bin -rm -rv ./include -rm -vf .endianness -rm -vf resources/check_endianness -mkdir -v ./bin -mkdir -v ./bin/lib -mkdir -v ./include +source scripts/lib.sh + +remove ./bin +remove ./include +remove .endianness +remove resources/check_endianness +create_directory ./bin +create_directory ./bin/lib +create_directory ./include ln -vs ../../dependencies/sockpp-0.7.1/build/libsockpp.so bin/lib/ ln -vs ../../dependencies/sockpp-0.7.1/build/libsockpp.so.0 bin/lib/ @@ -30,6 +32,5 @@ ln -vs ../../dependencies/sockpp-0.7.1/build/libsockpp.so.0.7.1 bin/lib/ ln -vs ../dependencies/sockpp-0.7.1/include/sockpp/ ./include/ ln -vs ../dependencies/tiny-utf8-4.4.3/include/tinyutf8/ ./include/ -set -v -echo -n "" > ./bin/.placeholder -echo -n "" > ./include/.placeholder +create_file ./bin/.placeholder +create_file ./include/.placeholder diff --git a/scripts/clean_dependencies.sh b/scripts/clean_dependencies.sh index 25b4285..ccf9009 100755 --- a/scripts/clean_dependencies.sh +++ b/scripts/clean_dependencies.sh @@ -15,11 +15,13 @@ # version 3 along with this program. # If not, see https://www.gnu.org/licenses/agpl-3.0.en.html +source scripts/lib.sh + echo "Checking download cache for unneeded or corrupted files..." for file in $(ls .download_cache); do #TODO: remove if unknown shasum if grep $file scripts/setup_project.sh >/dev/null 2>&1; then - if sha256sum --check <<< "$file *.download_cache/$file" >/dev/null 2>&1; then + if check_sha256 ".download_cache/$file" "$file"; then echo -n "." else echo -n "!" @@ -32,7 +34,6 @@ for file in $(ls .download_cache); do done echo "" -rm -rv ./dependencies -mkdir -v ./dependencies -set -v -echo -n "" > ./dependencies/.placeholder +remove ./dependencies +create_directory ./dependencies +create_file ./dependencies/.placeholder diff --git a/scripts/lib.sh b/scripts/lib.sh new file mode 100644 index 0000000..2f3f319 --- /dev/null +++ b/scripts/lib.sh @@ -0,0 +1,78 @@ +#!/bin/echo You are not supposed to run this file. + +# Copyright 2022, FOSS-VG Developers and Contributers +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# version 3 along with this program. +# If not, see https://www.gnu.org/licenses/agpl-3.0.en.html + +if [ "$(tr '[:upper:]' '[:lower:]' <<< $SINGLE)" = "yes" ]; then + echo "Single-threaded mode enabled." + WAIT_ANYWAY="wait" +else + WAIT_ANYWAY="" +fi + +if [ -z "$CXX" ]; then + CXX="c++" +fi +if [ -z "$CXXFLAGS" ]; then + CXXFLAGS="-std=c++20 -Wall -Wextra" +fi +CXX_WITH_FLAGS="$CXX $CXXFLAGS" + +# automatically find and use an appropriate sha256 sum command +# currently supports sha256sum and NetBSD's sha256 +if command -v sha256sum > /dev/null; then + function check_sha256 { + FILE="$1" + CHECKSUM="$2" + sha256sum --check <<< "$CHECKSUM *$FILE" >/dev/null 2>&1 + return $? + } +else + if command -v sha256 > /dev/null; then + function check_sha256 { + FILE="$1" + CHECKSUM="$2" + sha256 --c <<< "SHA256 ($FILE) = $CHECKSUM" >/dev/null 2>&1 + return $? + } + else + echo "Could not find sha256sum or sha256." + exit 1 + fi +fi + +# This function exists to make the output more sensible on platforms where +# `rm -v` only prints the names of the removed things instead of a more +# comprehensible message like "removing NAME". +function remove { + echo "Removing $1..." + if [ -d "$1" ]; then + rm -rv "$1" + else + rm -v "$1" + fi +} + +# some platforms don’t support `mkdir -v` +function create_directory { + echo "Creating directory: $1" + mkdir -p "$1" +} + +# `mkfile -v` if you will +function create_file { + echo "Creating file: $1" + touch "$1" +} diff --git a/scripts/setup_project.sh b/scripts/setup_project.sh index f01d11d..b3a87b7 100755 --- a/scripts/setup_project.sh +++ b/scripts/setup_project.sh @@ -15,6 +15,8 @@ # version 3 along with this program. # If not, see https://www.gnu.org/licenses/agpl-3.0.en.html +source scripts/lib.sh + echo -n "Wget or curl? " if command -v wget >/dev/null 2>&1; then USE_WGET=yes @@ -36,11 +38,11 @@ function download { if [ ! -d .download_cache ]; then echo "Cache directory missing." - mkdir -v .download_cache + create_directory .download_cache fi if [ -f ".download_cache/$SHA256SUM" ]; then - if sha256sum --check <<< "$SHA256SUM *.download_cache/$SHA256SUM" >/dev/null 2>&1; then + if check_sha256 ".download_cache/$SHA256SUM" "$SHA256SUM"; then echo "Using locally cached file for $DESTINATION" cp ".download_cache/$SHA256SUM" "$DESTINATION" return 0 @@ -57,7 +59,7 @@ function download { else curl -L "$URL" --output ".download_cache/$SHA256SUM" >/dev/null 2>&1 fi - if sha256sum --check <<< "$SHA256SUM *.download_cache/$SHA256SUM" >/dev/null 2>&1; then + if check_sha256 ".download_cache/$SHA256SUM" "$SHA256SUM"; then cp ".download_cache/$SHA256SUM" "$DESTINATION" echo "done." return 0 @@ -76,7 +78,7 @@ echo "Cleaning dependencies..." scripts/clean_dependencies.sh set -e # failures are not acceptable here -mkdir -v dependencies/tmp +create_directory dependencies/tmp download https://github.com/DuffsDevice/tiny-utf8/archive/refs/tags/v4.4.3.tar.gz dependencies/tmp/tiny-utf8.tar.gz 8e3f61651909c9f3105d3501932a96aa65733127fb6e7cf94cb1b0a2dff42c8f download https://github.com/fpagliughi/sockpp/archive/refs/tags/v0.7.1.tar.gz dependencies/tmp/sockpp.tar.gz 2e023528bebbd2ac083fc91fbe6d5c4158c3336bedbcff48f594f3b28f53b940 @@ -96,4 +98,4 @@ cmake --build build popd >/dev/null 2>&1 echo "done." -rm -rv dependencies/tmp +remove dependencies/tmp diff --git a/scripts/test.sh b/scripts/test.sh index fc289e1..1b7c304 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -15,20 +15,7 @@ # version 3 along with this program. # If not, see https://www.gnu.org/licenses/agpl-3.0.en.html -if [ "$(tr '[:upper:]' '[:lower:]' <<< $SINGLE)" = "yes" ]; then - echo "Building tests in single-threaded mode." - WAIT_ANYWAY="wait" -else - WAIT_ANYWAY="" -fi - -if [ -z "$CXX" ]; then - CXX="c++" -fi -if [ -z "$CXXFLAGS" ]; then - CXXFLAGS="-std=c++20 -Wall -Wextra" -fi -CXX_WITH_FLAGS="$CXX $CXXFLAGS" +source scripts/lib.sh echo -n "Using LD_LIBRARY_PATH " if [ -z "$LD_LIBRARY_PATH" ]; then @@ -38,7 +25,7 @@ else fi echo "$LD_LIBRARY_PATH" -mkdir -pv bin/test +create_directory bin/test echo "Building tests..."