adding-packages-handwritten.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. [[handwritten-tutorial]]
  2. Manual Makefile
  3. ---------------
  4. *NOTE: new manual makefiles should not be created, and existing manual
  5. makefiles should be converted either to the generic, autotools or
  6. cmake infrastructure. This section is only kept to document the
  7. existing manual makefiles and to help understand how they work.*
  8. ------------------------
  9. 01: #############################################################
  10. 02: #
  11. 03: # libfoo
  12. 04: #
  13. 05: #############################################################
  14. 06: LIBFOO_VERSION = 1.0
  15. 07: LIBFOO_SOURCE = libfoo-$(LIBFOO_VERSION).tar.gz
  16. 08: LIBFOO_SITE = http://www.foosoftware.org/downloads
  17. 09: LIBFOO_DIR = $(BUILD_DIR)/foo-$(FOO_VERSION)
  18. 10: LIBFOO_BINARY = foo
  19. 11: LIBFOO_TARGET_BINARY = usr/bin/foo
  20. 12:
  21. 13: $(DL_DIR)/$(LIBFOO_SOURCE):
  22. 14: $(call DOWNLOAD,$(LIBFOO_SITE)/$(LIBFOO_SOURCE))
  23. 15:
  24. 16: $(LIBFOO_DIR)/.source: $(DL_DIR)/$(LIBFOO_SOURCE)
  25. 17: $(ZCAT) $(DL_DIR)/$(LIBFOO_SOURCE) | tar -C $(BUILD_DIR) $(TAR_OPTIONS) -
  26. 18: touch $@
  27. 19:
  28. 20: $(LIBFOO_DIR)/.configured: $(LIBFOO_DIR)/.source
  29. 21: (cd $(LIBFOO_DIR); rm -rf config.cache; \
  30. 22: $(TARGET_CONFIGURE_OPTS) \
  31. 23: $(TARGET_CONFIGURE_ARGS) \
  32. 24: ./configure \
  33. 25: --target=$(GNU_TARGET_NAME) \
  34. 26: --host=$(GNU_TARGET_NAME) \
  35. 27: --build=$(GNU_HOST_NAME) \
  36. 28: --prefix=/usr \
  37. 29: --sysconfdir=/etc \
  38. 30: )
  39. 31: touch $@
  40. 32:
  41. 33: $(LIBFOO_DIR)/$(LIBFOO_BINARY): $(LIBFOO_DIR)/.configured
  42. 34: $(MAKE) CC=$(TARGET_CC) -C $(LIBFOO_DIR)
  43. 35:
  44. 36: $(TARGET_DIR)/$(LIBFOO_TARGET_BINARY): $(LIBFOO_DIR)/$(LIBFOO_BINARY)
  45. 37: $(MAKE) DESTDIR=$(TARGET_DIR) -C $(LIBFOO_DIR) install-strip
  46. 38: rm -Rf $(TARGET_DIR)/usr/man
  47. 39:
  48. 40: libfoo: uclibc ncurses $(TARGET_DIR)/$(LIBFOO_TARGET_BINARY)
  49. 41:
  50. 42: libfoo-source: $(DL_DIR)/$(LIBFOO_SOURCE)
  51. 43:
  52. 44: libfoo-clean:
  53. 45: $(MAKE) prefix=$(TARGET_DIR)/usr -C $(LIBFOO_DIR) uninstall
  54. 46: -$(MAKE) -C $(LIBFOO_DIR) clean
  55. 47:
  56. 48: libfoo-dirclean:
  57. 49: rm -rf $(LIBFOO_DIR)
  58. 50:
  59. 51: #############################################################
  60. 52: #
  61. 53: # Toplevel Makefile options
  62. 54: #
  63. 55: #############################################################
  64. 56: ifeq ($(BR2_PACKAGE_LIBFOO),y)
  65. 57: TARGETS += libfoo
  66. 58: endif
  67. ------------------------
  68. First of all, this Makefile example works for a package which
  69. comprises a single binary executable. For other software, such as
  70. libraries or more complex stuff with multiple binaries, it must be
  71. qqadapted. For examples look at the other +*.mk+ files in the
  72. +package+ directory.
  73. At lines 6-11, a couple of useful variables are defined:
  74. * +LIBFOO_VERSION+: The version of 'libfoo' that should be downloaded.
  75. * +LIBFOO_SOURCE+: The name of the tarball of 'libfoo' on the download
  76. website or FTP site. As you can see +LIBFOO_VERSION+ is used.
  77. * +LIBFOO_SITE+: The HTTP or FTP site from which 'libfoo' archive is
  78. downloaded. It must include the complete path to the directory where
  79. +LIBFOO_SOURCE+ can be found.
  80. * +LIBFOO_DIR+: The directory into which the software will be
  81. configured and compiled. Basically, it's a subdirectory of
  82. +BUILD_DIR+ which is created upon decompression of the tarball.
  83. * +LIBFOO_BINARY+: Software binary name. As said previously, this is
  84. an example for a package with a single binary.
  85. * +LIBFOO_TARGET_BINARY+: The full path of the binary inside the
  86. target filesystem. Lines 13-14 define a target that downloads the
  87. tarball from the remote site to the download directory (+DL_DIR+).
  88. Lines 16-18 define a target and associated rules that uncompress the
  89. downloaded tarball. As you can see, this target depends on the tarball
  90. file so that the previous target (lines 13-14) is called before
  91. executing the rules of the current target. Uncompressing is followed
  92. by 'touching' a hidden file to mark the software as having been
  93. uncompressed. This trick is used everywhere in a Buildroot Makefile to
  94. split steps (download, uncompress, configure, compile, install) while
  95. still having correct dependencies.
  96. Lines 20-31 define a target and associated rules that configure the
  97. software. It depends on the previous target (the hidden +.source+
  98. file) so that we are sure the software has been uncompressed. In order
  99. to configure the package, it basically runs the well-known
  100. +./configure+ script. As we may be doing cross-compilation, +target+,
  101. +host+ and +build+ arguments are given. The prefix is also set to
  102. +/usr+, not because the software will be installed in +/usr+ on your
  103. host system, but because the software will be installed in + /usr+ on
  104. the target filesystem. Finally it creates a +.configured+ file to mark
  105. the software as configured.
  106. Lines 33-34 define a target and a rule that compile the software. This
  107. target will create the binary file in the compilation directory and
  108. depends on the software being already configured (hence the reference
  109. to the +.configured+ file). It basically runs +make+ inside the
  110. source directory.
  111. Lines 36-38 define a target and associated rules that install the
  112. software inside the target filesystem. They depend on the binary file
  113. in the source directory to make sure the software has been
  114. compiled. They use the +install-strip+ target of the software
  115. +Makefile+ by passing a +DESTDIR+ argument so that the +Makefile+
  116. doesn't try to install the software in the host +/usr+ but rather in
  117. the target +/usr+. After the installation, the +/usr/man + directory
  118. inside the target filesystem is removed to save space.
  119. Line 40 defines the main target of the software — the one that
  120. will eventually be used by the top level +Makefile+ to download,
  121. compile, and then install this package. This target should first of
  122. all depend on all needed dependencies of the software (in our example,
  123. 'uclibc' and 'ncurses') and also depend on the final binary. This last
  124. dependency will call all previous dependencies in the correct order.
  125. Line 42 defines a simple target that only downloads the code
  126. source. This is not used during normal operation of Buildroot, but is
  127. needed if you intend to download all required sources at once for
  128. later offline build. Note that if you add a new package, providing a
  129. +libfoo-source+ target is 'mandatory' to support users that wish to do
  130. offline-builds. Furthermore, it eases checking if all package-sources
  131. are downloadable.
  132. Lines 44-46 define a simple target to clean the software build by
  133. calling the Makefile with the appropriate options. The +-clean+
  134. target should run +make clean+ on $(BUILD_DIR)/package-version and
  135. MUST uninstall all files of the package from $(STAGING_DIR) and from
  136. $(TARGET_DIR).
  137. Lines 48-49 define a simple target to completely remove the directory
  138. in which the software was uncompressed, configured and compiled. The
  139. +-dirclean+ target MUST completely rm $(BUILD_DIR)/ package-version.
  140. Lines 51-58 add the target +libfoo+ to the list of targets to be
  141. compiled by Buildroot, by first checking if the configuration option
  142. for this package has been enabled using the configuration tool. If so,
  143. it then "subscribes" this package to be compiled by adding
  144. the package to the TARGETS global variable. The name added to the
  145. TARGETS global variable is the name of this package's target, as
  146. defined on line 40, which is used by Buildroot to download, compile,
  147. and then install this package.