]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/meson.build
src/boot/efi/meson.build: if meson --werror is true, set gcc -Werror
[thirdparty/systemd.git] / src / boot / efi / meson.build
1 # SPDX-License-Identifier: LGPL-2.1+
2
3 efi_headers = files('''
4 console.h
5 crc32.h
6 disk.h
7 graphics.h
8 linux.h
9 measure.h
10 missing_efi.h
11 pe.h
12 random-seed.h
13 sha256.h
14 shim.h
15 splash.h
16 util.h
17 '''.split())
18
19 common_sources = '''
20 disk.c
21 graphics.c
22 measure.c
23 pe.c
24 util.c
25 '''.split()
26
27 systemd_boot_sources = '''
28 boot.c
29 console.c
30 crc32.c
31 random-seed.c
32 sha256.c
33 shim.c
34 '''.split()
35
36 stub_sources = '''
37 linux.c
38 splash.c
39 stub.c
40 '''.split()
41
42 if conf.get('ENABLE_EFI') == 1 and get_option('gnu-efi') != 'false'
43 efi_cc = get_option('efi-cc')
44 if efi_cc.length() == 0
45 efi_cc = cc.cmd_array()
46 endif
47 efi_ld = get_option('efi-ld')
48 if efi_ld == ''
49 efi_ld = find_program('ld', required: true)
50 endif
51 efi_incdir = get_option('efi-includedir')
52
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 == ''
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 == ''
67 ret = run_command(efi_cc + ['-print-multi-os-directory'])
68 if ret.returncode() == 0
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
74 endif
75 endif
76
77 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
78 else
79 have_gnu_efi = false
80 endif
81
82 if get_option('gnu-efi') == 'true' and not have_gnu_efi
83 error('gnu-efi support requested, but headers were not found')
84 endif
85
86 if have_gnu_efi
87 efi_conf = configuration_data()
88 efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
89 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
90 efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
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')
99 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)
100 if efi_ldsdir == ''
101 efi_ldsdir = join_paths(efi_libdir, 'gnuefi')
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
110 endif
111
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,
125 '-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
126 '-include', efi_config_h,
127 '-include', version_h]
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 if get_option('werror') == true
139 compile_args += ['-Werror']
140 endif
141
142 efi_ldflags = ['-T',
143 join_paths(efi_ldsdir, arch_lds),
144 '-shared',
145 '-Bsymbolic',
146 '-nostdlib',
147 '-znocombreloc',
148 '-L', efi_libdir,
149 join_paths(efi_ldsdir, 'crt0-efi-@0@.o'.format(gnu_efi_path_arch))]
150 if efi_arch == 'aarch64' or efi_arch == 'arm'
151 # Aarch64 and ARM32 don't have an EFI capable objcopy. Use 'binary'
152 # instead, and add required symbols manually.
153 efi_ldflags += ['--defsym=EFI_SUBSYSTEM=0xa']
154 efi_format = ['-O', 'binary']
155 else
156 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
157 endif
158
159 systemd_boot_objects = []
160 stub_objects = []
161 foreach file : common_sources + systemd_boot_sources + stub_sources
162 o_file = custom_target(file + '.o',
163 input : file,
164 output : file + '.o',
165 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
166 + compile_args,
167 depend_files : efi_headers)
168 if (common_sources + systemd_boot_sources).contains(file)
169 systemd_boot_objects += o_file
170 endif
171 if (common_sources + stub_sources).contains(file)
172 stub_objects += o_file
173 endif
174 endforeach
175
176 libgcc_file_name = run_command(efi_cc + ['-print-libgcc-file-name']).stdout().strip()
177 systemd_boot_efi_name = 'systemd-boot@0@.efi'.format(EFI_MACHINE_TYPE_NAME)
178 stub_efi_name = 'linux@0@.efi.stub'.format(EFI_MACHINE_TYPE_NAME)
179 no_undefined_symbols = find_program('no-undefined-symbols.sh')
180
181 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
182 ['stub.so', stub_efi_name, stub_objects]]
183 so = custom_target(
184 tuple[0],
185 input : tuple[2],
186 output : tuple[0],
187 command : [efi_ld, '-o', '@OUTPUT@'] +
188 efi_ldflags + tuple[2] +
189 ['-lefi', '-lgnuefi', libgcc_file_name])
190
191 if want_tests != 'false'
192 test('no-undefined-symbols-' + tuple[0],
193 no_undefined_symbols,
194 args : [so])
195 endif
196
197 stub = custom_target(
198 tuple[1],
199 input : so,
200 output : tuple[1],
201 command : [objcopy,
202 '-j', '.text',
203 '-j', '.sdata',
204 '-j', '.data',
205 '-j', '.dynamic',
206 '-j', '.dynsym',
207 '-j', '.rel*']
208 + efi_format +
209 ['@INPUT@', '@OUTPUT@'],
210 install : true,
211 install_dir : bootlibdir)
212
213 set_variable(tuple[0].underscorify(), so)
214 set_variable(tuple[0].underscorify() + '_stub', stub)
215 endforeach
216 endif
217
218 ############################################################
219
220 if have_gnu_efi
221 test_efi_disk_img = custom_target(
222 'test-efi-disk.img',
223 input : [systemd_boot_so, stub_so_stub],
224 output : 'test-efi-disk.img',
225 command : [test_efi_create_disk_sh, '@OUTPUT@',
226 '@INPUT0@', '@INPUT1@', splash_bmp])
227 endif