]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/efi/meson.build
boot: fix build with gnu-efi older than 3.0.5
[thirdparty/systemd.git] / src / boot / efi / meson.build
CommitLineData
3a726fcd 1# SPDX-License-Identifier: LGPL-2.1+
3a726fcd 2
b710072d 3efi_headers = files('''
37efbbd8 4 console.h
fa2a3bbd 5 crc32.h
37efbbd8
ZJS
6 disk.h
7 graphics.h
8 linux.h
9 measure.h
4c858c6f 10 missing_efi.h
d4cbada2 11 pe.h
e4dcf7aa 12 random-seed.h
073220bf 13 sha256.h
2cb45cdc 14 shim.h
37efbbd8
ZJS
15 splash.h
16 util.h
b710072d
ZJS
17'''.split())
18
19common_sources = '''
37efbbd8
ZJS
20 disk.c
21 graphics.c
22 measure.c
d4cbada2 23 pe.c
37efbbd8 24 util.c
b710072d
ZJS
25'''.split()
26
27systemd_boot_sources = '''
37efbbd8
ZJS
28 boot.c
29 console.c
fa2a3bbd 30 crc32.c
e4dcf7aa 31 random-seed.c
073220bf
LP
32 sha256.c
33 shim.c
b710072d
ZJS
34'''.split()
35
36stub_sources = '''
37efbbd8
ZJS
37 linux.c
38 splash.c
39 stub.c
b710072d
ZJS
40'''.split()
41
349cc4a5 42if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
37efbbd8 43 efi_cc = get_option('efi-cc')
595343fb
MG
44 if efi_cc.length() == 0
45 efi_cc = cc.cmd_array()
df7cacae 46 endif
37efbbd8 47 efi_ld = get_option('efi-ld')
df7cacae
HG
48 if efi_ld == ''
49 efi_ld = find_program('ld', required: true)
50 endif
37efbbd8 51 efi_incdir = get_option('efi-includedir')
37efbbd8 52
4b4ee0f7
YW
53 gnu_efi_path_arch = ''
54 foreach name : [gnu_efi_arch, EFI_MACHINE_TYPE_NAME]
55 if (gnu_efi_path_arch == '' and name != '' and
56 cc.has_header('@0@/@1@/efibind.h'.format(efi_incdir, name)))
57 gnu_efi_path_arch = name
58 endif
59 endforeach
60
61 if gnu_efi_path_arch != '' and EFI_MACHINE_TYPE_NAME == ''
37efbbd8
ZJS
62 error('gnu-efi is available, but EFI_MACHINE_TYPE_NAME is unknown')
63 endif
64
65 efi_libdir = get_option('efi-libdir')
66 if efi_libdir == ''
5f723125 67 ret = run_command(efi_cc + ['-print-multi-os-directory'])
37efbbd8 68 if ret.returncode() == 0
5f723125
MG
69 path = join_paths('/usr/lib', ret.stdout().strip())
70 ret = run_command('realpath', '-e', path)
71 if ret.returncode() == 0
72 efi_libdir = ret.stdout().strip()
73 endif
37efbbd8
ZJS
74 endif
75 endif
76
4b4ee0f7 77 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
b710072d 78else
37efbbd8 79 have_gnu_efi = false
b710072d
ZJS
80endif
81
4390be30 82if get_option('gnu-efi') == 'true' and not have_gnu_efi
37efbbd8 83 error('gnu-efi support requested, but headers were not found')
b710072d
ZJS
84endif
85
86if have_gnu_efi
37efbbd8 87 efi_conf = configuration_data()
37efbbd8 88 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
349cc4a5 89 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
489e15a8 90 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
37efbbd8
ZJS
91
92 efi_config_h = configure_file(
93 output : 'efi_config.h',
94 configuration : efi_conf)
95
96 objcopy = find_program('objcopy')
97
98 efi_ldsdir = get_option('efi-ldsdir')
4b4ee0f7 99 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)
37efbbd8
ZJS
100 if efi_ldsdir == ''
101 efi_ldsdir = join_paths(efi_libdir, 'gnuefi')
6800fe7f
ZJS
102 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
103 if cmd.returncode() != 0
104 efi_ldsdir = efi_libdir
105 cmd = run_command('test', '-f', join_paths(efi_ldsdir, arch_lds))
106 if cmd.returncode() != 0
107 error('Cannot find @0@'.format(arch_lds))
108 endif
109 endif
37efbbd8
ZJS
110 endif
111
37efbbd8
ZJS
112 compile_args = ['-Wall',
113 '-Wextra',
114 '-std=gnu90',
115 '-nostdinc',
116 '-ggdb', '-O0',
117 '-fpic',
118 '-fshort-wchar',
119 '-ffreestanding',
120 '-fno-strict-aliasing',
121 '-fno-stack-protector',
122 '-Wsign-compare',
123 '-Wno-missing-field-initializers',
124 '-isystem', efi_incdir,
4b4ee0f7 125 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
681bd2c5
ZJS
126 '-include', efi_config_h,
127 '-include', version_h]
37efbbd8
ZJS
128 if efi_arch == 'x86_64'
129 compile_args += ['-mno-red-zone',
130 '-mno-sse',
131 '-mno-mmx',
132 '-DEFI_FUNCTION_WRAPPER',
133 '-DGNU_EFI_USE_MS_ABI']
134 elif efi_arch == 'ia32'
135 compile_args += ['-mno-sse',
136 '-mno-mmx']
137 endif
138
139 efi_ldflags = ['-T',
6800fe7f 140 join_paths(efi_ldsdir, arch_lds),
37efbbd8
ZJS
141 '-shared',
142 '-Bsymbolic',
143 '-nostdlib',
144 '-znocombreloc',
145 '-L', efi_libdir,
4b4ee0f7 146 join_paths(efi_ldsdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]
37efbbd8
ZJS
147 if efi_arch == 'aarch64' or efi_arch == 'arm'
148 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
149 # instead, and add required symbols manually.
150 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
151 efi_format = ['-O', 'binary']
152 else
6800fe7f 153 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
37efbbd8
ZJS
154 endif
155
156 systemd_boot_objects = []
157 stub_objects = []
158 foreach file : common_sources + systemd_boot_sources + stub_sources
159 o_file = custom_target(file + '.o',
160 input : file,
161 output : file + '.o',
595343fb 162 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
37efbbd8
ZJS
163 + compile_args,
164 depend_files : efi_headers)
165 if (common_sources + systemd_boot_sources).contains(file)
5a8b1640 166 systemd_boot_objects += o_file
37efbbd8
ZJS
167 endif
168 if (common_sources + stub_sources).contains(file)
5a8b1640 169 stub_objects += o_file
37efbbd8
ZJS
170 endif
171 endforeach
172
595343fb 173 libgcc_file_name = run_command(efi_cc + ['-print-libgcc-file-name']).stdout().strip()
37efbbd8
ZJS
174 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
175 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
176 no_undefined_symbols = find_program('no-undefined-symbols.sh')
177
178 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
179 ['stub.so', stub_efi_name, stub_objects]]
180 so = custom_target(
181 tuple[0],
182 input : tuple[2],
183 output : tuple[0],
184 command : [efi_ld, '-o', '@OUTPUT@'] +
185 efi_ldflags + tuple[2] +
186 ['-lefi', '-lgnuefi', libgcc_file_name])
187
938be089
ZJS
188 if want_tests != 'false'
189 test('no-undefined-symbols-' + tuple[0],
190 no_undefined_symbols,
191 args : [so])
192 endif
37efbbd8
ZJS
193
194 stub = custom_target(
195 tuple[1],
196 input : so,
197 output : tuple[1],
198 command : [objcopy,
199 '-j', '.text',
200 '-j', '.sdata',
201 '-j', '.data',
202 '-j', '.dynamic',
203 '-j', '.dynsym',
2f92b1e9 204 '-j', '.rel*']
37efbbd8
ZJS
205 + efi_format +
206 ['@INPUT@', '@OUTPUT@'],
207 install : true,
208 install_dir : bootlibdir)
209
210 set_variable(tuple[0].underscorify(), so)
211 set_variable(tuple[0].underscorify() + '_stub', stub)
212 endforeach
b710072d 213endif
d83f4f50
ZJS
214
215############################################################
216
217if have_gnu_efi
37efbbd8
ZJS
218 test_efi_disk_img = custom_target(
219 'test-efi-disk.img',
220 input : [systemd_boot_so, stub_so_stub],
221 output : 'test-efi-disk.img',
222 command : [test_efi_create_disk_sh, '@OUTPUT@',
223 '@INPUT0@', '@INPUT1@', splash_bmp])
d83f4f50 224endif