Skip to content

Commit

Permalink
Add CMake build for the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan-MV committed Jul 18, 2024
1 parent 99be70c commit 6cfc0f1
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 18 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
name: CI
name: CI Extension

on:
workflow_dispatch:
push:
paths:
- 'ext/rubyraylib/**'
- 'third_party/**'
- '.github/workflows/ci.yml'
- '!ext/rubyraylib/main.cpp'
- '!src/main.hpp'
pull_request:
paths:
- 'ext/rubyraylib/**'
- 'third_party/**'
- '.github/workflows/ci.yml'
- '!ext/rubyraylib/main.cpp'
- '!ext/rubyraylib/main.hpp'

jobs:
build:
Expand All @@ -27,8 +40,7 @@ jobs:
with:
submodules: 'recursive'

- name: Set up Ruby
uses: ruby/setup-ruby@v1
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
Expand Down
64 changes: 64 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Linux Binary

on:
workflow_dispatch:
push:
paths:
- 'ext/rubyraylib/**'
- 'CMakeLists.txt'
- 'third_party/**'
- '.github/workflows/linux.yml'
- '!ext/rubyraylib/rubyraylib.cpp'
- '!ext/rubyraylib/rubyraylib.hpp'
pull_request:
paths:
- 'ext/rubyraylib/**'
- 'CMakeLists.txt'
- 'third_party/**'
- '.github/workflows/linux.yml'
- '!ext/rubyraylib/rubyraylib.cpp'
- '!ext/rubyraylib/rubyraylib.hpp'

jobs:
build-linux:
name: linux-x86_64
runs-on: ubuntu-latest
env:
OS: linux
ARCH: x86_64

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
submodules: 'recursive'

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3

- name: Update package database
run: sudo apt update -qq

- name: Install required packages
run: |
sudo apt-get install gcc-multilib cmake ninja-build
sudo apt-get install -y --no-install-recommends libglfw3 libglfw3-dev libx11-dev libxcursor-dev libxrandr-dev libxinerama-dev libxi-dev libxext-dev libxfixes-dev libwayland-dev libxkbcommon-dev
- name: Configure CMake
run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE:STRING=Release

- name: Build
run: cmake --build build --config Release --target all

- name: Strip Shared Libraries
run: strip --strip-unneeded ${{ github.workspace }}/build/libruby.so

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: raylib-cruby-${{ env.ARCH }}
path: |
${{ github.workspace }}/build/raylib-cruby-${{ env.ARCH }}
${{ github.workspace }}/build/*.so
retention-days: 90
54 changes: 54 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Windows Binary

on:
workflow_dispatch:
push:
paths:
- 'ext/rubyraylib/**'
- 'CMakeLists.txt'
- 'third_party/**'
- '.github/workflows/windows.yml'
- '!ext/rubyraylib/rubyraylib.cpp'
- '!ext/rubyraylib/rubyraylib.hpp'
pull_request:
paths:
- 'ext/rubyraylib/**'
- 'CMakeLists.txt'
- 'third_party/**'
- '.github/workflows/windows.yml'
- '!ext/rubyraylib/rubyraylib.cpp'
- '!ext/rubyraylib/rubyraylib.hpp'

jobs:
build-windows:
name: windows-x86_64
runs-on: windows-latest
env:
OS: windows
ARCH: x86_64

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
submodules: 'recursive'

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3

- name: Configure CMake
run: cmake -B build -DCMAKE_BUILD_TYPE:STRING=Release

- name: Build CMake
run: cmake --build build --config Release --target all

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: raylib-cruby-${{ env.ARCH }}
path: |
${{ github.workspace }}/build/raylib-cruby-${{ env.ARCH }}
${{ github.workspace }}/build/*.dll
retention-days: 90

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
mkmf.log

/.config/
/build/
5 changes: 3 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[submodule "third_party/raygui"]
path = third_party/raygui
url = https://github.com/Nathan-MV/raygui.git
branch = raylibruby
branch = raylibruby
[submodule "third_party/reasings"]
path = third_party/reasings
url = https://github.com/raylib-extras/reasings
url = https://github.com/Nathan-MV/reasings.git
branch = raylibruby
76 changes: 76 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.20)

# Project Settings
project(Game VERSION 0.1.0)

# Set C++ standard and required flag
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set Rpath
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "\${ORIGIN}")

# Set directories
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/rubyraylib")
set(THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party)

# set(OPENGL_VERSION "4.3")
# set(PLATFORM RGFW CACHE STRING "" FORCE)
# set(PLATFORM SDL CACHE STRING "" FORCE)

# Set up third-party libraries
set(LIBRARIES
raylib
raygui
reasings
)

foreach(LIB ${LIBRARIES})
set(LIB_DIR ${THIRD_PARTY_DIR}/${LIB})
set(LIB_BUILD_DIR ${CMAKE_BINARY_DIR}/third_party/${LIB})

add_subdirectory(${LIB_DIR})

if(${LIB} STREQUAL "raylib")
include_directories(${LIB_BUILD_DIR}/raylib/include)
target_compile_definitions(${LIB} PRIVATE PLATFORM_DESKTOP_RGFW)
else()
include_directories(${LIB_BUILD_DIR}/include)
endif()

if(${LIB} STREQUAL "raygui")
target_compile_definitions(${LIB} PRIVATE RAYGUI_IMPLEMENTATION)
endif()
endforeach()

# Executable definition
file(GLOB SRC_FILES ${SRC_DIR}/*.cpp)
add_executable(game ${SRC_FILES})
set_target_properties(game PROPERTIES OUTPUT_NAME "raylib-cruby-${CMAKE_SYSTEM_PROCESSOR}")

add_dependencies(game ${LIBRARIES})

# Find Ruby
find_package(Ruby 3.3 REQUIRED)

# Set up include directories
target_include_directories(game
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${RUBY_INCLUDE_PATH}
)

# Set up link libraries
target_link_libraries(game PRIVATE ${LIBRARIES} ${RUBY_LIBRARY})

# Copy Ruby library to output directory
add_custom_command(TARGET game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${RUBY_LIBRARY}
$<TARGET_FILE_DIR:game>
)
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ git clone https://github.com/Nathan-MV/raylib-cruby-extension
cd raylib-cruby-extension
git submodule update --init --recursive
```
Note: If you encounter an "undefined symbol: RayLoadImage" error after compiling, it means Raylib was not checked out to the origin/rubyraylib branch correctly (Still wondering how to fix it).

- Install Dependencies
```
Expand All @@ -25,24 +24,31 @@ bundle install --with compile

- Raylib Dependencies (Ubuntu)
```
sudo apt install libasound2-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libxcursor-dev libxinerama-dev libwayland-dev libxkbcommon-dev
sudo apt install cmake ninja-build ruby-devel libasound2-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libxcursor-dev libxinerama-dev libwayland-dev libxkbcommon-dev
```

- Raylib Dependencies (Fedora)
```
sudo dnf install alsa-lib-devel mesa-libGL-devel libX11-devel libXrandr-devel libXi-devel libXcursor-devel libXinerama-devel libatomic wayland-devel libxkbcommon-devel wayland-protocols-devel
sudo dnf install cmake ninja-build ruby-devel alsa-lib-devel mesa-libGL-devel libX11-devel libXrandr-devel libXi-devel libXcursor-devel libXinerama-devel libatomic wayland-devel libxkbcommon-devel wayland-protocols-devel
```

- Raylib Dependencies (Arch Linux)
```
sudo pacman -S alsa-lib mesa libx11 libxrandr libxi libxcursor libxinerama
sudo pacman -S cmake ninja ruby alsa-lib mesa libx11 libxrandr libxi libxcursor libxinerama
```

- Compile
- Compile (Ruby Extension)
```
rake
```

- Compile (Binary)
Note: Require CMake, Ninja and Ruby 3.3 (Currently the Windows binary does not work, help is appreciated)
```
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE:STRING=Release
cmake --build build --config Release --target all
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Nathan-MV/raylib-cruby-extension. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Nathan-MV/raylib-cruby-extension/blob/master/CODE_OF_CONDUCT.md).
Expand Down
14 changes: 10 additions & 4 deletions ext/rubyraylib/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
$INCFLAGS << " -I#{raylib_dir} -I#{raygui_dir} -I#{reasings_dir}"
$LDFLAGS << " -L#{raylib_dir} -L#{raygui_dir} -L#{reasings_dir}"

# Link against the static libraries
# Specify the static libraries to link against
raylib_lib = File.expand_path("#{raylib_dir}/libraylib.a")
raygui_lib = File.expand_path("#{raygui_dir}/libraygui.a")
reasings_lib = File.expand_path("#{reasings_dir}/libreasings.a")

$LDFLAGS << " #{raylib_lib} #{raygui_lib} #{reasings_lib}"

if /linux/i =~ RUBY_PLATFORM
$LDFLAGS << " -lraylib -lraygui -lreasings -lGL -lm -lpthread -ldl -lrt -lX11 -lXrandr"
$LDFLAGS << " -lGL -lm -lpthread -ldl -lrt -lX11 -lXrandr"
elsif /darwin/i =~ RUBY_PLATFORM
$LDFLAGS << " -lraylib -lraygui -lreasings -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL"
$LDFLAGS << " -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL"
else
$LDFLAGS << " -lraylib -lraygui -lreasings -lgdi32 -lwinmm"
$LDFLAGS << " -lgdi32 -lwinmm"
end

# Debugging information
Expand Down
2 changes: 2 additions & 0 deletions ext/rubyraylib/ruby_values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define RUBY_VALUES_H

#include <ruby.h>
#include <ruby/encoding.h>
#include <ruby/version.h>
#include <typeinfo>
#include <type_traits>
#include "vec2.hpp"
Expand Down
60 changes: 60 additions & 0 deletions ext/rubyraylib/rubyraylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,63 @@ RUBY_FUNC_EXPORTED void Init_rubyraylib(void) {
// Custom
initializeTextureAtlas();
}

static void initializeRubyInterpreter() {
int argc = 0;
char **argv = nullptr;

ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
rb_enc_set_default_internal(rb_enc_from_encoding(rb_utf8_encoding()));
rb_enc_set_default_external(rb_enc_from_encoding(rb_utf8_encoding()));
}

static void initializeRubyArguments() {
std::vector<const char *> args{"RUBY", "-e ", "--yjit", "--parser=prism"};
void *rubyNode = ruby_options(args.size(), const_cast<char **>(args.data()));
int executionState = 0;
bool success = ruby_executable_node(rubyNode, &executionState);
if (success) {
ruby_exec_node(rubyNode);
} else {
std::cerr << "RUBY: Failed to execute ruby arguments" << std::endl;
ruby_cleanup(executionState);
}
}

static VALUE script_run(VALUE) {
rb_ary_push(rb_gv_get("$LOAD_PATH"), rb_str_new_literal("./script"));
rb_load(rb_str_new_literal("./main.rb"), 0);
return Qnil;
}

static VALUE script_rescue(VALUE, VALUE exc) {
VALUE backtrace = rb_ary_to_ary(rb_funcall(exc, rb_intern("backtrace"), 0));
VALUE message = rb_str_to_str(rb_funcall(exc, rb_intern("message"), 0));
std::cerr << StringValueCStr(message) << std::endl;
for (long i = 0; i < RARRAY_LEN(backtrace); ++i) {
VALUE lp = rb_str_to_str(rb_ary_entry(backtrace, i));
std::cerr << StringValueCStr(lp) << std::endl;
}
return Qnil;
}

int main() {
// Initialize components
initializeRubyInterpreter();
initializeRubyArguments();
Init_rubyraylib();

try {
rb_rescue2(script_run, Qnil, script_rescue, Qnil, rb_eException,
static_cast<VALUE>(0));
} catch (std::exception &e) {
std::string msg = "Error:\n";
std::cerr << msg + e.what() << std::endl;
}
ruby_finalize();

// Return success or failure based on script evaluation
return Qnil;
}
Loading

0 comments on commit 6cfc0f1

Please sign in to comment.