]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/meson.build
Merge pull request #9685 from yuwata/fix-9663
[thirdparty/systemd.git] / src / boot / efi / meson.build
1 # SPDX-License-Identifier: LGPL-2.1+
2
3 efi_headers = files('''
4 console.h
5 disk.h
6 graphics.h
7 linux.h
8 measure.h
9 pe.h
10 splash.h
11 util.h
12 shim.h
13 '''.split())
14
15 common_sources = '''
16 disk.c
17 graphics.c
18 measure.c
19 pe.c
20 util.c
21 '''.split()
22
23 systemd_boot_sources = '''
24 boot.c
25 console.c
26 shim.c
27 '''.split()
28
29 stub_sources = '''
30 linux.c
31 splash.c
32 stub.c
33 '''.split()
34
35 if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
36 efi_cc = get_option('efi-cc')
37 efi_ld = get_option('efi-ld')
38 efi_incdir = get_option('efi-includedir')
39
40 gnu_efi_path_arch = ''
41 foreach name : [gnu_efi_arch, EFI_MACHINE_TYPE_NAME]
42 if (gnu_efi_path_arch == '' and name != '' and
43 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, name)))
44 gnu_efi_path_arch = name
45 endif
46 endforeach
47
48 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
49 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
50 endif
51
52 efi_libdir = get_option('efi-libdir')
53 if efi_libdir == ''
54 cmd = 'cd /usr/lib/$(@0@ -print-multi-os-directory) && pwd'.format(efi_cc)
55 ret = run_command('sh', '-c', cmd)
56 if ret.returncode() == 0
57 efi_libdir = ret.stdout().strip()
58 endif
59 endif
60
61 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
62 else
63 have_gnu_efi = false
64 endif
65
66 if get_option('gnu-efi') == 'true' and not have_gnu_efi
67 error('gnu-efi support requested, but headers were not found')
68 endif
69
70 if have_gnu_efi
71 efi_conf = configuration_data()
72 efi_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
73 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
74 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
75 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
76
77 efi_config_h = configure_file(
78 output : 'efi_config.h',
79 configuration : efi_conf)
80
81 objcopy = find_program('objcopy')
82
83 efi_ldsdir = get_option('efi-ldsdir')
84 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)
85 if efi_ldsdir == ''
86 efi_ldsdir = join_paths(efi_libdir, 'gnuefi')
87 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
88 if cmd.returncode() != 0
89 efi_ldsdir = efi_libdir
90 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
91 if cmd.returncode() != 0
92 error('Cannot find @0@'.format(arch_lds))
93 endif
94 endif
95 endif
96
97 compile_args = ['-Wall',
98 '-Wextra',
99 '-std=gnu90',
100 '-nostdinc',
101 '-ggdb', '-O0',
102 '-fpic',
103 '-fshort-wchar',
104 '-ffreestanding',
105 '-fno-strict-aliasing',
106 '-fno-stack-protector',
107 '-Wsign-compare',
108 '-Wno-missing-field-initializers',
109 '-isystem', efi_incdir,
110 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
111 '-include', efi_config_h]
112 if efi_arch == 'x86_64'
113 compile_args += ['-mno-red-zone',
114 '-mno-sse',
115 '-mno-mmx',
116 '-DEFI_FUNCTION_WRAPPER',
117 '-DGNU_EFI_USE_MS_ABI']
118 elif efi_arch == 'ia32'
119 compile_args += ['-mno-sse',
120 '-mno-mmx']
121 endif
122
123 efi_ldflags = ['-T',
124 join_paths(efi_ldsdir, arch_lds),
125 '-shared',
126 '-Bsymbolic',
127 '-nostdlib',
128 '-znocombreloc',
129 '-L', efi_libdir,
130 join_paths(efi_ldsdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]
131 if efi_arch == 'aarch64' or efi_arch == 'arm'
132 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
133 # instead, and add required symbols manually.
134 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
135 efi_format = ['-O', 'binary']
136 else
137 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
138 endif
139
140 systemd_boot_objects = []
141 stub_objects = []
142 foreach file : common_sources + systemd_boot_sources + stub_sources
143 o_file = custom_target(file + '.o',
144 input : file,
145 output : file + '.o',
146 command : [efi_cc, '-c', '@INPUT@', '-o', '@OUTPUT@']
147 + compile_args,
148 depend_files : efi_headers)
149 if (common_sources + systemd_boot_sources).contains(file)
150 systemd_boot_objects += [o_file]
151 endif
152 if (common_sources + stub_sources).contains(file)
153 stub_objects += [o_file]
154 endif
155 endforeach
156
157 libgcc_file_name = run_command(efi_cc, '-print-libgcc-file-name').stdout().strip()
158 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
159 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
160 no_undefined_symbols = find_program('no-undefined-symbols.sh')
161
162 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
163 ['stub.so', stub_efi_name, stub_objects]]
164 so = custom_target(
165 tuple[0],
166 input : tuple[2],
167 output : tuple[0],
168 command : [efi_ld, '-o', '@OUTPUT@'] +
169 efi_ldflags + tuple[2] +
170 ['-lefi', '-lgnuefi', libgcc_file_name])
171
172 test('no-undefined-symbols-' + tuple[0],
173 no_undefined_symbols,
174 args : [so])
175
176 stub = custom_target(
177 tuple[1],
178 input : so,
179 output : tuple[1],
180 command : [objcopy,
181 '-j', '.text',
182 '-j', '.sdata',
183 '-j', '.data',
184 '-j', '.dynamic',
185 '-j', '.dynsym',
186 '-j', '.rel',
187 '-j', '.rela',
188 '-j', '.reloc']
189 + efi_format +
190 ['@INPUT@', '@OUTPUT@'],
191 install : true,
192 install_dir : bootlibdir)
193
194 set_variable(tuple[0].underscorify(), so)
195 set_variable(tuple[0].underscorify() + '_stub', stub)
196 endforeach
197 endif
198
199 ############################################################
200
201 if have_gnu_efi
202 test_efi_disk_img = custom_target(
203 'test-efi-disk.img',
204 input : [systemd_boot_so, stub_so_stub],
205 output : 'test-efi-disk.img',
206 command : [test_efi_create_disk_sh, '@OUTPUT@',
207 '@INPUT0@', '@INPUT1@', splash_bmp])
208 endif