source: mainline/kernel/meson.build@ fa6fbad8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fa6fbad8 was 971849b1, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Enable LTO for kernel

  • Property mode set to 100644
File size: 4.8 KB
Line 
1
2arch_src = []
3
4# Fills arch_src.
5subdir('arch' / KARCH)
6
7# Defines genarch_src.
8subdir('genarch')
9
10# Defines generic_src, instrumentable_src.
11subdir('generic')
12
13# Defines test_src
14subdir('test')
15
16## Cross-platform assembly to start a symtab.data section
17#
18symtab_section = '.section symtab.data, "a", ' + atsign + 'progbits;'
19
20kernel_include_dirs = include_directories(
21 'generic/include',
22 'genarch/include',
23 'arch' / KARCH / 'include',
24 '..' / 'abi' / 'arch' / KARCH / 'include',
25 '..' / 'abi' / 'include',
26 'test',
27)
28
29kernel_defs = [
30 '-imacros', meson.build_root() / 'config.h',
31 '-D_HELENOS_SOURCE',
32 '-DKERNEL',
33 '-DRELEASE=' + HELENOS_RELEASE,
34 '-DCOPYRIGHT=' + HELENOS_COPYRIGHT,
35 '-DNAME=' + HELENOS_NAME,
36 '-D__@0@_BITS__'.format(meson.get_cross_property('bits')),
37]
38
39# Preprocess linker script using C preprocessor.
40kernel_ldscript = custom_target('_link.ld',
41 input: 'arch' / KARCH / '_link.ld.in',
42 output: '_link.ld',
43 command: [
44 cc.cmd_array(),
45 arch_kernel_c_args,
46 kernel_defs,
47 '-I' + meson.current_source_dir() / 'arch' / KARCH / 'include',
48 '-D__ASSEMBLER__',
49 '-D__LINKER__',
50 '-E',
51 '-P',
52 '-x', 'c',
53 '@INPUT@',
54 ],
55 capture: true,
56 build_by_default: true,
57)
58
59kernel_link_args = arch_kernel_link_args + [
60 '-Wl,--nmagic',
61 '-T', meson.current_build_dir() / '_link.ld',
62]
63
64if CONFIG_LTO
65 kernel_link_args += [ '-flto' ]
66endif
67
68if CONFIG_STRIP_BINARIES
69 # TODO: do this after disassembling
70 kernel_link_args += [ '-s' ]
71endif
72
73kernel_c_args = arch_kernel_c_args + kernel_defs + [
74 '-ffreestanding',
75 # TODO: remove this flag
76 cc.get_supported_arguments([ '-Wno-cast-function-type' ]),
77]
78
79if CONFIG_LTO
80 kernel_c_args += [ '-flto' ]
81endif
82
83if cc.get_id() == 'clang'
84 kernel_c_args += [
85 '-fno-stack-protector',
86 '-fno-PIC',
87 '-mllvm', '-asm-macro-max-nesting-depth=1000',
88 ]
89endif
90
91instrumentables = static_library('instrumentables', instrumentable_src,
92 include_directories: kernel_include_dirs,
93 implicit_include_directories: false,
94 c_args: kernel_c_args + (CONFIG_TRACE ? [ '-finstrument-functions' ] : []),
95 pic: false,
96)
97
98noninstrumentables = static_library('noninstrumentables', arch_src, genarch_src, generic_src, test_src,
99 include_directories: kernel_include_dirs,
100 implicit_include_directories: false,
101 c_args: kernel_c_args,
102 pic: false,
103)
104
105all_kernel_objects = [ instrumentables, noninstrumentables ]
106
107# We iterate the build several times to get symbol table right.
108# Three times is sufficient to get correct even symbols after symtab.
109
110if CONFIG_SYMTAB
111 # Iterate build three times.
112 iterations = [ 1, 2, 3 ]
113
114 # Generates symbol table information as an object file.
115 genmap = find_program('tools/genmap.py')
116
117 # Symbol table dump needed for genmap.
118 kernel_syms = custom_target('kernel_syms.txt',
119 input: all_kernel_objects,
120 output: 'kernel_syms.txt',
121 command: [ objdump, '-t', '@INPUT@' ],
122 capture: true,
123 )
124else
125 # Build just once.
126 iterations = [ 1 ]
127endif
128
129# Empty symbol map for first iteration.
130kernel_map_S = custom_target('empty_map.S',
131 output: 'empty_map.S',
132 capture: true,
133 command: [ 'echo', kernel_as_prolog + symtab_section ],
134)
135
136foreach iter : iterations
137 is_last = (iter == iterations.length())
138 kernel_name = 'kernel.@0@.elf'.format(iter)
139 kernel_map_name = kernel_name + '.map'
140 kernel_map_path = meson.current_build_dir() / kernel_map_name
141
142 kernel_elf = executable(kernel_name, kernel_map_S,
143 include_directories: kernel_include_dirs,
144 implicit_include_directories: false,
145 c_args: kernel_c_args,
146 link_args: kernel_c_args + kernel_link_args + [
147 '-Wl,-Map,' + kernel_map_path,
148 ],
149 link_depends: kernel_ldscript,
150 link_whole: all_kernel_objects,
151 pie: false,
152 )
153
154 # Generate symbol table if this is not the final iteration.
155 if not is_last
156
157 # TODO: Teach kernel to read its own ELF symbol table and get rid of this nonsense.
158 # Need to first make sure all architectures (even future ones with dumb bootloaders) can use ELF formatted kernel.
159
160 kernel_map_bin = custom_target(kernel_map_name + '.bin',
161 output: kernel_map_name + '.bin',
162 input: [ kernel_elf, kernel_syms ],
163 command: [ genmap, kernel_map_path, '@INPUT1@', '@OUTPUT@' ],
164 )
165
166 kernel_map_S_name = kernel_name + '.map.S'
167
168 kernel_map_S = custom_target(kernel_map_S_name,
169 input: kernel_map_bin,
170 output: kernel_map_S_name,
171 capture: true,
172 command: [ 'echo', kernel_as_prolog + symtab_section + ' .incbin "@INPUT@"' ],
173 )
174 endif
175endforeach
176
177rd_init_binaries += [[ kernel_elf, 'boot/kernel.elf' ]]
178
179install_files += [[ 'boot', kernel_elf.full_path(), 'kernel.elf' ]]
180install_deps += [ kernel_elf ]
181
182kernel_disasm = custom_target('kernel.elf.disasm',
183 command: [ objdump, '-S', '@INPUT@' ],
184 input: kernel_elf,
185 output: 'kernel.elf.disasm',
186 capture: true,
187 build_by_default: true,
188)
189
190# TODO: Add configuration option for installing debug files
191if false
192 install_files += [[ 'boot', kernel_disasm.full_path(), 'kernel.elf.disasm' ]]
193 install_deps += [ kernel_disasm ]
194endif
Note: See TracBrowser for help on using the repository browser.