1e41f4b71Sopenharmony_ci# Using Cargo2gn 2e41f4b71Sopenharmony_ci## Overview 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciRust 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. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciWhen 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. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci## Converting a Single Library 9e41f4b71Sopenharmony_ci1. Go to the directory of the rust library to be converted, for example, **bindgen**. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci ``` 12e41f4b71Sopenharmony_ci cd openharmony/third_party/rust/bindgen 13e41f4b71Sopenharmony_ci ``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci2. Create the configuration file **cargo2gn.json**. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci ```json 18e41f4b71Sopenharmony_ci { 19e41f4b71Sopenharmony_ci "copy-out": true, 20e41f4b71Sopenharmony_ci "run": true, 21e41f4b71Sopenharmony_ci "add-workspace": true, 22e41f4b71Sopenharmony_ci "cargo-bin": "/mnt/xxx/openharmony/prebuilts/rustc/linux-x86_64/current/bin" 23e41f4b71Sopenharmony_ci } 24e41f4b71Sopenharmony_ci ``` 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci3. Run the following command to perform the conversion: 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci ``` 29e41f4b71Sopenharmony_ci python3 /mnt/xxx/openharmony/build/scripts/cargo2gn.py --config cargo2gn.json 30e41f4b71Sopenharmony_ci ``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci The conversion result is as follows: 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci ``` 35e41f4b71Sopenharmony_ci import("//build/templates/rust/ohos_cargo_crate.gni") 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci ohos_cargo_crate("lib") { 38e41f4b71Sopenharmony_ci crate_name = "bindgen" 39e41f4b71Sopenharmony_ci crate_type = "rlib" 40e41f4b71Sopenharmony_ci crate_root = "./lib.rs" 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci sources = ["./lib.rs"] 43e41f4b71Sopenharmony_ci edition = "2018" 44e41f4b71Sopenharmony_ci cargo_pkg_version = "0.64.0" 45e41f4b71Sopenharmony_ci 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" 46e41f4b71Sopenharmony_ci cargo_pkg_name = "bindgen" 47e41f4b71Sopenharmony_ci cargo_pkg_description = "Automatically generates Rust FFI bindings to C and C++ libraries." 48e41f4b71Sopenharmony_ci deps = [ 49e41f4b71Sopenharmony_ci "//third_party/rust/bitflags:lib", 50e41f4b71Sopenharmony_ci "//third_party/rust/cexpr:lib", 51e41f4b71Sopenharmony_ci "//third_party/rust/clang-sys:lib", 52e41f4b71Sopenharmony_ci "//third_party/rust/lazy_static:lib", 53e41f4b71Sopenharmony_ci "//third_party/rust/lazycell:lib", 54e41f4b71Sopenharmony_ci "//third_party/rust/log:lib", 55e41f4b71Sopenharmony_ci "//third_party/rust/peeking_take_while:lib", 56e41f4b71Sopenharmony_ci "//third_party/rust/proc-macro2:lib", 57e41f4b71Sopenharmony_ci "//third_party/rust/quote:lib", 58e41f4b71Sopenharmony_ci "//third_party/rust/regex:lib", 59e41f4b71Sopenharmony_ci "//third_party/rust/rustc-hash:lib", 60e41f4b71Sopenharmony_ci "//third_party/rust/shlex:lib", 61e41f4b71Sopenharmony_ci "//third_party/rust/syn:lib", 62e41f4b71Sopenharmony_ci "//third_party/rust/which:lib", 63e41f4b71Sopenharmony_ci ] 64e41f4b71Sopenharmony_ci features = [ 65e41f4b71Sopenharmony_ci "default", 66e41f4b71Sopenharmony_ci "log", 67e41f4b71Sopenharmony_ci "logging", 68e41f4b71Sopenharmony_ci "static", 69e41f4b71Sopenharmony_ci "which", 70e41f4b71Sopenharmony_ci "which-rustfmt", 71e41f4b71Sopenharmony_ci ] 72e41f4b71Sopenharmony_ci build_root = "build.rs" 73e41f4b71Sopenharmony_ci build_sources = ["build.rs"] 74e41f4b71Sopenharmony_ci build_script_outputs = ["host-target.txt"] 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci## Converting Multiple Libraries in Batches 81e41f4b71Sopenharmony_ci1. Go to the **rust** directory. 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci ``` 84e41f4b71Sopenharmony_ci cd openharmony/third_party/rust 85e41f4b71Sopenharmony_ci ``` 86e41f4b71Sopenharmony_ci2. Add all the rust libraries to be converted to [workspace] in **Cargo.toml** in the **rust** directory. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci ```toml 89e41f4b71Sopenharmony_ci [workspace] 90e41f4b71Sopenharmony_ci members = [ 91e41f4b71Sopenharmony_ci "aho-corasick", 92e41f4b71Sopenharmony_ci "memchr", 93e41f4b71Sopenharmony_ci ] 94e41f4b71Sopenharmony_ci ``` 95e41f4b71Sopenharmony_ci3. Perform steps 2 and 3 in section "Converting a Single Library". 96