xref: /OK3568_Linux_fs/buildroot/docs/manual/adding-packages-cmake.txt (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun// -*- mode:doc; -*-
2*4882a593Smuzhiyun// vim: set syntax=asciidoc:
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun=== Infrastructure for CMake-based packages
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun[[cmake-package-tutorial]]
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun==== +cmake-package+ tutorial
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunFirst, let's see how to write a +.mk+ file for a CMake-based package,
11*4882a593Smuzhiyunwith an example :
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun------------------------
14*4882a593Smuzhiyun01: ################################################################################
15*4882a593Smuzhiyun02: #
16*4882a593Smuzhiyun03: # libfoo
17*4882a593Smuzhiyun04: #
18*4882a593Smuzhiyun05: ################################################################################
19*4882a593Smuzhiyun06:
20*4882a593Smuzhiyun07: LIBFOO_VERSION = 1.0
21*4882a593Smuzhiyun08: LIBFOO_SOURCE = libfoo-$(LIBFOO_VERSION).tar.gz
22*4882a593Smuzhiyun09: LIBFOO_SITE = http://www.foosoftware.org/download
23*4882a593Smuzhiyun10: LIBFOO_INSTALL_STAGING = YES
24*4882a593Smuzhiyun11: LIBFOO_INSTALL_TARGET = NO
25*4882a593Smuzhiyun12: LIBFOO_CONF_OPTS = -DBUILD_DEMOS=ON
26*4882a593Smuzhiyun13: LIBFOO_DEPENDENCIES = libglib2 host-pkgconf
27*4882a593Smuzhiyun14:
28*4882a593Smuzhiyun15: $(eval $(cmake-package))
29*4882a593Smuzhiyun------------------------
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunOn line 7, we declare the version of the package.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunOn line 8 and 9, we declare the name of the tarball (xz-ed tarball recommended)
34*4882a593Smuzhiyunand the location of the tarball on the Web. Buildroot will automatically
35*4882a593Smuzhiyundownload the tarball from this location.
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunOn line 10, we tell Buildroot to install the package to the staging
38*4882a593Smuzhiyundirectory. The staging directory, located in +output/staging/+
39*4882a593Smuzhiyunis the directory where all the packages are installed, including their
40*4882a593Smuzhiyundevelopment files, etc. By default, packages are not installed to the
41*4882a593Smuzhiyunstaging directory, since usually, only libraries need to be installed in
42*4882a593Smuzhiyunthe staging directory: their development files are needed to compile
43*4882a593Smuzhiyunother libraries or applications depending on them. Also by default, when
44*4882a593Smuzhiyunstaging installation is enabled, packages are installed in this location
45*4882a593Smuzhiyunusing the +make install+ command.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunOn line 11, we tell Buildroot to not install the package to the
48*4882a593Smuzhiyuntarget directory. This directory contains what will become the root
49*4882a593Smuzhiyunfilesystem running on the target. For purely static libraries, it is
50*4882a593Smuzhiyunnot necessary to install them in the target directory because they will
51*4882a593Smuzhiyunnot be used at runtime. By default, target installation is enabled; setting
52*4882a593Smuzhiyunthis variable to NO is almost never needed. Also by default, packages are
53*4882a593Smuzhiyuninstalled in this location using the +make install+ command.
54*4882a593Smuzhiyun
55*4882a593SmuzhiyunOn line 12, we tell Buildroot to pass custom options to CMake when it is
56*4882a593Smuzhiyunconfiguring the package.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunOn line 13, we declare our dependencies, so that they are built
59*4882a593Smuzhiyunbefore the build process of our package starts.
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunFinally, on line line 15, we invoke the +cmake-package+
62*4882a593Smuzhiyunmacro that generates all the Makefile rules that actually allows the
63*4882a593Smuzhiyunpackage to be built.
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun[[cmake-package-reference]]
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun==== +cmake-package+ reference
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunThe main macro of the CMake package infrastructure is
70*4882a593Smuzhiyun+cmake-package+. It is similar to the +generic-package+ macro. The ability to
71*4882a593Smuzhiyunhave target and host packages is also available, with the
72*4882a593Smuzhiyun+host-cmake-package+ macro.
73*4882a593Smuzhiyun
74*4882a593SmuzhiyunJust like the generic infrastructure, the CMake infrastructure works
75*4882a593Smuzhiyunby defining a number of variables before calling the +cmake-package+
76*4882a593Smuzhiyunmacro.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunFirst, all the package metadata information variables that exist in
79*4882a593Smuzhiyunthe generic infrastructure also exist in the CMake infrastructure:
80*4882a593Smuzhiyun+LIBFOO_VERSION+, +LIBFOO_SOURCE+, +LIBFOO_PATCH+, +LIBFOO_SITE+,
81*4882a593Smuzhiyun+LIBFOO_SUBDIR+, +LIBFOO_DEPENDENCIES+, +LIBFOO_INSTALL_STAGING+,
82*4882a593Smuzhiyun+LIBFOO_INSTALL_TARGET+.
83*4882a593Smuzhiyun
84*4882a593SmuzhiyunA few additional variables, specific to the CMake infrastructure, can
85*4882a593Smuzhiyunalso be defined. Many of them are only useful in very specific cases,
86*4882a593Smuzhiyuntypical packages will therefore only use a few of them.
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun* +LIBFOO_SUBDIR+ may contain the name of a subdirectory inside the
89*4882a593Smuzhiyun  package that contains the main CMakeLists.txt file. This is useful,
90*4882a593Smuzhiyun  if for example, the main CMakeLists.txt file is not at the root of
91*4882a593Smuzhiyun  the tree extracted by the tarball. If +HOST_LIBFOO_SUBDIR+ is not
92*4882a593Smuzhiyun  specified, it defaults to +LIBFOO_SUBDIR+.
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun* +LIBFOO_CONF_ENV+, to specify additional environment variables to
95*4882a593Smuzhiyun  pass to CMake. By default, empty.
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun* +LIBFOO_CONF_OPTS+, to specify additional configure options to pass
98*4882a593Smuzhiyun  to CMake. By default, empty. A number of common CMake options are
99*4882a593Smuzhiyun  set by the +cmake-package+ infrastructure; so it is normally not
100*4882a593Smuzhiyun  necessary to set them in the package's +*.mk+ file unless you want
101*4882a593Smuzhiyun  to override them:
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun** +CMAKE_BUILD_TYPE+ is driven by +BR2_ENABLE_RUNTIME_DEBUG+;
104*4882a593Smuzhiyun** +CMAKE_INSTALL_PREFIX+;
105*4882a593Smuzhiyun** +BUILD_SHARED_LIBS+ is driven by +BR2_STATIC_LIBS+;
106*4882a593Smuzhiyun** +BUILD_DOC+, +BUILD_DOCS+ are disabled;
107*4882a593Smuzhiyun** +BUILD_EXAMPLE+, +BUILD_EXAMPLES+ are disabled;
108*4882a593Smuzhiyun** +BUILD_TEST+, +BUILD_TESTS+, +BUILD_TESTING+ are disabled.
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun* +LIBFOO_SUPPORTS_IN_SOURCE_BUILD = NO+ should be set when the package
111*4882a593Smuzhiyun  cannot be built inside the source tree but needs a separate build
112*4882a593Smuzhiyun  directory.
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun* +LIBFOO_MAKE+, to specify an alternate +make+ command. This is
115*4882a593Smuzhiyun  typically useful when parallel make is enabled in the configuration
116*4882a593Smuzhiyun  (using +BR2_JLEVEL+) but that this feature should be disabled for
117*4882a593Smuzhiyun  the given package, for one reason or another. By default, set to
118*4882a593Smuzhiyun  +$(MAKE)+. If parallel building is not supported by the package,
119*4882a593Smuzhiyun  then it should be set to +LIBFOO_MAKE=$(MAKE1)+.
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun* +LIBFOO_MAKE_ENV+, to specify additional environment variables to
122*4882a593Smuzhiyun  pass to make in the build step. These are passed before the +make+
123*4882a593Smuzhiyun  command. By default, empty.
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun* +LIBFOO_MAKE_OPTS+, to specify additional variables to pass to make
126*4882a593Smuzhiyun  in the build step. These are passed after the +make+ command. By
127*4882a593Smuzhiyun  default, empty.
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun* +LIBFOO_INSTALL_OPTS+ contains the make options used to
130*4882a593Smuzhiyun  install the package to the host directory. By default, the value
131*4882a593Smuzhiyun  is +install+, which is correct for most CMake packages. It is still
132*4882a593Smuzhiyun  possible to override it.
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun* +LIBFOO_INSTALL_STAGING_OPTS+ contains the make options used to
135*4882a593Smuzhiyun  install the package to the staging directory. By default, the value
136*4882a593Smuzhiyun  is +DESTDIR=$(STAGING_DIR) install/fast+, which is correct for most
137*4882a593Smuzhiyun  CMake packages. It is still possible to override it.
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun* +LIBFOO_INSTALL_TARGET_OPTS+ contains the make options used to
140*4882a593Smuzhiyun  install the package to the target directory. By default, the value
141*4882a593Smuzhiyun  is +DESTDIR=$(TARGET_DIR) install/fast+. The default value is correct
142*4882a593Smuzhiyun  for most CMake packages, but it is still possible to override it if
143*4882a593Smuzhiyun  needed.
144*4882a593Smuzhiyun
145*4882a593SmuzhiyunWith the CMake infrastructure, all the steps required to build and
146*4882a593Smuzhiyuninstall the packages are already defined, and they generally work well
147*4882a593Smuzhiyunfor most CMake-based packages. However, when required, it is still
148*4882a593Smuzhiyunpossible to customize what is done in any particular step:
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun* By adding a post-operation hook (after extract, patch, configure,
151*4882a593Smuzhiyun  build or install). See xref:hooks[] for details.
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun* By overriding one of the steps. For example, even if the CMake
154*4882a593Smuzhiyun  infrastructure is used, if the package +.mk+ file defines its own
155*4882a593Smuzhiyun  +LIBFOO_CONFIGURE_CMDS+ variable, it will be used instead of the
156*4882a593Smuzhiyun  default CMake one. However, using this method should be restricted
157*4882a593Smuzhiyun  to very specific cases. Do not use it in the general case.
158