Use app-templates: Minor grammar fix and spacing updates.
[apps/app-templates.git] / docs / dev_guide / 3_advanced_usage.md
1 # Build a widget
2
3 ## config.xml.in file
4
5 To build a widget you need a _config.xml_ file describing what is your apps and
6 how Application Framework would launch it. This repo provide a simple default
7 file _config.xml.in_ that should work for simple application without
8 interactions with others bindings.
9
10 It is recommended that you use the sample one which is more complete. You can
11 find it at the same location under the name _config.xml.in.sample_ (stunning
12 isn't it). Just copy the sample file to your _conf.d/wgt_ directory and name it
13 _config.xml.in_, then edit it to fit your needs.
14
15 > ***CAUTION*** : The default file is only meant to be use for a
16 > simple widget app, more complicated ones which needed to export
17 > their api, or ship several app in one widget need to use the provided
18 > _config.xml.in.sample_ which had all new Application Framework
19 > features explained and examples.
20
21 ## Using cmake template macros
22
23 To leverage all cmake templates features, you have to specify ***properties***
24 on your targets. Some macros will not works without specifying which is the
25 target type.
26
27 As the type is not always specified for some custom targets, like an ***HTML5***
28 application, macros make the difference using ***LABELS*** property.
29
30 Choose between:
31
32 - **BINDING**: Shared library that be loaded by the AGL Application Framework
33 - **BINDINGV2**: Shared library that be loaded by the AGL Application Framework
34  This has to be accompagnied with a JSON file named like the
35  *${OUTPUT_NAME}-apidef* of the target that describes the API with OpenAPI
36  syntax (e.g: *mybinding-apidef*).
37  Or Alternatively, you can choose the name, without the extension, using macro
38  **set_openapi_filename**. If you use C++, you have to set **PROJECT_LANGUAGES**
39  to *CXX*.
40 - **BINDINGV3**: Shared library that be loaded by the AGL Application Framework
41  This has to be accompagnied with a JSON file named like the
42  *${OUTPUT_NAME}-apidef* of the target that describes the API with OpenAPI
43  syntax (e.g: *mybinding-apidef*).
44  Or Alternatively, you can choose the name, without the extension, using macro
45  **set_openapi_filename**. If you use C++, you have to set **PROJECT_LANGUAGES**
46  to *CXX*.
47 - **PLUGIN**: Shared library are meant to be used as a binding plugin. A binding
48  would load it as a plugin to extend its functionnalities. It should be named
49  with a special extension that you choose with SUFFIX cmake target property or
50  it'd be **.ctlso** by default.
51 - **HTDOCS**: Root directory of a web app. This target has to build its
52  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
53 - **DATA**: Resources used by your application. This target has to build its
54  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
55 - **EXECUTABLE**: Entry point of your application executed by the AGL
56  Application Framework
57 - **LIBRARY**: An external 3rd party library bundled with the binding for its
58  own purpose because platform doesn't provide it.
59 - **BINDING-CONFIG**: Any files used as configuration by your binding.
60
61 Optional **LABELS** are available to define which resources type your test
62 materials are:
63
64 - **TEST-CONFIG**: JSON configuration files that will be used by the afb-test
65  binding to know how to execute tests.
66 - **TEST-DATA**: Resources used to test your binding. It is at least your test
67  plan and also could be fixtures and any needed files by your tests. These files
68  will appear in a separate test widget.
69 - **TEST-PLUGIN**: Shared library are meant to be used as a binding
70  plugin. A binding would load it as a plugin to extend its functionalities. It
71  should be named with a special extension that you choose with SUFFIX cmake
72  target property or it'd be **.ctlso** by default.
73 - **TEST-HTDOCS**: Root directory of a web app. This target has to build its
74  directory and put its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
75 - **TEST-EXECUTABLE**: Entry point of your application executed by the AGL
76  Application Framework
77 - **TEST-LIBRARY**: An external 3rd party library bundled with the binding for its
78  own use in case of platform doesn't provide it.
79
80 Here is a mapping between LABELS and directories where files will be placed in
81 the widget:
82
83 - **EXECUTABLE** : \<wgtrootdir\>/bin
84 - **BINDING-CONFIG** : \<wgtrootdir\>/etc
85 - **BINDING** | **BINDINGV2** | **BINDINGV3** | **LIBRARY** : \<wgtrootdir\>/lib
86 - **PLUGIN** : \<wgtrootdir\>/lib/plugins
87 - **HTDOCS** : \<wgtrootdir\>/htdocs
88 - **BINDING-DATA** : \<wgtrootdir\>/var
89 - **DATA** : \<wgtrootdir\>/var
90
91 And about test dedicated **LABELS**:
92
93 - **TEST-EXECUTABLE** : \<wgtrootdir\>/bin
94 - **TEST-CONFIG** : \<TESTwgtrootdir\>/etc
95 - **TEST-PLUGIN** : \<wgtrootdir\>/lib/plugins
96 - **TEST-HTDOCS** : \<wgtrootdir\>/htdocs
97 - **TEST-DATA** : \<TESTwgtrootdir\>/var
98
99 > **TIP** you should use the prefix _afb-_ with your **BINDING* targets which
100 > stand for **Application Framework Binding**.
101
102 Example:
103
104 ```cmake
105 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
106                 LABELS "HTDOCS"
107                 OUTPUT_NAME dist.prod
108         )
109 ```
110
111 > **NOTE**: You doesn't need to specify an **INSTALL** command for these
112 > targets. This is already handle by template and will be installed in the
113 > following path : **${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}**
114
115 > **NOTE**: if you want to set and use `rpath` with your target you should use
116 > and set the target property `INSTALL_RPATH`.
117
118 ## Add external 3rd party library
119
120 ### Build, link and ship external library with the project
121
122 You could need to include an external library that isn't shipped in the
123 platform. Then you have to bundle the required library in the `lib` widget
124 directory.
125
126 Templates includes some facilities to help you to do so. Classic way to do so
127 is to declare as many CMake ExternalProject as library you need.
128
129 An ExternalProject is a special CMake module that let you define how to:
130 download, update, patch, configure, build and install an external project. It
131 doesn't have to be a CMake project and custom step could be added for special
132 needs using ExternalProject step. More informations on CMake [ExternalProject
133 documentation site](https://cmake.org/cmake/help/v3.5/module/ExternalProject.html?highlight=externalproject).
134
135 Example to include `mxml` library for [unicens2-binding](https://github.com/iotbzh/unicens2-binding)
136 project:
137
138 ```cmake
139 set(MXML external-mxml)
140 set(MXML_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mxml)
141 ExternalProject_Add(${MXML}
142     GIT_REPOSITORY https://github.com/michaelrsweet/mxml.git
143     GIT_TAG release-2.10
144     SOURCE_DIR ${MXML_SOURCE_DIR}
145     CONFIGURE_COMMAND ./configure --build x86_64 --host aarch64
146     BUILD_COMMAND make libmxml.so.1.5
147     BUILD_IN_SOURCE 1
148     INSTALL_COMMAND ""
149 )
150
151 PROJECT_TARGET_ADD(mxml)
152
153 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
154
155 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
156     LABELS LIBRARY
157     IMPORTED_LOCATION ${MXML_SOURCE_DIR}/libmxml.so.1
158     INTERFACE_INCLUDE_DIRECTORIES ${MXML_SOURCE_DIR}
159 )
160
161 add_dependencies(${TARGET_NAME} ${MXML})
162 ```
163
164 Here we define an external project that drive the build of the library then we
165 define new CMake target of type **IMPORTED**. Meaning that this target hasn't
166 been built using CMake but is available at the location defined in the target
167 property *IMPORTED_LOCATION*.
168
169 You could want to build the library as *SHARED* or *STATIC* depending on your needs
170 and goals. Then you only have to modify the external project configure step and change
171 filename used by **IMPORTED** library target defined after external project.
172
173 Then target *LABELS* property is set to **LIBRARY** to ship it in the widget.
174
175 Unicens project also need some header from this library, so we use the target
176 property *INTERFACE_INCLUDE_DIRECTORIES*. Setting that when another target link
177 to that imported target, it can access to the include directories.
178
179 We bound the target to the external project using a CMake dependency at last.
180
181 Then this target could be use like any other CMake target and be linked etc.
182
183 ### Only link and ship external library with the project
184
185 If you already have a binary version of the library that you want to use and you
186 can't or don't want to build the library then you only have to add an **IMPORTED**
187 library target.
188
189 So, taking the above example, `mxml` library inclusion would be:
190
191 ```cmake
192 PROJECT_TARGET_ADD(mxml)
193
194 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
195
196 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
197     LABELS LIBRARY
198     IMPORTED_LOCATION /path/to/library/libmxml.so.1
199     INTERFACE_INCLUDE_DIRECTORIES /path/to/mxml/include/dir
200 )
201 ```
202
203 Finally, you can link any other lib or executable target with this imported
204 library like any other target.
205
206 ## Macro reference
207
208 ### PROJECT_TARGET_ADD
209
210 Typical usage would be to add the target to your project using macro
211 `PROJECT_TARGET_ADD` with the name of your target as parameter.
212
213 Example:
214
215 ```cmake
216 PROJECT_TARGET_ADD(low-can-demo)
217 ```
218
219 > ***NOTE***: This will make available the variable `${TARGET_NAME}`
220 > set with the specificied name. This variable will change at the next call
221 > to this macros.
222
223 ### project_subdirs_add
224
225 This macro will search in all subfolder any `CMakeLists.txt` file. If found then
226 it will be added to your project. This could be use in an hybrid application by
227 example where the binding lay in a sub directory.
228
229 Usage :
230
231 ```cmake
232 project_subdirs_add()
233 ```
234
235 You also can specify a globbing pattern as argument to filter which folders
236 will be looked for.
237
238 To filter all directories that begin with a number followed by a dash the
239 anything:
240
241 ```cmake
242 project_subdirs_add("[0-9]-*")
243 ```
244
245 ### set_openapi_filename
246
247 Used with a target labelized **BINDINGV2** to define the file name, and
248 possibly a relative path with the current *CMakeLists.txt*.
249
250 If you don't use that macro to specify the name of your definition file
251 then the default one will be used, *${OUTPUT_NAME}-apidef* with
252 **OUTPUT_NAME** as the [target property].
253
254 > **CAUTION** you must only specify the name **WITHOUT** the extension.
255
256 ```cmake
257 set_openapi_filename('binding/mybinding_definition')
258 ```
259
260 [target property]: https://cmake.org/cmake/help/v3.6/prop_tgt/OUTPUT_NAME.html "OUTPUT_NAME property documentation"
261
262 ### add_input_files
263
264 Create custom target dedicated for HTML5 and data resource files. This macro
265 provides syntax and schema verification for differents languages which are
266 about now: LUA, JSON and XML.
267
268 You could change the tools used to check files with the following variables:
269
270 - XML_CHECKER: set to use **xmllint** provided with major linux distribution.
271 - LUA_CHECKER: set to use **luac** provided with major linux distribution.
272 - JSON_CHECKER: no tools found at the moment.
273
274 ```cmake
275 add_input_file("${MY_FILES_LIST}")
276 ```
277
278 > **NOTE**: an issue at the check step on a file will stop at the build step.