Skip to content

Commit

Permalink
feat: add cross-compiler for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
moesio-f committed Dec 1, 2023
1 parent f2f40f7 commit ad8370e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Mkfile.old
dkms.conf

# Build directory
build/
build*/

# CCLS cache
.ccls-cache/
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ compile:
debug:
@echo "[Makefile] Running debug target..."
@./build/sdl2_debug

# Compile for windows
compile-windows:
@echo "[Makefile] Compile for Windows..."
@cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=`pwd`/toolchain/win-mingw.cmake -B build-win -S src
@cmake --build build-win
8 changes: 7 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ project(ComputerGraphics
# TODO: potentially add bundled SDL2 version
# in an `external` directory (maybe submodules)
find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
if (WIN32)
find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)
endif (WIN32)

# Add subdirectories
add_subdirectory(core)
Expand All @@ -20,7 +23,10 @@ add_executable(sdl2_debug WIN32 debug.c)
add_executable(render WIN32 render.c)

# Obtain libraries
set(SHARED_LIBRARIES SDL2::SDL2)
if (WIN32)
set(SHARED_LIBRARIES mingw32 SDL2::SDL2main)
endif (WIN32)
set(SHARED_LIBRARIES ${SHARED_LIBRARIES} SDL2::SDL2)
find_library(math m)
if(math)
set(SHARED_LIBRARIES ${SHARED_LIBRARIES} ${math})
Expand Down
17 changes: 17 additions & 0 deletions toolchain/win-mingw.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# the name of the target operating system
set(CMAKE_SYSTEM_NAME WIN32)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)

# where is the target environment located
set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32/)

# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

0 comments on commit ad8370e

Please sign in to comment.