diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 66a0110..624709d 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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 diff --git a/.gitignore b/.gitignore index d22a5b6..2c1934f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ dart-sdk/* .build/* build/* -artifacts/* \ No newline at end of file +artifacts/* +depot_tools/* +out/* \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 3eac41c..dc0d59c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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}", diff --git a/.vscode/settings.json b/.vscode/settings.json index 19903bf..676d47d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,80 @@ }, "dart.analysisExcludedFolders": [ "dart-sdk" - ] + ], + "files.associations": { + "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" + } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ed60a7..5961e0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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/$<$:${OUTPUT_DEBUG}>$<$:${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) \ No newline at end of file +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}") \ No newline at end of file diff --git a/README.md b/README.md index eb782ad..1b8c303 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 -``` \ No newline at end of file +``` diff --git a/examples/realtime_example/CMakeLists.txt b/examples/realtime_example/CMakeLists.txt index 86887af..0e57216 100644 --- a/examples/realtime_example/CMakeLists.txt +++ b/examples/realtime_example/CMakeLists.txt @@ -18,6 +18,7 @@ FetchContent_MakeAvailable(cute) add_executable(realtime_example main.cpp drawable.cpp + exportfunc.cpp ) target_include_directories(realtime_example PRIVATE @@ -33,13 +34,14 @@ if(LINUX) endif() add_custom_command(TARGET realtime_example POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy -t $ $ - COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $/dart - COMMAND_EXPAND_LISTS -) + COMMAND ${CMAKE_COMMAND} -E copy -t $ $) +add_custom_command(TARGET realtime_example POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $/dart) target_link_libraries(realtime_example PUBLIC dart_dll cute) if (MSVC) set_property(TARGET realtime_example PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $) -endif() \ No newline at end of file +endif() + +set(EXECUTABLE_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}/realtime_example") \ No newline at end of file diff --git a/examples/realtime_example/dart/ffi_calls.dart b/examples/realtime_example/dart/ffi_calls.dart index 53a27af..60b05e8 100644 --- a/examples/realtime_example/dart/ffi_calls.dart +++ b/examples/realtime_example/dart/ffi_calls.dart @@ -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? final DynamicLibrary processLib = DynamicLibrary.process(); + //final DynamicLibrary processLib = DynamicLibrary.executable(); late final createEntity = processLib .lookup>( diff --git a/examples/realtime_example/drawable.h b/examples/realtime_example/drawable.h index ebb977c..7ddfb55 100644 --- a/examples/realtime_example/drawable.h +++ b/examples/realtime_example/drawable.h @@ -1,6 +1,5 @@ #pragma once -#include #include using namespace Cute; diff --git a/examples/realtime_example/exportfunc.cpp b/examples/realtime_example/exportfunc.cpp new file mode 100644 index 0000000..dbcccad --- /dev/null +++ b/examples/realtime_example/exportfunc.cpp @@ -0,0 +1,42 @@ +#include "exportfunc.h" + +std::unordered_map 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 \ No newline at end of file diff --git a/examples/realtime_example/exportfunc.h b/examples/realtime_example/exportfunc.h new file mode 100644 index 0000000..448801e --- /dev/null +++ b/examples/realtime_example/exportfunc.h @@ -0,0 +1,30 @@ +#pragma once +#include +#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 \ No newline at end of file diff --git a/examples/realtime_example/main.cpp b/examples/realtime_example/main.cpp index ca93829..cff3cd0 100644 --- a/examples/realtime_example/main.cpp +++ b/examples/realtime_example/main.cpp @@ -4,10 +4,22 @@ using namespace Cute; #include #include +#include #include #include +#include "exportfunc.h" #include "drawable.h" +extern std::unordered_map entity_drawable_map; +extern unsigned int next_entity_id; + +Dart_Handle HandleError(Dart_Handle handle) { + if (Dart_IsError(handle)) { + Dart_PropagateError(handle); + } + return handle; +} + static Dart_Isolate _dart_isolate = nullptr; static unsigned int _dart_pending_messages = 0; @@ -15,6 +27,15 @@ void dart_message_notify_callback(Dart_Isolate isolate) { _dart_pending_messages++; } +void* ResolveNativeFunction(const char* name, uintptr_t args_n) { + void* native_function = nullptr; + if (strcmp("create_entity", name) == 0) { + native_function = reinterpret_cast(create_entity); + } + + return native_function; +} + Dart_PersistentHandle _root_library; bool init_dart() { DartDllConfig config; @@ -33,11 +54,12 @@ bool init_dart() { Dart_EnterScope(); Dart_Handle root_library = Dart_RootLibrary(); _root_library = Dart_NewPersistentHandle(root_library); + Dart_SetFfiNativeResolver(root_library, ResolveNativeFunction); Dart_Handle init_function_name = Dart_NewStringFromCString("main"); Dart_Handle result = Dart_Invoke(root_library, init_function_name, 0, nullptr); if (Dart_IsError(result)) { - std::cout << Dart_GetError(result); + std::cout << Dart_GetError(result) << std::endl; Dart_ExitScope(); return false; } @@ -55,8 +77,7 @@ void dart_frame(float delta_time) { Dart_NewDouble(delta_time), }; Dart_Handle root_library = Dart_HandleFromPersistent(_root_library); - Dart_Handle result = - Dart_Invoke(root_library, frame_function_name, 1, args); + Dart_Handle result = Dart_Invoke(root_library, frame_function_name, 1, args); Dart_ExitScope(); } @@ -70,8 +91,10 @@ void dart_frame_maintanance() { Dart_EnterScope(); while (_dart_pending_messages > 0) { - - Dart_HandleMessage(); + auto handle = Dart_HandleMessage(); + if (Dart_IsError(handle)) { + std::cout << Dart_GetError(handle) << std::endl; + } _dart_pending_messages--; } @@ -83,8 +106,6 @@ void dart_frame_maintanance() { Dart_ExitScope(); } -std::unordered_map entity_drawable_map; -unsigned int next_entity_id = 1; void render_drawables() { for (const auto& pair : entity_drawable_map) { draw_push_color(pair.second->color); @@ -95,45 +116,6 @@ void render_drawables() { } } -// -// Dart accessible funcitons -// These need to be exposed as "C" functions and be exported -// -#if defined(_WIN32) -#define WORM_EXPORT exten "C" __declspec(dllexport) -#else -#define WORM_EXPORT extern "C" __attribute__((visibility("default"))) __attribute((used)) -#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); -} int main(int argc, char* argv[]) { // Create a window with a resolution of 640 x 480. @@ -143,14 +125,18 @@ int main(int argc, char* argv[]) { make_app("Fancy Window Title", 0, 0, 640, 480, options, argv[0]); if (is_error(result)) return -1; - if (!init_dart()) return -1; + if (!init_dart()) { + std::cout << "init dart failed" << std::endl; + destroy_app(); + return -1; + } while (app_is_running()) { app_update(); dart_frame(DELTA_TIME); dart_frame_maintanance(); - + render_drawables(); app_draw_onto_screen(); diff --git a/examples/simple_example/CMakeLists.txt b/examples/simple_example/CMakeLists.txt index 38991cd..72a3a38 100644 --- a/examples/simple_example/CMakeLists.txt +++ b/examples/simple_example/CMakeLists.txt @@ -5,20 +5,18 @@ project(simple_example) add_executable(simple_example main.cpp) target_include_directories(simple_example PRIVATE - "${DART_DLL_DIR}" "${DART_DIR}/runtime/include" ) add_custom_command(TARGET simple_example POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy -t $ $ - COMMAND ${CMAKE_COMMAND} -E copy -t $ ${PROJECT_SOURCE_DIR}/hello_world.dart - COMMAND_EXPAND_LISTS -) - -add_dependencies(simple_example ALWAYS_DO_POST_BUILD) + COMMAND ${CMAKE_COMMAND} -E copy -t $ $) +add_custom_command(TARGET simple_example POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy -t $ ${PROJECT_SOURCE_DIR}/hello_world.dart) target_link_libraries(simple_example PUBLIC dart_dll) if (MSVC) set_property(TARGET simple_example PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $) -endif() \ No newline at end of file +endif() + +set(EXECUTABLE_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}/simple_example") \ No newline at end of file diff --git a/examples/simple_example/main.cpp b/examples/simple_example/main.cpp index b10a034..62a1d9b 100644 --- a/examples/simple_example/main.cpp +++ b/examples/simple_example/main.cpp @@ -1,8 +1,7 @@ #include #include -#include "dart_dll.h" - +#include #include Dart_Handle HandleError(Dart_Handle handle) { diff --git a/examples/simple_example_ffi/CMakeLists.txt b/examples/simple_example_ffi/CMakeLists.txt index 866b3d1..bc4ea8f 100644 --- a/examples/simple_example_ffi/CMakeLists.txt +++ b/examples/simple_example_ffi/CMakeLists.txt @@ -10,10 +10,16 @@ target_include_directories(simple_example_ffi PRIVATE ) add_custom_command(TARGET simple_example_ffi POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy -t $ $ - COMMAND ${CMAKE_COMMAND} -E copy -t $ ${PROJECT_SOURCE_DIR}/hello_world_ffi.dart - COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/.dart_tool $/.dart_tool - COMMAND_EXPAND_LISTS -) + COMMAND ${CMAKE_COMMAND} -E copy -t $ $) +add_custom_command(TARGET simple_example_ffi POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy -t $ ${PROJECT_SOURCE_DIR}/hello_world_ffi.dart) +add_custom_command(TARGET simple_example_ffi POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/.dart_tool $/.dart_tool) + +target_link_libraries(simple_example_ffi PUBLIC dart_dll) + +if (MSVC) + set_property(TARGET simple_example_ffi PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $) +endif() -target_link_libraries(simple_example_ffi PUBLIC dart_dll) \ No newline at end of file +set(EXECUTABLE_OUTPUT_PATH "${EXECUTABLE_OUTPUT_PATH}/simple_example_ffi") \ No newline at end of file diff --git a/scripts/build_helpers/bin/assemble_artifacts.dart b/scripts/build_helpers/bin/assemble_artifacts.dart index 2981e87..202d672 100644 --- a/scripts/build_helpers/bin/assemble_artifacts.dart +++ b/scripts/build_helpers/bin/assemble_artifacts.dart @@ -39,8 +39,10 @@ void _copyIncludeFiles(Directory dest) { file.copySync(destPath); } - final dartDllHeader = File('src/dart_dll.h'); - dartDllHeader.copySync(path.join(dest.path, 'include', 'dart_dll.h')); + final dartDllHeader = File('src/include/dart_dll.h'); + final destPath = path.join(path.join(dest.path, 'include', 'dart_dll.h')); + dartDllHeader.copySync(destPath); + logger.i(' ${dartDllHeader.path} => $destPath'); } void _copyLibs(Directory dest) { @@ -56,7 +58,7 @@ void _copyLibs(Directory dest) { var copyGlob = Glob('*.so'); if (Platform.isWindows) { - copyGlob = Glob(r'Release/*.*', caseSensitive: false); + copyGlob = Glob('{Release/*.*,Debug/*.*}', caseSensitive: false); } else if (Platform.isMacOS) { copyGlob = Glob('*.dylib'); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 78f1c25..1c1b669 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,7 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.20) project(dart_dll VERSION 0.1.2) -set(DARTSDK_ROOTDIR "${PROJECT_SOURCE_DIR}/../dart-sdk" CACHE FILEPATH "Directory that dart-sdk is cloned too") -set(DART_DIR "${DARTSDK_ROOTDIR}/sdk") - set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_EXTENSIONS ) @@ -14,7 +11,7 @@ add_library(dart_dll SHARED isolate_setup.cpp ) -target_include_directories(dart_dll PUBLIC +target_include_directories(dart_dll PRIVATE "${DART_DIR}/runtime" ) @@ -22,8 +19,14 @@ if(WIN32) set(LIB_PREFIX "lib") endif() +cmake_path(ABSOLUTE_PATH DART_DIR NORMALIZE OUTPUT_VARIABLE DART_DIR) + MESSAGE(STATUS "Dart SDK ${DART_DIR}") +if(NOT EXISTS "${DART_DIR}/runtime/include/dart_api.h") +MESSAGE(FATAL_ERROR "Missing Dart SDK or not found") +endif() + find_library(LIB_DART_DEBUG NAMES "${LIB_PREFIX}dart" HINTS "${DART_DIR}/out/DebugX64/obj/runtime/bin" "${DART_DIR}/xcodebuild/ReleaseX64/obj/runtime/bin" @@ -41,10 +44,6 @@ target_compile_definitions(dart_dll PRIVATE ) if(WIN32) - set_property(TARGET dart_dll PROPERTY - MSVC_RUNTIME_LIBRARY "MultiThreaded" - ) - target_compile_definitions(dart_dll PRIVATE _HAS_EXCEPTIONS=0 _SCL_SECURE=0 @@ -87,6 +86,23 @@ endif() if(LIB_DART_DEBUG) target_link_libraries(dart_dll debug ${LIB_DART_DEBUG}) +else() + target_link_libraries(dart_dll debug ${LIB_DART_RELEASE}) endif() -target_link_libraries(dart_dll optimized ${LIB_DART_RELEASE}) \ No newline at end of file +target_link_libraries(dart_dll optimized ${LIB_DART_RELEASE}) + +MESSAGE(STATUS "debug ${LIB_DART_DEBUG} optimized ${LIB_DART_RELEASE}") + +install(TARGETS dart_dll + EXPORT dart_dllTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) + +target_include_directories(dart_dll PUBLIC + $ + $ # /include/mylib +) diff --git a/src/dart_dll.cpp b/src/dart_dll.cpp index 03ddcea..fdb3c7b 100644 --- a/src/dart_dll.cpp +++ b/src/dart_dll.cpp @@ -1,6 +1,6 @@ #include -#include "dart_dll.h" +#include "include/dart_dll.h" #include "isolate_setup.h" #include @@ -98,7 +98,11 @@ extern "C" { bool DartDll_Initialize(const DartDllConfig& config) { std::cout << "Initializig Dart ---- \n"; - Dart_SetVMFlags(0, nullptr); + auto resultMessage = Dart_SetVMFlags(0, nullptr); + if(resultMessage != nullptr) { + std::cerr << "Dart initialization failed: " << resultMessage << std::endl; + return false; + } char* error = nullptr; if (!dart::embedder::InitOnce(&error)) { @@ -132,7 +136,7 @@ bool DartDll_Initialize(const DartDllConfig& config) { std::cout << "Dart initialized, error was: " << (initError != nullptr ? initError : "null") << std::endl; - return true; + return initError != nullptr ? false : true; } Dart_Isolate DartDll_LoadScript(const char* script_uri, diff --git a/src/dart_dll.h b/src/include/dart_dll.h similarity index 100% rename from src/dart_dll.h rename to src/include/dart_dll.h