Skip to content

Commit 9675f17

Browse files
salkiniumrleh
authored andcommitted
[tools] Generate, build and run hosted examples
1 parent 457e815 commit 9675f17

File tree

9 files changed

+42
-9
lines changed

9 files changed

+42
-9
lines changed

examples/linux/assert/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run fail -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../build/linux/assert</option>

examples/linux/block_device/file/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../../build/linux/block_device/file</option>

examples/linux/block_device/mirror/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../../build/linux/block_device/mirror</option>

examples/linux/block_device/ram/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../../build/linux/block_device/ram</option>

examples/linux/build_info/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../build/linux/build_info</option>

examples/linux/git/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../build/linux/git</option>

examples/linux/logger/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../build/linux/logger</option>

examples/linux/printf/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<library>
2+
<!-- CI: run -->
23
<options>
34
<option name="modm:target">hosted-linux</option>
45
<option name="modm:build:build.path">../../../build/linux/printf</option>

tools/scripts/examples_compile.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import multiprocessing
1616
from pathlib import Path
1717

18-
is_running_in_ci = os.getenv("CIRCLECI") is not None or os.getenv("TRAVIS") is not None
18+
is_running_in_ci = os.getenv("CIRCLECI") is not None or \
19+
os.getenv("TRAVIS") is not None or \
20+
os.getenv("GITHUB_ACTIONS") is not None
1921
cpus = 4 if is_running_in_ci else os.cpu_count()
2022
build_dir = (Path(os.path.abspath(__file__)).parents[2] / "build")
2123
cache_dir = build_dir / "cache"
@@ -24,10 +26,10 @@
2426
global_options["::build.path"] = "build/"
2527
global_options[":::cache_dir"] = str(cache_dir)
2628

27-
def run(where, command):
29+
def run_command(where, command, all_output=False):
2830
result = subprocess.run(command, shell=True, cwd=where, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2931
output = ""
30-
if result.returncode:
32+
if result.returncode or all_output:
3133
output += result.stdout.decode("utf-8").strip(" \n")
3234
output += result.stderr.decode("utf-8").strip(" \n")
3335
return (result.returncode, output)
@@ -37,9 +39,9 @@ def generate(project):
3739
output = ["=" * 90, "Generating: {}".format(path)]
3840
options = " ".join("-D{}={}".format(k, v) for k,v in global_options.items())
3941
# Compile Linux examples under macOS with hosted-darwin target
40-
if "Darwin" in platform.system() and "hosted-linux" in project.read_text():
41-
options += " -D:target=hosted-darwin"
42-
rc, ro = run(path, "lbuild {} build".format(options))
42+
if "hosted-linux" in project.read_text():
43+
options += " -D:target=hosted-{}".format(platform.system().lower())
44+
rc, ro = run_command(path, "lbuild {} build".format(options))
4345
print("\n".join(output + [ro]))
4446
return None if rc else project
4547

@@ -48,20 +50,36 @@ def build(project):
4850
project_cfg = project.read_text()
4951
commands = []
5052
if ":build:scons" in project_cfg:
51-
commands.append("python3 `which scons` build --cache-show --random")
53+
if is_running_in_ci:
54+
commands.append("scons build --cache-show --random")
55+
else:
56+
commands.append("python3 `which scons` build --cache-show --random")
5257
if ":build:cmake" in project_cfg:
5358
commands.append("make cmake && make build")
5459

5560
rcs = 0
5661
for command in commands:
5762
output = ["=" * 90, "Building: {} with {}".format(
5863
path / "main.cpp", "SCons" if "scons" in command else "CMake")]
59-
rc, ro = run(path, command)
64+
rc, ro = run_command(path, command)
6065
rcs += rc
6166
print("\n".join(output + [ro]))
6267

6368
return None if rcs else project
6469

70+
def run(project):
71+
path = project.parent
72+
if is_running_in_ci:
73+
command = "scons run"
74+
else:
75+
command = "python3 `which scons` run"
76+
output = ["=" * 90, "Running: {} with {}".format(path / "main.cpp", "SCons" if "scons" in command else "CMake")]
77+
rc, ro = run_command(path, command, all_output=True)
78+
print("\n".join(output + [ro]))
79+
if "CI: run fail" in project.read_text():
80+
return None if not rc else project
81+
return None if rc else project
82+
6583
def compile_examples(paths):
6684
print("Using {}x parallelism".format(cpus))
6785
# Create build folder to prevent process race
@@ -81,8 +99,15 @@ def compile_examples(paths):
8199
projects = pool.map(build, projects)
82100
results += projects.count(None)
83101

102+
# Filter projects for successful compilation and runablity
103+
projects = [p for p in projects if p is not None and "CI: run" in p.read_text()]
104+
# Then run the successfully compiled ones
105+
with multiprocessing.Pool(cpus) as pool:
106+
projects = pool.map(run, projects)
107+
results += projects.count(None)
108+
84109
return results
85110

86111

87112
if __name__ == '__main__':
88-
exit(compile_examples(sys.argv[1:]))
113+
exit(compile_examples(sys.argv[1:]))

0 commit comments

Comments
 (0)