From c6983ac6c5f38da0b3c6c5788f5afb368ee86cad Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 6 Mar 2022 15:53:27 -0500 Subject: [PATCH] Improve Build System + Use GCC (Clang Is Just Too Broken) --- CMakeLists.txt | 8 -- cmake/base-toolchain.cmake | 117 +--------------------------- docs/BUILDING.md | 52 ++----------- media-layer/core/src/media.c | 2 +- mods/src/home/home.c | 6 +- scripts/build.sh | 62 +++++---------- scripts/install-dependencies.sh | 2 - scripts/setup.sh | 80 +++++++++++++++++++ symbols/include/symbols/minecraft.h | 4 +- 9 files changed, 113 insertions(+), 220 deletions(-) create mode 100755 scripts/setup.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index bfe9097e..da53fd71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,14 +25,6 @@ else() message(FATAL_ERROR "Invalid Mode") endif() -# Use Clang By Default -if(NOT DEFINED CMAKE_C_COMPILER) - set(CMAKE_C_COMPILER "clang") -endif() -if(NOT DEFINED CMAKE_CXX_COMPILER) - set(CMAKE_CXX_COMPILER "clang++") -endif() - # Utility Functions include(cmake/util.cmake) diff --git a/cmake/base-toolchain.cmake b/cmake/base-toolchain.cmake index 08a50724..4528ca45 100644 --- a/cmake/base-toolchain.cmake +++ b/cmake/base-toolchain.cmake @@ -1,122 +1,9 @@ -# Sanity Check Return -function(sanity_check_return ret) - if(NOT ret EQUAL "0") - message(FATAL_ERROR "Process Failed") - endif() -endfunction() - -# Get Host Architecture -find_program(UNAME uname /bin /usr/bin /usr/local/bin REQUIRED) -execute_process( - COMMAND "${UNAME}" "-m" - OUTPUT_VARIABLE HOST_ARCHITECTURE - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE ret -) -sanity_check_return("${ret}") - -# Get Include Directories -function(get_include_dirs target compiler result) - # Get Tool Name - set(tool "cc1") - if(compiler MATCHES "^.*g\\+\\+$") - set(tool "cc1plus") - endif() - - # Get Tool Path - execute_process( - COMMAND "${compiler}" "-print-prog-name=${tool}" - ERROR_QUIET - OUTPUT_VARIABLE tool - OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE ret - ) - sanity_check_return("${ret}") - - # Run Tool To Get Include Path - set(tool_output "") - execute_process( - COMMAND "${tool}" "-quiet" "-v" "-imultiarch" "${target}" - OUTPUT_QUIET - ERROR_VARIABLE tool_output - ERROR_STRIP_TRAILING_WHITESPACE - INPUT_FILE "/dev/null" - RESULT_VARIABLE ret - ) - sanity_check_return("${ret}") - string(REPLACE "\n" ";" tool_output "${tool_output}") - - # Loop - set(parsing_include_section FALSE) - foreach(line IN LISTS tool_output) - # Check Include Section Status - if(parsing_include_section) - # Check If Include Section Is Over - if(line MATCHES "^End of search list.$") - # Starting Include Section - set(parsing_include_section FALSE) - break() - else() - # Parsing Include Section - if(line MATCHES "^ .*$") - # Strip Line - string(STRIP "${line}" line) - # Add To List - list(APPEND "${result}" "${line}") - endif() - endif() - else() - # Check If Include Section Is Starting - if(line MATCHES "^#include <\\.\\.\\.> search starts here:$") - # Starting Include Section - set(parsing_include_section TRUE) - endif() - endif() - endforeach() - - # Return - set("${result}" "${${result}}" PARENT_SCOPE) -endfunction() - -# Setup Include Directories -function(setup_include_dirs compiler target result) - # Get Full Compiler - set(full_compiler "${target}-${compiler}") - - # Get Include Directories - set(include_dirs "") - get_include_dirs("${target}" "${full_compiler}" include_dirs) - - # Loop - set(flags "") - foreach(include_dir IN LISTS include_dirs) - set(flags "${flags} -isystem ${include_dir}") - endforeach() - - # Return - set("${result}" "${${result}} ${flags}" PARENT_SCOPE) -endfunction() - # Setup Toolchain macro(setup_toolchain target) # Use ARM Cross-Compiler - set(CMAKE_C_COMPILER "clang") - set(CMAKE_C_COMPILER_TARGET "${target}") - set(CMAKE_CXX_COMPILER "clang++") - set(CMAKE_CXX_COMPILER_TARGET "${target}") + set(CMAKE_C_COMPILER "${target}-gcc") + set(CMAKE_CXX_COMPILER "${target}-g++") set(CMAKE_FIND_ROOT_PATH "/usr/${target}" "/usr/lib/${target}") - # Flags - string(CONCAT NEW_FLAGS - "-nostdinc " - "-nostdinc++ " - "-Wno-unused-command-line-argument" - ) - set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} ${NEW_FLAGS}") - set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} ${NEW_FLAGS}") - # Include Directories - setup_include_dirs("gcc" "${target}" CMAKE_C_FLAGS_INIT) - setup_include_dirs("g++" "${target}" CMAKE_CXX_FLAGS_INIT) # Extra set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) endmacro() diff --git a/docs/BUILDING.md b/docs/BUILDING.md index 0a1b7986..db80f019 100644 --- a/docs/BUILDING.md +++ b/docs/BUILDING.md @@ -1,23 +1,5 @@ # Building -## Build Options -* ``MCPI_BUILD_MODE`` - * ``arm``: Only Build ARM Components - * ``native``: Only Build Native Components - * ``both`` (Default): Build Both ARM And Native Components For ARM -* ``MCPI_SERVER_MODE`` - * ``ON``: Enable Server Mode - * ``OFF`` (Default): Disable Server Mode -* ``MCPI_HEADLESS_MODE`` - * ``ON`` (Default In Server Mode): Enable Headless Mode - * ``OFF`` (Default In Client Mode): Disable Headless Mode -* ``MCPI_USE_MEDIA_LAYER_PROXY`` - * ``ON``: Enable The Media Layer Proxy - * ``OFF`` (Default): Disable The Media Layer Proxy -* ``MCPI_OPEN_SOURCE_ONLY`` - * ``ON``: Only Install Open-Source Code (Will Result In Broken Install) - * ``OFF`` (Default): Install All Code - ## Build Dependencies * Common * ARM Compiler @@ -40,37 +22,13 @@ * OpenAL * Zenity -## Two-Step Build -Use this when the host architecture is not ARM. - +## Instructions ```sh -# Create Build Directory -mkdir build && cd build - -# Build ARM Components -mkdir arm && cd arm -cmake -DCMAKE_TOOLCHAIN_FILE=../../cmake/armhf-toolchain.cmake -DMCPI_BUILD_MODE=arm ../.. -make -j$(nproc) && sudo make install -cd ../ - -# Build Native Components -mkdir native && cd native -cmake -DMCPI_BUILD_MODE=native ../.. -make -j$(nproc) && sudo make install -cd ../../ +./scripts/build.sh ``` -This will most likely not compile by itself. You will want to enable either server mode or the Media Layer Proxy. - -## One-Step Build -Use this when the host architecture is ARM. - +### Custom CMake Arguments ```sh -# Create Build Directory -mkdir build && cd build - -# Build -cmake .. -make -j$(nproc) && sudo make install -cd ../ +./scripts/setup.sh +./scripts/build.sh ``` diff --git a/media-layer/core/src/media.c b/media-layer/core/src/media.c index e892bdb6..1dd3510f 100644 --- a/media-layer/core/src/media.c +++ b/media-layer/core/src/media.c @@ -146,7 +146,7 @@ static SDLMod glfw_modifier_to_sdl_modifier(int mods) { } // Pass Key Presses To SDL -static void glfw_key(__attribute__((unused)) GLFWwindow *window, int key, int scancode, int action, __attribute__((unused)) int mods) { +static void glfw_key(__attribute__((unused)) GLFWwindow *window, int key, int scancode, int action, int mods) { if (is_interactable) { SDL_Event event; int up = action == GLFW_RELEASE; diff --git a/mods/src/home/home.c b/mods/src/home/home.c index 8333535e..cc72a37f 100644 --- a/mods/src/home/home.c +++ b/mods/src/home/home.c @@ -1,3 +1,5 @@ +#include + #include #include @@ -59,6 +61,8 @@ void init_home() { unsigned char nop_patch[4] = {0x00, 0xf0, 0x20, 0xe3}; // "nop" patch((void *) 0xe0ac, nop_patch); char *binary_directory = get_binary_directory(); - chdir(binary_directory); + if (chdir(binary_directory) != 0) { + ERR("Unable To Change Directory: %s", strerror(errno)); + } free(binary_directory); } diff --git a/scripts/build.sh b/scripts/build.sh index ea6a6ff2..4e32e09a 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -2,46 +2,27 @@ set -e -# ARM Toolchain File -ARM_TOOLCHAIN_FILE="$(pwd)/cmake/armhf-toolchain.cmake" - # Build build() { - # Find Toolchain - local toolchain_file="$(pwd)/cmake/$2-toolchain.cmake" - if [ ! -f "${toolchain_file}" ]; then - echo "Invalid Architecture: $2" > /dev/stderr - exit 1 + # Use Build Dir + if [ ! -d "build/${MODE}-${ARCH}" ]; then + ./scripts/setup.sh "${MODE}" "${ARCH}" fi - - # Create Build Dir - rm -rf "build/$1-$2" - mkdir -p "build/$1-$2" - cd "build/$1-$2" + cd "build/${MODE}-${ARCH}" # Create Prefix - local prefix="$(cd ../../; pwd)/out/$1-$2" + local prefix="$(cd ../../; pwd)/out/${MODE}-${ARCH}" rm -rf "${prefix}" mkdir -p "${prefix}" - # Prepare - local extra_arg='-DMCPI_USE_MEDIA_LAYER_PROXY=ON' - if [ "$1" = "server" ]; then - extra_arg='-DMCPI_SERVER_MODE=ON' - fi - # Build ARM Components - mkdir arm cd arm - cmake -DCMAKE_TOOLCHAIN_FILE="${ARM_TOOLCHAIN_FILE}" -DMCPI_BUILD_MODE=arm "${extra_arg}" ../../.. make -j$(nproc) make install DESTDIR="${prefix}" cd ../ # Build Native Components - mkdir native cd native - cmake -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}" -DMCPI_BUILD_MODE=native "${extra_arg}" ../../.. make -j$(nproc) make install DESTDIR="${prefix}" cd ../ @@ -52,24 +33,18 @@ build() { # Build For ARM armhf_build() { - # Create Build Dir - rm -rf "build/$1-armhf" - mkdir -p "build/$1-armhf" - cd "build/$1-armhf" + # Use Build Dir + if [ ! -d "build/${MODE}-armhf" ]; then + ./scripts/setup.sh "${MODE}" armhf + fi + cd "build/${MODE}-armhf" # Create Prefix - local prefix="$(cd ../../; pwd)/out/$1-armhf" + local prefix="$(cd ../../; pwd)/out/${MODE}-armhf" rm -rf "${prefix}" mkdir -p "${prefix}" - # Prepare - local server_mode='OFF' - if [ "$1" = "server" ]; then - server_mode='ON' - fi - # Build All Components - cmake -DCMAKE_TOOLCHAIN_FILE="${ARM_TOOLCHAIN_FILE}" -DMCPI_BUILD_MODE=both -DMCPI_SERVER_MODE="${server_mode}" ../.. make -j$(nproc) make install DESTDIR="${prefix}" @@ -77,15 +52,14 @@ armhf_build() { cd ../../ } -# Verify Mode -if [ "$1" != "client" ] && [ "$1" != "server" ]; then - echo "Invalid Mode: $1" > /dev/stderr - exit 1 -fi +# Variables +MODE="$1" +ARCH="$2" +shift 2 # Build -if [ "$2" = "armhf" ]; then - armhf_build "$1" +if [ "${ARCH}" = "armhf" ]; then + armhf_build "${MODE}" else - build "$1" "$2" + build "${MODE}" "${ARCH}" fi diff --git a/scripts/install-dependencies.sh b/scripts/install-dependencies.sh index 38336b90..01392f14 100755 --- a/scripts/install-dependencies.sh +++ b/scripts/install-dependencies.sh @@ -24,8 +24,6 @@ sudo apt-get install --no-install-recommends -y \ lsb-release \ dpkg-dev \ git \ - clang \ - lld \ cmake \ make \ libglfw3 libglfw3-dev \ diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100755 index 00000000..6b7f5b27 --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,80 @@ +#!/bin/sh + +set -e + +# ARM Toolchain File +ARM_TOOLCHAIN_FILE="$(pwd)/cmake/armhf-toolchain.cmake" + +# Setup +setup() { + # Find Toolchain + local toolchain_file="$(pwd)/cmake/${ARCH}-toolchain.cmake" + if [ ! -f "${toolchain_file}" ]; then + echo "Invalid Architecture: ${ARCH}" > /dev/stderr + exit 1 + fi + + # Create Build Dir + rm -rf "build/${MODE}-${ARCH}" + mkdir -p "build/${MODE}-${ARCH}" + cd "build/${MODE}-${ARCH}" + + # Prepare + local extra_arg='-DMCPI_USE_MEDIA_LAYER_PROXY=ON' + if [ "${MODE}" = "server" ]; then + extra_arg='-DMCPI_SERVER_MODE=ON' + fi + + # Build ARM Components + mkdir arm + cd arm + cmake -DCMAKE_TOOLCHAIN_FILE="${ARM_TOOLCHAIN_FILE}" -DMCPI_BUILD_MODE=arm "${extra_arg}" "$@" ../../.. + cd ../ + + # Build Native Components + mkdir native + cd native + cmake -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}" -DMCPI_BUILD_MODE=native "${extra_arg}" "$@" ../../.. + cd ../ + + # Exit + cd ../../ +} + +# Setup For ARM +armhf_setup() { + # Create Build Dir + rm -rf "build/${MODE}-armhf" + mkdir -p "build/${MODE}-armhf" + cd "build/${MODE}-armhf" + + # Prepare + local server_mode='OFF' + if [ "${MODE}" = "server" ]; then + server_mode='ON' + fi + + # Build All Components + cmake -DCMAKE_TOOLCHAIN_FILE="${ARM_TOOLCHAIN_FILE}" -DMCPI_BUILD_MODE=both -DMCPI_SERVER_MODE="${server_mode}" "$@" ../.. + + # Exit + cd ../../ +} + +# Variables +MODE="$1" +ARCH="$2" +shift 2 + +# Verify Mode +if [ "${MODE}" != "client" ] && [ "${MODE}" != "server" ]; then + echo "Invalid Mode: ${MODE}" > /dev/stderr + exit 1 +fi + +# Setup +if [ "${ARCH}" = "armhf" ]; then + armhf_setup "$@" +else + setup "$@" +fi diff --git a/symbols/include/symbols/minecraft.h b/symbols/include/symbols/minecraft.h index ca4674a4..334a4eb5 100644 --- a/symbols/include/symbols/minecraft.h +++ b/symbols/include/symbols/minecraft.h @@ -63,7 +63,7 @@ struct AABB { }; struct LevelSettings { - unsigned long seed; + int32_t seed; int32_t game_type; }; @@ -637,7 +637,7 @@ static AppPlatform_readAssetFile_t AppPlatform_readAssetFile = (AppPlatform_read // Minecraft -typedef void (*Minecraft_selectLevel_t)(unsigned char *minecraft, std::string const& level_dir, std::string const& level_name, LevelSettings const& vsettings); +typedef void (*Minecraft_selectLevel_t)(unsigned char *minecraft, std::string const& level_dir, std::string const& level_name, LevelSettings const& settings); static Minecraft_selectLevel_t Minecraft_selectLevel = (Minecraft_selectLevel_t) 0x16f38; // ExternalFileLevelStorageSource