porting-chip-board-bundle.md 3.6 KB
Newer Older
D
duangavin123 已提交
1
# Third-party Module Adaptation<a name="EN-US_TOPIC_0000001153842842"></a>
D
duangavin123 已提交
2

D
duangavin123 已提交
3
To use third-party modules in the  **third\_party**  directory, you may need to adapt the modules. This section uses mbedTLS as an example to describe how to integrate the adaptation code with the OpenHarmony compilation framework. For the principles of mbedTLS and the specific logic of the adaptation code, see the adaptation guide on the mbedTLS official website.
D
duangavin123 已提交
4

D
duangavin123 已提交
5
1.  <a name="li12446195633211"></a>Write the adaptation layer code.
D
duangavin123 已提交
6

D
duangavin123 已提交
7
    Write the required adaptation layer code based on the mbedTLS adaptation guide. In this example, adaptation of the hardware random number is used for illustration, and the paths are relative to  **third\_party/mbedtls**:
D
duangavin123 已提交
8

D
duangavin123 已提交
9 10 11 12
    1.  Copy the  **include/mbedtls/config.h**  file to the  **ports**  directory, and enable  **MBEDTLS\_ENTROPY\_HARDWARE\_ALT**  in the file.
    2.  In the  **ports**  directory, create the  **entropy\_poll\_alt.c**  file under  **include**  and implement the hardware random number API in  **entropy\_poll.h**.
    3.  Add the path of the adapted  **entropy\_poll\_alt.c**  file to  **mbedtls\_sources**  in the  **BUILD.gn**  file.
    4.  Add the line  **MBEDTLS\_CONFIG\_FILE**  to  **lite\_library\("mbedtls\_static"\)**  in the  **BUILD.gn**  file to specify the path of the new configuration file.
D
duangavin123 已提交
13 14 15 16 17 18 19 20 21

        ```
        lite_library("mbedtks_static") {
          ...  
          defines += ["MBEDTLS_CONFIG_FILE=<../port/config.h>"]
          ...
        }
        ```

D
duangavin123 已提交
22
    You are advised to make the preceding modifications in a new  **config.h**  file or  **_xxx_\_alt.c**  file. Do not directly edit the code in the original file. Intrusive modifications may cause a large number of scattered conflicts during subsequent version updates, increasing the update maintenance costs.
D
duangavin123 已提交
23

D
duangavin123 已提交
24
2.  Create a patch.
D
duangavin123 已提交
25

D
duangavin123 已提交
26
    The preceding adaptation is hardware-specific. Therefore, when uploading code to the library, you cannot directly store the code in the  **third\_party/mbedtls**  directory. Instead, you need to integrate the preceding modifications into a patch and inject the patch into the code for a build.
D
duangavin123 已提交
27

D
duangavin123 已提交
28 29
    1.  Add the patch configuration file  **device/<_company_\>/<_board_\>/patch.yml**.
    2.  In the  **device/<_company_\>/<_board_\>/patch.yml**  file, add the information about the patch to apply.
D
duangavin123 已提交
30 31

        ```
D
duangavin123 已提交
32
        # Path of the patch to apply. This path is relative to the code root directory.
D
duangavin123 已提交
33
        third_party/mbedtls:
D
duangavin123 已提交
34
          # Directory in which the patch is stored.
D
duangavin123 已提交
35 36
          - device/<company>/<board>/third_party/mbedtls/adapter.patch
        third_party/wpa_supplicant:
D
duangavin123 已提交
37
          # When there are multiple patches in a path, the patches are executed in sequence.
D
duangavin123 已提交
38 39 40 41 42
          - device/<company>/<board>/third_party/wpa_supplicant/xxxxx.patch
          - device/<company>/<board>/third_party/wpa_supplicant/yyyyy.patch
        ...
        ```

D
duangavin123 已提交
43
    3.  Create a patch file as described in step  [1](#li12446195633211)  and save it to the corresponding directory.
D
duangavin123 已提交
44

D
duangavin123 已提交
45
3.  Start a build with the patch.
D
duangavin123 已提交
46

D
duangavin123 已提交
47
    Add  **--patch**  when triggering a build. The following is the command for executing a full build with a patch:
D
duangavin123 已提交
48 49 50 51 52 53

    ```
    hb build -f --patch
    ```

    >![](../public_sys-resources/icon-caution.gif) **CAUTION:** 
D
duangavin123 已提交
54
    >The information about the product to which the patch is most recently applied will be recorded. Next time the build is performed, the previous patch is rolled back \(that is,  **\`patch -p1 -R < xxx\`**  is executed\). If the patch fails to be rolled back or a patch fails to be added, the build process is terminated. In this case, resolve the patch conflict and try again.
D
duangavin123 已提交
55 56