Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart is nigh impossible to embed with cmake #56750

Open
gaaclarke opened this issue Sep 18, 2024 · 2 comments
Open

Dart is nigh impossible to embed with cmake #56750

gaaclarke opened this issue Sep 18, 2024 · 2 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triaged Issue has been triaged by sub team type-enhancement A request for a change that isn't a bug

Comments

@gaaclarke
Copy link
Contributor

Description

I attempted to create a new C++ project with CMake that would build against Dart and execute Dart_VersionString() using the Dart documentation. I was unable to do it, I got as far as linking but found no way to resolve the linking errors. For example, the symbol __ZN4dart11ObjectStore19LazyInitCoreMembersEv was in none of the build libraries.

We don't have to use cmake as a build system, but we should be able to use external build systems to embed Dart.

Seen results

Unable to use the instructions in https://github.com/dart-lang/sdk/blob/main/docs/Building.md to link against a built dart.

Expected results

I should be able to wrap Dart's build system and embed dart in a cmake project.

The code

main.cc

#include <dart_api.h>
#include <iostream>

int main(int argc, const char* argv[]) {
  std::cout << Dart_VersionString();
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(dart_game)

include(FetchContent)

################################################################################

find_package(Python3 COMPONENTS Interpreter REQUIRED)
find_program(NINJA_EXECUTABLE ninja REQUIRED)
find_program(FETCH_EXECUTABLE fetch REQUIRED)

FetchContent_Declare(
    dart_sdk
    DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/_deps/dart_sdk-src
    DOWNLOAD_COMMAND fetch dart
)

set(FETCHCONTENT_QUIET OFF)

FetchContent_MakeAvailable(dart_sdk)

add_custom_target(build_dart_sdk ALL
    COMMAND ${CMAKE_COMMAND} -E echo "Configuring Dart SDK"
    COMMAND ${Python3_EXECUTABLE} tools/build.py --mode release --arch arm64 create_sdk
    WORKING_DIRECTORY ${dart_sdk_SOURCE_DIR}/sdk
    BYPRODUCTS "${dart_sdk_SOURCE_DIR}/sdk/xcodebuild/ReleaseARM64/obj/runtime/libdart_jit.a"
)

add_library(dart_sdk_lib STATIC IMPORTED GLOBAL)
add_dependencies(dart_sdk_lib build_dart_sdk)

set_target_properties(dart_sdk_lib PROPERTIES
    IMPORTED_LOCATION "${dart_sdk_SOURCE_DIR}/sdk/xcodebuild/ReleaseARM64/obj/runtime/libdart_jit.a"
    INTERFACE_INCLUDE_DIRECTORIES "${dart_sdk_SOURCE_DIR}/sdk/runtime/include"
)

################################################################################

add_executable(dart_game src/main.cc)
target_link_libraries(dart_game dart_sdk_lib)

Notes

  • Making mistakes in the CMakeLists.txt was very painful since fetch dart takes about 15 minutes and 15 GB of hard drive space.
  • I had to use FetchContent_Declare not ExternalProject_Add to make sure the headers exist for INTERFACE_INCLUDE_DIRECTORIES. This created a really long configure process where nothing was printed out for 15 minutes. I had to use set(FETCHCONTENT_QUIET OFF) to avoid that.
  • fetch dart doesn't allow you to pin to a specific version of Dart, which is what I'd want
  • The instructions for building dart mention using "create_sdk", but that's never mentioned in ./tools/build.py -h
  • Using the copy-code button on https://github.com/dart-lang/sdk/blob/main/docs/Building.md sometimes also copies the $ so it can't directly be pasted into the terminal
  • I tried ./tools/build.py runtime to see if that gave me the proper library files to link against, that didn't work either. It wasn't clear what that actually did compared to create_sdk.
@gaaclarke gaaclarke changed the title Dart is nigh impossible embed with cmake Dart is nigh impossible to embed with cmake Sep 18, 2024
@dart-github-bot
Copy link
Collaborator

Summary: The user is unable to embed Dart in a CMake project. They followed the instructions in the Dart documentation to link against a built Dart library, but encountered linking errors. The user was unable to find the necessary symbols, such as __ZN4dart11ObjectStore19LazyInitCoreMembersEv, in the build libraries.

@dart-github-bot dart-github-bot added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Sep 18, 2024
@mraleph
Copy link
Member

mraleph commented Sep 18, 2024

I would not go this route: e.g. I don't think we want to support wrapping build.py into some other build system.

I think what we should do instead is:

  • Consider if we can publish a documentation and example build files for building parts of the Dart SDK (e.g. Dart VM) using some common build system (e.g. CMake). This is not that hard really - it just requires copying a bunch of file lists.
  • We should start publishing prebuild "Dart engines" to link against as part of the SDK (e.g. a share library which can be loaded and used to execute Dart code). I think we will actually get this done as part of the work @iinozemtsev is doing.

@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Sep 18, 2024
@a-siva a-siva added the triaged Issue has been triaged by sub team label Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triaged Issue has been triaged by sub team type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants