1 # SPDX-License-Identifier: LGPL-2.1+
3 efi_headers = files('''
24 systemd_boot_sources = '''
37 if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
38 efi_cc = get_option('efi-cc')
39 if efi_cc.length() == 0
40 efi_cc = cc.cmd_array()
42 efi_ld = get_option('efi-ld')
44 efi_ld = find_program('ld', required: true)
46 efi_incdir = get_option('efi-includedir')
48 gnu_efi_path_arch = ''
49 foreach name : [gnu_efi_arch, EFI_MACHINE_TYPE_NAME]
50 if (gnu_efi_path_arch == '' and name != '' and
51 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, name)))
52 gnu_efi_path_arch = name
56 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
57 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
60 efi_libdir = get_option('efi-libdir')
62 ret = run_command(efi_cc + ['-print-multi-os-directory'])
63 if ret.returncode() == 0
64 path = join_paths('/usr/lib', ret.stdout().strip())
65 ret = run_command('realpath', '-e', path)
66 if ret.returncode() == 0
67 efi_libdir = ret.stdout().strip()
72 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
77 if get_option('gnu-efi') == 'true' and not have_gnu_efi
78 error('gnu-efi support requested, but headers were not found')
82 efi_conf = configuration_data()
83 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
84 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
85 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
87 efi_config_h = configure_file(
88 output : 'efi_config.h',
89 configuration : efi_conf)
91 objcopy = find_program('objcopy')
93 efi_ldsdir = get_option('efi-ldsdir')
94 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)
96 efi_ldsdir = join_paths(efi_libdir, 'gnuefi')
97 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
98 if cmd.returncode() != 0
99 efi_ldsdir = efi_libdir
100 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
101 if cmd.returncode() != 0
102 error('Cannot find @0@'.format(arch_lds))
107 compile_args = ['-Wall',
115 '-fno-strict-aliasing',
116 '-fno-stack-protector',
118 '-Wno-missing-field-initializers',
119 '-isystem', efi_incdir,
120 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
121 '-include', efi_config_h,
122 '-include', version_h]
123 if efi_arch == 'x86_64'
124 compile_args += ['-mno-red-zone',
127 '-DEFI_FUNCTION_WRAPPER',
128 '-DGNU_EFI_USE_MS_ABI']
129 elif efi_arch == 'ia32'
130 compile_args += ['-mno-sse',
135 join_paths(efi_ldsdir, arch_lds),
141 join_paths(efi_ldsdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]
142 if efi_arch == 'aarch64' or efi_arch == 'arm'
143 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
144 # instead, and add required symbols manually.
145 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
146 efi_format = ['-O', 'binary']
148 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
151 systemd_boot_objects = []
153 foreach file : common_sources + systemd_boot_sources + stub_sources
154 o_file = custom_target(file + '.o',
156 output : file + '.o',
157 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
159 depend_files : efi_headers)
160 if (common_sources + systemd_boot_sources).contains(file)
161 systemd_boot_objects += o_file
163 if (common_sources + stub_sources).contains(file)
164 stub_objects += o_file
168 libgcc_file_name = run_command(efi_cc + ['-print-libgcc-file-name']).stdout().strip()
169 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
170 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
171 no_undefined_symbols = find_program('no-undefined-symbols.sh')
173 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
174 ['stub.so', stub_efi_name, stub_objects]]
179 command : [efi_ld, '-o', '@OUTPUT@'] +
180 efi_ldflags + tuple[2] +
181 ['-lefi', '-lgnuefi', libgcc_file_name])
183 if want_tests != 'false'
184 test('no-undefined-symbols-' + tuple[0],
185 no_undefined_symbols,
189 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])