adding-packages-cmake.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // -*- mode:doc; -*-
  2. // vim: set syntax=asciidoc:
  3. Infrastructure for CMake-based packages
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. [[cmake-package-tutorial]]
  6. +cmake-package+ tutorial
  7. ^^^^^^^^^^^^^^^^^^^^^^^^
  8. First, let's see how to write a +.mk+ file for a CMake-based package,
  9. with an example :
  10. ------------------------
  11. 01: ################################################################################
  12. 02: #
  13. 03: # libfoo
  14. 04: #
  15. 05: ################################################################################
  16. 06:
  17. 07: LIBFOO_VERSION = 1.0
  18. 08: LIBFOO_SOURCE = libfoo-$(LIBFOO_VERSION).tar.gz
  19. 09: LIBFOO_SITE = http://www.foosoftware.org/download
  20. 10: LIBFOO_INSTALL_STAGING = YES
  21. 11: LIBFOO_INSTALL_TARGET = NO
  22. 12: LIBFOO_CONF_OPT = -DBUILD_DEMOS=ON
  23. 13: LIBFOO_DEPENDENCIES = libglib2 host-pkgconf
  24. 14:
  25. 15: $(eval $(cmake-package))
  26. ------------------------
  27. On line 7, we declare the version of the package.
  28. On line 8 and 9, we declare the name of the tarball and the location
  29. of the tarball on the Web. Buildroot will automatically download the
  30. tarball from this location.
  31. On line 10, we tell Buildroot to install the package to the staging
  32. directory. The staging directory, located in +output/staging/+
  33. is the directory where all the packages are installed, including their
  34. development files, etc. By default, packages are not installed to the
  35. staging directory, since usually, only libraries need to be installed in
  36. the staging directory: their development files are needed to compile
  37. other libraries or applications depending on them. Also by default, when
  38. staging installation is enabled, packages are installed in this location
  39. using the +make install+ command.
  40. On line 11, we tell Buildroot to not install the package to the
  41. target directory. This directory contains what will become the root
  42. filesystem running on the target. For purely static libraries, it is
  43. not necessary to install them in the target directory because they will
  44. not be used at runtime. By default, target installation is enabled; setting
  45. this variable to NO is almost never needed. Also by default, packages are
  46. installed in this location using the +make install+ command.
  47. On line 12, we tell Buildroot to pass custom options to CMake when it is
  48. configuring the package.
  49. On line 13, we declare our dependencies, so that they are built
  50. before the build process of our package starts.
  51. Finally, on line line 15, we invoke the +cmake-package+
  52. macro that generates all the Makefile rules that actually allows the
  53. package to be built.
  54. [[cmake-package-reference]]
  55. +cmake-package+ reference
  56. ^^^^^^^^^^^^^^^^^^^^^^^^^
  57. The main macro of the CMake package infrastructure is
  58. +cmake-package+. It is similar to the +generic-package+ macro. The ability to
  59. have target and host packages is also available, with the
  60. +host-cmake-package+ macro.
  61. Just like the generic infrastructure, the CMake infrastructure works
  62. by defining a number of variables before calling the +cmake-package+
  63. macro.
  64. First, all the package metadata information variables that exist in
  65. the generic infrastructure also exist in the CMake infrastructure:
  66. +LIBFOO_VERSION+, +LIBFOO_SOURCE+, +LIBFOO_PATCH+, +LIBFOO_SITE+,
  67. +LIBFOO_SUBDIR+, +LIBFOO_DEPENDENCIES+, +LIBFOO_INSTALL_STAGING+,
  68. +LIBFOO_INSTALL_TARGET+.
  69. A few additional variables, specific to the CMake infrastructure, can
  70. also be defined. Many of them are only useful in very specific cases,
  71. typical packages will therefore only use a few of them.
  72. * +LIBFOO_SUBDIR+ may contain the name of a subdirectory inside the
  73. package that contains the main CMakeLists.txt file. This is useful,
  74. if for example, the main CMakeLists.txt file is not at the root of
  75. the tree extracted by the tarball. If +HOST_LIBFOO_SUBDIR+ is not
  76. specified, it defaults to +LIBFOO_SUBDIR+.
  77. * +LIBFOO_CONF_ENV+, to specify additional environment variables to
  78. pass to CMake. By default, empty.
  79. * +LIBFOO_CONF_OPT+, to specify additional configure options to pass
  80. to CMake. By default, empty.
  81. * +LIBFOO_MAKE+, to specify an alternate +make+ command. This is
  82. typically useful when parallel make is enabled in the configuration
  83. (using +BR2_JLEVEL+) but that this feature should be disabled for
  84. the given package, for one reason or another. By default, set to
  85. +$(MAKE)+. If parallel building is not supported by the package,
  86. then it should be set to +LIBFOO_MAKE=$(MAKE1)+.
  87. * +LIBFOO_MAKE_ENV+, to specify additional environment variables to
  88. pass to make in the build step. These are passed before the +make+
  89. command. By default, empty.
  90. * +LIBFOO_MAKE_OPT+, to specify additional variables to pass to make
  91. in the build step. These are passed after the +make+ command. By
  92. default, empty.
  93. * +LIBFOO_INSTALL_STAGING_OPT+ contains the make options used to
  94. install the package to the staging directory. By default, the value
  95. is +DESTDIR=$(STAGING_DIR) install+, which is correct for most
  96. CMake packages. It is still possible to override it.
  97. * +LIBFOO_INSTALL_TARGET_OPT+ contains the make options used to
  98. install the package to the target directory. By default, the value
  99. is +DESTDIR=$(TARGET_DIR) install+. The default value is correct
  100. for most CMake packages, but it is still possible to override it if
  101. needed.
  102. * +LIBFOO_CLEAN_OPT+ contains the make options used to clean the
  103. package. By default, the value is +clean+.
  104. With the CMake infrastructure, all the steps required to build and
  105. install the packages are already defined, and they generally work well
  106. for most CMake-based packages. However, when required, it is still
  107. possible to customize what is done in any particular step:
  108. * By adding a post-operation hook (after extract, patch, configure,
  109. build or install). See the reference documentation of the generic
  110. infrastructure for details.
  111. * By overriding one of the steps. For example, even if the CMake
  112. infrastructure is used, if the package +.mk+ file defines its own
  113. +LIBFOO_CONFIGURE_CMDS+ variable, it will be used instead of the
  114. default CMake one. However, using this method should be restricted
  115. to very specific cases. Do not use it in the general case.