1# Porting a Library Built Using Makefile<a name="EN-US_TOPIC_0000001154372572"></a> 2 3- [Source Code Acquisition](#section114115321416) 4- [Cross-Compilation Settings](#section81263255384) 5- [Library Test](#section1830015913391) 6- [Adding the Compiled yxml Library to the OpenHarmony Project](#section1898016213406) 7 8The following shows how to port the yxml library. 9 10## Source Code Acquisition<a name="section114115321416"></a> 11 12Acquire the source code of yxml from [the open-source repository](https://github.com/getdnsapi/yxml). The following table lists the directory structure. 13 14**Table 1** Directory structure of the source code 15 16<a name="table16520154171813"></a> 17<table><thead align="left"><tr id="row2052115419188"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p1352111417181"><a name="p1352111417181"></a><a name="p1352111417181"></a>Directory</p> 18</th> 19<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p2521541171812"><a name="p2521541171812"></a><a name="p2521541171812"></a>Description</p> 20</th> 21</tr> 22</thead> 23<tbody><tr id="row252164101814"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p747319438225"><a name="p747319438225"></a><a name="p747319438225"></a>yxml/bench/</p> 24</td> 25<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1852214114183"><a name="p1852214114183"></a><a name="p1852214114183"></a>Benchmark-related code</p> 26</td> 27</tr> 28<tr id="row11402205216237"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p6473134382216"><a name="p6473134382216"></a><a name="p6473134382216"></a>yxml/test/</p> 29</td> 30<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p2040355219237"><a name="p2040355219237"></a><a name="p2040355219237"></a>Input and output files as well as scripts of the test cases</p> 31</td> 32</tr> 33<tr id="row35220418182"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1547384313229"><a name="p1547384313229"></a><a name="p1547384313229"></a>yxml/Makefile</p> 34</td> 35<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p7522041131814"><a name="p7522041131814"></a><a name="p7522041131814"></a>File for organizing compilation</p> 36</td> 37</tr> 38<tr id="row17522144114180"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p6473164392210"><a name="p6473164392210"></a><a name="p6473164392210"></a>yxml/.gitattributes</p> 39</td> 40<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p145221541151817"><a name="p145221541151817"></a><a name="p145221541151817"></a>-</p> 41</td> 42</tr> 43<tr id="row7522941131811"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p12473144312217"><a name="p12473144312217"></a><a name="p12473144312217"></a>yxml/.gitignore</p> 44</td> 45<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p95231141121810"><a name="p95231141121810"></a><a name="p95231141121810"></a>-</p> 46</td> 47</tr> 48<tr id="row13523841161812"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p72441217182819"><a name="p72441217182819"></a><a name="p72441217182819"></a>yxml/COPYING</p> 49</td> 50<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1752319414186"><a name="p1752319414186"></a><a name="p1752319414186"></a>-</p> 51</td> 52</tr> 53<tr id="row165231641111810"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p347304312226"><a name="p347304312226"></a><a name="p347304312226"></a>yxml/yxml.c</p> 54</td> 55<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p4523174113188"><a name="p4523174113188"></a><a name="p4523174113188"></a>-</p> 56</td> 57</tr> 58<tr id="row1452344110184"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p18473643152213"><a name="p18473643152213"></a><a name="p18473643152213"></a>yxml/yxml.c.in</p> 59</td> 60<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1252317413187"><a name="p1252317413187"></a><a name="p1252317413187"></a>-</p> 61</td> 62</tr> 63<tr id="row12744431142214"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p274693119222"><a name="p274693119222"></a><a name="p274693119222"></a>yxml/yxml-gen.pl</p> 64</td> 65<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p274753111227"><a name="p274753111227"></a><a name="p274753111227"></a>-</p> 66</td> 67</tr> 68<tr id="row7967123412215"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1947314314225"><a name="p1947314314225"></a><a name="p1947314314225"></a>yxml/yxml.h</p> 69</td> 70<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p16967203462218"><a name="p16967203462218"></a><a name="p16967203462218"></a>-</p> 71</td> 72</tr> 73<tr id="row3988153742217"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p109891737112216"><a name="p109891737112216"></a><a name="p109891737112216"></a>yxml/yxml.md</p> 74</td> 75<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p10989123732213"><a name="p10989123732213"></a><a name="p10989123732213"></a>-</p> 76</td> 77</tr> 78<tr id="row184521641102214"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p847314433221"><a name="p847314433221"></a><a name="p847314433221"></a>yxml/yxml-states</p> 79</td> 80<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p4453154111224"><a name="p4453154111224"></a><a name="p4453154111224"></a>-</p> 81</td> 82</tr> 83</tbody> 84</table> 85 86## Cross-Compilation Settings<a name="section81263255384"></a> 87 88The 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. 89 901. Configure the toolchains. 91 92 Replace the original configuration of MakeFile \(listed in [Table 1](#table16520154171813)\) in the root directory of the yxml library with the following clang toolchains configuration. 93 94 clang toolchains configuration: 95 96 ``` 97 # Set the cross-compilation toolchain and ensure that the path of the toolchain has been added to the PATH environment variable. 98 CC:=clang 99 AR:=llvm-ar 100 # The --target and --sysroot flags must be specified. 101 CFLAGS:=-Wall -Wextra -Wno-unused-parameter -O2 -g --target=arm-liteos -march=armv7-a --sysroot=$(OHOS_SYSROOT_PATH) 102 ``` 103 104 Original configuration: 105 106 ``` 107 CC:=gcc 108 AR:=ar 109 CFLAGS:=-Wall -Wextra -Wno-unused-parameter -O2 -g 110 ``` 111 1122. Perform the compilation. 113 114 Run a Linux command to enter the directory \(listed in [Table 1](#table16520154171813)\) for storing yxml source files and then run the following command: 115 116 ``` 117 make test OHOS_SYSROOT_PATH=... 118 ``` 119 120 **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. 121 1223. View the result. 123 124 After step 2 is complete, a static library file and test case are generated in the **out** directory of the yxml library. 125 126 **Table 2** Directory structure of compiled files 127 128 <a name="table3271115435618"></a> 129 <table><thead align="left"><tr id="row1273454185610"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p3273154185616"><a name="p3273154185616"></a><a name="p3273154185616"></a>Directory</p> 130 </th> 131 <th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p18273145445612"><a name="p18273145445612"></a><a name="p18273145445612"></a>Description</p> 132 </th> 133 </tr> 134 </thead> 135 <tbody><tr id="row1627317542567"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p13273154175620"><a name="p13273154175620"></a><a name="p13273154175620"></a>openHarmony/third_party/yxml/yxml/out/lib/</p> 136 </td> 137 <td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p132744541562"><a name="p132744541562"></a><a name="p132744541562"></a>Static library file</p> 138 </td> 139 </tr> 140 <tr id="row112741554165612"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p17274165475611"><a name="p17274165475611"></a><a name="p17274165475611"></a>openHarmony/third_party/yxml/yxml/out/test/</p> 141 </td> 142 <td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p152741354135614"><a name="p152741354135614"></a><a name="p152741354135614"></a>Test cases as well as the input and output files</p> 143 </td> 144 </tr> 145 </tbody> 146 </table> 147 148 149## Library Test<a name="section1830015913391"></a> 150 151The 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-thirdparty-cmake.md#section6686144293611). The following describes how to use the test cases of the yxml library. 152 153**Table 3** Directory structure of the test directory 154 155<a name="table115941423164318"></a> 156<table><thead align="left"><tr id="row1059515236433"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p55950233437"><a name="p55950233437"></a><a name="p55950233437"></a>Directory</p> 157</th> 158<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p4595192324313"><a name="p4595192324313"></a><a name="p4595192324313"></a>Description</p> 159</th> 160</tr> 161</thead> 162<tbody><tr id="row1759572304318"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p17595323144319"><a name="p17595323144319"></a><a name="p17595323144319"></a>openHarmony/third_party/yxml/yxml/out/test/test.sh</p> 163</td> 164<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p25956235437"><a name="p25956235437"></a><a name="p25956235437"></a>Automatic test script, which cannot be used because <span id="text625264318457"><a name="text625264318457"></a><a name="text625264318457"></a>OpenHarmony</span> does not support automatic script execution. However, you can refer to this script to conduct a manual test.</p> 165</td> 166</tr> 167<tr id="row155951823184312"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p659572313436"><a name="p659572313436"></a><a name="p659572313436"></a>openHarmony/third_party/yxml/yxml/out/test/test</p> 168</td> 169<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p7595102314319"><a name="p7595102314319"></a><a name="p7595102314319"></a>Executable file for testing.</p> 170</td> 171</tr> 172<tr id="row359619238437"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p205969232436"><a name="p205969232436"></a><a name="p205969232436"></a>openHarmony/third_party/yxml/yxml/out/test/*.xml</p> 173</td> 174<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p559662344315"><a name="p559662344315"></a><a name="p559662344315"></a>Input test file.</p> 175</td> 176</tr> 177<tr id="row959613236439"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1159652312438"><a name="p1159652312438"></a><a name="p1159652312438"></a>openHarmony/third_party/yxml/yxml/out/test/*.out</p> 178</td> 179<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p18596182314318"><a name="p18596182314318"></a><a name="p18596182314318"></a>Expected output file.</p> 180</td> 181</tr> 182</tbody> 183</table> 184 185The content of the **test.sh** file is as follows: 186 187``` 188#!/bin/sh 189for i in *.xml; do 190 b=`basename $i .xml` 191 o=${b}.out 192 t=${b}.test 193 ./test <$i >$t 194 if [ -n "`diff -q $o $t`" ]; then 195 echo "Test failed for $i:" 196 diff -u $o $t 197 exit 1 198 fi 199done 200echo "All tests completed successfully." 201``` 202 203The 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. 204 205The 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. 206 2071. Execute the following command: 208 209 ``` 210 ./test 211 ``` 212 2132. Copy the content in the **_\*_.xml** file to shell. 214 215 Taking the **pi01.xml** file in the [test](#table115941423164318) directory as an example, copy the following content to shell and press **Enter**: 216 217 ``` 218 <?SomePI abc?><a/> 219 ``` 220 2213. Check whether the output in the shell is the same as that of the **_\*_.out** file in the [test](#table115941423164318) directory. 222 223 The output is as follows: 224 225 ``` 226 pistart SomePI 227 picontent abc 228 piend 229 elemstart a 230 elemend 231 ok 232 ``` 233 234 The output is the same as the **pi01.out** file in the [test](#table115941423164318) directory. The test is passed. 235 236 237## Adding the Compiled yxml Library to the OpenHarmony Project<a name="section1898016213406"></a> 238 239The 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). 240 241- The implementation of the newly added **BUILD.gn** file in the yxml library is as follows: 242 243``` 244import("config.gni") 245group("yxml") { 246 if (ohos_build_thirdparty_migrated_from_fuchisa == true) { 247 deps = [":make"] 248 } 249} 250if (ohos_build_thirdparty_migrated_from_fuchisa == true) { 251 action("make") { 252 script = "//third_party/yxml/build_thirdparty.py" 253 outputs = ["$target_out_dir/log_yxml.txt"] 254 exec_path = rebase_path(rebase_path("./yxml", root_build_dir)) 255 command = "make clean && $MAKE_COMMAND" 256 args = [ 257 "--path=$exec_path", 258 "--command=${command}" 259 ] 260 } 261} 262``` 263 264- The configuration of the newly added **config.gni** file in the yxml library is as follows: 265 266``` 267TEST_ENABLE = "YES" 268 269if (TEST_ENABLE == "YES") { 270 MAKE_COMMAND = "make test OHOS_SYSROOT_PATH=${ohos_root_path}prebuilts/lite/sysroot/" 271} else { 272 MAKE_COMMAND = "make OHOS_SYSROOT_PATH=${ohos_root_path}prebuilts/lite/sysroot/" 273} 274``` 275 276- The following table lists the directory structure of the OpenHarmony project. 277 278**Table 4** Directory structure of the ported library 279 280<a name="table13265185817173"></a> 281<table><thead align="left"><tr id="row92666583171"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p2266105816178"><a name="p2266105816178"></a><a name="p2266105816178"></a>Directory</p> 282</th> 283<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p32661158161718"><a name="p32661158161718"></a><a name="p32661158161718"></a>Description</p> 284</th> 285</tr> 286</thead> 287<tbody><tr id="row1526655816175"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p226605811710"><a name="p226605811710"></a><a name="p226605811710"></a>openHarmony/third_party/yxml/BUILD.gn</p> 288</td> 289<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1626675812177"><a name="p1626675812177"></a><a name="p1626675812177"></a>GN file for adding the third-party library to the <span id="text1699851716117"><a name="text1699851716117"></a><a name="text1699851716117"></a>OpenHarmony</span> project</p> 290</td> 291</tr> 292<tr id="row1726610589179"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p12266115815170"><a name="p12266115815170"></a><a name="p12266115815170"></a>openHarmony/third_party/yxml/build_thirdparty.py</p> 293</td> 294<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p122661958201719"><a name="p122661958201719"></a><a name="p122661958201719"></a>Script file for GN to call the <strong id="b8482154111113"><a name="b8482154111113"></a><a name="b8482154111113"></a>shell</strong> command to convert compilation from GN to Makefile.</p> 295</td> 296</tr> 297<tr id="row7266195891714"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p162665581170"><a name="p162665581170"></a><a name="p162665581170"></a>openHarmony/third_party/yxml/config.gni</p> 298</td> 299<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p626712588175"><a name="p626712588175"></a><a name="p626712588175"></a>Third-party library compilation configuration file, which can be modified to determine whether the test cases will be used during the building</p> 300</td> 301</tr> 302<tr id="row1272420109203"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p17725101052012"><a name="p17725101052012"></a><a name="p17725101052012"></a>openHarmony/third_party/yxml/yxml/</p> 303</td> 304<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p87252109205"><a name="p87252109205"></a><a name="p87252109205"></a>Directory of the third-party library to be ported</p> 305</td> 306</tr> 307</tbody> 308</table> 309 310