Skip to content

Commit

Permalink
feat(build): cross-compile to windows support
Browse files Browse the repository at this point in the history
No compiling on Windows directly atm (maybe with mingw?). Also removed conan dep.
  • Loading branch information
billyb2 committed Jul 18, 2022
1 parent 68d82d7 commit 7eaeba8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build/
bots/*.wasm
bot_src/**/*.wasm
deps/wasmer/*
wasmer.dll
55 changes: 43 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)

set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-std=c99 -Wall -Wextra -pipe")

if(CMAKE_BUILD_TYPE MATCHES RELEASE)
Expand All @@ -12,7 +10,7 @@ if(CMAKE_BUILD_TYPE MATCHES DEBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
endif()

if (UNIX)
if (${CMAKE_SYSTEM_NAME} NOT STREQUAL "Windows")
# Use mold on Linux
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=mold")
Expand All @@ -22,14 +20,17 @@ project(main)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(OpenGL_GL_PREFERENCE GLVND)

find_package(raylib 4.0.0 QUIET) # QUIET or REQUIRED
if (NOT raylib_FOUND) # If there's none, fetch and build raylib
include(FetchContent)
FetchContent_Declare(
raylib
URL https://github.com/raysan5/raylib/archive/refs/tags/4.0.0.tar.gz
)
FetchContent_GetProperties(raylib)
include(FetchContent)
FetchContent_Declare(
raylib
URL https://github.com/raysan5/raylib/archive/refs/tags/4.0.0.tar.gz
URL_HASH SHA256=11f6087dc7bedf9efb3f69c0c872f637e421d914e5ecea99bbe7781f173dc38c
)
FetchContent_GetProperties(raylib)
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib)
Expand All @@ -38,14 +39,44 @@ if (NOT raylib_FOUND) # If there's none, fetch and build raylib
endif()
endif()

conan_basic_setup()
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(IS_WINDOWS "-windows")
set(WASMER_LIB "${CMAKE_CURRENT_SOURCE_DIR}/deps/wasmer/package/lib/wasmer.dll.a")
endif()

if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(WASMER_LIB "${CMAKE_CURRENT_SOURCE_DIR}/deps/wasmer/package/lib/libwasmer.a")

endif()


add_custom_command(
OUTPUT
${CMAKE_CURRENT_SOURCE_DIR}/deps/wasmer/package/
${WASMER_LIB}
COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/deps/wasmer/ build-capi${IS_WINDOWS}
COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/deps/wasmer/ package-capi
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/deps/wasmer/package/lib/wasmer.dll . || true
)

add_custom_target(
wasmer ALL
DEPENDS ${WASMER_LIB}
)

if(NOT WASMER_LIB)
message(FATAL_ERROR "wasmer library not found")
endif()

if(WASMER_LIB)
message("Wasmer lib located at ${WASMER_LIB}")
endif()

set(OpenGL_GL_PREFERENCE GLVND)

file(GLOB SRC_FILES src/*.c)

add_executable(${PROJECT_NAME} ${SRC_FILES})
target_link_libraries(${PROJECT_NAME} raylib ${CONAN_LIBS})
target_link_libraries(${PROJECT_NAME} raylib ${WASMER_LIB})

# Checks if OSX and links appropriate frameworks (Only required on MacOS)
if (APPLE)
Expand Down
12 changes: 0 additions & 12 deletions conan_linux_profile

This file was deleted.

5 changes: 0 additions & 5 deletions conanfile.txt

This file was deleted.

35 changes: 20 additions & 15 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
install_deps:
mkdir -p build/debug
conan install --profile conan_linux_profile --build=missing . -if build/debug -of build/debug/

install_deps_release:
mkdir -p build/release
conan install --profile conan_linux_profile --build=missing . -if build/release -of build/release/

configure:
cmake -Bbuild/debug -H. -GNinja -DCMAKE_BUILD_TYPE=Debug

configure_release:
cmake -Bbuild/release -H. -GNinja -DCMAKE_BUILD_TYPE=Release

configure_windows:
cmake -Bbuild/debug_win -H. -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=win.toolchain.cmake

configure_windows_release:
cmake -Bbuild/release_win -H. -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=win.toolchain.cmake

build:
ninja -Cbuild/debug -j4

build_release:
ninja -Cbuild/release -j4

build_windows:
ninja -Cbuild/debug_win -j4

build_windows_release:
ninja -Cbuild/release_win -j4

run: build
build/debug/bin/main
build/debug/main

run_release: build_release
build/release/bin/main
build/release/main

clean_debug:
rm -r build/debug
run_windows: build_windows
wine build/debug_win/main.exe

clean_release:
rm -r build/release
run_windows_release: build_windows_release
wine build/release_win/main.exe

clean: clean_debug clean_release
clean:
rm -r build/
22 changes: 20 additions & 2 deletions src/bots.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool check_wasm_function(const char* func_name, wasm_func_t* func) {

bool setup_bot(const char* wasm_file_path, WasmData* wasm_data, uint8_t player_index) {
// First, load the wasmfile
FILE* wasm_file = fopen(wasm_file_path, "r");
FILE* wasm_file = fopen(wasm_file_path, "rb");

if (wasm_file == NULL) {
printf("Opening wasm file failed");
Expand All @@ -63,7 +63,25 @@ bool setup_bot(const char* wasm_file_path, WasmData* wasm_data, uint8_t player_i

}

fread(wasm_byte_vec.data, wasm_file_size, 1, wasm_file);
size_t total_num_read_bytes = 0;
size_t amt_read = 0;

while (total_num_read_bytes < wasm_file_size) {
size_t amt_read = fread(&wasm_byte_vec.data[total_num_read_bytes], 1, wasm_file_size, wasm_file);
total_num_read_bytes += amt_read;

if (amt_read == 0) {
break;

}
}

if (total_num_read_bytes != wasm_byte_vec.size) {
printf("Error reading file: Tried to read %ld, instead read %ld\n", wasm_byte_vec.size, total_num_read_bytes);
return false;

}

fclose(wasm_file);

wasm_engine_t* wasm_engine = wasm_data->wasm_engine;
Expand Down
20 changes: 20 additions & 0 deletions win.toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

# cross compilers to use for C and C++
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 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
# set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

0 comments on commit 7eaeba8

Please sign in to comment.