Update README with latest docs modifications
[apps/app-templates.git] / README.md
1 # AGL CMake template
2
3 Files used to build an application, or binding, project with the
4 AGL Application Framework.
5
6 To build your AGL project using these templates, you have to install
7 them within your project and adjust compilation option in `config.cmake`.
8 For technical reasons, you also have to specify **cmake** target in
9 sub CMakeLists.txt installed. Make a globbing search to find source files
10 isn't recommended now to handle project build especially in a multiuser
11 project because CMake will not be aware of new or removed source files.
12
13 You'll find usage samples here:
14
15 - [helloworld-service](https://github.com/iotbzh/helloworld-service)
16 - [low-level-can-service](https://gerrit.automotivelinux.org/gerrit/apps/low-level-can-service)
17 - [high-level-viwi-service](https://github.com/iotbzh/high-level-viwi-service)
18 - [audio-binding](https://github.com/iotbzh/audio-binding)
19 - [unicens2-binding](https://github.com/iotbzh/unicens2-binding)
20
21 ## Quickstart
22
23 ### Initialization
24
25 To use these templates files on your project just install the reference files using
26 **git submodule** then use `config.cmake` file to configure your project specificities :
27
28 ```bash
29 git submodule add https://gerrit.automotivelinux.org/gerrit/p/apps/app-templates.git conf.d/app-templates
30 mkdir conf.d/cmake
31 cp conf.d/app-templates/samples.d/config.cmake.sample conf.d/cmake/config.cmake
32 ```
33
34 Edit the copied config.cmake file to fit your needs.
35
36 Now, create your top CMakeLists.txt file which include `config.cmake` file.
37
38 An example is available in **app-templates** submodule that you can copy and
39 use:
40
41 ```bash
42 cp conf.d/app-templates/samples.d/CMakeLists.txt.sample CMakeLists.txt
43 ```
44
45 ### Create your CMake targets
46
47 For each target part of your project, you need to use ***PROJECT_TARGET_ADD***
48 to include this target to your project.
49
50 Using it, make available the cmake variable ***TARGET_NAME*** until the next
51 ***PROJECT_TARGET_ADD*** is invoked with a new target name.
52
53 So, typical usage defining a target is:
54
55 ```cmake
56 PROJECT_TARGET_ADD(SuperExampleName) --> Adding target to your project
57
58 add_executable/add_library(${TARGET_NAME}.... --> defining your target sources
59
60 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES.... --> fit target properties
61 for macros usage
62 ```
63
64 ### Targets PROPERTIES
65
66 You should set properties on your targets that will be used to package your
67 apps in a widget file that could be installed on an AGL system.
68
69 Specify what is the type of your targets that you want to be included in the
70 widget package with the property **LABELS**:
71
72 Choose between:
73
74 - **BINDING**: Shared library that be loaded by the AGL Application Framework
75 - **BINDINGV2**: Shared library that be loaded by the AGL Application Framework
76  This has to be accompagnied with a JSON file named like the
77  *${OUTPUT_NAME}-apidef* of the target that describe the API with OpenAPI
78  syntax (e.g: *mybinding-apidef*). Or you can choose the name, without the
79  extension, by setting the *CACHE* cmake variable *OPENAPI_DEF* (***CAUTION***:
80  setting a CACHE variable is needed, or set a normal variable with the
81  *PARENT_SCOPE* option to make it visible for the parent scope where the target
82  is defined) JSON file will be used to generate header file using `afb-genskel`
83  tool.
84 - **PLUGIN**: Shared library meant to be used as a binding plugin. Binding
85  would load it as a plugin to extend its functionnalities. It should be named
86  with a special extension that you choose with SUFFIX cmake target property or
87  it'd be **.ctlso** by default.
88 - **HTDOCS**: Root directory of a web app. This target has to build its
89  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
90 - **DATA**: Resources used by your application. This target has to build its
91  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
92 - **EXECUTABLE**: Entry point of your application executed by the AGL
93  Application Framework
94 - **LIBRARY**: An external 3rd party library bundled with the binding for its
95  own purpose because platform doesn't provide it.
96
97 > **TIP** you should use the prefix _afb-_ with your **BINDING* targets which
98 > stand for **Application Framework Binding**.
99
100 ```cmake
101 SET_TARGET_PROPERTIES(${TARGET_NAME}
102         PREFIX "afb-"
103         LABELS "BINDING"
104         OUTPUT_NAME "file_output_name"
105 )
106 ```
107
108 > **NOTE**: You doesn't need to specify an **INSTALL** command for these
109 > targets. This is already handle by template and will be installed in the
110 > following path : **${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}**
111
112 ## More details: Typical project architecture
113
114 A typical project architecture would be :
115
116 ```tree
117 <project-root-path>
118
119 ├── conf.d/
120 │   ├── autobuild/
121 │   │   ├── agl
122 │   │   │   └── autobuild
123 │   │   ├── linux
124 │   │   │   └── autobuild
125 │   │   └── windows
126 │   │       └── autobuild
127 │   ├── app-templates/
128 │   │   ├── README.md
129 │   │   ├── cmake/
130 │   │   │   ├── export.map
131 │   │   │   └── macros.cmake
132 │   │   ├── samples.d/
133 │   │   │   ├── CMakeLists.txt.sample
134 │   │   │   ├── config.cmake.sample
135 │   │   │   ├── config.xml.in.sample
136 │   │   │   └── xds-config.env.sample
137 │   │   ├── template.d/
138 │   │   │   ├── autobuild/
139 │   │   │   │   ├── agl
140 │   │   │   │   │   └── autobuild.in
141 │   │   │   │   ├── linux
142 │   │   │   │   │   └── autobuild.in
143 │   │   │   │   └── windows
144 │   │   │   │       └── autobuild.in
145 │   │   │   ├── config.xml.in
146 │   │   │   ├── deb-config.dsc.in
147 │   │   │   ├── deb-config.install.in
148 │   │   │   ├── debian.changelog.in
149 │   │   │   ├── debian.compat.in
150 │   │   │   ├── debian.rules.in
151 │   │   │   ├── gdb-on-target.ini.in
152 │   │   │   ├── install-wgt-on-target.sh.in
153 │   │   │   ├── start-on-target.sh.in
154 │   │   │   ├── rpm-config.spec.in
155 │   │   │   └── xds-project-target.conf.in
156 │   │   └── wgt/
157 │   │       ├── icon-default.png
158 │   │       ├── icon-html5.png
159 │   │       ├── icon-native.png
160 │   │       ├── icon-qml.png
161 │   │       └── icon-service.png
162 │   ├── packaging/
163 │   │   ├── config.spec
164 │   │   └── config.deb
165 │   ├── cmake
166 │   │   └── config.cmake
167 │   └── wgt
168 │      └── config.xml.in
169 ├── <libs>
170 ├── <target>
171 │   └── <files>
172 ├── <target>
173 │   └── <file>
174 └── <target>
175     └── <files>
176 ```
177
178 | # | Parent | Description |
179 | - | -------| ----------- |
180 | \<root-path\> | - | Path to your project. Hold master CMakeLists.txt and general files of your projects. |
181 | conf.d | \<root-path\> | Holds needed files to build, install, debug, package an AGL app project |
182 | app-templates | conf.d | Git submodule to app-templates AGL repository which provides CMake helpers macros library, and build scripts. config.cmake is a copy of config.cmake.sample configured for the projects. SHOULD NOT BE MODIFIED MANUALLY !|
183 | autobuild | conf.d | Scripts generated from app-templates to build packages the same way for differents platforms.|
184 | cmake | conf.d | Contains at least config.cmake file modified from the sample provided in app-templates submodule. |
185 | wgt | conf.d | Contains at least config.xml.in template file modified from the sample provided in app-templates submodule for the needs of project (See config.xml.in.sample file for more details). |
186 | packaging | conf.d | Contains output files used to build packages. |
187 | \<libs\> | \<root-path\> | External dependencies libraries. This isn't to be used to include header file but build and link statically specifics libraries. | Library sources files. Can be a decompressed library archive file or project fork. |
188 | \<target\> | \<root-path\> | A target to build, typically library, executable, etc. |
189
190 ### Update app-templates submodule
191
192 You may have some news bug fixes or features available from app-templates
193 repository that you want. To update your submodule proceed like the following:
194
195 ```bash
196 git submodule update --remote
197 git commit -s conf.d/app-templates
198 ```
199
200 This will update the submodule to the HEAD of master branch repository.
201
202 You could just want to update at a specified repository tag or branch or commit
203 , here are the method to do so:
204
205 ```bash
206 cd conf.d/app-templates
207 # Choose one of the following depending what you want
208 git checkout <tag_name>
209 git checkout --detach <branch_name>
210 git checkout --detach <commit_id>
211 # Then commit
212 cd ../..
213 git commit -s conf.d/app-templates
214 ```
215
216 ### Build a widget
217
218 #### config.xml.in file
219
220 To build a widget you need a _config.xml_ file describing what is your apps and
221 how Application Framework would launch it. This repo provide a simple default
222 file _config.xml.in_ that should work for simple application without
223 interactions with others bindings.
224
225 It is recommanded that you use the sample one which is more complete. You can
226 find it at the same location under the name _config.xml.in.sample_ (stunning
227 isn't it). Just copy the sample file to your _conf.d/wgt_ directory and name it
228 _config.xml.in_, then edit it to fit your needs.
229
230 > ***CAUTION*** : The default file is only meant to be use for a
231 > simple widget app, more complicated ones which needed to export
232 > their api, or ship several app in one widget need to use the provided
233 > _config.xml.in.sample_ which had all new Application Framework
234 > features explained and examples.
235
236 #### Using cmake template macros
237
238 To leverage all cmake templates features, you have to specify ***properties***
239 on your targets. Some macros will not works without specifying which is the
240 target type.
241
242 As the type is not always specified for some custom targets, like an ***HTML5***
243 application, macros make the difference using ***LABELS*** property.
244
245 Choose between:
246
247 - **BINDING**: Shared library that be loaded by the AGL Application Framework
248 - **BINDINGV2**: Shared library that be loaded by the AGL Application Framework
249  This has to be accompagnied with a JSON file named like the
250  *${OUTPUT_NAME}-apidef* of the target that describe the API with OpenAPI
251  syntax (e.g: *mybinding-apidef*). Or you can choose the name, without the
252  extension, by setting the *CACHE* cmake variable *OPENAPI_DEF* (***CAUTION***:
253  setting a CACHE variable is needed, or set a normal variable with the
254  *PARENT_SCOPE* option to make it visible for the parent scope where the target
255  is defined) JSON file will be used to generate header file using `afb-genskel`
256  tool.
257 - **PLUGIN**: Shared library meant to be used as a binding plugin. Binding
258  would load it as a plugin to extend its functionnalities. It should be named
259  with a special extension that you choose with SUFFIX cmake target property or
260  it'd be **.ctlso** by default.
261 - **HTDOCS**: Root directory of a web app. This target has to build its
262  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
263 - **DATA**: Resources used by your application. This target has to build its
264  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
265 - **EXECUTABLE**: Entry point of your application executed by the AGL
266  Application Framework
267 - **LIBRARY**: An external 3rd party library bundled with the binding for its
268  own purpose because platform doesn't provide it.
269
270 > **TIP** you should use the prefix _afb-_ with your **BINDING* targets which
271 > stand for **Application Framework Binding**.
272
273 Example:
274
275 ```cmake
276 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
277                 LABELS "HTDOCS"
278                 OUTPUT_NAME dist.prod
279         )
280 ```
281
282 > **NOTE**: You doesn't need to specify an **INSTALL** command for these
283 > targets. This is already handle by template and will be installed in the
284 > following path : **${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}**
285
286 > **NOTE**: if you want to set and use `rpath` with your target you should use
287 > and set the target property `INSTALL_RPATH`.
288
289 ## Add external 3rd party library
290
291 ### Build, link and ship external library with the project
292
293 You could need to include an external library that isn't shipped in the
294 platform. Then you have to bundle the required library in the `lib` widget
295 directory.
296
297 Templates includes some facilities to help you to do so. Classic way to do so
298 is to declare as many CMake ExternalProject as library you need.
299
300 An ExternalProject is a special CMake module that let you define how to:
301 download, update, patch, configure, build and install an external project. It
302 doesn't have to be a CMake project and custom step could be added for special
303 needs using ExternalProject step. More informations on CMake [ExternalProject
304 documentation site](https://cmake.org/cmake/help/v3.5/module/ExternalProject.html?highlight=externalproject).
305
306 Example to include `mxml` library for [unicens2-binding](https://github.com/iotbzh/unicens2-binding)
307 project:
308
309 ```cmake
310 set(MXML external-mxml)
311 set(MXML_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mxml)
312 ExternalProject_Add(${MXML}
313     GIT_REPOSITORY https://github.com/michaelrsweet/mxml.git
314     GIT_TAG release-2.10
315     SOURCE_DIR ${MXML_SOURCE_DIR}
316     CONFIGURE_COMMAND ./configure --build x86_64 --host aarch64
317     BUILD_COMMAND make libmxml.so.1.5
318     BUILD_IN_SOURCE 1
319     INSTALL_COMMAND ""
320 )
321
322 PROJECT_TARGET_ADD(mxml)
323
324 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
325
326 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
327     LABELS LIBRARY
328     IMPORTED_LOCATION ${MXML_SOURCE_DIR}/libmxml.so.1
329     INTERFACE_INCLUDE_DIRECTORIES ${MXML_SOURCE_DIR}
330 )
331
332 add_dependencies(${TARGET_NAME} ${MXML})
333 ```
334
335 Here we define an external project that drive the build of the library then we
336 define new CMake target of type **IMPORTED**. Meaning that this target hasn't
337 been built using CMake but is available at the location defined in the target
338 property *IMPORTED_LOCATION*.
339
340 You could want to build the library as *SHARED* or *STATIC* depending on your needs
341 and goals. Then you only have to modify the external project configure step and change
342 filename used by **IMPORTED** library target defined after external project.
343
344 Then target *LABELS* property is set to **LIBRARY** to ship it in the widget.
345
346 Unicens project also need some header from this library, so we use the target
347 property *INTERFACE_INCLUDE_DIRECTORIES*. Setting that when another target link
348 to that imported target, it can access to the include directories.
349
350 We bound the target to the external project using a CMake dependency at last.
351
352 Then this target could be use like any other CMake target and be linked etc.
353
354 ### Only link and ship external library with the project
355
356 If you already have a binary version of the library that you want to use and you
357 can't or don't want to build the library then you only have to add an **IMPORTED**
358 library target.
359
360 So, taking the above example, `mxml` library inclusion would be:
361
362 ```cmake
363 PROJECT_TARGET_ADD(mxml)
364
365 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
366
367 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
368     LABELS LIBRARY
369     IMPORTED_LOCATION /path/to/library/libmxml.so.1
370     INTERFACE_INCLUDE_DIRECTORIES /path/to/mxml/include/dir
371 )
372 ```
373
374 Finally, you can link any other lib or executable target with this imported
375 library like any other target.
376
377 #### Macro reference
378
379 ##### PROJECT_TARGET_ADD
380
381 Typical usage would be to add the target to your project using macro
382 `PROJECT_TARGET_ADD` with the name of your target as parameter.
383
384 Example:
385
386 ```cmake
387 PROJECT_TARGET_ADD(low-can-demo)
388 ```
389
390 > ***NOTE***: This will make available the variable `${TARGET_NAME}`
391 > set with the specificied name. This variable will change at the next call
392 > to this macros.
393
394 ##### project_subdirs_add
395
396 This macro will search in all subfolder any `CMakeLists.txt` file. If found then
397 it will be added to your project. This could be use in an hybrid application by
398 example where the binding lay in a sub directory.
399
400 Usage :
401
402 ```cmake
403 project_subdirs_add()
404 ```
405
406 You also can specify a globbing pattern as argument to filter which folders
407 will be looked for.
408
409 To filter all directories that begin with a number followed by a dash the
410 anything:
411
412 ```cmake
413 project_subdirs_add("[0-9]-*")
414 ```
415
416 ## Advanced build customization
417
418 ### Including additionnals cmake files
419
420 #### Machine and system custom cmake files
421
422 Advanced tuning is possible using addionnals cmake files that are included
423 automatically from some specifics locations. They are included in that order:
424
425 - Project CMake files normaly located in _<project-root-path>/conf.d/app-templates/cmake/cmake.d_
426 - Home CMake files located in _$HOME/.config/app-templates/cmake.d_
427 - System CMake files located in _/etc/app-templates/cmake.d_
428
429 CMake files has to be named using the following convention: `XX-common*.cmake`
430 or `XX-${PROJECT_NAME}*.cmake`, where `XX` are numbers, `*` file name
431 (ie. `99-common-my_customs.cmake`).
432
433 > **NOTE** You need to specify after numbers that indicate include order, to
434 which project that file applies, if it applies to all project then use keyword
435 `common`.
436
437 So, saying that you should be aware that every normal cmake variables used at
438 project level could be overwrited by home or system located cmake files if
439 variables got the same name. Exceptions are cached variables set using
440 **CACHE** keyword:
441
442 Example:
443
444 ```cmake
445 set(VARIABLE_NAME 'value string random' CACHE STRING 'docstring')
446 ```
447
448 #### OS custom cmake files
449
450 This is meant to personalize the project depending on the OS your are using.
451 At the end of config.cmake, common.cmake will include lot of cmake file to
452 customize project build depending on your plateform. It will detect your OS
453 deducing it from file _/etc/os-release_ now as default in almost all Linux
454 distribution.
455
456 So you can use the value of field **ID_LIKE** or **ID** if the
457 first one doesn't exists and add a cmake file for that distribution in your
458 _conf.d/cmake/_ directory or relatively to your _app-templates_ submodule path
459 _app-templates/../cmake/_
460
461 Those files has to be named use the following scheme _XX-${OSRELEASE}*.cmake_
462 where _XX_ are numbers, ${OSRELEASE} the **ID_LIKE** or **ID** field from
463 _/etc/os-release_ file. You can also define default OS configuration file
464 to use as fallback is none specific OS configuration is available using the
465 scheme _XX-default*.cmake_. Then is you need by example a module that isn't
466 named the same in one distro only, you only has to define a specific file to
467 handle that case then for all the other case put the configuration in the
468 default file.
469
470 ### Include customs templated scripts
471
472 As well as for additionnals cmake files you can include your own templated
473 scripts that will be passed to cmake command `configure_file`.
474
475 Just create your own script to the following directories:
476
477 - Home location in _$HOME/.config/app-templates/scripts_
478 - System location in _/etc/app-templates/scripts_
479
480 Scripts only needs to use the extension `.in` to be parsed and configured by
481 CMake command.
482
483 ## Autobuild script usage
484
485 ### Generation
486
487 To be integrated in the Yocto build workflow you have to generate `autobuild`
488 scripts using _autobuild_ target.
489
490 To generate those scripts proceeds:
491
492 ```bash
493 mkdir -p build
494 cd build
495 cmake .. && make autobuild
496 ```
497
498 You should see _conf.d/autobuild/agl/autobuild_ file now.
499
500 ### Available targets
501
502 Here are the available targets available from _autobuild_ scripts:
503
504 - **clean** : clean build directory from object file and targets results.
505 - **distclean** : delete build directory
506 - **configure** : generate project Makefile from CMakeLists.txt files.
507 - **build** : compile all project targets.
508 - **package** : build and output a wgt package.
509
510 You can specify variables that modify the behavior of compilation using
511 the following variables:
512
513 - **CONFIGURE_ARGS** : Variable used at **configure** time.
514 - **BUILD_ARGS** : Variable used at **build** time.
515 - **DEST** : Directory where to output ***wgt*** file.
516
517 Variable as to be in CMake format. (ie: BUILD_ARGS="-DC_FLAGS='-g -O2'")
518
519 Usage example:
520
521 ```bash
522 ./conf.d/autobuild/wgt/autobuild package DEST=/tmp
523 ```