]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/meson.build
Merge pull request #18886 from anitazha/shutdownconsole
[thirdparty/systemd.git] / src / boot / efi / meson.build
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2
3 efi_headers = files('''
4 console.h
5 crc32.h
6 disk.h
7 graphics.h
8 linux.h
9 measure.h
10 missing_efi.h
11 pe.h
12 random-seed.h
13 sha256.h
14 shim.h
15 splash.h
16 util.h
17 '''.split())
18
19 common_sources = '''
20 disk.c
21 graphics.c
22 measure.c
23 pe.c
24 secure-boot.c
25 util.c
26 '''.split()
27
28 systemd_boot_sources = '''
29 boot.c
30 console.c
31 crc32.c
32 random-seed.c
33 sha256.c
34 shim.c
35 '''.split()
36
37 stub_sources = '''
38 linux.c
39 splash.c
40 stub.c
41 '''.split()
42
43 if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
44 efi_cc = get_option('efi-cc')
45 if efi_cc.length() == 0
46 efi_cc = cc.cmd_array()
47 endif
48
49 efi_ld = find_program(get_option('efi-ld'), required: true)
50 efi_ld_name = efi_ld.path().split('/')[-1]
51 if efi_ld_name == 'lld' or efi_ld_name == 'ld.lld'
52 # LLVM/LLD does not support PE/COFF relocations
53 # https://lists.llvm.org/pipermail/llvm-dev/2021-March/149234.html
54 error('LLVM/lld does not support PE/COFF relocations. Use different linker for EFI image.')
55 endif
56
57 efi_incdir = get_option('efi-includedir')
58
59 gnu_efi_path_arch = ''
60 foreach name : [gnu_efi_arch, EFI_MACHINE_TYPE_NAME]
61 if (gnu_efi_path_arch == '' and name != '' and
62 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, name)))
63 gnu_efi_path_arch = name
64 endif
65 endforeach
66
67 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
68 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
69 endif
70
71 efi_libdir = get_option('efi-libdir')
72 if efi_libdir == ''
73 # New location first introduced with gnu-efi 3.0.11
74 efi_libdir = join_paths('/usr/lib/gnuefi', EFI_MACHINE_TYPE_NAME)
75 cmd = run_command('test', '-e', efi_libdir)
76
77 if cmd.returncode() != 0
78 # Fall back to the old approach
79 cmd = run_command(efi_cc + ['-print-multi-os-directory'])
80 if cmd.returncode() == 0
81 path = join_paths('/usr/lib', cmd.stdout().strip())
82 cmd = run_command('realpath', '-e', path)
83 if cmd.returncode() == 0
84 efi_libdir = cmd.stdout().strip()
85 endif
86 endif
87 endif
88 endif
89
90 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
91 else
92 have_gnu_efi = false
93 endif
94
95 if get_option('gnu-efi') == 'true' and not have_gnu_efi
96 error('gnu-efi support requested, but headers were not found')
97 endif
98
99 if have_gnu_efi
100 efi_conf = configuration_data()
101 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
102 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
103 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
104
105 efi_config_h = configure_file(
106 output : 'efi_config.h',
107 configuration : efi_conf)
108
109 objcopy = find_program('objcopy')
110
111 efi_location_map = [
112 # New locations first introduced with gnu-efi 3.0.11
113 [join_paths(efi_libdir, 'efi.lds'),
114 join_paths(efi_libdir, 'crt0.o')],
115 # Older locations...
116 [join_paths(efi_libdir, 'gnuefi', 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)),
117 join_paths(efi_libdir, 'gnuefi', 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))],
118 [join_paths(efi_libdir, 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)),
119 join_paths(efi_libdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]]
120 efi_lds = ''
121 foreach location : efi_location_map
122 if efi_lds == ''
123 cmd = run_command('test', '-f', location[0])
124 if cmd.returncode() == 0
125 efi_lds = location[0]
126 efi_crt0 = location[1]
127 endif
128 endif
129 endforeach
130 if efi_lds == ''
131 if get_option('gnu-efi') == 'true'
132 error('gnu-efi support requested, but cannot find efi.lds')
133 else
134 have_gnu_efi = false
135 endif
136 endif
137 endif
138
139 if have_gnu_efi
140 compile_args = ['-Wall',
141 '-Wextra',
142 '-std=gnu99',
143 '-nostdlib',
144 '-fpic',
145 '-fshort-wchar',
146 '-ffreestanding',
147 '-fno-strict-aliasing',
148 '-fno-stack-protector',
149 '-Wsign-compare',
150 '-Wno-missing-field-initializers',
151 '-isystem', efi_incdir,
152 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
153 '-I', fundamental_path,
154 '-DSD_BOOT',
155 '-include', efi_config_h,
156 '-include', version_h]
157 if efi_arch == 'x86_64'
158 compile_args += ['-mno-red-zone',
159 '-mno-sse',
160 '-mno-mmx',
161 '-DEFI_FUNCTION_WRAPPER',
162 '-DGNU_EFI_USE_MS_ABI']
163 elif efi_arch == 'ia32'
164 compile_args += ['-mno-sse',
165 '-mno-mmx']
166 elif efi_arch == 'arm'
167 if cc.has_argument('-mgeneral-regs-only')
168 compile_args += ['-mgeneral-regs-only']
169 endif
170
171 if cc.has_argument('-mfpu=none')
172 compile_args += ['-mfpu=none']
173 endif
174 endif
175 if get_option('werror') == true
176 compile_args += ['-Werror']
177 endif
178 if get_option('buildtype') == 'debug'
179 compile_args += ['-ggdb', '-O0']
180 elif get_option('buildtype') == 'debugoptimized'
181 compile_args += ['-ggdb', '-Og']
182 else
183 compile_args += ['-O2']
184 endif
185
186 efi_ldflags = ['-T', efi_lds,
187 '-shared',
188 '-Bsymbolic',
189 '-nostdlib',
190 '-znocombreloc',
191 '-L', efi_libdir,
192 efi_crt0]
193 if efi_arch == 'aarch64' or efi_arch == 'arm'
194 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
195 # instead, and add required symbols manually.
196 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
197 efi_format = ['-O', 'binary']
198 else
199 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
200 endif
201
202 systemd_boot_objects = []
203 stub_objects = []
204 foreach file : fundamental_source_paths + common_sources + systemd_boot_sources + stub_sources
205 o_file = custom_target(file.split('/')[-1] + '.o',
206 input : file,
207 output : file.split('/')[-1] + '.o',
208 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
209 + compile_args,
210 depend_files : efi_headers + fundamental_headers)
211 if (fundamental_source_paths + common_sources + systemd_boot_sources).contains(file)
212 systemd_boot_objects += o_file
213 endif
214 if (fundamental_source_paths + common_sources + stub_sources).contains(file)
215 stub_objects += o_file
216 endif
217 endforeach
218
219 libgcc_file_name = run_command(efi_cc + ['-print-libgcc-file-name']).stdout().strip()
220 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
221 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
222 no_undefined_symbols = find_program('no-undefined-symbols.sh')
223
224 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
225 ['stub.so', stub_efi_name, stub_objects]]
226 so = custom_target(
227 tuple[0],
228 input : tuple[2],
229 output : tuple[0],
230 command : [efi_ld, '-o', '@OUTPUT@'] +
231 efi_ldflags + tuple[2] +
232 ['-lefi', '-lgnuefi', libgcc_file_name])
233
234 if want_tests != 'false'
235 test('no-undefined-symbols-' + tuple[0],
236 no_undefined_symbols,
237 args : [so])
238 endif
239
240 stub = custom_target(
241 tuple[1],
242 input : so,
243 output : tuple[1],
244 command : [objcopy,
245 '-j', '.text',
246 '-j', '.sdata',
247 '-j', '.data',
248 '-j', '.dynamic',
249 '-j', '.dynsym',
250 '-j', '.rel*']
251 + efi_format +
252 ['@INPUT@', '@OUTPUT@'],
253 install : true,
254 install_dir : bootlibdir)
255
256 set_variable(tuple[0].underscorify(), so)
257 set_variable(tuple[0].underscorify() + '_stub', stub)
258 endforeach
259
260 ############################################################
261
262 test_efi_disk_img = custom_target(
263 'test-efi-disk.img',
264 input : [systemd_boot_so, stub_so_stub],
265 output : 'test-efi-disk.img',
266 command : [test_efi_create_disk_sh, '@OUTPUT@',
267 '@INPUT0@', '@INPUT1@', splash_bmp])
268 endif