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