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()
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.')
57 efi_incdir = get_option('efi-includedir')
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
67 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
68 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
71 efi_libdir = get_option('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)
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()
90 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
95 if get_option('gnu-efi') == 'true' and not have_gnu_efi
96 error('gnu-efi support requested, but headers were not found')
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'))
105 efi_config_h = configure_file(
106 output : 'efi_config.h',
107 configuration : efi_conf)
109 objcopy = find_program('objcopy')
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')],
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))]]
121 foreach location : efi_location_map
123 cmd = run_command('test', '-f', location[0])
124 if cmd.returncode() == 0
125 efi_lds = location[0]
126 efi_crt0 = location[1]
131 if get_option('gnu-efi') == 'true'
132 error('gnu-efi support requested, but cannot find efi.lds')
140 compile_args = ['-Wall',
147 '-fno-strict-aliasing',
148 '-fno-stack-protector',
150 '-Wno-missing-field-initializers',
151 '-isystem', efi_incdir,
152 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
153 '-I', fundamental_path,
155 '-include', efi_config_h,
156 '-include', version_h]
157 if efi_arch == 'x86_64'
158 compile_args += ['-mno-red-zone',
161 '-DEFI_FUNCTION_WRAPPER',
162 '-DGNU_EFI_USE_MS_ABI']
163 elif efi_arch == 'ia32'
164 compile_args += ['-mno-sse',
166 elif efi_arch == 'arm'
167 if cc.has_argument('-mgeneral-regs-only')
168 compile_args += ['-mgeneral-regs-only']
171 if cc.has_argument('-mfpu=none')
172 compile_args += ['-mfpu=none']
175 if get_option('werror') == true
176 compile_args += ['-Werror']
178 if get_option('buildtype') == 'debug'
179 compile_args += ['-ggdb', '-O0']
180 elif get_option('buildtype') == 'debugoptimized'
181 compile_args += ['-ggdb', '-Og']
183 compile_args += ['-O2']
186 efi_ldflags = ['-T', efi_lds,
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']
199 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
202 systemd_boot_objects = []
204 foreach file : fundamental_source_paths + common_sources + systemd_boot_sources + stub_sources
205 o_file = custom_target(file.split('/')[-1] + '.o',
207 output : file.split('/')[-1] + '.o',
208 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
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
214 if (fundamental_source_paths + common_sources + stub_sources).contains(file)
215 stub_objects += o_file
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')
224 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
225 ['stub.so', stub_efi_name, stub_objects]]
230 command : [efi_ld, '-o', '@OUTPUT@'] +
231 efi_ldflags + tuple[2] +
232 ['-lefi', '-lgnuefi', libgcc_file_name])
234 if want_tests != 'false'
235 test('no-undefined-symbols-' + tuple[0],
236 no_undefined_symbols,
240 stub = custom_target(
252 ['@INPUT@', '@OUTPUT@'],
254 install_dir : bootlibdir)
256 set_variable(tuple[0].underscorify(), so)
257 set_variable(tuple[0].underscorify() + '_stub', stub)
260 ############################################################
262 test_efi_disk_img = custom_target(
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])