1515import multiprocessing
1616from 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
1921cpus = 4 if is_running_in_ci else os .cpu_count ()
2022build_dir = (Path (os .path .abspath (__file__ )).parents [2 ] / "build" )
2123cache_dir = build_dir / "cache"
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+
6583def 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
87112if __name__ == '__main__' :
88- exit (compile_examples (sys .argv [1 :]))
113+ exit (compile_examples (sys .argv [1 :]))
0 commit comments