This commit is contained in:
xxoommd 2025-05-27 16:35:42 +08:00
parent 71cce2b79b
commit dbd242cfb4

View File

@ -20,69 +20,86 @@ function set_proxy() {
echo
}
function _c3_tool_help() {
echo
echo "Usage:"
echo " - c3tool build|b [ARGS]"
echo " [-u | --update-all] Updating Augustus custom resources and libs: SDL2 SDL2_mixer"
echo " [-f | --force] Clean all and rebuild, including resources and libs"
echo " [-r | --release] Build release version, default is debug"
echo " - c3tool clean|c"
echo
}
function c3tool() {
__GREEN__='\033[0;32m'
__RED__='\033[0;31m'
__BLUE__='\033[0;34m'
__YELLOW__='\033[0;33m'
__UNDERLINE__='\033[4m' # 下划线
__RESET_COLOR__='\033[0m' # No Color
__NC__='\033[0m' # No Color
# check `cmake`
if command -v cmake &>/dev/null; then
echo "[CHECK] cmake 已安装"
else
echo "[CHECK] FAIL: cmake 未安装,请先安装 cmake"
echo "[${__RED__}Err${__NC__}] cmake 未安装,请先安装 cmake"
return 0
fi
if command -v mingw32-make &>/dev/null; then
echo "[CHECK] mingw32-make 已安装"
else
echo "[CHECK] FAIL: mingw32-make 未安装,请先安装 mingw32-make"
echo "[${__RED__}Err${__NC__}] mingw32-make 未安装,请先安装 mingw32-make"
return 0
fi
# check if augustus or julius build folder
if [[ $PWD =~ "augustus" || $PWD =~ "julius" ]]; then
# 判断 basename 是否为 build
if [ "$(basename "$PWD")" = "build" ]; then
echo "[CHECK] Working directory: $PWD"
else
echo "[CHECK] FAIL: 进入 'augustus' 或者 'julius' 的 'build' 目录"
return 1
fi
# check augustus or julius project folder
if [ $(basename $PWD) == "build" ]; then
PROJECT_DIR=$(cd .. && pwd)
BUILD_DIR=$PWD
else
echo "[CHECK] FAIL: 进入 'augustus' 或者 'julius' 的 'build' 目录"
PROJECT_DIR=$PWD
BUILD_DIR="$PWD/build"
fi
if [ ! -f $PROJECT_DIR/CMakeLists.txt ]; then
echo -e "[${__RED__}ERR${__NC__}] Enter '${__YELLOW__}augustus${__NC__}' or '${__YELLOW__}julius${__NC__}' project first."
return 1
fi
printf "[CHECK] %-20s${__GREEN__}$PROJECT_DIR${__NC__}\n" "Project location:"
echo
if [ ! -d $BUILD_DIR ]; then
mkdir -p $BUILD_DIR
fi
printf "[CHECK] %-20s${__GREEN__}$BUILD_DIR${__NC__}\n" "Build location:"
cmd=""
if [[ $1 == "" || "$1" == -* ]]; then
cmd="build"
else
cmd=$1
shift
TARGET_NAME=$(awk '/set\(SHORT_NAME/ {print $2}' $PROJECT_DIR/CMakeLists.txt | sed 's/)//g')
printf "[CHECK] %-20s${__GREEN__}$TARGET_NAME${__NC__}\n" "Target name:"
if [[ $1 == "" || "$1" == "-h" || "$1" == "--help" ]]; then
_c3_tool_help
return 0
fi
cmd=$1 && shift
force=false
update_res=false
update_libs=false
update=false
# Parse args...
for arg in "$@"; do
case "$arg" in
-r | --update-resources)
update_res=true
;;
-l | --update-libs)
update_libs=true
-u | --update)
update=true
;;
-f | --force)
force=true
update=true
;;
-a | --all)
force=true
update_res=true
update_libs=true
;;
--release)
export CMAKE_BUILD_TYPE=release
-r | --release)
BUILD_TYPE=release
;;
esac
done
@ -91,70 +108,70 @@ function c3tool() {
case "$cmd" in
b | build)
if $force; then
printf "[BUILD] 清理make文件:" && rm -rf CMakeCache.txt CMakeFiles Makefile cmake_install.cmake compile_commands.json augustus* .cmake &&
echo "Done"
printf "[CLEAN] ... " && rm -rf $BUILD_DIR/* $BUILD_DIR/.* && echo "Done"
fi
printf "evn: - CMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE\n\n"
printf "[CHECK] %-20s${__GREEN__}$BUILD_TYPE${__NC__}\n" "CMAKE_BUILD_TYPE:"
# cmake
if [[ ! -f "Makefile" ]]; then
exec_str="cmake .. -G \"MinGW Makefiles\"" && eval $exec_str
exec_str="cd $BUILD_DIR && cmake .. -G \"MinGW Makefiles\" > /dev/null"
eval $exec_str
if [ $? -ne 0 ]; then
echo "[BUILD] FAIL. Execute string: ${exec_str}"
echo -e "[${__RED__}ERR${__NC__}] Execute string: ${exec_str}"
return 1
fi
fi
# mingw32-make
echo "开始编译..."
echo "[BUILD] Start ..."
if command -v nproc &>/dev/null; then
makenum=$(nproc)
else
makenum=2
fi
exec_str="mingw32-make -j$makenum" && eval $exec_str
export CMAKE_BUILD_TYPE=${BUILD_TYPE}
exec_str="cd $BUILD_DIR && mingw32-make -j$makenum --quiet > /dev/null"
eval $exec_str
if [ $? -ne 0 ]; then
echo "[BUILD] FAIL. Execute string: ${exec_str}"
unset CMAKE_BUILD_TYPE
echo -e "[${__RED__}ERR${__NC__}] FAIL. Execute string: ${exec_str}"
return 1
fi
unset CMAKE_BUILD_TYPE
if $update_res; then
printf "[BUILD] 清理res..." && rm -rf assets maps && echo "Done"
fi
if $update_libs; then
printf "[BUILD] 清理libs..." && rm -f SDL2.dll SDL2_mixer.dll && echo "Done"
if $update; then
if [[ $TARGET_NAME == "augustus" ]]; then
printf "[UPDATE] Cleaning res..." && rm -rf assets maps && echo "Done"
fi
printf "[UPDATE] Cleaning libs..." && rm -f SDL2.dll SDL2_mixer.dll && echo "Done"
fi
# Updating res:
if [[ ! -d assets || ! -d maps ]]; then
printf "[BUILD] Updating assets ..." && cp -r ../res/assets . && echo "Done"
printf "[BUILD] Updating maps ..." && cp -r ../res/maps . && echo "Done"
if [[ $TARGET_NAME == "augustus" ]]; then
if [[ ! -d assets || ! -d maps ]]; then
printf "[UPDATE] Copying assets ..." && cp -r ../res/assets . && echo "Done"
printf "[UPDATE] Copying maps ..." && cp -r ../res/maps . && echo "Done"
fi
fi
# Updating libs:
if [[ ! -f SDL2.dll || ! -f SDL2_mixer.dll ]]; then
printf "[BUILD] Updating libs ..." && cp ../ext/SDL2/SDL2-2.32.4/x86_64-w64-mingw32/bin/SDL2.dll . &&
printf "[UPDATE] Copying libs ..." && cp ../ext/SDL2/SDL2-2.32.4/x86_64-w64-mingw32/bin/SDL2.dll . &&
cp ../ext/SDL2/SDL2_mixer-2.8.1/x86_64-w64-mingw32/bin/SDL2_mixer.dll . &&
echo "Done"
fi
echo "[BUILD] success"
echo -e "[BUILD] Build ${__GREEN__}${TARGET_NAME}${__NC__} success"
;;
c | clean)
printf "开始清理..."
rm -rf CMakeCache.txt CMakeFiles Makefile cmake_install.cmake compile_commands.json augustus* &&
rm -rf assets maps &&
rm -f SDL2* &&
echo "Done"
printf "[CLEAN] ... " && rm -rf $BUILD_DIR/* $BUILD_DIR/.* && echo "Done"
;;
*)
echo "[Err] Invalid cmd: $cmd"
echo
echo "Usage:"
echo " - c3tool build|b [-r -l -f -a --release]"
echo " - c3tool clean|c"
_c3_tool_help
return 1
;;
esac