1# Using Cargo2gn
2## Overview
3
4Rust uses Cargo as its build system and package manager. **Cargo.toml** is the manifest file of Cargo. It contains metadata such as the name, version, and dependencies for crates (packages) in Rust.
5
6When used in OpenHarmony, **Cargo.toml** needs to be converted into **BUILD.gn** rules. cargo2gn is the tool that can achieve this purpose. It can convert one or more Rust libraries.
7
8##  Converting a Single Library
91. Go to the directory of the rust library to be converted, for example, **bindgen**.
10
11    ```
12    cd openharmony/third_party/rust/bindgen
13    ```
14
152. Create the configuration file **cargo2gn.json**.
16
17    ```json
18    {
19        "copy-out": true,
20        "run": true,
21        "add-workspace": true,
22        "cargo-bin": "/mnt/xxx/openharmony/prebuilts/rustc/linux-x86_64/current/bin"
23    }
24    ```
25
263. Run the following command to perform the conversion:
27
28   ```
29   python3 /mnt/xxx/openharmony/build/scripts/cargo2gn.py --config cargo2gn.json
30   ```
31
32   The conversion result is as follows:
33
34   ```
35   import("//build/templates/rust/ohos_cargo_crate.gni")
36
37   ohos_cargo_crate("lib") {
38       crate_name = "bindgen"
39       crate_type = "rlib"
40       crate_root = "./lib.rs"
41
42       sources = ["./lib.rs"]
43       edition = "2018"
44       cargo_pkg_version = "0.64.0"
45       cargo_pkg_authors = "Jyun-Yan You <jyyou.tw@gmail.com>,  Emilio Cobos Álvarez <emilio@crisal.io>,  Nick Fitzgerald <fitzgen@gmail.com>,  The Servo project developers"
46       cargo_pkg_name = "bindgen"
47       cargo_pkg_description = "Automatically generates Rust FFI bindings to C and C++ libraries."
48       deps = [
49           "//third_party/rust/bitflags:lib",
50           "//third_party/rust/cexpr:lib",
51           "//third_party/rust/clang-sys:lib",
52           "//third_party/rust/lazy_static:lib",
53           "//third_party/rust/lazycell:lib",
54           "//third_party/rust/log:lib",
55           "//third_party/rust/peeking_take_while:lib",
56           "//third_party/rust/proc-macro2:lib",
57           "//third_party/rust/quote:lib",
58           "//third_party/rust/regex:lib",
59           "//third_party/rust/rustc-hash:lib",
60           "//third_party/rust/shlex:lib",
61           "//third_party/rust/syn:lib",
62           "//third_party/rust/which:lib",
63       ]
64       features = [
65           "default",
66           "log",
67           "logging",
68           "static",
69           "which",
70           "which-rustfmt",
71       ]
72       build_root = "build.rs"
73       build_sources = ["build.rs"]
74       build_script_outputs = ["host-target.txt"]
75   }
76   ```
77
78
79
80##  Converting Multiple Libraries in Batches
811. Go to the **rust** directory.
82
83   ```
84   cd openharmony/third_party/rust
85   ```
862. Add all the rust libraries to be converted to [workspace] in **Cargo.toml** in the **rust** directory.
87
88    ```toml
89    [workspace]
90    members = [
91        "aho-corasick",
92        "memchr",
93    ]
94    ```
953. Perform steps 2 and 3 in section "Converting a Single Library".
96