5 https://git.automotivelinux.org/src/cmake-apps-module/plain/docs/dev_guide/advanced-usage.md?h=master
8 <!-- WARNING: This file is generated by fetch_docs.js using /home/boron/Documents/AGL/docs-webtemplate/site/_data/tocs/devguides/master/cmake-apps-module-guides-devguides-book.yml -->
12 This topic describes some advanced ways of using the CMake templates.
16 To build a widget, you need a `config.xml` file that describes
17 your application (widget) and how the Application Framework launches it.
18 Your repository contains a simple default file named
19 `config.xml.in` that should work for simple applications and should
20 not require interactions with other bindings.
22 It is also recommended that you use the sample configuration
23 file that you can find in the location.
24 This file is named `config.xms.in.sample` and is more complete.
25 Copy the sample file to your `conf.d/wgt` directory and name it
27 Once you have your copy, edit the file to fit your needs.
29 **CAUTION:** The default file is only meant to be used for a
30 simple widget application.
31 For more complicated applications that need to export
32 their API, or ship several applications in one widget
33 need to use the provided `config.xml.in.sample` file, which has
34 all new Application Framework features explained and provides
37 ## Using CMake Template Macros
39 To leverage all CMake template features, you must specify properties
41 Some macros do not work unless you specify the target type.
42 If you do not specify a type (e.g. a custom target such as an
43 HTML5 application), the macro uses the `LABELS` property to
44 determine the target type.
46 The `LABELS` property can be set to the values shown in the
47 [Target Properties](configuring-cmake.html#target-properties)
50 Aside from those values, the following optional values can be
51 assigned to the `LABELS` property.
52 These values define the resource types that make up your test materials:
54 - **TEST-CONFIG**: JSON configuration files used by the `afb-test`
56 These files execute the tests.
57 - **TEST-DATA**: Resources used to test your binding.
58 Minimally, you need a test plan.
59 You should also consider fixtures and any files required by your tests.
60 These required files appear as part of a separate test widget.
61 - **TEST-PLUGIN**: A shared library used as a binding plugin.
62 A binding loads the library as a plugin to extend the binding's functionality.
63 You should use a special file extension when you name the library
64 by using the `SUFFIX` CMake target property.
65 If you do not choose an extension, `.ctlso` is used by default.
66 - **TEST-HTDOCS**: The root directory of a web application.
67 This target has to build its directory and put its files in
68 the `${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}` directory.
69 - **TEST-EXECUTABLE**: The entry point of your application executed by the AGL
70 Application Framework.
71 - **TEST-LIBRARY**: An external third-party library bundled with the binding
73 The platform does not provide this library.
75 Following is a mapping between `LABELS` and directories where files reside in
78 - **EXECUTABLE** : \<wgtrootdir\>/bin
79 - **BINDING-CONFIG** : \<wgtrootdir\>/etc
80 - **BINDING** | **BINDINGV2** | **BINDINGV3** | **LIBRARY** : \<wgtrootdir\>/lib
81 - **PLUGIN** : \<wgtrootdir\>/lib/plugins
82 - **HTDOCS** : \<wgtrootdir\>/htdocs
83 - **BINDING-DATA** : \<wgtrootdir\>/var
84 - **DATA** : \<wgtrootdir\>/var
86 Following is a mapping between test-dedicated `LABELS` and directories where
87 files reside in the widget:
89 - **TEST-EXECUTABLE** : \<wgtrootdir\>/bin
90 - **TEST-CONFIG** : \<TESTwgtrootdir\>/etc
91 - **TEST-PLUGIN** : \<wgtrootdir\>/lib/plugins
92 - **TEST-HTDOCS** : \<wgtrootdir\>/htdocs
93 - **TEST-DATA** : \<TESTwgtrootdir\>/var
95 **TIP:** Use the prefix `afb-` (Application Framework Binding)
96 with your **BINDING** targets.
98 Following is an example that sets the `LABELS` and `OUTPUT_NAME` properties:
101 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
103 OUTPUT_NAME dist.prod
107 **NOTE**: You do not need to specify an **INSTALL** command for these
109 Installation is handled by the template and installs using the
110 following path : **${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}**
112 Also, if you want to set and use `rpath` with your target, you should use
113 and set the target property `INSTALL_RPATH`.
115 ## Adding an External Third-Party Library
117 You can add an external third-party library that is built, linked,
118 and shipped with the project.
119 Or, you can link and ship the library only with the project.
121 ### Building, Linking, and Shipping an External Library with the Project
123 If you need to include an external library that is not shipped
124 with the project, you can bundle the required library in the
125 `lib` widget directory.
127 Templates includes facilities to help you work with external
129 A standard method is to declare as many CMake ExternalProject
130 modules as you need to match the number of needed libraries.
132 An ExternalProject module is a special CMake module that lets you define how
133 to download, update, patch, configure, build, and install an external project.
134 The project does not need to be a CMake project.
135 Additionally, you can provide custom steps to account for special
136 needs using ExternalProject step.
138 [ExternalProject documentation site](https://cmake.org/cmake/help/v3.5/module/ExternalProject.html?highlight=externalproject)
139 for more information.
141 Following is an example that includes the `mxml` library for the
142 [unicens2-binding](https://github.com/iotbzh/unicens2-binding)
146 set(MXML external-mxml)
147 set(MXML_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mxml)
148 ExternalProject_Add(${MXML}
149 GIT_REPOSITORY https://github.com/michaelrsweet/mxml.git
151 SOURCE_DIR ${MXML_SOURCE_DIR}
152 CONFIGURE_COMMAND ./configure --build x86_64 --host aarch64
153 BUILD_COMMAND make libmxml.so.1.5
158 PROJECT_TARGET_ADD(mxml)
160 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
162 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
164 IMPORTED_LOCATION ${MXML_SOURCE_DIR}/libmxml.so.1
165 INTERFACE_INCLUDE_DIRECTORIES ${MXML_SOURCE_DIR}
168 add_dependencies(${TARGET_NAME} ${MXML})
171 The example defines an external project that drives the building of the library.
172 The example also defines a new CMake target whose type is **IMPORTED**.
173 The **IMPORTED** target type indicates the target has yet to be built using
174 CMake but is available at the location defined using the **IMPORTED_LOCATION**
177 You might want to build the library as **SHARED** or **STATIC** depending on your needs
179 Next, the example only has to modify the external project configure step and change
180 the filename used by **IMPORTED** library target defined after external project.
182 The target's **LABELS** property is set to **LIBRARY** to ship it in the widget.
184 In this example, the Unicens project also needs header
185 information from this library.
186 Consequently, the **INTERFACE_INCLUDE_DIRECTORIES** target property
188 Setting that property when another target links to that imported target
189 allows access to included directories.
191 Finally, the example binds the target to the external project
192 by using a CMake dependency.
194 The target can now be linked and used like any other CMake target.
196 ### Link and Ship an External Library with the Project
198 If you already have a binary version of the library that you want to use and you
199 cannot or do not want to build the library, you can use the **IMPORTED**
202 To illustrate, consider the same example in the previous section.
203 Following are the relevant modifications:
206 PROJECT_TARGET_ADD(mxml)
208 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
210 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
212 IMPORTED_LOCATION /path_to_library/libmxml.so.1
213 INTERFACE_INCLUDE_DIRECTORIES /path_to_mxml/include/dir
217 In the previous example, notice the changes to the
218 `IMPORTED_LOCATION` and `INTERFACE_INCLUDE_DIRECTORIES` statements.
219 These locate the binary version of the library.
221 Finally, you can link any other library or executable target with this imported
222 library just as you would for any other target.
226 Following are several macros that are useful for advanced CMake usage.
228 ### PROJECT_TARGET_ADD
230 This macro adds the target to your project.
231 Following is a typical example that adds the target to your project.
232 You need to provide the name of your target as the macro's parameter:
237 PROJECT_TARGET_ADD(low-can-demo)
240 The macro makes the variable `${TARGET_NAME}` available and it is defined
241 using the specified name (e.g. `low-can-demo`).
242 The variable changes each time the `PROJECT_TARGET_ADD` macro is called.
244 ### project_subdirs_add
246 This macro searches within specified subfolders of the CMake project for
247 any `CMakeLists.txt` file.
248 If the file is found, it is added to your project.
249 You can use this macro in a hybrid application (e.g. where the binding
250 exists in a subfolder).
252 The following example searches within all subfolders:
257 project_subdirs_add()
260 You might want to restrict the subfolders being searched.
261 If so, you can specify a
262 [globbing](https://en.wikipedia.org/wiki/Glob_(programming)) pattern
264 Doing so effectively creates a search filter.
266 Following is an example that specifies all directories that begin
267 with a number, are followed by the dash character, and then followed
271 project_subdirs_add("[0-9]-*")
274 ### set_openapi_filename
276 This macro is used with a **BINDINGV2** target and defines the
277 binding definition filename.
278 You can use it to also define a relative path to
279 the current `CMakeLists.txt` file.
281 If you do not use this macro to specify the name of your definition file,
282 the default one is used, which is `${OUTPUT_NAME}-apidef` and uses
283 **OUTPUT_NAME** as the [target property].
285 **CAUTION** When specifying the binding definition filename,
286 you must not use the file's extension as part of the name.
287 Following is an example:
290 set_openapi_filename('binding/mybinding_definition')
293 [target property]: https://cmake.org/cmake/help/v3.6/prop_tgt/OUTPUT_NAME.html "OUTPUT_NAME property documentation"
297 This macro creates a custom target dedicated for HTML5 and data resource files.
298 The macro provides syntax and schema verification for different languages that
299 include LUA, JSON and XML.
301 Alongside the macro are tools used to check files.
302 You can configure the tools by setting the
305 - XML_CHECKER: Uses **xmllint** that is provided with major linux distributions.
306 - LUA_CHECKER: Uses **luac** that is provided with major linux distributions.
307 - JSON_CHECKER: Currently, not used by any tools.
309 Following is an example:
312 add_input_file("${MY_FILES_LIST}")
315 **NOTE**: If an issue occurs during the "check" step of the macro,