From 7b6e27cd8413ca90f0cb54bd5e47a54b2c8e4557 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Mon, 19 Aug 2024 10:21:00 +0200 Subject: [PATCH] Setup a windows toolchain for use on linux (#66) Removed some files from the lua build that contained main functions and a REPL implementation. --- .github/workflows/cmake-multi-platform.yml | 26 +++++++++++---- CMakeLists.txt | 22 +++++++------ Makefile | 34 ++++++++++++-------- build_deps/toolchains/mingw-w64-x86_64.cmake | 16 +++++++++ lib/lua-5.3.5/CMakeLists.txt | 4 +-- src/time.c | 2 +- 6 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 build_deps/toolchains/mingw-w64-x86_64.cmake diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 6b15e24..d41d13c 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -25,37 +25,49 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] build_type: [Release, Debug] - c_compiler: [gcc, clang, cl] + c_compiler: [gcc, clang, cl, mingw-w64-gcc] include: - os: windows-latest c_compiler: cl cpp_compiler: cl - cmake: '-DSDLMIXER_VENDORED=ON -GNinja' + cmake_args: '-DSDLMIXER_VENDORED=ON' - os: ubuntu-latest c_compiler: gcc cpp_compiler: g++ - cmake: '-GNinja' + cmake_args: '-GNinja' - os: ubuntu-latest c_compiler: clang cpp_compiler: clang++ - cmake: '-GNinja' + cmake_args: '-GNinja' + - os: ubuntu-latest + c_compiler: mingw-w64-gcc + cpp_compiler: mingw-w64-g++ + cmake_args: > + -DSDL2MIXER_VENDORED=ON + -DSDL2TTF_VENDORED=ON + -DCMAKE_TOOLCHAIN_FILE=build_deps/toolchains/mingw-w64-x86_64.cmake + -GNinja - os: macos-latest c_compiler: gcc cpp_compiler: g++ - cmake: '-DSDLMIXER_VENDORED=ON -GNinja' + cmake_args: '-DSDLMIXER_VENDORED=ON -GNinja' - os: macos-latest c_compiler: clang cpp_compiler: clang++ - cmake: '-DSDLMIXER_VENDORED=ON -GNinja' + cmake_args: '-DSDLMIXER_VENDORED=ON -GNinja' exclude: - os: windows-latest c_compiler: gcc - os: windows-latest c_compiler: clang + - os: windows-latest + c_compiler: mingw-w64-gcc - os: ubuntu-latest c_compiler: cl - os: macos-latest c_compiler: cl + - os: macos-latest + c_compiler: mingw-w64-gcc steps: - uses: actions/checkout@v4 @@ -73,6 +85,7 @@ jobs: sudo apt-get update sudo apt-get -y install \ cmake \ + mingw-w64 \ libflac-dev \ libfluidsynth-dev \ libgme-dev \ @@ -119,6 +132,7 @@ jobs: -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -S ${{ github.workspace }} + ${{ matrix.cmake_args }} - name: Build # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). diff --git a/CMakeLists.txt b/CMakeLists.txt index ab124f3..0cfe203 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,12 +89,12 @@ ELSEIF ( WIN32 ) "${CMAKE_C_FLAGS_RELEASE} -mwindows" ) ENDIF () -IF ( GCC ) - SET ( - CMAKE_C_FLAGS_DEBUG - "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" - ) -ENDIF ( GCC ) +#IF ( GCC ) +#SET ( +#CMAKE_C_FLAGS_DEBUG +#"${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer" +#) +#ENDIF ( GCC ) if (STEAM) include_directories( @@ -110,7 +110,7 @@ set(SDL_TEST OFF) add_subdirectory(lib/SDL) set(SDL2_INCLUDE_DIR lib/SDL/include) set(SDL2_LIBRARY SDL2-static) -set(SDL2_MAIN SDL2main) +set(SDL2_MAIN_LIBRARY SDL2main) # Include SDL2_image/mixer/ttf set(BUILD_SHARED_LIBS OFF) @@ -284,7 +284,6 @@ set_source_files_properties(lib/sqlite3/sqlite3.c COMPILE_FLAGS -w) target_link_libraries(breakhack ${CMAKE_DL_LIBS} # Sqlite needs DL libs ${SDL2_LIBRARY} - ${SDL2_MAIN} ${SDL2_IMAGE_LIBRARY} ${SDL2_TTF_LIBRARY} ${SDL2_MIXER_LIBRARY} @@ -294,6 +293,12 @@ target_link_libraries(breakhack checksum ) +if (MSVC OR MINGW) + target_link_libraries(breakhack + ${SDL2_MAIN_LIBRARY} + ) +endif () + if (STEAM) target_link_libraries(breakhack steamworks_c_wrapper @@ -329,7 +334,6 @@ IF (CMOCKA_FOUND AND NOT OSX AND NOT CLANG) target_link_libraries(test_input ${CMOCKA_LIBRARY} ${SDL2_LIBRARY} - ${SDL2MAIN_LIBRARY} ) #set_target_properties(test_input PROPERTIES #LINK_FLAGS "-Wl,--wrap,keyboard_direction_press -Wl,--wrap,keyboard_press") diff --git a/Makefile b/Makefile index 9fdb75e..c1377ca 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,23 @@ all: - @make -sC build/debug + @cmake --build build/debug .PHONY: all release: - @make -sC build/release + @cmake --build build/release .PHONY: release +windows: + @cmake --build build/win-release +.PHONY: windows + clean: - @make -sC build/debug clean - @make -sC build/release clean + @cmake --build build/debug --target clean + @cmake --build build/release --target clean + @cmake --build build/win-release --target clean .PHONY: clean test: - @make -sC build/debug test + @cmake --build build/debug --target test .PHONY: test run: $(all) @@ -24,22 +29,25 @@ playtest: $(all) .PHONY: playtest lint: - @make -sC build/debug lint + @cmake --build build/debug --target lint .PHONY: lint package: - @make -sC build/release package + @cmake --build build/release --target package .PHONY: package setup: @mkdir -p build/debug @mkdir -p build/release - @cd build/debug/ && \ - cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=YES ../.. && \ - cd - - @cd build/release/ && \ - cmake -DCMAKE_BUILD_TYPE=Release ../.. && \ - cd - + @mkdir -p build/win-release + @cmake -B build/debug -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=YES -GNinja + @cmake -B build/release -DCMAKE_BUILD_TYPE=Release -GNinja + @cmake -B build/win-release \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_TOOLCHAIN_FILE=build_deps/toolchains/mingw-w64-x86_64.cmake \ + -DSDL2MIXER_VENDORED=ON \ + -DSDL2TTF_VENDORED=ON \ + -GNinja @ln -fs build/debug/compile_commands.json @echo "Setup complete" .PHONY: setup diff --git a/build_deps/toolchains/mingw-w64-x86_64.cmake b/build_deps/toolchains/mingw-w64-x86_64.cmake new file mode 100644 index 0000000..2f22e13 --- /dev/null +++ b/build_deps/toolchains/mingw-w64-x86_64.cmake @@ -0,0 +1,16 @@ +set(CMAKE_SYSTEM_NAME Windows) +set(TOOLCHAIN_PREFIX x86_64-w64-mingw32) + +# cross compilers to use for C, C++ and Fortran +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) +set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran) +set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) + +# target environment on the build host system +set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}) + +# modify default behavior of FIND_XXX() commands +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) diff --git a/lib/lua-5.3.5/CMakeLists.txt b/lib/lua-5.3.5/CMakeLists.txt index c678e91..2705a2e 100644 --- a/lib/lua-5.3.5/CMakeLists.txt +++ b/lib/lua-5.3.5/CMakeLists.txt @@ -8,7 +8,7 @@ set(lua_MAJOR_VERSION 5) set(lua_MINOR_VERSION 3) set(lua_PATCH_VERSION 5) -if (NOT MSVC) +if (NOT MSVC AND NOT MINGW) add_definitions(-DLUA_USE_POSIX) endif () @@ -42,8 +42,6 @@ add_library(lua STATIC src/ltable.c src/ltablib.c src/ltm.c - src/lua.c - src/luac.c src/lundump.c src/lutf8lib.c src/lvm.c diff --git a/src/time.c b/src/time.c index e96faca..6c99eae 100644 --- a/src/time.c +++ b/src/time.c @@ -24,7 +24,7 @@ #define SECONDS_PER_HOUR 3600 #define SECONDS_PER_MINUTE 60 -#ifdef _MSC_VER +#if defined(_MSC_VER) || defined(__MINGW32__) #define m_gmtime(time, obj) gmtime_s(obj, time) #else #define m_gmtime gmtime_r