DESK-MX8M-L-AN-0004: Using DART for embedded applications

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box


200px-Emblem-important.svg.png

This application note has been validated using the kit version in the History table.


History
Issue Date Notes
2024/03/26 DESK-MX8M-L 4.0.0


Introduction[edit | edit source]

Dart is an open-source, scalable programming language, with robust libraries and runtimes, for building web, server, and mobile apps. Dart is a client-optimized language for fast apps on any platform. As described in the Dart github repository, Dart is:

  • Optimized for UI: Develop with a programming language specialized around the needs of user interface creation.
  • Productive: Make changes iteratively: use hot reload to see the result instantly in your running app.
  • Fast on all platforms: Compile to ARM & x64 machine code for mobile, desktop, and backend. Or compile to JavaScript for the web.

Dart's flexible compiler technology lets you run Dart code in different ways, depending on your target platform and goals:

  • Dart Native: For programs targeting devices (mobile, desktop, server, and more), Dart Native includes both a Dart VM with JIT (just-in-time) compilation and an AOT (ahead-of-time) compiler for producing machine code
  • Dart Web: For programs targeting the web, Dart Web includes both a development time compiler (dartdevc) and a production time compiler (dart2js)

Dart-platforms.png

Creating the SDK[edit | edit source]

It is possible to build the Dart VM and/or SDK using the instruction listed in the Building Dart SDK for ARM github page.

The following steps have been performed using the Ubuntu 20.04 DESK-MX8M-L 4.0.0 Virtual Machine provided along the DESK-MX8M-L DAVE's Embedded systems developing environment for the i.MX8M Plus ORCA and MITO 8M Mini products.

Preparing the building environment[edit | edit source]

  • Ubuntu cross-toolchain for ARM has to be installed, using apt-get
sudo apt-get install curl
sudo apt-get install g++-aarch64-linux-gnu

Building the SDK[edit | edit source]

The git repository from Google sources can be cloned

dvdk@vagrant:~/dart$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
Cloning into 'depot_tools'...
remote: Sending approximately 46.89 MiB ...
remote: Counting objects: 3, done
remote: Finding sources: 100% (3/3)
remote: Total 58236 (delta 42064), reused 58234 (delta 42064)
Receiving objects: 100% (58236/58236), 46.90 MiB | 11.64 MiB/s, done.
Resolving deltas: 100% (42064/42064), done.
dvdk@vagrant:~/dart$ export PATH="$PATH:$PWD/depot_tools"
dvdk@vagrant:~/dart$ 

Then, the proper dart sources fetched in the build directory

dvdk@vagrant:~/dart$ mkdir dart-sdk
dvdk@vagrant:~/dart$ cd dart-sdk
dvdk@vagrant:~/dart/dart-sdk$ fetch dart
Running: gclient root
WARNING: Your metrics.cfg file was invalid or nonexistent. A new one will be created.
Running: gclient config --spec 'solutions = [
  {
    "name": "sdk",
    "url": "https://dart.googlesource.com/sdk.git",
    "deps_file": "DEPS",
    "managed": False,
    "custom_deps": {},
  },
]
'
Running: gclient sync
________ running 'git -c core.deltaBaseCacheLimit=2g clone --no-checkout --progress https://dart.googlesource.com/sdk.git /home/dvdk/dart/dart-sdk/_gclient_sdk_5ci28x8d' in '/home/dvdk/dart/dart-sdk'
Cloning into '/home/dvdk/dart/dart-sdk/_gclient_sdk_5ci28x8d'...
remote: Sending approximately 1.11 GiB ...
remote: Counting objects: 11597, done
remote: Finding sources: 100% (78/78)
remote: Total 1525300 (delta 1276749), reused 1525289 (delta 1276749)
Receiving objects: 100% (1525300/1525300), 1.10 GiB | 11.50 MiB/s, done.
Resolving deltas: 100% (1276749/1276749), done.
Syncing projects: 100% (105/105), done.                                                                                        
Running: git config --add remote.origin.fetch '+refs/tags/*:refs/tags/*'
Running: git config diff.ignoreSubmodules dirty
dvdk@vagrant:~/dart/dart-sdk$ 
  • The linux target has be selected and synced
dvdk@vagrant:~/dart/dart-sdk$ echo "target_os = ['linux']" >> .gclient
dvdk@vagrant:~/dart/dart-sdk$ 
dvdk@vagrant:~/dart/dart-sdk$ gclient sync
Syncing projects: 100% (105/105), done.                                          
dvdk@vagrant:~/dart/dart-sdk$ 
  • for building the SDK, a dedicated Python script can be used. Running the script, it produces the Dart SDK:
dvdk@vagrant:~/dart/dart-sdk/sdk$ ./tools/build.py --no-goma --no-clang --mode release --arch arm64 create_sdk
...
...
[5390/5390] STAMP obj/create_sdk.stamp
The build took 4678.870 seconds
dvdk@vagrant:~/dart/dart-sdk/sdk$ cd out/ReleaseXARM64/dart-sdk/
dvdk@vagrant:~/dart/dart-sdk/sdk/out/ReleaseXARM64/dart-sdk$ ls -la
total 40
drwxrwxr-x  5 dvdk dvdk 4096 Mar 26 10:37 .
drwxrwxr-x  9 dvdk dvdk 4096 Mar 26 11:34 ..
drwxrwxr-x  5 dvdk dvdk 4096 Mar 26 11:34 bin
-rw-rw-r--  1 dvdk dvdk 1768 Mar 26 10:37 dartdoc_options.yaml
drwxrwxr-x  3 dvdk dvdk 4096 Mar 26 10:34 include
drwxrwxr-x 29 dvdk dvdk 4096 Mar 26 10:37 lib
-rw-rw-r--  2 dvdk dvdk 1502 Mar 26 10:23 LICENSE
-rw-rw-r--  2 dvdk dvdk 1271 Mar 26 10:23 README
-rw-rw-r--  1 dvdk dvdk   41 Mar 26 10:37 revision
-rw-rw-r--  1 dvdk dvdk   52 Mar 26 10:37 version
dvdk@vagrant:~/dart/dart-sdk/sdk/out/ReleaseXARM64/dart-sdk$ 
You can also produce only a Dart VM runtime, no SDK, by replacing create_sdk with runtime. This process involves also building a VM that targets ia32/x64, which is used to generate a few parts of the SDK

Installing the SDK in the target[edit | edit source]

Once the SDK image has been created, it is possible to create an archive and copy it in the target (for example using scp or ftp):

dvdk@vagrant:~/dart/dart-sdk/sdk/out/ReleaseXARM64/dart-sdk$ sudo tar zcpf ../../../../../dart-sdk-arm64.tar.gz *
dvdk@vagrant:~/dart/dart-sdk/sdk/out/ReleaseXARM64/dart-sdk$ cd ../../../../..
dvdk@vagrant:~/dart$ ls -la
total 133484
drwxrwxr-x  4 dvdk dvdk      4096 Mar 26 12:05 ./
drwxr-xr-x 20 dvdk dvdk      4096 Mar 26 10:34 ../
drwxrwxr-x  4 dvdk dvdk      4096 Mar 26 10:33 dart-sdk/
-rw-r--r--  1 root root 136658509 Mar 26 12:05 dart-sdk-arm64.tar.gz
drwxrwxr-x 21 dvdk dvdk     12288 Mar 26 10:21 depot_tools/
dvdk@vagrant:~/dart$ 

Then remote copy the SDK tarball into the target:

dvdk@vagrant:~/dart$ scp dart-sdk-arm64.tar.gz root@192.168.0.90:/home/root
The authenticity of host '192.168.0.90 (192.168.0.90)' can't be established.
ECDSA key fingerprint is SHA256:QEN8INGUdOrzVfwhRgoa1Wqxmg/DZUXcTBe9f/PaPAM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.90' (ECDSA) to the list of known hosts.
dart-sdk-arm64.tar.gz                                                                                                                                      100%  130MB  53.3MB/s   00:02    
dvdk@vagrant:~/dart$ 

and extract the archive in the /usr/bin target directory:

root@desk-mx8mp:~# mkdir /usr/bin/dart-sdk
root@desk-mx8mp:~# cd /usr/bin/dart-sdk
root@desk-mx8mp:/usr/bin/dart-sdk# tar zxpf /home/root/dart-sdk-arm64.tar.gz
root@desk-mx8mp:/usr/bin/dart-sdk# export PATH=$PATH:/usr/bin/dart-sdk/bin
root@desk-mx8mp:/usr/bin/dart-sdk#

It runs and shows the version

root@desk-mx8mp:~# dart --version
Dart SDK version: 3.4.0-edge.93d2d44b26040fb3e0f4dc03b85e87aa008f9909 (main) (Tue Mar 26 06:57:47 2024 +0000) on "linux_arm64"
root@desk-mx8mp:~#

Running the Hello world example[edit | edit source]

Before running a Hello World in Dart the Dart SDK has to be installed on the target, i.e. the previous steps have to be already accomplished.

The simpler example written in Dart can be the following one:

void main(){
   print("Hello World");
}

corresponding to the created hello_world.dart text file. It produces on the target:

root@desk-mx8mp:~# dart hello_world.dart
Hello World
root@desk-mx8mp:~#

The compiled version can be created for faster execution:

dart compile exe hello_world.dart

which produce a fast executable version:

root@desk-mx8mp:~# time ./hello_world.exe
Hello World

real    0m0.016s
user    0m0.015s
sys     0m0.005s
root@desk-mx8mp:~#

versus the VM one:

root@desk-mx8mp:~# time dart hello_world.dart
Hello World

real    0m4.575s
user    0m5.400s
sys     0m0.322s
root@desk-mx8mp:~# 

More information[edit | edit source]