]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/meson.build
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 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()
66 endif
67 endif
68 endif
69
70 have_gnu_efi = gnu_efi_path_arch != '' and efi_libdir != ''
71 else
72 have_gnu_efi = false
73 endif
74
75 if get_option('gnu-efi') == 'true' and not have_gnu_efi
76 error('gnu-efi support requested, but headers were not found')
77 endif
78
79 if have_gnu_efi
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'))
85
86 efi_config_h = configure_file(
87 output : 'efi_config.h',
88 configuration : efi_conf)
89
90 objcopy = find_program('objcopy')
91
92 efi_ldsdir = get_option('efi-ldsdir')
93 arch_lds = 'elf_@0@_efi.lds'.format(gnu_efi_path_arch)
94 if efi_ldsdir == ''
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))
102 endif
103 endif
104 endif
105
106 compile_args = ['-Wall',
107 '-Wextra',
108 '-std=gnu90',
109 '-nostdinc',
110 '-ggdb', '-O0',
111 '-fpic',
112 '-fshort-wchar',
113 '-ffreestanding',
114 '-fno-strict-aliasing',
115 '-fno-stack-protector',
116 '-Wsign-compare',
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',
123 '-mno-sse',
124 '-mno-mmx',
125 '-DEFI_FUNCTION_WRAPPER',
126 '-DGNU_EFI_USE_MS_ABI']
127 elif efi_arch == 'ia32'
128 compile_args += ['-mno-sse',
129 '-mno-mmx']
130 endif
131
132 efi_ldflags = ['-T',
133 join_paths(efi_ldsdir, arch_lds),
134 '-shared',
135 '-Bsymbolic',
136 '-nostdlib',
137 '-znocombreloc',
138 '-L', efi_libdir,
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']
145 else
146 efi_format = ['--target=efi-app-@0@'.format(gnu_efi_arch)]
147 endif
148
149 systemd_boot_objects = []
150 stub_objects = []
151 foreach file : common_sources + systemd_boot_sources + stub_sources
152 o_file = custom_target(file + '.o',
153 input : file,
154 output : file + '.o',
155 command : efi_cc + ['-c', '@INPUT@', '-o', '@OUTPUT@']
156 + compile_args,
157 depend_files : efi_headers)
158 if (common_sources + systemd_boot_sources).contains(file)
159 systemd_boot_objects += o_file
160 endif
161 if (common_sources + stub_sources).contains(file)
162 stub_objects += o_file
163 endif
164 endforeach
165
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')
170
171 foreach tuple : [['systemd_boot.so', systemd_boot_efi_name, systemd_boot_objects],
172 ['stub.so', stub_efi_name, stub_objects]]
173 so = custom_target(
174 tuple[0],
175 input : tuple[2],
176 output : tuple[0],
177 command : [efi_ld, '-o', '@OUTPUT@'] +
178 efi_ldflags + tuple[2] +
179 ['-lefi', '-lgnuefi', libgcc_file_name])
180
181 if want_tests != 'false'
182 test('no-undefined-symbols-' + tuple[0],
183 no_undefined_symbols,
184 args : [so])
185 endif
186
187 stub = custom_target(
188 tuple[1],
189 input : so,
190 output : tuple[1],
191 command : [objcopy,
192 '-j', '.text',
193 '-j', '.sdata',
194 '-j', '.data',
195 '-j', '.dynamic',
196 '-j', '.dynsym',
197 '-j', '.rel',
198 '-j', '.rela',
199 '-j', '.reloc']
200 + efi_format +
201 ['@INPUT@', '@OUTPUT@'],
202 install : true,
203 install_dir : bootlibdir)
204
205 set_variable(tuple[0].underscorify(), so)
206 set_variable(tuple[0].underscorify() + '_stub', stub)
207 endforeach
208 endif
209
210 ############################################################
211
212 if have_gnu_efi
213 test_efi_disk_img = custom_target(
214 'test-efi-disk.img',
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])
219 endif