• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HPM Part Development
2
3
4## HPM Part Development Overview
5
6You can search for common development resources on [DevEco Marketplace](https://repo.harmonyos.com) and import required resources to your projects through dependencies. If you cannot find the desired resources there, develop the resources on your own and publish them in the form of an HPM Part. To develop HPM Parts, install the hpm-cli tool first.
7
8> **NOTE**
9>
10> You are advised to perform the following operations on Ubuntu 18.04 or later.
11
12
13## Installing hpm-cli
14
15The hpm-cli tool is developed based on Node.js. Therefore, you must install Node.js and then use npm to install hpm-cli.
16
17
18### Installing Node.js and hpm-cli
19
201. Install Node.js.
21   Download Node.js from its official website and install it on your local PC.
22
23   You are advised to install the latest LTS version (not earlier than 12.x) of [Node.js](https://nodejs.org/).
24
252. Install the hpm-cli tool using **npm** delivered with **Node.js**. Run the following command:
26
27   ```
28   npm install -g @ohos/hpm-cli
29   ```
30
313. Run the following command to check whether the installation is successful. If a hpm version is displayed, the installation is successful.
32
33   ```
34   hpm -V or hpm --version
35   ```
36
374. (Optional) Run the following command to update the hpm version if needed:
38
39   ```
40   npm update -g @ohos/hpm-cli
41   ```
42
43
44### (Optional) Configuring hpm-cli
45
46After hpm-cli is installed, run the following command to view the hpm configuration:
47
48
49```
50hpm config
51```
52
53  The default hpm configuration is displayed upon successful command execution. You can modify the default configuration as required. The following lists the common hpm configuration:
54
55```
56registry = https://hpm.harmonyos.com
57### login Settings
58# loginUser = invitation_code
59
60#### Path Settings
61shellPath = C:\WINDOWS\System32\cmd.exe
62# shellPath = C:\Program Files\Git\bin\sh.exe
63# globalRepo = C:\Users\username\.hpm\global
64
65#### Network Settings
66# no_proxy = *.server.com
67# http_proxy = http://user:pwd@proxy_server:port
68# https_proxy = http://user:pwd@proxy_server:port
69# strictSsl = true
70
71#### Other Settings
72# privateSupport = true|false
73# ignoreBundles = @ohos/llvm,@ohos/gn,
74# OSPlatform = Auto|linux|darwin|win32
75```
76
77
78## Creating an HPM Part
79
80You can create an HPM Part by using a template or define an existing project as an HPM Part.
81
82
83### Creating an HPM Part Using a Template
84
851. Run the following command to create a directory:
86
87   ```
88   hpm init -t default  mybundle
89   ```
90
91   > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
92   > -In the preceding command, **default** indicates that the **default** template is used. You can use another template such as **simple** or **dist**.
93
94     The generated directory structure is as follows:
95
96   ```
97   /
98   ├── headers            # Header file (example)
99   │   └── main.h
100   └── src                # Source code (example)
101   │    └─ main.c
102   ├── bundle.json        # Metadata declaration file
103   └── LICENSE            # License
104   └── Makefile           # Build description file (example)
105   └── README.md           # Readme file
106
107   ```
108
1092. Write the code and build script of the HPM Part.
110
111   ```
112   hpm init -t {templatename} -d {dir} {name}
113   ```
114
115   > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
116   > - -**t {templatename}** indicates the template name.
117   >
118   > - -**d {dir}** indicates the path for storing the HPM Part to be created.
119   >
120   > - **name** indicates the name of the HPM Part to be created.
121
122The hpm-cli tool provides a few default templates. More templates are available on [DevEco Marketplace](https://repo.harmonyos.com). You can run the **hpm search -t template** command to search for a template stored on [DevEco Marketplace](https://repo.harmonyos.com).
123
124![en-us_image_0000001217486680](figures/en-us_image_0000001217486680.png)
125
126
127### Defining an Existing Project as an HPM Part
128
129If you have a code project and need to distribute it on the HPM platform, run the following command in the project directory, for example, **mybundle2**:
130
131
132```
133hpm init
134```
135
1361. Enter an HPM Part name (**mybundle2** for example) and press **Enter**.
137
1382. Enter the version and description. A **bundle.json** file will be generated in the current directory.
139
1403. Open the **bundle.json** file.
141
142   ```
143   $ hpm init
144   Your bundle will be created in directory ~\demo\mybundle2
145   ? bundle name mybundle2
146   ? version 1.0.0
147   ...
148   Initialization finished.
149   ```
150
1514. Modify other information (such as the author, code repository, code directories, scripts, and dependencies) in **bundle.json**. An example is shown below:
152
153   ```
154   {
155   "name": "mybundle2",
156   "version": "1.0.0",
157   "publishAs": "code-segment",
158   "dirs":{
159       ".":["README.md"],
160       "src":["test.c"],
161       "header":["header/test.h" ],
162       "src/common":["src/common/foobar.txt"]
163     },
164   "scripts": {
165       "build": "make -${args}"
166     },
167   "dependencies": {
168       "@ohos/cjson": "^1.0.0",
169       "@ohos/foobar": "^1.2.0"
170     }
171   }
172   ```
173
174
175## Defining a Distribution
176
177In the distribution metadata file, define the dependent HPM Parts and how to build and link them to generate image files.
178
179The following uses the **bundle.json** file as an example. In this example, the **dist** command uses the **hb** framework.
180
181
182```
183{
184  "name": "@your/dist_name",
185  "version": "2.2.0",
186  "publishAs": "distribution",
187  "description": "describe it",
188  "scripts": {
189    "config_hb": "hb set -root $DEP_BUNDLE_BASE",
190    "dist": "PATH=/root/.local/bin:${DEP_OHOS_gn}:${DEP_OHOS_ninja}/ninja:${DEP_OHOS_llvm}/llvm/bin:${DEP_OHOS_hc_gen}/hc-gen:${PATH} && ./scripts/dist.sh"
191  },
192  "envs": {
193    "debug": false
194  },
195  "dirs": {
196    "scripts": "scripts/*"
197  },
198  "dependencies": {
199    "@ohos/build_lite": "2.2.0",
200    "@ohos/gn": "1.1.1",
201    "@ohos/llvm": "1.1.1",
202    "@ohos/hc_gen": "1.1.0",
203    "@ohos/ninja": "1.1.0",
204    ......
205  },
206  "ohos": {
207    "os": "2.2-Beta",
208    "board": "hi3516",
209    "kernel": "liteos-a"
210  },
211  "keywords": ["hispark", "hi3516"],
212  "repository": "https://gitee.com/openharmony/your-project",
213  "license": "Apache V2"
214}
215```
216
217
218## Building an HPM Part
219
220If the code of an HPM Part can be independently built, you can configure the build tool and scripts to generate binary files.
221
222The hpm-cli tool supports command integration so that you can select any build tool (such as make, gcc, and gn) suitable for your project. You only need to define the **build** command in **scripts** in the **bundle.json** file of your project, and then run the **hpm build** command to start the build.
223
224
225### Defining the Build Script
226
227This section describes how to build an executable file named **helloworld** in the **app** directory.
228
229
230```
231app
232├── BUILD.gn
233├── include
234│   └── helloworld.h
235└── src
236    └── helloworld.c
237```
238
239Create a **BUILD.gn** file in the same directory as **helloworld.c**.
240
241
242```
243touch BUILD.gn
244vim BUILD.gn
245```
246
247The following is an example of **BUILD.gn**:
248
249
250```
251executable("hello_world") {
252    sources = [
253        "src/helloworld.c"
254    ]
255
256    include_dirs = [
257        "include"
258    ]
259}
260```
261
262> **NOTE**
263>
264> - **executable** is a built-in template of gn. You can run the **gn help executable** command to view how to use this template.
265>
266> - **sources** represents the source code path, and **include_dirs** represents the header file path.
267
268
269### Executing the Build Script
270
271Run the following command to start the build:
272
273
274```
275hpm build
276```
277
278After all build operations are complete, the message "build succeed" is displayed. You need to check the build result.
279
280![en-us_image_0000001262166533](figures/en-us_image_0000001262166533.png)
281
282
283### Building an Image
284
285Run the following command in the root directory of the current distribution:
286
287
288```
289hpm dist
290```
291
292The hpm-cli tool automatically starts the build. After the build is complete, an image file will be generated, as shown below:
293
294
295```
296out
297|-xxdist.img
298|-xx.file
299```
300
301
302## Publishing an HPM Part
303
304To publish an HPM Part, you must obtain an account and create an organization (or join an existing organization). For details, see the online help on [DevEco Marketplace](https://repo.harmonyos.com).
305
306After that, generate a public key on the local PC based on your invitation code (which can be obtained on the **My profile** page on [DevEco Marketplace](https://repo.harmonyos.com)), and configure the public key on the **My profile** page.
307
308
309```
310hpm config set loginUser {your-invitation-code}
311hpm gen-keys
312```
313
314The generated file will be stored in **~\Users\yourname\.hpm\key**. Copy the content in the public key file **publicKey_your-account.pem** to the SSH public key on your **My profile** page.
315
316Now you have the permission to publish HPM Parts in your organization.
317
318In the directory where the HPM Part is located, run the following command to pack and publish the HPM Part:
319
320
321```
322hpm publish
323```
324
325> **NOTE**
326>
327> - To avoid HPM Part name conflicts, name an HPM Part in the format of **\@org_name/bundle_name**.
328>
329> - Your account must also be a member of **org_name** so that you can publish or update HPM Parts in the organization.
330>
331> - The published HPM Parts take effect only after security and content reviews.
332
333
334## Using an HPM Part
335
336
337### Using a Part
338
339When developing a project, you usually need to reference other HPM Parts to accelerate development of specific functions. Installing dependencies could be a good way.
340
341Visit [DevEco Marketplace](https://repo.harmonyos.com), search for HPM Parts that meet your service requirements by keywords, and then introduce them to your project. The typical operation procedure is as follows:
342
3431. Run the **hpm init** command to create a bundle description file named **bundle.json**, which contains dependencies and other metadata such as the name and version.
344
3452. Run the **hpm install <name>** command to install the dependency. The dependency will be written into the **dependencies** field in **bundle.json**.
346
3473. Reference the header file in the code to implement the function.
348
3494. Run the **hpm build** command to perform build and output the build result.
350
351  Run the following command in your project. Make sure that the project directory contains the **bundle.json** file.
352
353```
354$ hpm install @scope/the_bundle
355```
356
357  The referenced HPM Part will be installed under **ohos_bundle** in the directory where your project is located. The directory structure is as follows:
358
359```
360project
361├── ohos_bundle
362│   └── scope
363│    └─ the_bundle     # <--- Referenced HPM Part
364└── src
365│    └─ main.c
366├── bundle.json        # Metadata declaration file
367└── LICENSE
368└── Makefile
369└── README.md
370```
371
372Open the **bundle.json** file. You will see that the HPM Part has been introduced to the dependencies of your project.
373
374
375```
376{
377  "dependencies": {
378    "@scope/the_bundle": "^1.0.0"
379  }
380}
381```
382
383You can edit the dependencies of multiple HPM Parts in this file.
384
385
386```
387{
388  "dependencies": {
389    "@scope/the_bundle1": "^1.0.0",
390    "@scope/the_bundle2": "^2.0.0",
391    "@scope/the_bundle3": "^3.0.0",
392    "@scope/the_bundle4": "^1.1.0"
393  }
394}
395```
396
397Run the **hpm install** command again to download and install all HPM Parts that have not been installed.
398
399
400### Installing Global HPM Parts
401
402If the referenced HPM Part is shared by multiple projects, for example, the compiler toolchain, you can install it globally.
403
404Run the following command in your project. Make sure that the project directory contains the **bundle.json** file.
405
406```
407$ hpm install -g @scope/the_tool
408```
409
410The referenced HPM Part will be installed in the directory specified by the **globalRepo** parameter of the **hpm config** command.
411
412```
413~\.hpm\global\ohos_bundles
414│   └── scope
415│    └─ the_tool     # <--- Referenced HPM Part
416```
417
418> **NOTE**
419>
420> - For an HPM Part installed in a project, you can reference it using the environment variable **DEP_SCOPE_bundle_name** when running the hpm build command.
421>  For example, after you run **hpm i \@opensource/gn** to install an HPM Part, edit the build script in the **bundle.json** file as follows:
422>
423> ```
424> "scripts": {
425>    "build": "${DEP_OPENSOURCE_gn}/gn  --version"
426>  },
427> ```
428>
429> Then, you can run the **hpm build** command to call gn functions.
430>
431> - For a globally installed HPM Part, you can directly call the HPM Part using an environment variable or reference the HPM Part using the **${key}/tool_name** parameter in the **hpm config set key value** command. Example:
432>
433> ```
434> hpm i -g @ohos/opensource/gn
435> hpm config BUILD_SYS_GN ~/.hpm/global/ohos_bundles/opensource/gn
436> ```
437>
438>  You can edit the build script in the **bundle.json** file as follows:
439>
440> ```
441> "scripts": {
442>    "build": "${BUILD_SYS_GN}/gn  --version"
443>  },
444> ```
445>
446> Then, you can run the **hpm build** command to call gn functions.
447