porting-thirdparty-makefile.md 8.4 KB
Newer Older
E
ester.zhou 已提交
1
# Porting a Library Built Using Makefile
Y
yangni 已提交
2 3 4

The following shows how to port the yxml library.

E
ester.zhou 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
## Source Code Acquisition

Acquire the source code of yxml from [the open-source repository](https://github.com/getdnsapi/yxml). The following table lists the directory structure.

**Table 1** Directory structure of the source code

| Directory           | Description                                                 |
| ------------------- | ----------------------------------------------------------- |
| yxml/bench/         | Benchmark-related code                                      |
| yxml/test/          | Input and output files as well as scripts of the test cases |
| yxml/Makefile       | File for organizing compilation                             |
| yxml/.gitattributes | -                                                           |
| yxml/.gitignore     | -                                                           |
| yxml/COPYING        | -                                                           |
| yxml/yxml.c         | -                                                           |
| yxml/yxml.c.in      | -                                                           |
| yxml/yxml-gen.pl    | -                                                           |
| yxml/yxml.h         | -                                                           |
| yxml/yxml.md        | -                                                           |
| yxml/yxml-states    | -                                                           |


## Cross-Compilation Settings
Y
yangni 已提交
28 29 30 31 32

The following steps show how to configure and modify the toolchains for cross-compiling the libraries built using CMake to compile executable files for the OpenHarmony platform.

1.  Configure the toolchains.

E
ester.zhou 已提交
33
    Replace the original configuration of MakeFile \(listed in Table 1) in the root directory of the yxml library with the following clang toolchains configuration.
Y
yangni 已提交
34 35 36 37 38 39 40 41

    clang toolchains configuration:

    ```
    # Set the cross-compilation toolchain and ensure that the path of the toolchain has been added to the PATH environment variable.
    CC:=clang
    AR:=llvm-ar
    # The --target and --sysroot flags must be specified.
D
duangavin123 已提交
42
    CFLAGS:=-Wall -Wextra -Wno-unused-parameter -O2 -g --target=arm-liteos -march=armv7-a --sysroot=$(OHOS_SYSROOT_PATH)
Y
yangni 已提交
43 44 45 46
    ```

    Original configuration:

E
ester.zhou 已提交
47 48 49 50 51 52
    
   ```
   CC:=gcc
   AR:=ar
   CFLAGS:=-Wall -Wextra -Wno-unused-parameter -O2 -g
   ```
Y
yangni 已提交
53 54 55

2.  Perform the compilation.

E
ester.zhou 已提交
56 57 58 59 60
    Run a Linux command to enter the directory \(listed in Table 1) for storing yxml source files and then run the following command:
    
   ```
   make test OHOS_SYSROOT_PATH=...
   ```
Y
yangni 已提交
61

E
ester.zhou 已提交
62
    **OHOS\_SYSROOT\_PATH** specifies the absolute path of the directory where **sysroot** is located. For OpenHarmony, set **OHOS\_SYSROOT\_PATH** to the absolute path of the **out/hispark\__xxx_/ipcamera\_hispark\__xxx_/sysroot** directory. This directory is generated after full compilation is complete. Therefore, complete full compilation before porting.
Y
yangni 已提交
63 64 65

3.  View the result.

E
ester.zhou 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    After step 2 is complete, a static library file and test case are generated in the **out** directory of the yxml library.

    **Table 2** Directory structure of compiled files

    | Directory                                   | Description                                      |
    | ------------------------------------------- | ------------------------------------------------ |
    | OpenHarmony/third_party/yxml/yxml/out/lib/  | Static library file.                              |
    | OpenHarmony/third_party/yxml/yxml/out/test/  | Test cases as well as the input and output files. |
    
    
    


## Library Test

The test procedure for the yxml library is similar to that for the double-conversion library. For details, see the procedure described in [Porting a Library Built Using CMake](../porting/porting-thirdparty-cmake.md). The following describes how to use the test cases of the yxml library.

**Table 3** Directory structure of the test directory

| Directory                                          | Description                                                  |
| -------------------------------------------------- | ------------------------------------------------------------ |
| OpenHarmony/third_party/yxml/yxml/out/test/test.sh | Automatic test script, which cannot be used because OpenHarmony does not support automatic script execution. However, you can refer to this script to conduct a manual test. |
| OpenHarmony/third_party/yxml/yxml/out/test/test    | Executable file for testing.                                 |
| OpenHarmony/third_party/yxml/yxml/out/test/\*.xml   | Input test file.                                             |
| OpenHarmony/third_party/yxml/yxml/out/test/\*.out   | Expected output file.                                        |

The content of the **test.sh** file is as follows:
Y
yangni 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

```
#!/bin/sh
for i in *.xml; do
  b=`basename $i .xml`
  o=${b}.out
  t=${b}.test
  ./test <$i >$t
  if [ -n "`diff -q $o $t`" ]; then
    echo "Test failed for $i:"
    diff -u $o $t
    exit 1
  fi
done
echo "All tests completed successfully."
```

E
ester.zhou 已提交
110
The shell of OpenHarmony does not support input and output redirection symbols \(< and \>\). During the test, you need to copy the content in the **_\*_.xml** file to the shell and press **Enter**. The output content is displayed in the shell screen.
Y
yangni 已提交
111

E
ester.zhou 已提交
112
The following operations are performed based on the assumption that the OpenHarmony environment has been set up and the **nfs** directory has been mounted and accessed.
Y
yangni 已提交
113 114 115 116 117 118 119

1.  Execute the following command:

    ```
    ./test
    ```

E
ester.zhou 已提交
120
2.  Copy the content in the **_\*_.xml**  file to shell.
Y
yangni 已提交
121

E
ester.zhou 已提交
122
    Taking the **pi01.xml** file in the [test](#table115941423164318) directory as an example, copy the following content to shell and press **Enter**:
Y
yangni 已提交
123

E
ester.zhou 已提交
124 125 126
   ```
   <?SomePI abc?><a/>
   ```
Y
yangni 已提交
127

E
ester.zhou 已提交
128
3.  Check whether the output in the shell is the same as that of the **_\*_.out** file in the [test](#table115941423164318) directory.
Y
yangni 已提交
129 130 131 132 133 134 135 136 137 138 139 140

    The output is as follows:

    ```
    pistart SomePI
    picontent abc
    piend
    elemstart a
    elemend
    ok
    ```

E
ester.zhou 已提交
141
    The output is the same as the **pi01.out** file in the **test** directory listed in Table 3. In this case, the test is passed.
Y
yangni 已提交
142 143


E
ester.zhou 已提交
144
## Adding the Compiled yxml Library to the OpenHarmony Project
Y
yangni 已提交
145

E
ester.zhou 已提交
146
The procedure for adding the yxml library is almost the same as that for adding the double-conversion library, except that the implementation of **build.gn** and **config.gni** files. For details, see the procedure described in  [Porting a Library Built Using CMake](porting-thirdparty-cmake.md#section1651053153715).
Y
yangni 已提交
147

E
ester.zhou 已提交
148
-   The implementation of the newly added **BUILD.gn** file in the yxml library is as follows:
Y
yangni 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

```
import("config.gni")
group("yxml") {
    if (ohos_build_thirdparty_migrated_from_fuchisa == true) {
        deps = [":make"]
    }
}
if (ohos_build_thirdparty_migrated_from_fuchisa == true) {
    action("make") {
        script = "//third_party/yxml/build_thirdparty.py"
        outputs = ["$target_out_dir/log_yxml.txt"]
        exec_path = rebase_path(rebase_path("./yxml", root_build_dir))
        command = "make clean && $MAKE_COMMAND"
        args = [
            "--path=$exec_path",
            "--command=${command}"
        ]
    }
}
```

-   The configuration of the newly added  **config.gni**  file in the yxml library is as follows:

```
TEST_ENABLE = "YES"

if (TEST_ENABLE == "YES") {
A
arvinzzz 已提交
177
    MAKE_COMMAND = "make test OHOS_SYSROOT_PATH=${root_out_dir}sysroot/"
Y
yangni 已提交
178
} else {
A
arvinzzz 已提交
179
    MAKE_COMMAND = "make OHOS_SYSROOT_PATH=${root_out_dir}sysroot/"
Y
yangni 已提交
180 181 182 183 184
}
```

-   The following table lists the directory structure of the OpenHarmony project.

E
ester.zhou 已提交
185 186 187 188 189 190 191 192 193
**Table 4** Directory structure of the ported library

| Directory                                       | Description                                                  |
| ----------------------------------------------- | ------------------------------------------------------------ |
| OpenHarmony/third_party/yxml/BUILD.gn           | GN file for adding the third-party library to the OpenHarmony project. |
| OpenHarmony/third_party/yxml/build_thirdparty.py | Script file for GN to call the **shell** command to convert compilation from GN to Makefile. |
| OpenHarmony/third_party/yxml/config.gni         | Third-party library compilation configuration file, which can be modified to determine whether the test cases will be used during the building. |
| OpenHarmony/third_party/yxml/yxml/              | Directory of the third-party library to be ported.           |

Y
yangni 已提交
194