-
Notifications
You must be signed in to change notification settings - Fork 12
/
entrypoint
executable file
·201 lines (177 loc) · 6.8 KB
/
entrypoint
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# This file is subject to the terms and conditions defined in
# file 'LICENSE', which is part of this source code package.
import re
import sys
import os
import subprocess
from contextlib import contextmanager
compiler_groups = {
"gcc": ("gcc", "g++"),
"clang": ("clang", "clang++"),
}
@contextmanager
def ghgroup(group):
print("::group::{}".format(group))
yield
print("::endgroup::")
def error(title, msg):
print("::error title={}::{}".format(title, msg))
sys.stderr.write("> {}\n".format(msg))
class Entrypoint(object):
def __init__(self, extenv):
self.extenv = extenv
self.cmd_cmake = None
self.cmd_build = None
self.cmd_test = None
self.cmd_post = None
def runcmd(self, cmd, logfile=None):
print("+ {}".format(cmd))
sys.stdout.flush()
p = subprocess.Popen(
cmd,
shell=True,
encoding="utf-8",
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
)
stdout = open(os.dup(p.stdout.fileno()), newline="")
# ^ Makes newlines in apt-get work
logfile = logfile or "/dev/null"
with open(logfile, "w", encoding="utf-8") as logfd:
for line in stdout:
sys.stdout.write(line)
if logfile:
logfd.write(line)
p.wait()
if p.returncode != 0:
error("popen", "> Error {} while running {}".format(p.returncode, cmd))
return False
return True
def runcmdsu(self, cmd, *args, **kwargs):
return self.runcmd('su - -c "{}"'.format(cmd), *args, **kwargs)
def run(self):
phases = {
"cmake": self.cmd_cmake,
"build": self.cmd_build,
"test": self.cmd_test,
"post": self.cmd_post,
}
for phasename, phasecmd in phases.items():
logfile = os.path.join("/tmp/{}.log".format(phasename))
if phasecmd:
grp = "Running phase [{}]: {}".format(phasename, phasecmd)
else:
grp = "Skipping phase [{}]".format(phasename)
with ghgroup(grp):
if phasecmd:
if not self.runcmd(phasecmd, logfile=logfile):
return False
return True
def dependencies(self):
dependencies = self.extenv.get("INPUT_DEPENDENCIES_DEBIAN")
if not dependencies:
return True
if not self.runcmdsu("apt-get update"):
print("::error title=dependencies::Error in apt-get update")
return False
if not self.runcmdsu(
"apt-get install --no-install-recommends -y {}".format(dependencies)
):
print("::error title=dependencies::Error in apt-get install")
return False
return True
def parse(self):
# Setup phase can be executed immediately, it ignores presets:
with ghgroup("Setup"):
# Directory:
directory = self.extenv.get("INPUT_WORKING-DIRECTORY")
if directory:
print("+ cd " + directory)
os.chdir(directory)
# pre_command:
cmd_pre = self.extenv.get("INPUT_PRE_COMMAND")
if cmd_pre:
self.runcmd(cmd_pre)
# Dependencies:
if not self.dependencies():
return False
# Defaults, affected by presets:
self.cmd_cmake = "cmake . "
self.cmd_cmake += self.extenv.get("INPUT_CMAKEFLAGS", "")
self.cmd_build = "make VERBOSE=1"
self.cmd_test = "ctest --output-on-failure ."
# Presets, overriding or complementing defaults:
preset = self.extenv.get("INPUT_PRESET")
if preset:
match = re.match(f"({'|'.join(compiler_groups)})-sanitize-(.*)", preset)
if match:
compilers = compiler_groups[match.group(1)]
sanitizer = match.group(2)
self.cmd_cmake += " -DCMAKE_C_COMPILER={}".format(compilers[0])
self.cmd_cmake += " -DCMAKE_CXX_COMPILER={}".format(compilers[1])
for lang in ["C", "CXX"]:
self.cmd_cmake += (
' -DCMAKE_{}_FLAGS="-fno-omit-frame-pointer'
' -fsanitize={}"'.format(lang, sanitizer)
)
elif preset == "install":
self.cmd_cmake += " -DCMAKE_INSTALL_PREFIX=/tmp/_install"
self.cmd_test = "make install"
self.cmd_post = "find /tmp/_install -type f"
elif preset == "cpack":
self.cmd_test = "cpack"
elif preset == "coverage":
for lang in ["C", "CXX"]:
self.cmd_cmake += " -DCMAKE_{}_FLAGS=--coverage".format(lang)
self.cmd_post = (
"lcov --compat-libtool --capture"
" --directory ."
" | sed s@^SF:%s/@SF:@ > lcov.info"
" && lcov --list lcov.info" % os.getcwd()
)
elif preset == "valgrind":
self.cmd_test += " -DExperimentalMemCheck"
# Static analysis presets
elif preset == "cppcheck":
for lang in ["C", "CXX"]:
self.cmd_cmake += " -DCMAKE_{}_CPPCHECK=cppcheck".format(lang)
self.cmd_test = ""
elif preset == "iwyu":
for lang in ["C", "CXX"]:
self.cmd_cmake += " -DCMAKE_{}_INCLUDE_WHAT_YOU_USE=iwyu".format(
lang
)
self.cmd_test = (
"! grep -q" ' "Warning: include-what-you-use"' " /tmp/build.log"
)
elif preset == "clang-tidy":
for lang in ["C", "CXX"]:
self.cmd_cmake += " -DCMAKE_{}_CLANG_TIDY=clang-tidy".format(lang)
self.cmd_test = ""
else:
error("preset", "Error: invalid preset {}".format(preset))
sys.exit(1)
print("Preset is {}".format(preset))
# The other inputs that override presets:
# Build
cmd_build_override = self.extenv.get("INPUT_BUILD_COMMAND")
if cmd_build_override:
self.cmd_build = cmd_build_override
# Test
cmd_test_override = self.extenv.get("INPUT_TEST_COMMAND")
if cmd_test_override:
self.cmd_test = cmd_test_override
# Post
cmd_post_override = self.extenv.get("INPUT_POST_COMMAND")
if cmd_post_override:
self.cmd_post = cmd_post_override
def main(self):
self.parse()
return self.run()
def main():
entrypoint = Entrypoint(os.environ)
if not entrypoint.main():
sys.exit(1)
if __name__ == "__main__":
main()