Skip to content

Commit

Permalink
Merge pull request #29 from libriscv/dockerization
Browse files Browse the repository at this point in the history
Use a Docker container to compile C++ sources to a local .elf
  • Loading branch information
fwsGonzo authored Aug 3, 2024
2 parents ee8da80 + 2ac811e commit 02a6c18
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ program/test
.DS_Store
*.o
*.os
*.elf
bin/addons/godot_sandbox/bin/*
15 changes: 10 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ option(SANITIZE "Enable sanitizers" OFF)
option(STATIC_BUILD "Build statically" OFF)

set(SOURCES
src/gvar.cpp
src/cpp/resource_loader_cpp.cpp
src/cpp/resource_saver_cpp.cpp
src/cpp/script_cpp.cpp
src/cpp/script_language_cpp.cpp
src/elf/resource_loader_elf.cpp
src/elf/resource_saver_elf.cpp
src/elf/script_elf.cpp
src/elf/script_language_elf.cpp
src/gvar.cpp
src/register_types.cpp
src/resource_cpp.cpp
src/resource_loader_cpp.cpp
src/resource_elf.cpp
src/resource_loader_elf.cpp
src/sandbox.cpp
src/sandbox_functions.cpp
src/sandbox_project_settings.cpp
src/syscalls.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,5 @@ This project provides a dockerfile for compiling cpp code locally. For usage exa

```
cd program
docker run -v .:/usr/src -d ghcr.io/fwsgonzo/compiler build.sh
docker run --rm -v .:/usr/src -d ghcr.io/fwsgonzo/compiler myoutputfile.elf
```
22 changes: 14 additions & 8 deletions program/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ FROM ubuntu:24.04
# docker build -t riscv64-linux-gnu .
#
# To run the container:
# docker run -it -v .:/usr/src riscv64-linux-gnu
#
# Once inside run 'build.sh' to build the project
# ./build.sh
# docker run -it -v .:/usr/src riscv64-linux-gnu <my.elf>
#
# Install dependencies
RUN apt-get update && apt-get install -y \
g++-13-riscv64-linux-gnu \
ninja-build \
cmake
g++-14-riscv64-linux-gnu \
g++ git cmake

# Clone and build mold
WORKDIR /usr/mold
RUN git clone https://github.com/rui314/mold.git /usr/mold
RUN mkdir -p /usr/mold/build && cd /usr/mold/build && cmake .. && make -j8
RUN cd /usr/mold/build && make install
RUN rm -rf /usr/mold

# Enter the shared directory
WORKDIR /usr/src
ENTRYPOINT ["/bin/bash"]
# Copy API files
RUN mkdir -p /usr/api
COPY build.sh api/* /usr/api/
# Set the entrypoint to the build script
ENTRYPOINT "/usr/api/build.sh" "$0" "$@"
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions program/build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

riscv64-linux-gnu-g++-13 -static -O2 -std=gnu++23 test.cpp variant.cpp -o test
echo $@
riscv64-linux-gnu-g++-14 -static -O2 -std=gnu++23 -I/usr/api -fuse-ld=mold -Wl,--quick-exit -Wl,--execute-only /usr/api/*.cpp *.cpp -o $1
12 changes: 12 additions & 0 deletions src/cpp/resource_saver_cpp.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
#include "resource_saver_cpp.h"
#include "script_cpp.h"
#include <godot_cpp/classes/file_access.hpp>
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

Error ResourceFormatSaverCPP::_save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
CPPScript *script = Object::cast_to<CPPScript>(p_resource.ptr());
if (script != nullptr) {
Ref<FileAccess> handle = FileAccess::open(p_path, FileAccess::ModeFlags::WRITE);
if (handle.is_valid()) {
handle->store_string(script->_get_source_code());
// Get the absolute path without the file name
String path = handle->get_path_absolute().get_base_dir();
String fname = handle->get_path_absolute().get_file().get_basename() + String(".elf");

// Invoke docker to compile the file
godot::OS *OS = godot::OS::get_singleton();
PackedStringArray arguments = { "run", "--rm", "-v", path + String(":/usr/src"), "ghcr.io/libriscv/compiler", fname };
Array outp;
OS->execute("docker", arguments, outp);
// TODO: What now?
return Error::OK;
} else {
return Error::ERR_FILE_CANT_OPEN;
Expand Down
2 changes: 1 addition & 1 deletion src/syscalls.h

0 comments on commit 02a6c18

Please sign in to comment.