-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
configure-oss-fuzz.py
executable file
·133 lines (119 loc) · 4.32 KB
/
configure-oss-fuzz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Filipe Laíns <[email protected]>
import os
import pathlib
import traceback
from typing import Dict
import build_system
import build_system.builder
import build_system.dependencies
import build_system.ninja
import configure
class OSSFuzzNinjaBuilder(build_system.ninja.NinjaBuilder):
"""ninja build for oss-fuzz.
Uses $CXX to link and injects $CFLAGS, $CXXFLAGS and $LIB_FUZZING_ENGINE.
https://google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements
"""
def write_variables(
self,
details: build_system.BuildDetails,
settings: build_system.BuildSettings,
dependencies: Dict[str, build_system.dependencies.Dependency],
) -> None:
self.writer.comment('variables')
self.writer.newline()
# paths
for placeholder, path in self._path_placeholders.items():
self.writer.variable(placeholder, path)
# tools
self.writer.variable('cc', os.environ.get('CC', 'clang'))
self.writer.variable('cxx', os.environ.get('CXX', 'clang++'))
self.writer.variable('ar', 'ar')
self.writer.variable('objcopy', 'objcopy')
self.writer.variable('size', 'size')
# flags
self.writer.variable('c_flags', details.c_flags + [
os.environ.get('CFLAGS', ''),
])
self.writer.variable('cxx_flags', details.c_flags + [
os.environ.get('CXXFLAGS', ''),
])
self.writer.variable('c_include_flags', [
f'-I{self.path(self._location.code)}'
] + [
f'-include {self.path(path)}' for path in details.include_files
] + [
f'-I{self.path(path)}'
for dep in dependencies.values()
for path in dep.external_include
])
self.writer.variable('ld_flags', details.ld_flags + ([
f'-L{self.path(details.linker_dir)}',
f'-T{details.linker.relative_to(details.linker_dir)}',
] if details.linker else []) + [
os.environ.get('LIB_FUZZING_ENGINE', '-fsanitize=fuzzer,address'),
])
self.writer.newline()
def write_rules(self) -> None:
self.writer.comment('rules')
self.writer.newline()
self.writer.rule(
'cc',
command='$cc -MMD -MT $out -MF $out.d $c_flags $c_include_flags -c $in -o $out',
depfile='$out.d',
deps='gcc',
description='CC $out',
)
self.writer.rule(
'cxx',
command='$cc -MMD -MT $out -MF $out.d $cxx_flags $c_include_flags -c $in -o $out',
depfile='$out.d',
deps='gcc',
description='CXX $out',
)
self.writer.newline()
self.writer.rule(
'ar',
command='rm -f $out && $ar crs $out $in',
description='AR $out',
)
self.writer.newline()
self.writer.rule(
'link',
command='$cxx -o $out $in $ld_flags',
description='LINK $out',
)
self.writer.newline()
self.writer.rule(
'bin',
command='$objcopy -O binary $in $out',
description='BIN $out',
)
self.writer.newline()
self.writer.rule(
'hex',
command='$objcopy -O ihex $in $out',
description='HEX $out',
)
self.writer.newline()
if __name__ == '__main__':
project_root = pathlib.Path(__file__).absolute().parent
cc = os.environ.get('CC', 'clang')
if cc not in ('gcc', 'clang'):
raise ValueError('Incompatible $CC, only `gcc` or `clang` are supported')
try:
builder = build_system.builder.Builder(
build_system.BuildLocation(project_root, 'build-fuzz'),
build_system.TargetInfo('fuzz'),
build_system.BuildSettings(compiler=cc), # type: ignore[arg-type]
build_system.VendorInfo(),
build_system.VersionInfo.from_git(),
)
builder.print_summary()
builder.write_ninja(ninja_build_cls=OSSFuzzNinjaBuilder)
print("\nbuild.ninja written! Call 'ninja' to compile...")
except Exception as e:
print()
print(configure._DIM + traceback.format_exc() + configure._RESET)
configure._error(str(e))