1
2# CMake
3LVGL supports integrating with [CMake](https://cmake.org/). It comes with preconfigured targets for:
4- [Espressif (ESP32)](https://docs.espressif.com/projects/esp-idf/en/v3.3/get-started-cmake/index.html)
5- [MicroPython](https://docs.micropython.org/en/v1.15/develop/cmodules.html)
6- [Zephyr](https://docs.zephyrproject.org/latest/guides/zephyr_cmake_package.html)
7
8On top of the preconfigured targets you can also use "plain" CMake to integrate LVGL into any custom C/C++ project.
9
10### Prerequisites
11- CMake ( >= 3.12.4 )
12- Compatible build tool e.g.
13  - [Make](https://www.gnu.org/software/make/)
14  - [Ninja](https://ninja-build.org/)
15
16## Building LVGL with CMake
17There are many ways to include external CMake projects into your own. A modern one also used in this example is the CMake [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module. This module conveniently allows us to download dependencies directly at configure time from e.g. [GitHub](https://github.com/). Here is an example how we might include LVGL into our own project.
18
19```cmake
20cmake_minimum_required(VERSION 3.14)
21include(FetchContent)
22
23project(MyProject LANGUAGES C CXX)
24
25# Build an executable called "MyFirmware"
26add_executable(MyFirmware src/main.c)
27
28# Specify path to own LVGL config header
29set(LV_CONF_PATH
30    ${CMAKE_CURRENT_SOURCE_DIR}/src/lv_conf.h
31    CACHE STRING "" FORCE)
32
33# Fetch LVGL from GitHub
34FetchContent_Declare(lvgl URL https://github.com/lvgl/lvgl.git)
35FetchContent_MakeAvailable(lvgl)
36
37# The target "MyFirmware" depends on LVGL
38target_link_libraries(MyFirmware PRIVATE lvgl::lvgl)
39```
40
41This configuration declares a dependency between the two targets **MyFirmware** and **lvgl**. Upon building the target **MyFirmware** this dependency will be resolved and **lvgl** will be built and linked with it. Since LVGL requires a config header called [lv_conf.h](https://github.com/lvgl/lvgl/blob/master/lv_conf_template.h) to be includable by its sources we also set the option `LV_CONF_PATH` to point to our own copy of it.
42
43### Additional CMake options
44Besides `LV_CONF_PATH` there are two additional CMake options to specify include paths.
45
46`LV_LVGL_H_INCLUDE_SIMPLE` which specifies whether to `#include "lvgl.h"` absolut or relative
47
48| ON (default) | OFF            |
49| ------------ | -------------- |
50| "lvgl.h"     | "../../lvgl.h" |
51
52`LV_CONF_INCLUDE_SIMPLE` which specifies whether to `#include "lv_conf.h"` and `"lv_drv_conf.h"` absolut or relative
53
54| ON (default)    | OFF                   |
55| --------------- | --------------------- |
56| "lv_conf.h"     | "../../lv_conf.h"     |
57| "lv_drv_conf.h" | "../../lv_drv_conf.h" |
58
59I do not recommend disabling those options unless your folder layout makes it absolutely necessary.
60
61## Building LVGL examples with CMake
62LVGL [examples](https://docs.lvgl.io/master/examples.html) have their own CMake target. If you want to build the examples simply add them to your dependencies.
63
64```cmake
65# The target "MyFirmware" depends on LVGL and examples
66target_link_libraries(MyFirmware PRIVATE lvgl::lvgl lvgl::examples)
67```
68
69## Building LVGL drivers and demos with CMake
70Exactly the same goes for the [drivers](https://github.com/lvgl/lv_drivers) and the [demos](https://github.com/lvgl/lvgl/demos).
71
72```cmake
73FetchContent_Declare(lv_drivers
74                     GIT_REPOSITORY https://github.com/lvgl/lv_drivers)
75FetchContent_MakeAvailable(lv_drivers)
76
77# The target "MyFirmware" depends on LVGL, drivers and demos
78target_link_libraries(MyFirmware PRIVATE lvgl::lvgl lvgl::drivers lvgl::examples)
79```
80
81# Build shared libraries with CMake
82By default, LVGL will be built as a static library (archive). CMake can instead be instructed to build LVGL as shared library (.so/.dll/etc.):
83```cmake
84set(BUILD_SHARED_LIBS ON)
85```
86OR
87```
88$ cmake "-DBUILD_SHARED_LIBS=ON" .
89```
90