Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Last active May 22, 2018 19:52
Show Gist options
  • Save thosakwe/f8cc5f0fee0e8869350b01af8ef033d7 to your computer and use it in GitHub Desktop.
Save thosakwe/f8cc5f0fee0e8869350b01af8ef033d7 to your computer and use it in GitHub Desktop.
Dart build system?

DNB

This is a prototype of a CMake-based build tool for Dart native extensions. Combined with scripts, this would be comparable to node-gyp, but with actual Windows support, and no Python dependency.

DNB is designed to ensure portability across various platforms where Dart runs.

Examples explained

Note: In real projects, filenames would be dnb.yaml, dnb.macos.yaml, etc.

dnb.yaml

This file would be used to build a hypothetical Dart native extension named sample_extension. The extension would build to lib/src/libsample_extension.so (or .dll, .dylib, etc.).

In a real project, dnb.yaml would be a sibling to pubspec.yaml.

Kitchen sink

The files kitchen_sink.yaml and kitchen_sink.macos.yaml contain every feature planned thusfar. Most projects will only need to use a small subset of the features, so it will be simple to produce portable native extensions without too much ugly configuration.

Usage

$ dnb --help
$ dnb init # Create a minimal `dnb.yaml` (virtually the same as the one in the example)
$ dnb configure # Generate CMakeLists.txt, run "cmake ."
$ dnb build # Run "cmake --build . -- -j <num_cores>"
$ dnb clean # Run "cmake --build . --target clean"
$ dnb rebuild # Delete CMakeCache.txt, `configure`, then `build`
$ dnb boilerplate foo_extension > lib/src/foo_extension.cc # Generate extension boilerplate C/C++ code
$ dnb -j 4 --target rebuild my_extension # Specify `-j` arg to pass to CMake
$ dnb --cmake="/path/to/cmake" rebuild # Override CMake executable path
$ dnb --watch rebuild # Watch dnb.yaml for changes
$ dnb --debug rebuild # Enable debug-specific flags. Default is release

Ultimately, the command end-users will need to run is dnb rebuild.

Cross-compiling

$ dnb --platform=macos rebuild # Specify a specific platform

Platforms:

  • windows
  • macos
  • linux
  • android
  • ios
  • fuchsia
  • unknown

Definitions on-the-fly

$ dnb --define foo=bar --define baz
$ dnb -dfoo=bar -dbaz

Default definitions

  • DART_SHARED_LIB: Always defined
  • DART_PACKAGE_NAME: The name of the package
  • DART_PACKAGE_VERSION: The package's version string. Defaults to 0.0.0
  • DEBUG: defined when --debug is present
  • NDEBUG, RELEASE: defined by default. Undefined when --debug is present.

Environment variables

  • DART_SDK: Defined to the path to your Dart SDK.
targets:
sample_extension: # add_library(sample_extension ...)
sources:
- lib/src/sample_extension.cc
# Note: If `sources` is a List, instead of the form {paths: [...], globs: [...]},
# then it is assumed that all members of the list provided are paths.
#
# So effectively, this configuration above is {paths: ["lib/src/sample_extension.cc"]}.
targets:
name: sample_extension
flags:
- "-undefined dynamic_lookup" # This will actually be defined by default
definitions:
DEBUG:
MAC:
ARCH: "$ENV{ARCH}"
environment:
CC: gcc-7
CXX: g++-7
targets:
name: sample_extension
output_directory: lib/src # Default
sources:
glob_recurse: true # Default
paths:
- lib/src/sample_extension.cc
globs:
- lib/src/*.cc
includes:
before:
- foo.cmake
after:
- bar.cmake
subdirectories:
before:
- third_party
- "${SOME_VARIABLE_FROM_THE_CACHE}"
after:
- fourth_party
dependencies:
- MY_EXTERNAL_PROJECT
- ANOTHER_TARGET_DEFINED_IN_THIS_FILE
scripts: # You can only run Dart files as scripts. This is by design.
before:
- tool/foo.dart
after:
- tool/bar.dart
debug: # Specify debug-only info
sample_extension:
definitions:
DEBUG:
release:
sample_extension:
definitions:
NDEBUG:
find_libraries:
- foo # Produces FOO_DIR, FOO_LIBRARIES, FOO_INCLUDE_DIRS
include_directories:
- "${foo_INCLUDE_DIRS}"
- bar/baz
link_directories:
- cv
- ffi
link_libraries:
- some_lib
- "-framework CoreFoundation"
- "${FOO_LIBRARIES}"
#include <dart_api.h> // `dnb` finds the SDK `include/` dir and configures CMake accordingly.
Dart_Handle sample_extension_init(Dart_Handle parent_library) {
// ...
}
@thosakwe
Copy link
Author

Note: There should be an option to specify which directory the CMakeLists.txt file should be generated in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment