Original address: medium.com/dartlang/da…

Original author: medium.com/mit.mit

Published: November 6, 2019-6 minutes to read

Dart already provides an extensive set of compilers for building production-optimized code for mobile devices and networks. These flexible compilers enable our framework partners to target a wide range of form factors. The Flutter app on Android and iOS, the Flutter app on the Web and desktop, the AngularDart app on the Web, and Google Assistant on embedded devices. Today we are announcing the launch of Dart 2Native, an extension to our existing set of compilers that will compile Dart programs into a built-in executable containing pre-compiled machine code. With Dart 2 Native, you can use Dart to create command-line tools on macOS, Windows, or Linux. The announcement image for this feature is implemented with the feature itself 🙂

Dart Native and Dart 2 Native compilers

Dart has supported AOT (advanced) compilation to Native machine code for several years, so Dart Native is a fairly mature technology. However, in the past, we only exposed this feature with Flutter on iOS and Android mobile devices.

With dart2native, we extend native compilation support to support traditional desktop operating systems running macOS, Windows, and Linux. Because executables created using Dart 2 Native are standalone, they can run on machines that do not have the Dart SDK installed. Because they are compiled using Dart’s AOT compiler, the executable can start running in milliseconds. In Dart, as with other Dart compilers and runtimes, there is a rich and consistent set of core libraries when compiling native code.

We hear a lot of requests for AOT compilation for desktop operating systems — it’s the sixth highest rated issue in our issue tracker — so we’re excited to be able to offer this feature.

If you have used dart2aot before, then starting with 2.6, you will use dart2native. It provides a superset of dart2aot features.

Create a command line application with dart2native

The Dart 2 Native compiler is the best choice for building and deploying DARt-based command line applications. These applications typically use libraries such as DART: IO (basic I/O), Package: HTTP (networking), and Package: ARGS (parameter parsing). Let’s review the basics of compiling the “Hello, world” application into an executable:

Source code hello.dart:

main() {
  print('Hello Dart developers');
}
Copy the code

Compile hello.dart into the Hello executable:

$ dart2native src/hello.dart -o hello
Generated: /Users/mit/hello
Copy the code

Run hello to measure execution time:

$ time ./helloHello Dart Developers Real 0M0.049s user 0M0.018s SYS 0M0.020SCopy the code

Notice that this command started, printed, stdout, and then quit in 49 milliseconds!

We’ve already seen some Dart developers experimenting with Dart 2Native as a command-line tool:

  • Natalie from the SASS team, a popular CSS extension tool, reports that after converting their dart-based SASS implementation to compile using Dart 2native, it is now performing as well as the C++ -based LibSass.
  • Filip from the Dart DevRel team recompiled his LinkChecker tool with Dart 2 Native, making it 27 times faster when checking small websites.

Interoperability with C code via DART: FFI

Native applications often need access to the native functionality of the surrounding operating system. These system apis are often exposed in native C-based libraries, and Dart supports interoperability with these libraries through Dart: FFI, our new C interoperability mechanism introduced in Dart 2.5 preview. Dart 2 Native compiler is compatible with DART: FFI, so you can create and compile native DART applications that use it.

Recently, a team member used DART: FFI to create a Dart_console library for console application development that has the ability to get window dimensions, read and set cursor positions, manage colors, read keys, and control sequences. Dart: FFI’s capabilities make DART a very powerful console application language.

Kilo: a 7MB code editor written in less than 500 lines of Dart code

Using the Dart core library, Dart: FFI, and dart_Console libraries, we can create very interesting console applications. Included in the Dart_Console package is a complete Kilo demo, a console text editor written with about 500 lines of Dart code. The name kilo comes from its origin, Kilo.c, which is c implemented in about 1000 lines of code.

With the new dart2native compiler, we could easily package it, and we ended up with a 7MB stand-alone code editor. Here is a compilation editor demo, and then use the compiled editor to edit your own source code to fix a bug:

A kilo editor written in Dart and compiled into an executable, edited by Dart 2native with its own source code.

Build the service with dart2native

Another potential use of the dart2native compiler is for small services — for example, a back end that supports front-end applications written using Flutter. In recent years, a growing trend has been to use services that run on serverless computing. These are fully hosted services that scale automatically, including from zero to zero (not running), offering the potential for significantly lower costs as they are only billed at actual run time. Google Cloud offers serverless computing through Cloud Run.

Fast startup of services is critical for a serverless back end. Dart based services have traditionally been run using our JUST-in-time (JIT) compiler, but JIT based execution has a high latency at startup because the code needs to be compiled and warmed up before execution can begin. By compiling the service’s code into native code ahead of time, you can avoid this delay and start running immediately. In addition, using native code, you can create self-contained Dart services with small disk footprint, greatly reducing the size of the container on which the Dart service runs. Dart developer Paul Mundt recently documented his experience with the Dart 2 Native compiler; He was able to reduce the size of his Docker image by 91% by using native code, from 220MB using JIT-compiled code to just 20MB. See our documentation for more details on server-side applications and packages.

availability

Dart 2 Native compiler is available with the Dart SDK since version 2.6 and is available today from dart.dev/get-dart. Once you install the SDK, you should see the new compiler in the bin/ directory and PATH. Dart.dev has more documentation. If you are getting the Dart SDK via Flutter, please note that the current Flutter build has incomplete Dart 2 Native support. We recommend that you install the Dart 2.6 SDK from dart.dev/get-dart.

Known limitations

The initial version of this dart2native compiler has some known limitations listed below. You can let us know which questions are important to you by adding a “thumbs up” to the GitHub Question tracker.

  • Cross-compilation is not supported (problem 28617). The dart2native compiler only supports creating machine code for the operating system it runs on. So if you want to create executables for all three systems, you need to run the compiler three times on macOS, Windows, and Linux. One approach is to use a CI (continuous integration) provider that supports all three operating systems.
  • Signature not supported (Question 39106). The resulting executable file uses a format incompatible with standard signing tools, such ascodesignandsigntool.
  • Does not supportdart:mirrorsanddart:developer(seeThe Dart core library).

Other changes in Dart 2.6

The 2.6 Dart SDK also has some other changes.

Dart 2.5 is a preview of Dart: FFI, our new C interoperability mechanism. Dart 2.6 has a new version of Dart: FFI. This new release has a number of breakthrough API changes that make our API easier to use, provide more type safety, and provide easy memory access. For more details, see the Dart 2.6 Change Log. With these changes, DART: FFI is in beta, and we expect much less API change and high overall stability. Please continue to give us feedback via the issue tracker.

Dart 2.6 also includes a preview of exciting new language features, extended methods. We still have some polishing and tooling to do to complete this feature, but we hope to officially launch it in the next Dart SDK release. We’ll have more to say about extension methods; Now, you can read about the design considerations behind this feature.

Next steps

Download the Dart 2.6 SDK (dart.dev/get-dart), build something cool with Dart 2Native, and let us know. If you’d like to share details, please leave a comment at the bottom of this article. We’re happy to see your work.


Translated through (www.DeepL.com/Translator) (free version)