1 # SPDX-License-Identifier: LGPL-2.1-or-later
3 efi_headers = files('''
28 systemd_boot_sources = '''
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()
48 efi_ld = get_option('efi-ld')
50 efi_ld = find_program('ld', required: true)
52 efi_incdir = get_option('efi-includedir')
54 gnu_efi_path_arch = ''
55 foreach name : [gnu_efi_arch, EFI_MACHINE_TYPE_NAME]
56 if (gnu_efi_path_arch == '' and name != '' and
57 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, name)))
58 gnu_efi_path_arch = name
62 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
63 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
66 efi_libdir = get_option('efi-libdir')
68 # New location first introduced with gnu-efi 3.0.11
69 efi_libdir = join_paths('/usr/lib/gnuefi', EFI_MACHINE_TYPE_NAME)
70 cmd = run_command('test', '-e', efi_libdir)
72 if cmd.returncode() != 0
73 # Fall back to the old approach
74 cmd = run_command(efi_cc + ['-print-multi-os-directory'])
75 if cmd.returncode() == 0
76 path = join_paths('/usr/lib', cmd.stdout().strip())
77 cmd = run_command('realpath', '-e', path)
78 if cmd.returncode() == 0
79 efi_libdir = cmd.stdout().strip()
85 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
90 if get_option('gnu-efi') == 'true' and not have_gnu_efi
91 error('gnu-efi support requested, but headers were not found')
95 efi_conf = configuration_data()
96 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
97 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
98 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
100 efi_config_h = configure_file(
101 output : 'efi_config.h',
102 configuration : efi_conf)
104 objcopy = find_program('objcopy')
107 # New locations first introduced with gnu-efi 3.0.11
108 [join_paths(efi_libdir, 'efi.lds'),
109 join_paths(efi_libdir, 'crt0.o')],
111 [join_paths(efi_libdir, 'gnuefi', 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)),
112 join_paths(efi_libdir, 'gnuefi', 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))],
113 [join_paths(efi_libdir, 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)),
114 join_paths(efi_libdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]]
116 foreach location : efi_location_map
118 cmd = run_command('test', '-f', location[0])
119 if cmd.returncode() == 0
120 efi_lds = location[0]
121 efi_crt0 = location[1]
126 if get_option('gnu-efi') == 'true'
127 error('gnu-efi support requested, but cannot find efi.lds')
135 compile_args = ['-Wall',
142 '-fno-strict-aliasing',
143 '-fno-stack-protector',
145 '-Wno-missing-field-initializers',
146 '-isystem', efi_incdir,
147 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
148 '-I', fundamental_path,
150 '-include', efi_config_h,
151 '-include', version_h]
152 if efi_arch == 'x86_64'
153 compile_args += ['-mno-red-zone',
156 '-DEFI_FUNCTION_WRAPPER',
157 '-DGNU_EFI_USE_MS_ABI']
158 elif efi_arch == 'ia32'
159 compile_args += ['-mno-sse',
161 elif efi_arch == 'arm'
162 if cc.has_argument('-mgeneral-regs-only')
163 compile_args += ['-mgeneral-regs-only']
166 if cc.has_argument('-mfpu=none')
167 compile_args += ['-mfpu=none']
170 if get_option('werror') == true
171 compile_args += ['-Werror']
173 if get_option('buildtype') == 'debug'
174 compile_args += ['-ggdb', '-O0']
175 elif get_option('buildtype') == 'debugoptimized'
176 compile_args += ['-ggdb', '-Og']
178 compile_args += ['-O2']
181 efi_ldflags = ['-T', efi_lds,
188 if efi_arch == 'aarch64' or efi_arch == 'arm'
189 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
190 # instead, and add required symbols manually.
191 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
192 efi_format = ['-O', 'binary']
194 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
197 systemd_boot_objects = []
199 foreach file : fundamental_source_paths + common_sources + systemd_boot_sources + stub_sources
200 o_file = custom_target(file.split('/')[-1] + '.o',
202 output : file.split('/')[-1] + '.o',
203 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
205 depend_files : efi_headers + fundamental_headers)
206 if (fundamental_source_paths + common_sources + systemd_boot_sources).contains(file)
207 systemd_boot_objects += o_file
209 if (fundamental_source_paths + common_sources + stub_sources).contains(file)
210 stub_objects += o_file
214 libgcc_file_name = run_command(efi_cc + ['-print-libgcc-file-name']).stdout().strip()
215 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
216 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
217 no_undefined_symbols = find_program('no-undefined-symbols.sh')
219 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
220 ['stub.so', stub_efi_name, stub_objects]]
225 command : [efi_ld, '-o', '@OUTPUT@'] +
226 efi_ldflags + tuple[2] +
227 ['-lefi', '-lgnuefi', libgcc_file_name])
229 if want_tests != 'false'
230 test('no-undefined-symbols-' + tuple[0],
231 no_undefined_symbols,
235 stub = custom_target(
247 ['@INPUT@', '@OUTPUT@'],
249 install_dir : bootlibdir)
251 set_variable(tuple[0].underscorify(), so)
252 set_variable(tuple[0].underscorify() + '_stub', stub)
255 ############################################################
257 test_efi_disk_img = custom_target(
259 input : [systemd_boot_so, stub_so_stub],
260 output : 'test-efi-disk.img',
261 command : [test_efi_create_disk_sh, '@OUTPUT@',
262 '@INPUT0@', '@INPUT1@', splash_bmp])