common.mk 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #
  2. # Macro that builds the needed Makefile target to create a root
  3. # filesystem image.
  4. #
  5. # The following variable must be defined before calling this macro
  6. #
  7. # ROOTFS_$(FSTYPE)_CMD, the command that generates the root
  8. # filesystem image. A single command is allowed. The filename of the
  9. # filesystem image that it must generate is $$@.
  10. #
  11. # The following variables can optionaly be defined
  12. #
  13. # ROOTFS_$(FSTYPE)_DEPENDENCIES, the list of dependencies needed to
  14. # build the root filesystem (usually host tools)
  15. #
  16. # ROOTFS_$(FSTYPE)_PRE_GEN_HOOKS, a list of hooks to call before
  17. # generating the filesystem image
  18. #
  19. # ROOTFS_$(FSTYPE)_POST_GEN_HOOKS, a list of hooks to call after
  20. # generating the filesystem image
  21. #
  22. # In terms of configuration option, this macro assumes that the
  23. # BR2_TARGET_ROOTFS_$(FSTYPE) config option allows to enable/disable
  24. # the generation of a filesystem image of a particular type. If
  25. # the configuration options BR2_TARGET_ROOTFS_$(FSTYPE)_GZIP,
  26. # BR2_TARGET_ROOTFS_$(FSTYPE)_BZIP2 or
  27. # BR2_TARGET_ROOTFS_$(FSTYPE)_LZMA exist and are enabled, then the
  28. # macro will automatically generate a compressed filesystem image.
  29. FS_DIR = $(BUILD_DIR)/buildroot-fs
  30. FULL_DEVICE_TABLE = $(FS_DIR)/device_table.txt
  31. ROOTFS_DEVICE_TABLES = $(call qstrip,$(BR2_ROOTFS_DEVICE_TABLE) \
  32. $(BR2_ROOTFS_STATIC_DEVICE_TABLE))
  33. USERS_TABLE = $(FS_DIR)/users_table.txt
  34. ROOTFS_USERS_TABLES = $(call qstrip,$(BR2_ROOTFS_USERS_TABLES))
  35. ifeq ($(BR2_REPRODUCIBLE),y)
  36. define ROOTFS_REPRODUCIBLE
  37. find $(TARGET_DIR) -print0 | xargs -0 -r touch -hd @$(SOURCE_DATE_EPOCH)
  38. endef
  39. endif
  40. ROOTFS_COMMON_TAR = $(FS_DIR)/rootfs.common.tar
  41. # Command to create the common tarball from the base target directory.
  42. define ROOTFS_COMMON_TAR_CMD
  43. tar cf $(ROOTFS_COMMON_TAR) --numeric-owner \
  44. --exclude=$(notdir $(TARGET_DIR_WARNING_FILE)) \
  45. -C $(TARGET_DIR) .
  46. endef
  47. # Command to extract the common tarball into the per-rootfs target directory
  48. define ROOTFS_COMMON_UNTAR_CMD
  49. mkdir -p $(TARGET_DIR)
  50. tar xf $(ROOTFS_COMMON_TAR) -C $(TARGET_DIR)
  51. endef
  52. .PHONY: rootfs-common
  53. rootfs-common: $(ROOTFS_COMMON_TAR)
  54. # Emulate being in a filesystem, so that we can have our own TARGET_DIR.
  55. ROOTFS_COMMON_TARGET_DIR = $(FS_DIR)/target
  56. ROOTFS_COMMON_DEPENDENCIES = \
  57. host-fakeroot host-makedevs \
  58. $(if $(PACKAGES_USERS)$(ROOTFS_USERS_TABLES),host-mkpasswd)
  59. $(ROOTFS_COMMON_TAR): ROOTFS=COMMON
  60. $(ROOTFS_COMMON_TAR): FAKEROOT_SCRIPT=$(FS_DIR)/fakeroot.fs
  61. $(ROOTFS_COMMON_TAR): $(ROOTFS_COMMON_DEPENDENCIES) target-finalize
  62. @$(call MESSAGE,"Generating common rootfs tarball")
  63. rm -rf $(FS_DIR)
  64. mkdir -p $(FS_DIR)
  65. rsync -auH $(BASE_TARGET_DIR)/ $(TARGET_DIR)
  66. echo '#!/bin/sh' > $(FAKEROOT_SCRIPT)
  67. echo "set -e" >> $(FAKEROOT_SCRIPT)
  68. echo "chown -h -R 0:0 $(TARGET_DIR)" >> $(FAKEROOT_SCRIPT)
  69. ifneq ($(ROOTFS_USERS_TABLES),)
  70. cat $(ROOTFS_USERS_TABLES) >> $(USERS_TABLE)
  71. endif
  72. $(call PRINTF,$(PACKAGES_USERS)) >> $(USERS_TABLE)
  73. PATH=$(BR_PATH) $(TOPDIR)/support/scripts/mkusers $(USERS_TABLE) $(TARGET_DIR) >> $(FAKEROOT_SCRIPT)
  74. ifneq ($(ROOTFS_DEVICE_TABLES),)
  75. cat $(ROOTFS_DEVICE_TABLES) > $(FULL_DEVICE_TABLE)
  76. ifeq ($(BR2_ROOTFS_DEVICE_CREATION_STATIC),y)
  77. $(call PRINTF,$(PACKAGES_DEVICES_TABLE)) >> $(FULL_DEVICE_TABLE)
  78. endif
  79. endif
  80. $(call PRINTF,$(PACKAGES_PERMISSIONS_TABLE)) >> $(FULL_DEVICE_TABLE)
  81. echo "$(HOST_DIR)/bin/makedevs -d $(FULL_DEVICE_TABLE) $(TARGET_DIR)" >> $(FAKEROOT_SCRIPT)
  82. $(foreach s,$(call qstrip,$(BR2_ROOTFS_POST_FAKEROOT_SCRIPT)),\
  83. echo "echo '$(TERM_BOLD)>>> Executing fakeroot script $(s)$(TERM_RESET)'" >> $(FAKEROOT_SCRIPT); \
  84. echo $(EXTRA_ENV) $(s) $(TARGET_DIR) $(BR2_ROOTFS_POST_SCRIPT_ARGS) >> $(FAKEROOT_SCRIPT)$(sep))
  85. $(foreach hook,$(ROOTFS_PRE_CMD_HOOKS),\
  86. $(call PRINTF,$($(hook))) >> $(FAKEROOT_SCRIPT)$(sep))
  87. $(call PRINTF,$(ROOTFS_COMMON_TAR_CMD)) >> $(FAKEROOT_SCRIPT)
  88. chmod a+x $(FAKEROOT_SCRIPT)
  89. PATH=$(BR_PATH) $(HOST_DIR)/bin/fakeroot -- $(FAKEROOT_SCRIPT)
  90. $(Q)rm -rf $(TARGET_DIR)
  91. rootfs-common-show-depends:
  92. @echo $(ROOTFS_COMMON_DEPENDENCIES)
  93. # Since this function will be called from within an $(eval ...)
  94. # all variable references except the arguments must be $$-quoted.
  95. define inner-rootfs
  96. ROOTFS_$(2)_DIR = $$(FS_DIR)/$(1)
  97. ROOTFS_$(2)_TARGET_DIR = $$(ROOTFS_$(2)_DIR)/target
  98. ROOTFS_$(2)_DEPENDENCIES += rootfs-common
  99. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_GZIP),y)
  100. ROOTFS_$(2)_COMPRESS_EXT = .gz
  101. ROOTFS_$(2)_COMPRESS_CMD = gzip -9 -c
  102. endif
  103. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_BZIP2),y)
  104. ROOTFS_$(2)_COMPRESS_EXT = .bz2
  105. ROOTFS_$(2)_COMPRESS_CMD = bzip2 -9 -c
  106. endif
  107. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_LZMA),y)
  108. ROOTFS_$(2)_DEPENDENCIES += host-lzma
  109. ROOTFS_$(2)_COMPRESS_EXT = .lzma
  110. ROOTFS_$(2)_COMPRESS_CMD = $$(LZMA) -9 -c
  111. endif
  112. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_LZ4),y)
  113. ROOTFS_$(2)_DEPENDENCIES += host-lz4
  114. ROOTFS_$(2)_COMPRESS_EXT = .lz4
  115. ROOTFS_$(2)_COMPRESS_CMD = lz4 -l -9 -c
  116. endif
  117. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_LZO),y)
  118. ROOTFS_$(2)_DEPENDENCIES += host-lzop
  119. ROOTFS_$(2)_COMPRESS_EXT = .lzo
  120. ROOTFS_$(2)_COMPRESS_CMD = $$(LZOP) -9 -c
  121. endif
  122. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_XZ),y)
  123. ROOTFS_$(2)_DEPENDENCIES += host-xz
  124. ROOTFS_$(2)_COMPRESS_EXT = .xz
  125. ROOTFS_$(2)_COMPRESS_CMD = xz -9 -C crc32 -c
  126. endif
  127. $$(BINARIES_DIR)/rootfs.$(1): ROOTFS=$(2)
  128. $$(BINARIES_DIR)/rootfs.$(1): FAKEROOT_SCRIPT=$$(ROOTFS_$(2)_DIR)/fakeroot
  129. $$(BINARIES_DIR)/rootfs.$(1): $$(ROOTFS_$(2)_DEPENDENCIES)
  130. @$$(call MESSAGE,"Generating root filesystem image rootfs.$(1)")
  131. rm -rf $$(ROOTFS_$(2)_DIR)
  132. mkdir -p $$(ROOTFS_$(2)_DIR)
  133. echo '#!/bin/sh' > $$(FAKEROOT_SCRIPT)
  134. echo "set -e" >> $$(FAKEROOT_SCRIPT)
  135. $$(call PRINTF,$$(ROOTFS_COMMON_UNTAR_CMD)) >> $$(FAKEROOT_SCRIPT)
  136. $$(foreach hook,$$(ROOTFS_$(2)_PRE_GEN_HOOKS),\
  137. $$(call PRINTF,$$($$(hook))) >> $$(FAKEROOT_SCRIPT)$$(sep))
  138. $$(call PRINTF,$$(ROOTFS_REPRODUCIBLE)) >> $$(FAKEROOT_SCRIPT)
  139. $$(call PRINTF,$$(ROOTFS_$(2)_CMD)) >> $$(FAKEROOT_SCRIPT)
  140. chmod a+x $$(FAKEROOT_SCRIPT)
  141. PATH=$$(BR_PATH) $$(HOST_DIR)/bin/fakeroot -- $$(FAKEROOT_SCRIPT)
  142. $(Q)rm -rf $$(TARGET_DIR)
  143. ifneq ($$(ROOTFS_$(2)_COMPRESS_CMD),)
  144. PATH=$$(BR_PATH) $$(ROOTFS_$(2)_COMPRESS_CMD) $$@ > $$@$$(ROOTFS_$(2)_COMPRESS_EXT)
  145. endif
  146. $$(foreach hook,$$(ROOTFS_$(2)_POST_GEN_HOOKS),$$(call $$(hook))$$(sep))
  147. rootfs-$(1)-show-depends:
  148. @echo $$(ROOTFS_$(2)_DEPENDENCIES)
  149. rootfs-$(1): $$(BINARIES_DIR)/rootfs.$(1)
  150. .PHONY: rootfs-$(1) rootfs-$(1)-show-depends
  151. ifeq ($$(BR2_TARGET_ROOTFS_$(2)),y)
  152. TARGETS_ROOTFS += rootfs-$(1)
  153. PACKAGES += $$(filter-out rootfs-%,$$(ROOTFS_$(2)_DEPENDENCIES))
  154. endif
  155. # Check for legacy POST_TARGETS rules
  156. ifneq ($$(ROOTFS_$(2)_POST_TARGETS),)
  157. $$(error Filesystem $(1) uses post-target rules, which are no longer supported.\
  158. Update $(1) to use post-gen hooks instead)
  159. endif
  160. endef
  161. # $(pkgname) also works well to return the filesystem name
  162. rootfs = $(call inner-rootfs,$(pkgname),$(call UPPERCASE,$(pkgname)))
  163. include $(sort $(wildcard fs/*/*.mk))