Backend Development 20 min read

Porting the V8 JavaScript Engine to HarmonyOS: Cross‑Compilation, Build Process, and Integration

This article details the process of adapting the V8 JavaScript engine for HarmonyOS, covering engine selection, cross‑compilation using CMake and Ninja, toolchain configuration, building builtins and snapshots, and integrating the resulting library into native HarmonyOS applications.

JD Tech
JD Tech
JD Tech
Porting the V8 JavaScript Engine to HarmonyOS: Cross‑Compilation, Build Process, and Integration

The Roma framework, a dynamic cross‑platform solution used in JD Finance, requires a JavaScript engine on HarmonyOS. Since HarmonyOS lacks a built‑in JS engine capable of running Roma, the V8 engine must be ported.

1. JS Engine Selection

Common engines include V8 (Google/Chrome/Node), SpiderMonkey (Firefox), JavaScriptCore (Safari), Chakra (IE), Hermes (React Native), and lightweight engines such as JerryScript. V8 is the most widely adopted, powering browsers, desktop apps (Electron), server‑side Node.js, and IoT.

2. V8 Porting Tool Selection

V8 uses the gn + ninja build system. HarmonyOS provides a cmake + ninja native build system, so the V8 build configuration must be translated from GN to CMake.

3. Cross‑Compilation with CMake

Cross‑compilation builds the target binary on a host machine (e.g., macOS M1) for a different architecture (HarmonyOS arm64‑v8a). A CMake toolchain file defines the compiler, linker, and system settings:

set(CMAKE_C_COMPILER "/path/to/c/compiler")
set(CMAKE_CXX_COMPILER "/path/to/cxx/compiler")
set(CMAKE_LINKER "/path/to/linker")
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

Build commands:

$ mkdir build
$ cd build
$ cmake -G Ninja ..
$ ninja   # or cmake --build .

4. V8‑Specific Differences

V8’s builtins and snapshots are generated by the mksnapshot tool, which runs on the host and emits assembly ( embedded.S ) and snapshot files for the target. Builtins are created via two paths: direct machine‑code generation for ASM/CPP types, or Turbofan IR generation for other types, later compiled to native code.

5. Building V8 for HarmonyOS

Steps:

Compile mksnapshot on the host.

Run mksnapshot to produce arm64‑v8a assembly and snapshot.

Use HarmonyOS’s toolchain ( ohos.toolchain.cmake ) to compile the generated assembly together with V8 source, producing a static library ( libv8_snapshot.a ).

Example CMake invocation for the target:

$ cmake -DOHOS_STL=c++_shared -DOHOS_ARCH=arm64-v8a -DOHOS_PLATFORM=OHOS \
    -DCMAKE_TOOLCHAIN_FILE=/Applications/DevEco-Studio.app/Contents/sdk/HarmonyOS-NEXT-DB5/openharmony/native/build/cmake/ohos.toolchain.cmake -G Ninja ..
$ ninja

6. Integrating V8 into a HarmonyOS Native Project

Create a native C++ module, copy V8’s include directory and the compiled .a library into the project, and adjust the module’s CMakeLists.txt to use C++17 and link against the V8 static library. A simple NAPI demo shows how to expose C++ functions to the JavaScript side.

7. Future Trends

For IoT and resource‑constrained devices, reducing the engine’s footprint is crucial. Approaches include using TypeScript with ahead‑of‑time bytecode generation, stripping Turbofan, or adopting lightweight engines such as Hermes or the HarmonyOS‑native Panda engine.

Porting V8 to HarmonyOS is a complex embedded‑systems task involving cross‑compilation, CMake, Ninja, C++, and V8’s internal tooling, but it enables a powerful JavaScript runtime for the Roma framework and other cross‑platform applications.

HarmonyOSV8embeddedcmakeJavaScript Enginecross compilation
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.