common.mk 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. ROOTFS_DEVICE_TABLES = $(call qstrip,$(BR2_ROOTFS_DEVICE_TABLE) \
  31. $(BR2_ROOTFS_STATIC_DEVICE_TABLE))
  32. ROOTFS_USERS_TABLES = $(call qstrip,$(BR2_ROOTFS_USERS_TABLES))
  33. ROOTFS_FULL_DEVICES_TABLE = $(FS_DIR)/full_devices_table.txt
  34. ROOTFS_FULL_USERS_TABLE = $(FS_DIR)/full_users_table.txt
  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_NAME = rootfs-common
  41. ROOTFS_COMMON_TYPE = rootfs
  42. ROOTFS_COMMON_DEPENDENCIES = \
  43. host-fakeroot host-makedevs \
  44. $(BR2_TAR_HOST_DEPENDENCY) \
  45. $(if $(PACKAGES_USERS)$(ROOTFS_USERS_TABLES),host-mkpasswd)
  46. ROOTFS_COMMON_FINAL_RECURSIVE_DEPENDENCIES = $(sort \
  47. $(if $(filter undefined,$(origin ROOTFS_COMMON_FINAL_RECURSIVE_DEPENDENCIES__X)), \
  48. $(eval ROOTFS_COMMON_FINAL_RECURSIVE_DEPENDENCIES__X := \
  49. $(foreach p, \
  50. $(ROOTFS_COMMON_DEPENDENCIES), \
  51. $(p) \
  52. $($(call UPPERCASE,$(p))_FINAL_RECURSIVE_DEPENDENCIES) \
  53. ) \
  54. ) \
  55. ) \
  56. $(ROOTFS_COMMON_FINAL_RECURSIVE_DEPENDENCIES__X))
  57. .PHONY: rootfs-common
  58. rootfs-common: $(ROOTFS_COMMON_DEPENDENCIES) target-finalize
  59. @$(call MESSAGE,"Generating root filesystems common tables")
  60. rm -rf $(FS_DIR)
  61. mkdir -p $(FS_DIR)
  62. $(call PRINTF,$(PACKAGES_USERS)) >> $(ROOTFS_FULL_USERS_TABLE)
  63. ifneq ($(ROOTFS_USERS_TABLES),)
  64. cat $(ROOTFS_USERS_TABLES) >> $(ROOTFS_FULL_USERS_TABLE)
  65. endif
  66. $(call PRINTF,$(PACKAGES_PERMISSIONS_TABLE)) > $(ROOTFS_FULL_DEVICES_TABLE)
  67. ifneq ($(ROOTFS_DEVICE_TABLES),)
  68. cat $(ROOTFS_DEVICE_TABLES) >> $(ROOTFS_FULL_DEVICES_TABLE)
  69. endif
  70. ifeq ($(BR2_ROOTFS_DEVICE_CREATION_STATIC),y)
  71. $(call PRINTF,$(PACKAGES_DEVICES_TABLE)) >> $(ROOTFS_FULL_DEVICES_TABLE)
  72. endif
  73. rootfs-common-show-depends:
  74. @echo $(ROOTFS_COMMON_DEPENDENCIES)
  75. .PHONY: rootfs-common-show-info
  76. rootfs-common-show-info:
  77. @:
  78. $(info $(call clean-json,{ $(call json-info,ROOTFS_COMMON) }))
  79. # Since this function will be called from within an $(eval ...)
  80. # all variable references except the arguments must be $$-quoted.
  81. define inner-rootfs
  82. ROOTFS_$(2)_NAME = rootfs-$(1)
  83. ROOTFS_$(2)_TYPE = rootfs
  84. ROOTFS_$(2)_IMAGE_NAME ?= rootfs.$(1)
  85. ROOTFS_$(2)_FINAL_IMAGE_NAME = $$(strip $$(ROOTFS_$(2)_IMAGE_NAME))
  86. ROOTFS_$(2)_DIR = $$(FS_DIR)/$(1)
  87. ROOTFS_$(2)_TARGET_DIR = $$(ROOTFS_$(2)_DIR)/target
  88. ROOTFS_$(2)_DEPENDENCIES += rootfs-common
  89. ROOTFS_$(2)_FINAL_RECURSIVE_DEPENDENCIES = $$(sort \
  90. $$(if $$(filter undefined,$$(origin ROOTFS_$(2)_FINAL_RECURSIVE_DEPENDENCIES__X)), \
  91. $$(eval ROOTFS_$(2)_FINAL_RECURSIVE_DEPENDENCIES__X := \
  92. $$(foreach p, \
  93. $$(ROOTFS_$(2)_DEPENDENCIES), \
  94. $$(p) \
  95. $$($$(call UPPERCASE,$$(p))_FINAL_RECURSIVE_DEPENDENCIES) \
  96. ) \
  97. ) \
  98. ) \
  99. $$(ROOTFS_$(2)_FINAL_RECURSIVE_DEPENDENCIES__X))
  100. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_GZIP),y)
  101. ROOTFS_$(2)_COMPRESS_EXT = .gz
  102. ROOTFS_$(2)_COMPRESS_CMD = gzip -9 -c -n
  103. endif
  104. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_BZIP2),y)
  105. ROOTFS_$(2)_COMPRESS_EXT = .bz2
  106. ROOTFS_$(2)_COMPRESS_CMD = bzip2 -9 -c
  107. endif
  108. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_LZMA),y)
  109. ROOTFS_$(2)_DEPENDENCIES += host-lzma
  110. ROOTFS_$(2)_COMPRESS_EXT = .lzma
  111. ROOTFS_$(2)_COMPRESS_CMD = $$(LZMA) -9 -c
  112. endif
  113. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_LZ4),y)
  114. ROOTFS_$(2)_DEPENDENCIES += host-lz4
  115. ROOTFS_$(2)_COMPRESS_EXT = .lz4
  116. ROOTFS_$(2)_COMPRESS_CMD = lz4 -l -9 -c
  117. endif
  118. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_LZO),y)
  119. ROOTFS_$(2)_DEPENDENCIES += host-lzop
  120. ROOTFS_$(2)_COMPRESS_EXT = .lzo
  121. ROOTFS_$(2)_COMPRESS_CMD = $$(LZOP) -9 -c
  122. endif
  123. ifeq ($$(BR2_TARGET_ROOTFS_$(2)_XZ),y)
  124. ROOTFS_$(2)_DEPENDENCIES += host-xz
  125. ROOTFS_$(2)_COMPRESS_EXT = .xz
  126. ROOTFS_$(2)_COMPRESS_CMD = xz -9 -C crc32 -c
  127. ifeq ($(BR2_REPRODUCIBLE),)
  128. ROOTFS_$(2)_COMPRESS_CMD += -T $(PARALLEL_JOBS)
  129. endif
  130. endif
  131. $$(BINARIES_DIR)/$$(ROOTFS_$(2)_FINAL_IMAGE_NAME): ROOTFS=$(2)
  132. $$(BINARIES_DIR)/$$(ROOTFS_$(2)_FINAL_IMAGE_NAME): FAKEROOT_SCRIPT=$$(ROOTFS_$(2)_DIR)/fakeroot
  133. $$(BINARIES_DIR)/$$(ROOTFS_$(2)_FINAL_IMAGE_NAME): $$(ROOTFS_$(2)_DEPENDENCIES)
  134. @$$(call MESSAGE,"Generating filesystem image $$(ROOTFS_$(2)_FINAL_IMAGE_NAME)")
  135. mkdir -p $$(@D)
  136. rm -rf $$(ROOTFS_$(2)_DIR)
  137. mkdir -p $$(ROOTFS_$(2)_DIR)
  138. rsync -auH \
  139. --exclude=/$$(notdir $$(TARGET_DIR_WARNING_FILE)) \
  140. $$(BASE_TARGET_DIR)/ \
  141. $$(TARGET_DIR)
  142. echo '#!/bin/sh' > $$(FAKEROOT_SCRIPT)
  143. echo "set -e" >> $$(FAKEROOT_SCRIPT)
  144. echo "chown -h -R 0:0 $$(TARGET_DIR)" >> $$(FAKEROOT_SCRIPT)
  145. PATH=$$(BR_PATH) $$(TOPDIR)/support/scripts/mkusers $$(ROOTFS_FULL_USERS_TABLE) $$(TARGET_DIR) >> $$(FAKEROOT_SCRIPT)
  146. echo "$$(HOST_DIR)/bin/makedevs -d $$(ROOTFS_FULL_DEVICES_TABLE) $$(TARGET_DIR)" >> $$(FAKEROOT_SCRIPT)
  147. $$(foreach s,$$(call qstrip,$$(BR2_ROOTFS_POST_FAKEROOT_SCRIPT)),\
  148. echo "echo '$$(TERM_BOLD)>>> Executing fakeroot script $$(s)$$(TERM_RESET)'" >> $$(FAKEROOT_SCRIPT); \
  149. echo $$(EXTRA_ENV) $$(s) $$(TARGET_DIR) $$(BR2_ROOTFS_POST_SCRIPT_ARGS) >> $$(FAKEROOT_SCRIPT)$$(sep))
  150. $$(foreach hook,$$(ROOTFS_PRE_CMD_HOOKS),\
  151. $$(call PRINTF,$$($$(hook))) >> $$(FAKEROOT_SCRIPT)$$(sep))
  152. $$(foreach hook,$$(ROOTFS_$(2)_PRE_GEN_HOOKS),\
  153. $$(call PRINTF,$$($$(hook))) >> $$(FAKEROOT_SCRIPT)$$(sep))
  154. $$(call PRINTF,$$(ROOTFS_REPRODUCIBLE)) >> $$(FAKEROOT_SCRIPT)
  155. $$(call PRINTF,$$(ROOTFS_$(2)_CMD)) >> $$(FAKEROOT_SCRIPT)
  156. chmod a+x $$(FAKEROOT_SCRIPT)
  157. PATH=$$(BR_PATH) FAKEROOTDONTTRYCHOWN=1 $$(HOST_DIR)/bin/fakeroot -- $$(FAKEROOT_SCRIPT)
  158. $(Q)rm -rf $$(TARGET_DIR)
  159. ifneq ($$(ROOTFS_$(2)_COMPRESS_CMD),)
  160. PATH=$$(BR_PATH) $$(ROOTFS_$(2)_COMPRESS_CMD) $$@ > $$@$$(ROOTFS_$(2)_COMPRESS_EXT)
  161. endif
  162. $$(foreach hook,$$(ROOTFS_$(2)_POST_GEN_HOOKS),$$(call $$(hook))$$(sep))
  163. rootfs-$(1)-show-depends:
  164. @echo $$(ROOTFS_$(2)_DEPENDENCIES)
  165. rootfs-$(1)-show-info:
  166. @:
  167. $$(info $$(call clean-json,{ $$(call json-info,ROOTFS_$(2)) }))
  168. rootfs-$(1): $$(BINARIES_DIR)/$$(ROOTFS_$(2)_FINAL_IMAGE_NAME)
  169. .PHONY: rootfs-$(1) rootfs-$(1)-show-depends rootfs-$(1)-show-info
  170. ifeq ($$(BR2_TARGET_ROOTFS_$(2)),y)
  171. TARGETS_ROOTFS += rootfs-$(1)
  172. PACKAGES += $$(filter-out rootfs-%,$$(ROOTFS_$(2)_FINAL_RECURSIVE_DEPENDENCIES))
  173. endif
  174. # Check for legacy POST_TARGETS rules
  175. ifneq ($$(ROOTFS_$(2)_POST_TARGETS),)
  176. $$(error Filesystem $(1) uses post-target rules, which are no longer supported.\
  177. Update $(1) to use post-gen hooks instead)
  178. endif
  179. endef
  180. # $(pkgname) also works well to return the filesystem name
  181. rootfs = $(call inner-rootfs,$(pkgname),$(call UPPERCASE,$(pkgname)))
  182. include $(sort $(wildcard fs/*/*.mk))