1 # SPDX-License-Identifier: LGPL-2.1+
3 efi_headers = files('''
23 systemd_boot_sources = '''
35 if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
36 efi_cc = get_option('efi-cc')
37 if efi_cc.length() == 0
38 efi_cc = cc.cmd_array()
40 efi_ld = get_option('efi-ld')
42 efi_ld = find_program('ld', required: true)
44 efi_incdir = get_option('efi-includedir')
46 gnu_efi_path_arch = ''
47 foreach name : [gnu_efi_arch, EFI_MACHINE_TYPE_NAME]
48 if (gnu_efi_path_arch == '' and name != '' and
49 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, name)))
50 gnu_efi_path_arch = name
54 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
55 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
58 efi_libdir = get_option('efi-libdir')
60 ret = run_command(efi_cc + ['-print-multi-os-directory'])
61 if ret.returncode() == 0
62 path = join_paths('/usr/lib', ret.stdout().strip())
63 ret = run_command('realpath', '-e', path)
64 if ret.returncode() == 0
65 efi_libdir = ret.stdout().strip()
70 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
75 if get_option('gnu-efi') == 'true' and not have_gnu_efi
76 error('gnu-efi support requested, but headers were not found')
80 efi_conf = configuration_data()
81 efi_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
82 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
83 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
84 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
86 efi_config_h = configure_file(
87 output : 'efi_config.h',
88 configuration : efi_conf)
90 objcopy = find_program('objcopy')
92 efi_ldsdir = get_option('efi-ldsdir')
93 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)
95 efi_ldsdir = join_paths(efi_libdir, 'gnuefi')
96 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
97 if cmd.returncode() != 0
98 efi_ldsdir = efi_libdir
99 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
100 if cmd.returncode() != 0
101 error('Cannot find @0@'.format(arch_lds))
106 compile_args = ['-Wall',
114 '-fno-strict-aliasing',
115 '-fno-stack-protector',
117 '-Wno-missing-field-initializers',
118 '-isystem', efi_incdir,
119 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
120 '-include', efi_config_h]
121 if efi_arch == 'x86_64'
122 compile_args += ['-mno-red-zone',
125 '-DEFI_FUNCTION_WRAPPER',
126 '-DGNU_EFI_USE_MS_ABI']
127 elif efi_arch == 'ia32'
128 compile_args += ['-mno-sse',
133 join_paths(efi_ldsdir, arch_lds),
139 join_paths(efi_ldsdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]
140 if efi_arch == 'aarch64' or efi_arch == 'arm'
141 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
142 # instead, and add required symbols manually.
143 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
144 efi_format = ['-O', 'binary']
146 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
149 systemd_boot_objects = []
151 foreach file : common_sources + systemd_boot_sources + stub_sources
152 o_file = custom_target(file + '.o',
154 output : file + '.o',
155 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
157 depend_files : efi_headers)
158 if (common_sources + systemd_boot_sources).contains(file)
159 systemd_boot_objects += o_file
161 if (common_sources + stub_sources).contains(file)
162 stub_objects += o_file
166 libgcc_file_name = run_command(efi_cc + ['-print-libgcc-file-name']).stdout().strip()
167 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
168 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
169 no_undefined_symbols = find_program('no-undefined-symbols.sh')
171 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
172 ['stub.so', stub_efi_name, stub_objects]]
177 command : [efi_ld, '-o', '@OUTPUT@'] +
178 efi_ldflags + tuple[2] +
179 ['-lefi', '-lgnuefi', libgcc_file_name])
181 if want_tests != 'false'
182 test('no-undefined-symbols-' + tuple[0],
183 no_undefined_symbols,
187 stub = custom_target(
201 ['@INPUT@', '@OUTPUT@'],
203 install_dir : bootlibdir)
205 set_variable(tuple[0].underscorify(), so)
206 set_variable(tuple[0].underscorify() + '_stub', stub)
210 ############################################################
213 test_efi_disk_img = custom_target(
215 input : [systemd_boot_so, stub_so_stub],
216 output : 'test-efi-disk.img',
217 command : [test_efi_create_disk_sh, '@OUTPUT@',
218 '@INPUT0@', '@INPUT1@', splash_bmp])