xref: /linux/usr/Makefile (revision 997208ce9f43f01febac956cf9aeffe89945e3c1)
1# SPDX-License-Identifier: GPL-2.0
2#
3# kbuild file for usr/ - including initramfs image
4#
5
6compress-y					:= copy
7compress-$(CONFIG_INITRAMFS_COMPRESSION_GZIP)	:= gzip
8compress-$(CONFIG_INITRAMFS_COMPRESSION_BZIP2)	:= bzip2
9compress-$(CONFIG_INITRAMFS_COMPRESSION_LZMA)	:= lzma
10compress-$(CONFIG_INITRAMFS_COMPRESSION_XZ)	:= xzmisc
11compress-$(CONFIG_INITRAMFS_COMPRESSION_LZO)	:= lzo
12compress-$(CONFIG_INITRAMFS_COMPRESSION_LZ4)	:= lz4
13compress-$(CONFIG_INITRAMFS_COMPRESSION_ZSTD)	:= zstd
14
15obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o
16
17$(obj)/initramfs_data.o: $(obj)/initramfs_inc_data
18
19ramfs-input := $(CONFIG_INITRAMFS_SOURCE)
20cpio-data :=
21
22# If CONFIG_INITRAMFS_SOURCE is empty, generate a small initramfs with the
23# default contents.
24ifeq ($(ramfs-input),)
25ramfs-input := $(src)/default_cpio_list
26endif
27
28ifeq ($(words $(ramfs-input)),1)
29
30# If CONFIG_INITRAMFS_SOURCE specifies a single file, and it is suffixed with
31# .cpio, use it directly as an initramfs.
32ifneq ($(filter %.cpio,$(ramfs-input)),)
33cpio-data := $(ramfs-input)
34endif
35
36# If CONFIG_INITRAMFS_SOURCE specifies a single file, and it is suffixed with
37# .cpio.*, use it directly as an initramfs, and avoid double compression.
38ifeq ($(words $(subst .cpio.,$(space),$(ramfs-input))),2)
39cpio-data := $(ramfs-input)
40compress-y := copy
41endif
42
43endif
44
45# For other cases, generate the initramfs cpio archive based on the contents
46# specified by CONFIG_INITRAMFS_SOURCE.
47ifeq ($(cpio-data),)
48
49cpio-data := $(obj)/initramfs_data.cpio
50
51# .initramfs_data.cpio.d is used to identify all files included
52# in initramfs and to detect if any files are added/removed.
53# Removed files are identified by directory timestamp being updated
54# The dependency list is generated by gen_initramfs.sh -l
55-include $(obj)/.initramfs_data.cpio.d
56
57# do not try to update files included in initramfs
58$(deps_initramfs): ;
59
60quiet_cmd_initfs = GEN     $@
61      cmd_initfs = \
62	$(CONFIG_SHELL) $< -o $@ -l $(obj)/.initramfs_data.cpio.d \
63	$(addprefix -u , $(CONFIG_INITRAMFS_ROOT_UID)) \
64	$(addprefix -g , $(CONFIG_INITRAMFS_ROOT_GID)) \
65	$(if $(KBUILD_BUILD_TIMESTAMP), -d "$(KBUILD_BUILD_TIMESTAMP)") \
66	$(ramfs-input)
67
68# We rebuild initramfs_data.cpio if:
69# 1) Any included file is newer than initramfs_data.cpio
70# 2) There are changes in which files are included (added or deleted)
71# 3) If gen_init_cpio are newer than initramfs_data.cpio
72# 4) Arguments to gen_initramfs.sh changes
73$(obj)/initramfs_data.cpio: $(srctree)/scripts/gen_initramfs.sh scripts/gen_init_cpio $(deps_initramfs) FORCE
74	$(call if_changed,initfs)
75
76endif
77
78$(obj)/initramfs_inc_data: $(cpio-data) FORCE
79	+$(call if_changed,$(compress-y))
80
81targets += initramfs_data.cpio initramfs_inc_data
82
83subdir-$(CONFIG_UAPI_HEADER_TEST) += include
84