3ea8344563fabc2e2d4790c1693c3ac730cdc7a9
[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 recommanded 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 describe the API with OpenAPI
36  syntax (e.g: *mybinding-apidef*). Or you can choose the name, without the
37  extension, by setting the *CACHE* cmake variable *OPENAPI_DEF* (***CAUTION***:
38  setting a CACHE variable is needed, or set a normal variable with the
39  *PARENT_SCOPE* option to make it visible for the parent scope where the target
40  is defined) JSON file will be used to generate header file using `afb-genskel`
41  tool.
42 - **PLUGIN**: Shared library meant to be used as a binding plugin. Binding
43  would load it as a plugin to extend its functionnalities. It should be named
44  with a special extension that you choose with SUFFIX cmake target property or
45  it'd be **.ctlso** by default.
46 - **HTDOCS**: Root directory of a web app. This target has to build its
47  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
48 - **DATA**: Resources used by your application. This target has to build its
49  directory and puts its files in the ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
50 - **EXECUTABLE**: Entry point of your application executed by the AGL
51  Application Framework
52 - **LIBRARY**: An external 3rd party library bundled with the binding for its
53  own purpose because platform doesn't provide it.
54
55 > **TIP** you should use the prefix _afb-_ with your **BINDING* targets which
56 > stand for **Application Framework Binding**.
57
58 Example:
59
60 ```cmake
61 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
62                 LABELS "HTDOCS"
63                 OUTPUT_NAME dist.prod
64         )
65 ```
66
67 > **NOTE**: You doesn't need to specify an **INSTALL** command for these
68 > targets. This is already handle by template and will be installed in the
69 > following path : **${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}**
70
71 > **NOTE**: if you want to set and use `rpath` with your target you should use
72 > and set the target property `INSTALL_RPATH`.
73
74 ## Add external 3rd party library
75
76 ### Build, link and ship external library with the project
77
78 You could need to include an external library that isn't shipped in the
79 platform. Then you have to bundle the required library in the `lib` widget
80 directory.
81
82 Templates includes some facilities to help you to do so. Classic way to do so
83 is to declare as many CMake ExternalProject as library you need.
84
85 An ExternalProject is a special CMake module that let you define how to:
86 download, update, patch, configure, build and install an external project. It
87 doesn't have to be a CMake project and custom step could be added for special
88 needs using ExternalProject step. More informations on CMake [ExternalProject
89 documentation site](https://cmake.org/cmake/help/v3.5/module/ExternalProject.html?highlight=externalproject).
90
91 Example to include `mxml` library for [unicens2-binding](https://github.com/iotbzh/unicens2-binding)
92 project:
93
94 ```cmake
95 set(MXML external-mxml)
96 set(MXML_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mxml)
97 ExternalProject_Add(${MXML}
98     GIT_REPOSITORY https://github.com/michaelrsweet/mxml.git
99     GIT_TAG release-2.10
100     SOURCE_DIR ${MXML_SOURCE_DIR}
101     CONFIGURE_COMMAND ./configure --build x86_64 --host aarch64
102     BUILD_COMMAND make libmxml.so.1.5
103     BUILD_IN_SOURCE 1
104     INSTALL_COMMAND ""
105 )
106
107 PROJECT_TARGET_ADD(mxml)
108
109 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
110
111 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
112     LABELS LIBRARY
113     IMPORTED_LOCATION ${MXML_SOURCE_DIR}/libmxml.so.1
114     INTERFACE_INCLUDE_DIRECTORIES ${MXML_SOURCE_DIR}
115 )
116
117 add_dependencies(${TARGET_NAME} ${MXML})
118 ```
119
120 Here we define an external project that drive the build of the library then we
121 define new CMake target of type **IMPORTED**. Meaning that this target hasn't
122 been built using CMake but is available at the location defined in the target
123 property *IMPORTED_LOCATION*.
124
125 You could want to build the library as *SHARED* or *STATIC* depending on your needs
126 and goals. Then you only have to modify the external project configure step and change
127 filename used by **IMPORTED** library target defined after external project.
128
129 Then target *LABELS* property is set to **LIBRARY** to ship it in the widget.
130
131 Unicens project also need some header from this library, so we use the target
132 property *INTERFACE_INCLUDE_DIRECTORIES*. Setting that when another target link
133 to that imported target, it can access to the include directories.
134
135 We bound the target to the external project using a CMake dependency at last.
136
137 Then this target could be use like any other CMake target and be linked etc.
138
139 ### Only link and ship external library with the project
140
141 If you already have a binary version of the library that you want to use and you
142 can't or don't want to build the library then you only have to add an **IMPORTED**
143 library target.
144
145 So, taking the above example, `mxml` library inclusion would be:
146
147 ```cmake
148 PROJECT_TARGET_ADD(mxml)
149
150 add_library(${TARGET_NAME} SHARED IMPORTED GLOBAL)
151
152 SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
153     LABELS LIBRARY
154     IMPORTED_LOCATION /path/to/library/libmxml.so.1
155     INTERFACE_INCLUDE_DIRECTORIES /path/to/mxml/include/dir
156 )
157 ```
158
159 Finally, you can link any other lib or executable target with this imported
160 library like any other target.
161
162 ## Macro reference
163
164 ### PROJECT_TARGET_ADD
165
166 Typical usage would be to add the target to your project using macro
167 `PROJECT_TARGET_ADD` with the name of your target as parameter.
168
169 Example:
170
171 ```cmake
172 PROJECT_TARGET_ADD(low-can-demo)
173 ```
174
175 > ***NOTE***: This will make available the variable `${TARGET_NAME}`
176 > set with the specificied name. This variable will change at the next call
177 > to this macros.
178
179 ### project_subdirs_add
180
181 This macro will search in all subfolder any `CMakeLists.txt` file. If found then
182 it will be added to your project. This could be use in an hybrid application by
183 example where the binding lay in a sub directory.
184
185 Usage :
186
187 ```cmake
188 project_subdirs_add()
189 ```
190
191 You also can specify a globbing pattern as argument to filter which folders
192 will be looked for.
193
194 To filter all directories that begin with a number followed by a dash the
195 anything:
196
197 ```cmake
198 project_subdirs_add("[0-9]-*")
199 ```