From 3ad10d7077f645c50642402c600b0ced75a780e9 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Fri, 4 Mar 2022 17:33:10 +1100 Subject: [PATCH] Tools: convert test_build_options to an object --- Tools/autotest/test_build_options.py | 141 +++++++++++++-------------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/Tools/autotest/test_build_options.py b/Tools/autotest/test_build_options.py index b659e3ef77..562f466263 100755 --- a/Tools/autotest/test_build_options.py +++ b/Tools/autotest/test_build_options.py @@ -14,87 +14,82 @@ import os from pysim import util -# swiped from app.py: -def get_build_options_from_ardupilot_tree(): - '''return a list of build options''' - import importlib.util - spec = importlib.util.spec_from_file_location( - "build_options.py", - os.path.join(os.path.dirname(os.path.realpath(__file__)), - '..', 'scripts', 'build_options.py')) - mod = importlib.util.module_from_spec(spec) - spec.loader.exec_module(mod) - return mod.BUILD_OPTIONS +class TestBuildOptions(object): + # swiped from app.py: + def get_build_options_from_ardupilot_tree(self): + '''return a list of build options''' + import importlib.util + spec = importlib.util.spec_from_file_location( + "build_options.py", + os.path.join(os.path.dirname(os.path.realpath(__file__)), + '..', 'scripts', 'build_options.py')) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod.BUILD_OPTIONS + def write_defines_to_file(self, defines, filepath): + content = "\n".join(["define %s %s" % (a, b) for (a, b) in defines.items()]) + with open(filepath, "w") as f: + f.write(content) -def write_defines_to_file(defines, filepath): - content = "\n".join(["define %s %s" % (a, b) for (a, b) in defines.items()]) - with open(filepath, "w") as f: - f.write(content) - - -def get_defines(feature, options): - '''returns a hash of (name, value) defines to turn feature off - - recursively gets dependencies''' - ret = { - feature.define: 0, - } - if feature.dependency is None: + def get_defines(self, feature, options): + '''returns a hash of (name, value) defines to turn feature off - + recursively gets dependencies''' + ret = { + feature.define: 0, + } + if feature.dependency is None: + return ret + for depname in feature.dependency.split(','): + dep = None + for f in options: + if f.label == depname: + dep = f + if dep is None: + raise ValueError("Invalid dep (%s)" % dep) + ret.update(self.get_defines(dep, options)) return ret - for depname in feature.dependency.split(','): - dep = None - for f in options: - if f.label == depname: - dep = f - if dep is None: - raise ValueError("Invalid dep (%s)" % dep) - ret.update(get_defines(dep, options)) - return ret + def test_feature(self, feature, options): + defines = self.get_defines(feature, options) + self.test_compile_with_defines(defines) -def test_feature(feature, options): - defines = get_defines(feature, options) - test_compile_with_defines(defines) + def test_compile_with_defines(self, defines): + extra_hwdef_filepath = "/tmp/extra.hwdef" + self.write_defines_to_file(defines, extra_hwdef_filepath) + util.waf_configure( + "CubeOrange", + extra_hwdef=extra_hwdef_filepath, + ) + for t in 'copter', 'plane', 'rover', 'antennatracker', 'sub', 'blimp': + try: + util.waf_build(t) + except Exception: + print("Failed to build (%s) with everything disabled" % + (t,)) + raise + def run_disable_in_turn(self): + options = self.get_build_options_from_ardupilot_tree() + count = 1 + for feature in options: + print("##### Disabling feature %s(%s) (%u/%u)" % + (feature.label, feature.define, count, len(options))) + self.test_feature(feature, options) + count += 1 -def test_compile_with_defines(defines): - extra_hwdef_filepath = "/tmp/extra.hwdef" - write_defines_to_file(defines, extra_hwdef_filepath) - util.waf_configure( - "CubeOrange", - extra_hwdef=extra_hwdef_filepath, - ) - for t in 'copter', 'plane', 'rover', 'antennatracker', 'sub', 'blimp': - try: - util.waf_build(t) - except Exception: - print("Failed to build (%s) with everything disabled" % - (t,)) - raise + def run_disable_all(self): + options = self.get_build_options_from_ardupilot_tree() + defines = {} + for feature in options: + defines[feature.define] = 0 + self.test_compile_with_defines(defines) - -def run_disable_in_turn(): - options = get_build_options_from_ardupilot_tree() - count = 1 - for feature in options: - print("##### Disabling feature %s(%s) (%u/%u)" % - (feature.label, feature.define, count, len(options))) - test_feature(feature, options) - count += 1 - - -def run_disable_all(): - options = get_build_options_from_ardupilot_tree() - defines = {} - for feature in options: - defines[feature.define] = 0 - test_compile_with_defines(defines) - - -def run(): - run_disable_all() - run_disable_in_turn() + def run(self): + self.run_disable_all() + self.run_disable_in_turn() if __name__ == '__main__': - run() + tbo = TestBuildOptions() + tbo.run()