dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

Dart is nigh impossible to embed with cmake #56750

Open gaaclarke opened 1 month ago

gaaclarke commented 1 month ago

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

dart-github-bot commented 1 month ago

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.

mraleph commented 1 month ago

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: