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