1# HPM Part Reference 2 3 4This section describes the HPM Part division rules, HPM Part types, HPM Part composition, metadata fields, and common CLI commands. 5 6 7### HPM Part Division Rules 8 9In principle, HPM Parts should be grouped at a fine-grained granularity to achieve maximum reuse. The following factors are taken into account: 10 11- Independence: HPM Parts provide relatively independent features and can be independently built. Each HPM Part is capable of providing its own APIs and services for external systems. 12 13- Coupling: If an HPM Part must depend on another one to provide services, they can be coupled to one HPM Part. 14 15- Correlation: If a group of HPM Parts jointly implement a feature, and if other HPM Parts never depend on them, the group of HPM Parts can be combined into one HPM Part. 16 17 18### HPM Part Type 19 20HPM Parts are introduced for reuse purposes. 21 22 **Table 1** HPM Part types 23 24| Item| Description| 25| -------- | -------- | 26| source | Distributed in the form of source code. It is depended by the target project, and can be compiled independently.| 27| binary | Distributed in binary mode. It does not contain source code, such as build tools.| 28| code-segment | Distributed in the form of code snippets and cannot be compiled independently. After the installation, it is restored to the specified directory and participates in other code build in the target project.| 29| distribution | Distributed as a distribution and dependent on other HPM Parts. It does not contain source code (except build scripts). The build output is system images.| 30| template | Distributed as a template and used by the **hpm init** command to create a template.| 31| plugin | Distributed as a plugin of hpm-cli to provide more functions.| 32 33 34### HPM Part Composition 35 36An HPM Part contains the following: 37 38- **src** directory for storing code files or code library of the HPM Part. 39 40- **ohos_bundles** folder for storing dependent HPM Parts. The folder is automatically generated during HPM Part installation and does not need to be committed to the code library. 41 42- **README.md** file for describing the HPM Part. 43 44- **bundle.json** file for declaring metadata of the HPM Part. 45 46- LICENSE file for open-source code. 47 48 ``` 49 my-bundle 50 |_ohos_bundles 51 |_headers 52 |_src 53 |_bundle.json 54 |_README.md 55 |_LICENSE 56 ``` 57 58 59### src Source Code 60 61HPM Part code files are the same as those in a common code directory. The only difference lies in the open APIs (declared in header files) provided by HPM Parts, which will be referenced by other HPM Parts and must be declared in **dirs** of **bundle.json**. 62 63 64### README File 65 66To help consumers find your HPM Part on [DevEco Marketplace](https://repo.harmonyos.com) and use it more conveniently, include a **README.md** file in the root directory of the HPM Part. 67 68**README.md** is a file written using the markdown syntax. 69 70The **README.md** file may include instructions on how to install, configure, and use the sample code in the HPM Part, as well as any other information helpful to the consumers. 71 72The Readme file will be displayed on the homepage of the HPM Part on [DevEco Marketplace](https://repo.harmonyos.com). 73 74 75## bundle.json File 76 77The **bundle.json** file describes the metadata of an HPM Part. Each HPM Part has its own **bundle.json** file. The file content is as follows: 78 79 80``` 81{ 82 "name": "@myorg/demo-bundle", 83 "version": "1.0.0", 84 "license": "MIT", 85 "description": "bundle description", 86 "keywords": ["hos"], 87 "tags": ["applications", "drivers"], 88 "author": {"name":"","email":"","url":""}, 89 "contributors":[{"name":"","email":"","url":""},{"name":"","email":"","url":""}], 90 "homepage": "http://www.foo.bar.com", 91 "repository": "https://git@gitee.com:foo/bar.git", 92 "private": false, 93 "publishAs": "code-segment", 94 "segment":{ 95 "destPath":"/the/dest/path" 96 }, 97 "dirs": { 98 "src": ["src/**/*.c"], 99 "headers": ["headers/**/*.h"], 100 "bin": ["bin/**/*.o"] 101 }, 102 "scripts": { 103 "build": "make" 104 }, 105 "envs": {}, 106 "ohos": { 107 "os": "2.0.0", 108 "board": "hi3516", 109 "kernel": "liteos-a" 110 }, 111 "rom": "10240", 112 "ram": "1024", 113 "dependencies": { 114 "@myorg/net":"1.0.0" 115 } 116} 117``` 118 119Each **bundle.json** file has the following fields: 120 121- **name**: an HPM Part name, which starts with the at sign (@) and is separated with the organization name by the slash (/), for example, **\@myorg/mybundle**. 122 123- **version**: version of the HPM Part, for example, 1.0.0. The version must comply with the Semantic Versioning Specification (SemVer) standards. 124 125- **description**: a brief description of the HPM Part. 126 127- **dependencies**: dependent HPM Parts. 128 129- **envs**: parameters required for building the HPM Part, including global parameters and dependency parameters. 130 131- **scripts**: commands executable to the HPM Part, such as commands for compiling, building, testing, and burning. 132 133- **publishAs**: HPM Part publishing type, which can be **source**, **binary**, **distribution**, or **code-segment**. 134 135- **segment**: destination path of the code-segment HPM Part, that is, the destination path of the files contained in the HPM Part after the HPM Part is installed. 136 137- **dirs**: directory structure (such as the header file) generated for publishing. 138 139- **ram** and **rom**: statistical information about the estimated read-only memory (ROM) and random access memory (RAM) usage. 140 141- **ohos**: mappings among OpenHarmony versions, development boards, and kernels, separated by commas (,). 142 143- Extended information: author, home page, code repository, license, tags, and keywords. 144 145- For a distribution-level HPM Part, you can define the inheritance relationship and use the **base** field to describe the basic distribution and version that are inherited from. 146 147- **private**: indicates whether an HPM Part is private. A private HPM Part cannot be found by other organizations. The default value is **false**. 148 149 150## HPM CLI Commands 151 152You can use the hpm-cli tool to manage the lifecycle of an HPM Part. The following table describes available hpm-cli commands. You can run the **hpm -h** command to get the command details. 153 154 **Table 2** Commands available on hpm-cli 155 156| Command Type| Command Line| Description| 157| -------- | -------- | -------- | 158| Querying version information| **hpm -V** or **hpm --version**| Displays the version of the hpm-cli tool.| 159| Querying help information| **hpm -h** or **hpm --version**| Displays the command list and help information.| 160|| hpm -h | Displays command help information.| 161| Creating a project| hpm init bundle | Creates an HPM Part project.| 162|| hpm init -t template | Creates a scaffolding project from a template.| 163| Installing HPM Parts| **hpm install** or **hpm i**| Installs dependent HPM Parts in the bundle.json file.| 164|| hpm install bundle\@version | Installs HPM Parts of a specified version.| 165| Uninstalling HPM Parts| hpm uninstall bundle | Uninstalls dependent HPM Parts.| 166|| **hpm remove** or **hpm rm bundlename**| Uninstalls dependent HPM Parts.| 167| Viewing information| **hpm list** or **hpm ls**| Displays the dependency tree of the HPM part.| 168|| hpm dependencies | Generates the dependency relationship data of the HPM Part. (This command is also integrated in the **hpm ui** command. After you run the hpm ui command, a hpm UI is displayed with the dependencies.)| 169| Searching for HPM Parts| hpm search name | Searches for HPM Parts. **--json** is used to specify the search result in JSON format, and **-type** is used to set the target type, which can be **part**, **distribution**, or **code-segment**.| 170| Setting HPM configuration items| hpm config set key value | Sets configuration items, such as the server address and network proxy.| 171|| hpm config delete key | Deletes configurations.| 172| Updating HPM Part versions| hpm update | Updates the versions of dependent HPM Parts.| 173|| hpm check-update | Checks whether version updates are available to dependent HPM Parts.| 174| Building| hpm build | Builds an HPM Part.| 175|| hpm dist | Builds a distribution. The build depends on the **dist** script in **scripts** of **bundle.json**.| 176| Packing | hpm pack | Packs dependencies of local HPM Parts.| 177| Burning| hpm run flash | Burns the firmware. The firmware burning depends on the **flash** script in **scripts** of **bundle.json**.| 178| Publishing| hpm publish | Publishes an HPM Part, which must be unique in the repository and has a unique version. (An account is required for login.)| 179| Running of extended commands| hpm run | Runs the commands in **scripts** defined in **bundle.json**. Multiple commands can be executed at a time by using **&&**.| 180| Decompressing| hpm extract | Decompresses files in zip, tar, tgz, or .tar.gz format.| 181| Starting UI| hpm ui | Starts the hpm UI locally. You can use the **-p** parameter to specify a port. On the Windows platform, the default browser is used to open the HPM UI.| 182| Changing language| hpm lang | Alternates between Chinese and English on the CLI and UI.| 183| Converting to HPM format| hpm x2h | Converts a Maven or npm package to an HPM Part and publishes it on the HPM platform.| 184| Code segment restoration or cleanup| hpm code clean\|restore | Clears or restores the dependent code segment, which is equivalent to copying or deleting the code segment based on **segment.destPath**.| 185| Generating a key| hpm gen-keys | Generates a public-private key pair and configures the public key on [DevEco Marketplace](https://repo.harmonyos.com), which enables password-free hpm-cli login for HPM Part publishing.| 186| Generation of the third-party open source notice| hpm gen-notice | Generates a file providing the notice on third-party open source by combining the description of each HPM Part.| 187 188 189## About Dependency 190 191HPM Part dependencies are classified as mandatory and optional dependencies. 192 193- Mandatory dependency: If HPM Part A must depend on HPM Part B to implement a feature (the APIs or services specific to HPM Part B must be called), HPM Part B is a mandatory dependency of HPM Part A. 194 195- Optional dependency: If either HPM Part C or D is required for HPM Part A to implement a feature, and if HPM Parts C and D are interchangeable, HPM Parts C and D are optional dependencies of HPM Part A. 196 197 198``` 199"dependencies": { 200 "@myorg/core":"1.0.0", 201 "?@myorg/plugin1":"1.0.0", 202 "?@myorg/plugin2":"1.1.0" 203 } 204``` 205 206HPM Part dependencies can also be classified as compilation and development dependencies. 207 208- Compilation dependency: dependency required for runtime. 209 210- Development dependency: dependency required in non-runtime scenarios, such as static check, build, packing, test, and formatting tools. 211 212 213``` 214"dependencies": { 215 "@myorg/core":"1.0.0" 216 }, 217"devDependencies": { 218 "@myorg/tool":"1.0.0" 219 } 220``` 221 222 Tags can be defined to group dependent HPM Parts. You can obtain the path of a group of dependent HPM Parts based on their tag. A tag starts with a number sign (#) and is defined as follows: 223 224``` 225{ 226 "dependencies": { 227 "#tool": { 228 "first_bundle": "1.0.0", 229 "second_bundle": "1.0.0" 230 }, 231 "#drivers": { 232 "xx_bundle": "1.0.0", 233 "yy_bundle": "1.0.0" 234 } 235 } 236} 237``` 238 239HPM Part dependencies (including indirect dependencies) can be displayed by running the **hpm list** command or on the hpm UI, which is started by running the **hpm ui** command. 240 241 242``` 243$ hpm list 244+--demo@1.0.0 245| +--@example/media@1.0.2 246| +--@demo/sport_hi3518ev300_liteos_a@1.0.0 247| | +--@demo/app@4.0.1 248| | | +--@demo/build@4.0.1 249| | | +--@demo/arm_harmonyeabi_gcc@4.0.0 250| | +--@demo/liteos_a@4.0.0 251| | | +--@demo/third_party_fatfs@4.0.0 252| | | +--@demo/arm_harmonyeabi_gcc@4.0.0 253| | +--@demo/init@4.0.0 254| | +--@demo/dist_tools@4.0.0 255``` 256 257 258## About Environment Variables 259 260During HPM Part building, system-provided environment variables are required to define the output and link the required binary files. 261 262These variables are injected into the context for executing scripts based on service requirements. 263 264Therefore, their values can be directly obtained from the scripts. The following environment variables are available: 265 266**Built-in environment variables** 267 268- **DEP_OHOS_BUNDLES**: path of the **ohos_bundles** folder 269 270- **DEP_BUNDLE_BASE**: path of the outermost HPM Part 271 272**Global environment variables** 273 274Global environment variables are defined by the **envs** attribute in **bundle.json**. You can obtain the values of global environment variables from all independent HPM Parts. 275 276 277``` 278{ 279 "envs": { 280 "compileEnv": "arm" 281 } 282} 283``` 284 285Different parameters can be passed to HPM Parts when introducing dependencies so that the compilation of dependent HPM Parts can meet the requirements of the current HPM Part. The parameters defined in the dependencies can be obtained from the context for executing the corresponding scripts. 286 287 288``` 289{ 290 "dependencies": { 291 "my-bundle": { 292 "version": "1.0.0", 293 "mode": "debug" 294 } 295 } 296} 297``` 298 299When linking to a binary file, the HPM Part needs to know the file path regarding the dependencies. Therefore, you need to pass the path as an environment variable to the HPM Part for building. 300 301The passed environment variable is in **DEP\_BundleName** format, where **BundleName** indicates the name of the dependent HPM Part, for example, **DEP\_first\_bundle**. 302 303 304## About Naming and Version Number 305 3061. The name must be lowercase letters or digits and can be separated by underscores (\_), for example, **bundle**, **my\_bundle**, and **json2**. 307 3082. Name of an HPM Part published to [DevEco Marketplace](https://repo.harmonyos.com) must start with the at sign (@) and is separated with the organization name by a slash (/), for example, **@my_org/part_name**. 309 3103. The organization name and part name must be unique and distinguishably different with other names. 311 3124. Names should be easy to understand. 313 3145. An HPM Part version must be in the format of ***majorVersion***.***minorVersion***.***revisionVersion*** or ***majorVersion***.***minorVersion***.***revisionVersion***-***pre-releaseVersion***, for example, **1.0.0** and **1.0.0-beta**. For details, see [https://semver.org](https://semver.org/). 315