1# Porting a Library Built Using Makefile 2 3The following shows how to port the yxml library. 4 5## Source Code Acquisition 6 7Acquire the source code of yxml from [the open-source repository](https://github.com/getdnsapi/yxml). The following table lists the directory structure. 8 9**Table 1** Directory structure of the source code 10 11| Directory | Description | 12| ------------------- | ----------------------------------------------------------- | 13| yxml/bench/ | Benchmark-related code | 14| yxml/test/ | Input and output files as well as scripts of the test cases | 15| yxml/Makefile | File for organizing compilation | 16| yxml/.gitattributes | - | 17| yxml/.gitignore | - | 18| yxml/COPYING | - | 19| yxml/yxml.c | - | 20| yxml/yxml.c.in | - | 21| yxml/yxml-gen.pl | - | 22| yxml/yxml.h | - | 23| yxml/yxml.md | - | 24| yxml/yxml-states | - | 25 26 27## Cross-Compilation Settings 28 29The 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. 30 311. Configure the toolchains. 32 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. 34 35 clang toolchains configuration: 36 37 ``` 38 # Set the cross-compilation toolchain and ensure that the path of the toolchain has been added to the PATH environment variable. 39 CC:=clang 40 AR:=llvm-ar 41 # The --target and --sysroot flags must be specified. 42 CFLAGS:=-Wall -Wextra -Wno-unused-parameter -O2 -g --target=arm-liteos -march=armv7-a --sysroot=$(OHOS_SYSROOT_PATH) 43 ``` 44 45 Original configuration: 46 47 48 ``` 49 CC:=gcc 50 AR:=ar 51 CFLAGS:=-Wall -Wextra -Wno-unused-parameter -O2 -g 52 ``` 53 542. Perform the compilation. 55 56 Run a Linux command to enter the directory \(listed in Table 1) for storing yxml source files and then run the following command: 57 58 ``` 59 make test OHOS_SYSROOT_PATH=... 60 ``` 61 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. 63 643. View the result. 65 66 After step 2 is complete, a static library file and test case are generated in the **out** directory of the yxml library. 67 68 **Table 2** Directory structure of compiled files 69 70 | Directory | Description | 71 | ------------------------------------------- | ------------------------------------------------ | 72 | OpenHarmony/third_party/yxml/yxml/out/lib/ | Static library file. | 73 | OpenHarmony/third_party/yxml/yxml/out/test/ | Test cases as well as the input and output files. | 74 75 76 77 78 79## Library Test 80 81The 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#library-test). The following describes how to use the test cases of the yxml library. 82 83**Table 3** Directory structure of the test directory 84 85| Directory | Description | 86| -------------------------------------------------- | ------------------------------------------------------------ | 87| 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. | 88| OpenHarmony/third_party/yxml/yxml/out/test/test | Executable file for testing. | 89| OpenHarmony/third_party/yxml/yxml/out/test/\*.xml | Input test file. | 90| OpenHarmony/third_party/yxml/yxml/out/test/\*.out | Expected output file. | 91 92The content of the **test.sh** file is as follows: 93 94``` 95#!/bin/sh 96for i in *.xml; do 97 b=`basename $i .xml` 98 o=${b}.out 99 t=${b}.test 100 ./test <$i >$t 101 if [ -n "`diff -q $o $t`" ]; then 102 echo "Test failed for $i:" 103 diff -u $o $t 104 exit 1 105 fi 106done 107echo "All tests completed successfully." 108``` 109 110The 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. 111 112The 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. 113 1141. Execute the following command: 115 116 ``` 117 ./test 118 ``` 119 1202. Copy the content in the **_\*_.xml** file to shell. 121 122 Taking the **pi01.xml** file in the **test** directory as an example, copy the following content to shell and press **Enter**: 123 124 ``` 125 <?SomePI abc?><a/> 126 ``` 127 1283. Check whether the output in the shell is the same as that of the **_\*_.out** file in the **test** directory described in Table 3. 129 130 The output is as follows: 131 132 ``` 133 pistart SomePI 134 picontent abc 135 piend 136 elemstart a 137 elemend 138 ok 139 ``` 140 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. 142 143 144## Adding the Compiled yxml Library to the OpenHarmony Project 145 146The 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 [Adding the Compiled double-conversion Library to the OpenHarmony Project](../porting/porting-thirdparty-cmake.md#adding-the-compiled-double-conversion-library-to-the-openharmony-project). 147 148- The implementation of the newly added **BUILD.gn** file in the yxml library is as follows: 149 150 ``` 151 import("config.gni") 152 group("yxml") { 153 if (ohos_build_thirdparty_migrated_from_fuchisa == true) { 154 deps = [":make"] 155 } 156 } 157 if (ohos_build_thirdparty_migrated_from_fuchisa == true) { 158 action("make") { 159 script = "//third_party/yxml/build_thirdparty.py" 160 outputs = ["$target_out_dir/log_yxml.txt"] 161 exec_path = rebase_path(rebase_path("./yxml", root_build_dir)) 162 command = "make clean && $MAKE_COMMAND" 163 args = [ 164 "--path=$exec_path", 165 "--command=${command}" 166 ] 167 } 168 } 169 ``` 170 171- The configuration of the newly added **config.gni** file in the yxml library is as follows: 172 173 ``` 174 TEST_ENABLE = "YES" 175 176 if (TEST_ENABLE == "YES") { 177 MAKE_COMMAND = "make test OHOS_SYSROOT_PATH=${root_out_dir}sysroot/" 178 } else { 179 MAKE_COMMAND = "make OHOS_SYSROOT_PATH=${root_out_dir}sysroot/" 180 } 181 ``` 182 183- The following table lists the directory structure of the OpenHarmony project. 184 185 **Table 4** Directory structure of the ported library 186 187 | Directory | Description | 188 | ----------------------------------------------- | ------------------------------------------------------------ | 189 | OpenHarmony/third_party/yxml/BUILD.gn | GN file for adding the third-party library to the OpenHarmony project. | 190 | OpenHarmony/third_party/yxml/build_thirdparty.py | Script file for GN to call the **shell** command to convert compilation from GN to Makefile. | 191 | 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. | 192 | OpenHarmony/third_party/yxml/yxml/ | Directory of the third-party library to be ported. | 193 194 195