embedded mono runtime

This commit is contained in:
Timerix 2024-08-09 23:02:20 +03:00
parent 0aefa70fb8
commit 2710e5fc9d
7 changed files with 127 additions and 17 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ temp/
logs/
log/
*.log
mono/

View File

@ -7,6 +7,7 @@
"DEBUG=1"
],
"includePath": [
"dependencies/include",
"dependencies/include/SDL2",
"dependencies/imgui",
"${default}"

View File

@ -12,12 +12,37 @@ git clone --recurse-submodules https://timerix.ddns.net:3322/Timerix/ougge.git
- On **Windows** download pre-built dll's from github releases and put them into `dependencies/precompiled/`.
4. Symlink SDL headers directory to `dependencies/include`.
```sh
cd ../ougge
ln -s SDL2_HEADERS_DIRECTORY_ABSOLUTE_PATH -T dependencies/include/SDL2
```
Location of the headers can be found by `pkg-config --cflags --libs sdl2`.
Mingw installs SDL2 headers to `/mingw64/include/SDL2`.
5. Compile the program
5. Install [mono](https://github.com/mono/mono).
1. Download and extract [sources](https://download.mono-project.com/sources/mono/index.html)
```sh
mkdir mono
cd mono
wget https://download.mono-project.com/sources/mono/mono-6.12.0.199.tar.xz
tar xJf mono-6.12.0.199.tar.xz
mv mono-6.12.0.199/* ./
rm -r mono-6.12.0.199
rm mono-6.12.0.199.tar.xz
```
2. Install `libz autoconf automake libtool gettext cmake python3 curl` and
3. Build mono. If something doesn't work, read [documentation](https://www.mono-project.com/docs/compiling-mono/)
```sh
mkdir -p mono_prefix
./autogen.sh --prefix=$(realpath mono_prefix) --disable-boehm
make get-monolite-latest
make -j8
make install
```
3. Install `patchelf`
4. Install mono files in project.
```sh
cd ..
cbuild get_mono_files_from=mono/mono_prefix
```
7. Compile the program
```sh
cbuild build_exec_dbg
```

View File

@ -7,13 +7,17 @@ DEP_POST_BUILD_COMMAND=''
DEP_CLEAN_COMMAND=''
# won't be copied to project $OUTDIR
DEP_STATIC_OUT_FILES=$(find dependencies/precompiled -name '*.a' | sed 's,dependencies/precompiled/,,')
PRESERVE_OUT_DIRECTORY_STRUCTURE=true
# will be copied tp project $OUTDIR
case $OS in
WINDOWS)
DEP_DYNAMIC_OUT_FILES=$(find dependencies/precompiled -name '*.dll' | sed 's,dependencies/precompiled/,,')
DEP_DYNAMIC_OUT_FILES="$(find dependencies/precompiled -name '*.dll' | sed 's,dependencies/precompiled/,,')"
DEP_OTHER_OUT_FILES=""
;;
LINUX)
DEP_DYNAMIC_OUT_FILES=$(find dependencies/precompiled -name '*.so' | sed 's,dependencies/precompiled/,,')
DEP_DYNAMIC_OUT_FILES="$(find dependencies/precompiled -name '*.so' | sed 's,dependencies/precompiled/,,')
$(find dependencies/precompiled -name '*.so.*' | sed 's,dependencies/precompiled/,,')"
DEP_OTHER_OUT_FILES="mono-libs/mscorlib.dll mono-libs/config.xml"
;;
*)
error "operating system $OS has no configuration variants"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
CBUILD_VERSION=2.1.2
CBUILD_VERSION=2.1.4
CONFIG_VERSION=1
PROJECT="ougge"
@ -28,7 +28,7 @@ OUTDIR="bin"
STATIC_LIB_FILE="lib$PROJECT.a"
# header include directories
INCLUDE="-I./dependencies/imgui -I./dependencies/include/SDL2"
INCLUDE="-I./dependencies/imgui -I./dependencies/include -I./dependencies/include/SDL2"
# OS-specific options
case "$OS" in
@ -58,7 +58,7 @@ case "$TASK" in
# -fprofile-use enables compiler to use profiling info files to optimize executable
# -fprofile-prefix-path sets path where profiling info about objects are be saved
# -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
C_ARGS="-rdynamic -O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=
@ -67,16 +67,16 @@ case "$TASK" in
;;
# creates executable with debug info and no optimizations
build_exec_dbg)
C_ARGS="-O0 -g3"
C_ARGS="-rdynamic -O0 -g3"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS -Wl,-rpath=mono-libs"
PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
POST_TASK_SCRIPT=
;;
# creates shared library
build_shared_lib)
C_ARGS="-O2 -fpic -flto -shared"
C_ARGS="-rdynamic -O2 -fpic -flto -shared"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS -Wl,-soname,$SHARED_LIB_FILE"
PRE_TASK_SCRIPT=
@ -85,7 +85,7 @@ case "$TASK" in
;;
# creates shared library with debug symbols and no optimizations
build_shared_lib_dbg)
C_ARGS="-O0 -g3 -fpic -shared"
C_ARGS="-rdynamic -O0 -g3 -fpic -shared"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS -Wl,-soname,$SHARED_LIB_FILE"
PRE_TASK_SCRIPT=
@ -94,7 +94,7 @@ case "$TASK" in
;;
# creates static library
build_static_lib)
C_ARGS="-O2 -fpic -fdata-sections -ffunction-sections"
C_ARGS="-rdynamic -O2 -fpic -fdata-sections -ffunction-sections"
CPP_ARGS="$C_ARGS"
PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh
@ -102,7 +102,7 @@ case "$TASK" in
;;
# creates static library with debug symbols and no optimizations
build_static_lib_dbg)
C_ARGS="-O0 -g3"
C_ARGS="-rdynamic -O0 -g3"
CPP_ARGS="$C_ARGS"
PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh
@ -126,7 +126,7 @@ case "$TASK" in
# -pg adds code to executable, that generates file containing function call info (gmon.out)
# -fprofile-generate generates executable with profiling code
# -fprofile-prefix-path sets path where profiling info about objects will be saved
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects"
C_ARGS="-rdynamic -O2 -flto=auto -fuse-linker-plugin -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
@ -139,7 +139,7 @@ case "$TASK" in
gprof)
OUTDIR="$OUTDIR/gprof"
# -pg adds code to executable, that generates file containing function call info (gmon.out)
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg"
C_ARGS="-rdynamic -O2 -flto=auto -fuse-linker-plugin -pg"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
@ -153,7 +153,7 @@ case "$TASK" in
callgrind)
OUTDIR="$OUTDIR/callgrind"
# -pg adds code to executable, that generates file containing function call info (gmon.out)
C_ARGS="-O2 -flto=auto -fuse-linker-plugin"
C_ARGS="-rdynamic -O2 -flto=auto -fuse-linker-plugin"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
@ -163,7 +163,7 @@ case "$TASK" in
# compiles executable with sanitizers and executes it to find errors and warnings
sanitize)
OUTDIR="$OUTDIR/sanitize"
C_ARGS="-O0 -g3 -fsanitize=undefined,address"
C_ARGS="-rdynamic -O0 -g3 -fsanitize=undefined,address"
CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
@ -176,6 +176,9 @@ case "$TASK" in
rebuild_dependencies)
TASK_SCRIPT=cbuild/default_tasks/rebuild_dependencies.sh
;;
get_mono_files_from)
TASK_SCRIPT=tasks/get_mono_files_from.sh
;;
# deletes generated files
clean)
TASK_SCRIPT=cbuild/default_tasks/clean.sh

View File

@ -5,6 +5,9 @@
#include "Game/Scene.hpp"
#include "format.hpp"
#include "exceptions.hpp"
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/mono-config.h>
using namespace ougge;
@ -15,6 +18,26 @@ void update(f64 deltaTime){
int main(int argc, const char** argv){
try {
Resources::init();
mono_set_dirs("mono-libs", "mono-libs");
mono_set_assemblies_path("mono-libs");
mono_config_parse("mono-libs/config.xml");
std::cout<<"mono_root_dir: "<<mono_assembly_getrootdir()<<std::endl;
std::cout<<"mono_config_dir: "<<mono_get_config_dir()<<std::endl;
MonoDomain* domain = mono_jit_init("myapp");
if(!domain)
throw UsefulException("can't initialize mono domain");
std::cout<<"jit domain initialized"<<std::endl;
MonoAssembly* assembly = mono_domain_assembly_open(domain, "app.exe");
if(!assembly)
throw UsefulException("can't load assembly");
char* mono_argv[] = { "app.exe" };
int mono_argc = 1;
int retval = mono_jit_exec(domain, assembly, mono_argc, mono_argv);
if(retval)
throw new UsefulException(format("managed assembly returned error code: %i", retval));
mono_jit_cleanup(domain);
return 0;
GUI::MainWindow w;
w.open("ougge", update);
w.startUpdateLoop();

View File

@ -0,0 +1,53 @@
#!/usr/bin/env bash
mono_prefix_path="$TASK_ARGS"
if [ -z "$mono_prefix_path" ]; then
error "required argument 'mono_prefix_path'
Usage: cbuild get_mono_files_from=mono_prefix_path"
fi
myprint "${BLUE}mono prefix: ${WHITE}$mono_prefix_path"
# copy headers
rm -rf 'dependencies/include/mono'
mkdir -p 'dependencies/include'
cp -r "$mono_prefix_path/include/mono-2.0/mono" 'dependencies/include/'
rm -rf "dependencies/precompiled/mono-libs"
mkdir -p "dependencies/precompiled/mono-libs"
case "$OS" in
LINUX)
# copy mono dynamic libraries
shared_libs=$(find "$mono_prefix_path/lib" -maxdepth 1 \( -name '*.so*' -a -not -name '*-profiler*' \) -type f)
if [ -z "$shared_libs" ]; then
error "can't find mono shared libraries"
fi
for l in $shared_libs ; do
cp -v "$l" "dependencies/precompiled/mono-libs/"
done
so_v_libs=$(find "dependencies/precompiled/mono-libs" -name '*.so.*')
for l in $so_v_libs ; do
l_basename=$(basename "$l")
soname_without_version=$(safeprint "$l_basename" | sed 's,.so.*,.so,')
myprint "${BLUE}patching ${WHITE}$l_basename${BLUE}: replacing soname with ${CYAN}$soname_without_version"
patchelf --set-soname "$soname_without_version" "$l"
mv -v "$l" "dependencies/precompiled/mono-libs/$soname_without_version"
done
# copy mono c# libraries
managed_libraries="mscorlib.dll"
myprint "${BLUE}copying mono managed libraries"
for l in $managed_libraries ; do
cp -v "$mono_prefix_path/lib/mono/4.5/$l" "dependencies/precompiled/mono-libs/"
done
# copy config
myprint "${BLUE}copying mono config"
cp "$mono_prefix_path/etc/mono/config" "dependencies/precompiled/mono-libs/config.xml"
myprint "${BLUE}removing '$mono_libdir/' from mono config"
sed 's,$mono_libdir/,,g' -i "dependencies/precompiled/mono-libs/config.xml"
;;
*)
error "operating system $OS has no configuration variants"
;;
esac
myprint "${GREEN}mono files were copied successfully!"