Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ jobs:
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1
- uses: ilammy/msvc-dev-cmd@v1
- name: Install dependencies
run: |
if [ "$RUNNER_OS" == "Linux" ]; then
sudo apt-get install libegl1-mesa-dev
fi
shell: bash
- run: dart pub get
working-directory: ./scripts/build_helpers
- run: dart pub get
working-directory: ./examples/realtime_example/dart
- run: dart pub get
working-directory: ./examples/simple_example_ffi
- name: Build Dart
run: dart ./scripts/build_helpers/bin/build_dart.dart -v
- uses: threeal/cmake-action@v1.3.0
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
dart-sdk/*
.build/*
build/*
artifacts/*
artifacts/*
depot_tools/*
out/*
26 changes: 25 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,37 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) RealTime Sample",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/out/x64/Debug/realtime_example/realtime_example",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/out/x64/Debug/realtime_example",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{

"name": "(WIN)RealTime Sample",
"type": "cppvsdbg",
"request": "launch",
//"preLaunchTask": "buildSimpelTest",
"program": "${workspaceFolder}/build/Debug/realtime_example.exe",
"program": "${workspaceFolder}/out/x64/Debug/realtime_example/realtime_example.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
Expand Down
77 changes: 76 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,80 @@
},
"dart.analysisExcludedFolders": [
"dart-sdk"
]
],
"files.associations": {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it's more of a personal setting than a project setting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"dart.analysisExcludedFolders": [
"dart-sdk"
]

This was hard too find without this you see millions from Warnings in VS Code. And not the Warnings of Code in this Lib. I don't how handle it.

"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp"
}
}
27 changes: 25 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.20)

project(DartSharedLibrary VERSION 0.1)

option(BUILD_SMAPLES "Build the Sampels" ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(DART_DLL_DIR "${PROJECT_SOURCE_DIR}/src")
if(NOT DART_DIR)
set(DART_DIR "${PROJECT_SOURCE_DIR}/dart-sdk/sdk")
endif()

set( OUTPUT_PATH_EXT "x86" )
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set( IS_64_BIT 1 )
set( OUTPUT_PATH_EXT "x64" )
endif ()

set(OUTPUT_DEBUG "${OUTPUT_PATH_EXT}/Debug")
set(OUTPUT_RELEASE "${OUTPUT_PATH_EXT}/Release")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/out/$<$<CONFIG:DEBUG>:${OUTPUT_DEBUG}>$<$<CONFIG:RELEASE>:${OUTPUT_RELEASE}>")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/out/${OUTPUT_PATH_EXT}/${CMAKE_BUILD_TYPE}")
set(LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/out/${OUTPUT_PATH_EXT}/${CMAKE_BUILD_TYPE}")


add_subdirectory(src)
add_subdirectory(examples)
if(BUILD_SMAPLES)
add_subdirectory(examples)
endif(BUILD_SMAPLES)

MESSAGE(STATUS "CMake output directory is ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
MESSAGE(STATUS "Building executables to ${EXECUTABLE_OUTPUT_PATH}")
MESSAGE(STATUS "Building libraries to ${LIBRARY_OUTPUT_DIRECTORY}")
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ This is an attempt / POC to build the Dart VM into a dynamic library, importable
## Eventual support

The hope is that the the dynamic library will eventually support the following targets:

* A "Fully Featured" .dll / .so that supports booting Dart in different configurations:

* Boot (or not) the service and kernel isolates
* Support Dart Source Compilation and / or Kernel Isolates
* JIT from source or .dil
Expand All @@ -24,11 +26,13 @@ Github Actions currently builds a Windows x64 `.dll`, A Linux x64 `.so`, and a m
## Building

### Prerequisets

You need:

* git
* Dart 3+
* C++ build tools for your platform (Visual Studio, XCode, gcc, etc)
* For Windows
* C++ build tools for your platform (Visual Studio, XCode, gcc, etc)
* For Windows
* 2019 16.61 with 10.0.20348.0 SDK don't forget install Debugger Tools
* 2022 17 with ? SDK don't forget install Debugger Tools
* 2017 15 with ? SDK don't forget install Debugger Tools
Expand All @@ -43,9 +47,10 @@ Optionally, I recommend installing [`depot_tools`](https://www.chromium.org/deve
> This will set up some environment variables that will be needed to build Dart properly.

The first step is to build a statically linkable verison of Dart. This requires that we download Dart, patch some of the Dart build files, and then run the actual build. Thankfully there is a Dart script to do this.
build_dart commandline
* -v -> Verbose Log
* -t -> Build Type all, release, debug
build_dart commandline

* -v -> Verbose Log
* -t -> Build Type all, release, debug

```bash
cd ./scripts/build_helpers
Expand All @@ -55,11 +60,12 @@ dart ./scripts/build_helpers/bin/build_dart.dart
```

This script does the following:
* Pulls down `depot_tools` if needed.
* Clones a fresh copy of the Dart sdk git repo using `fetch` if needed.
* Uses `gsync` to syncs the repo the the version of dart specificed in `.dart_version`.
* Applies `dart_sdk.patch` to the repo to create the statically linkable `libdart` library
* Builds `libdart`

* Pulls down `depot_tools` if needed.
* Clones a fresh copy of the Dart sdk git repo using `fetch` if needed.
* Uses `gsync` to syncs the repo the the version of dart specificed in `.dart_version`.
* Applies `dart_sdk.patch` to the repo to create the statically linkable `libdart` library
* Builds `libdart`

### CMake

Expand All @@ -68,4 +74,4 @@ Once Dart is built, you can use CMake to generate build files and / or build the
```bash
cmake -B ./.build .
cmake --build .\.build\ --config release
```
```
12 changes: 7 additions & 5 deletions examples/realtime_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ FetchContent_MakeAvailable(cute)
add_executable(realtime_example
main.cpp
drawable.cpp
exportfunc.cpp
)

target_include_directories(realtime_example PRIVATE
Expand All @@ -33,13 +34,14 @@ if(LINUX)
endif()

add_custom_command(TARGET realtime_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:realtime_example> $<TARGET_RUNTIME_DLLS:simple_example_ffi>
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $<TARGET_FILE_DIR:realtime_example>/dart
COMMAND_EXPAND_LISTS
)
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:realtime_example> $<TARGET_RUNTIME_DLLS:realtime_example>)
add_custom_command(TARGET realtime_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $<TARGET_FILE_DIR:realtime_example>/dart)

target_link_libraries(realtime_example PUBLIC dart_dll cute)

if (MSVC)
set_property(TARGET realtime_example PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:realtime_example>)
endif()
endif()

set(EXECUTABLE_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}/realtime_example")
2 changes: 2 additions & 0 deletions examples/realtime_example/dart/ffi_calls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:ffi';
import 'drawable.dart';

class WorkFfiCalls {
// On Linux this search fo Function in libdart_dll why on Windows it Works?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DynamicLibrary.process() should pull in the full global symbol table all statically and dynamically linked libraries. It's posslbe that either this isn't working properly on Linux? But I'll try to double check. Either way we should likely figure it out and update the comment to be clearer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed FFI resolution for the realtime_example on Linux. The issue is that symbols in executables are not exported by default, so Dart can't find them. You need to add -export-dynamic as a linker flag for it to work. See this commit

final DynamicLibrary processLib = DynamicLibrary.process();
//final DynamicLibrary processLib = DynamicLibrary.executable();

late final createEntity = processLib
.lookup<NativeFunction<Uint32 Function(Int32, Int32, Int32, Int32)>>(
Expand Down
1 change: 0 additions & 1 deletion examples/realtime_example/drawable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <dart_api.h>
#include <cute.h>
using namespace Cute;

Expand Down
42 changes: 42 additions & 0 deletions examples/realtime_example/exportfunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "exportfunc.h"

std::unordered_map<unsigned int, Drawable*> entity_drawable_map;
unsigned int next_entity_id = 1;

#ifdef __cplusplus
extern "C" {
#endif

WORM_EXPORT unsigned int create_entity(int x, int y, int width, int height) {
Drawable* d = new Drawable{x, y, width, height, color_blue()};
unsigned int entity_id = next_entity_id;
entity_drawable_map[entity_id] = d;

next_entity_id++;

return entity_id;
}

WORM_EXPORT void destroy_entity(unsigned int entity_id) {
const auto& itr = entity_drawable_map.find(entity_id);
if (itr != entity_drawable_map.end()) {
delete itr->second;
entity_drawable_map.erase(itr);
}
}

WORM_EXPORT Drawable* get_drawable(unsigned int entity_id) {
const auto& itr = entity_drawable_map.find(entity_id);
if (itr != entity_drawable_map.end()) {
return itr->second;
}
return nullptr;
}

WORM_EXPORT bool get_key_just_pressed(int key_code) {
return cf_key_just_pressed((CF_KeyButton)key_code);
}

#ifdef __cplusplus
}
#endif
30 changes: 30 additions & 0 deletions examples/realtime_example/exportfunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <unordered_map>
#include "drawable.h"

//
// Dart accessible funcitons
// These need to be exposed as "C" functions and be exported
//
#if defined(_WIN32)
#define WORM_EXPORT __declspec(dllexport)
#elif defined(__GNUC__)
#define WORM_EXPORT __attribute__((visibility("default")))
#else
#define WORM_EXPORT
#endif



#ifdef __cplusplus
extern "C" {
#endif

WORM_EXPORT unsigned int create_entity(int x, int y, int width, int height);
WORM_EXPORT void destroy_entity(unsigned int entity_id);
WORM_EXPORT Drawable* get_drawable(unsigned int entity_id);
WORM_EXPORT bool get_key_just_pressed(int key_code);

#ifdef __cplusplus
}
#endif
Loading