Research into exporting BitBake recipes for consumption elsewhere
Hi
I wrote a proof-of-concept patch which lets you export all the tasks from a BitBake project, as actual executable shell and Python scripts. I'm sharing it here to see if anyone is interested in this sort of thing, and to see if anyone reading can point out obvious improvements that I haven't spotted.
This is the first time I've hacked on BitBake's internals, so this code has quite a lot of room for improvement :-) Exporting is slow (~10 mins), blocks the 'cooker' process completely while it runs, and causes BitBake to hang once it's finished. But, the generated scripts do actually work, which is what motived me to share the results.
The goal of this is to have a tool for 'flattening' a set of recipes, which I think is useful when reasoning about what they actually do.
Right now it produces almost 3GB of output for my test project. This is mostly because the entire data dict is dumped for each .py file. I've looked at deduplicating, but my naive implementation was incredibly slow (checking each variable in the dict for each task dump), and I've not thought of a more clever approach.
Anyway, I hope this is interesting to someone; I may be able to spend some time turning it into a viable patch, if there is interest.
Sam
Sam Thursfield (1):
Make an '--export-tasks' option
bitbake/lib/bb/cooker.py | 15 +- bitbake/lib/bb/cookerdata.py | 9 + bitbake/lib/bb/data.py | 9 +- bitbake/lib/bb/export.py | 426 +++++++++++++++++++++++++++++++++++++++++++ bitbake/lib/bb/main.py | 3 + bitbake/lib/bb/runqueue.py | 11 ++ 6 files changed, 471 insertions(+), 2 deletions(-) create mode 100644 bitbake/lib/bb/export.py
Signature · 1 line
2.4.3
PATCH 1/1 Make an '--export-tasks' option
This causes the build to fail, and dumps a huge amount of output, but the resulting script does seem to work, kinda.
This is also horrible currently because the server doesn't quit when you CTRL+C. Probably because it expects to be running lots of idle callbacks and instead it just runs a huge mainloop-blocking function.
Need to decide if the dump_task code needs to be so slow. If so, need to split it up into small tasks so the mainloop doesn't get blocked. Or, hopefully it can just be made really fast.
--- bitbake/lib/bb/cooker.py | 15 +- bitbake/lib/bb/cookerdata.py | 9 + bitbake/lib/bb/data.py | 9 +- bitbake/lib/bb/export.py | 426 +++++++++++++++++++++++++++++++++++++++++++ bitbake/lib/bb/main.py | 3 + bitbake/lib/bb/runqueue.py | 11 ++ 6 files changed, 471 insertions(+), 2 deletions(-) create mode 100644 bitbake/lib/bb/export.py
bitbake/lib/bb/cooker.py +14 -1
@@ -34,7 +34,7 @@ from cStringIO import StringIO from contextlib import closing from functools import wraps from collections import defaultdict -import bb, bb.exceptions, bb.command +import bb, bb.exceptions, bb.command, bb.export from bb import utils, data, parse, event, cache, providers, taskdata, runqueue, build import Queue import signal
@@ -186,6 +186,19 @@ class BBCooker: # Let SIGHUP exit as SIGTERM signal.signal(signal.SIGHUP, self.sigterm_exception) + if configuration.export_tasks_dir: + python_globals_filename = os.path.join( + configuration.export_tasks_dir, 'data.py') + with open(python_globals_filename, 'w') as f: + f.write('# Global BitBake data.\n') + bb.export.export_variables(f, self.data, bb.utils.get_context()) + + shell_globals_filename = os.path.join( + configuration.export_tasks_dir, 'globals.sh') + with open(shell_globals_filename, 'w') as f: + f.write('# Global BitBake shell variables.\n') + bb.export.export_shell_variables(f, self.data) + def config_notifications(self, event): if not event.pathname in self.configwatcher.bbwatchedfiles: return
bitbake/lib/bb/cookerdata.py +9 -0
@@ -141,6 +141,7 @@ class CookerConfiguration(object): self.tracking = False self.interface = [] self.writeeventlog = False + self.export_tasks_dir = None self.env = {}
@@ -221,6 +222,14 @@ class CookerDataBuilder(object): bb.utils.set_context(bb.utils.clean_context()) bb.event.set_class_handlers(bb.event.clean_class_handlers()) self.data = bb.data.init() + + if cookercfg.export_tasks_dir: + # Exporting the tasks requires exporting the _context dict, and to + # do that we need to be able to look at the original values of + # overridden variables. The VariableHistory object gives us a way + # of doing that. + self.tracking = True + if self.tracking: self.data.enableTracking()
bitbake/lib/bb/data.py +8 -1
@@ -305,7 +305,6 @@ def emit_func_python(func, o=sys.__stdout__, d = init()): if call: o.write(func + "(d)" + "\n\n") - write_func(func, o, True) pp = bb.codeparser.PythonParser(func, logger) pp.parse_python(d.getVar(func, True)) newdeps = pp.execs
@@ -324,6 +323,14 @@ def emit_func_python(func, o=sys.__stdout__, d = init()): newdeps |= set((d.getVarFlag(dep, "vardeps", True) or "").split()) newdeps -= seen + ### FIXME: Moved to *after* the deps are written to the file, by Sam Thursfield. + # Seems this is needed for the dumped 'do_populate_lic' file to actually + # work. It was failing because 'find_license_files()' wasn't defined, but + # really the definition was just ordered after the first use. Not sure why + # this doesn't affect normal execution inside BitBake! + + write_func(func, o, True) + def update_data(d): """Performs final steps upon the datastore, including application of overrides""" d.finalize(parent = True)
bitbake/lib/bb/export.py new file +426 -0
426 lines not on this page · raw message
bitbake/lib/bb/main.py +3 -0
@@ -234,6 +234,9 @@ class BitBakeConfigParameters(cookerdata.ConfigParameters): parser.add_option("-w", "--write-log", help = "Writes the event log of the build to a bitbake event json file. Use '' (empty string) to assign the name automatically.", action = "store", dest = "writeeventlog") + parser.add_option("", "--export-tasks", help = "Export each task as a script in a given directory.", + action = "store", dest = "export_tasks_dir", default = None) + options, targets = parser.parse_args(argv) # some environmental variables set also configuration options
bitbake/lib/bb/runqueue.py +11 -0
@@ -1036,6 +1036,17 @@ class RunQueue: depgraph = self.cooker.buildDependTree(self, self.rqdata.taskData) bb.event.fire(bb.event.DepTreeGenerated(depgraph), self.cooker.data) + ### Hack to dump running order. + # This should become its own task, and also reset the runqueue when + # complete ... + if self.cooker.configuration.export_tasks_dir: + dump_dir = self.cooker.configuration.export_tasks_dir + schedule_filename = os.path.join(dump_dir, 'run.sh') + with open(schedule_filename, 'w') as f: + f.write('# Overall task schedule.\n') + f.write('set -e -x\n') + bb.export.export_task_queue(dump_dir, f, self) + if self.state is runQueueSceneInit: dump = self.cooker.configuration.dump_signatures if dump:
Signature · 1 line
2.4.3
Quoted 15 lines · Sam Thursfield
I wrote a proof-of-concept patch which lets you export all the tasks from a BitBake project, as actual executable shell and Python scripts. I'm sharing it here to see if anyone is interested in this sort of thing, and to see if anyone reading can point out obvious improvements that I haven't spotted.
This is the first time I've hacked on BitBake's internals, so this code has quite a lot of room for improvement :-) Exporting is slow (~10 mins), blocks the 'cooker' process completely while it runs, and causes BitBake to hang once it's finished. But, the generated scripts do actually work, which is what motived me to share the results.
The goal of this is to have a tool for 'flattening' a set of recipes, which I think is useful when reasoning about what they actually do.
Quoted 9 lines
Right now it produces almost 3GB of output for my test project. This is mostly because the entire data dict is dumped for each .py file. I've looked at deduplicating, but my naive implementation was incredibly slow (checking each variable in the dict for each task dump), and I've not thought of a more clever approach.
Anyway, I hope this is interesting to someone; I may be able to spend some time turning it into a viable patch, if there is interest.
I find this very interesting, and will definitely be checking out the patch.
Regarding exporting full data dict, we maintain variable dependency knowledge as a part of the metadata checksumming capability, so it should be straightforward to only emit what's used for the task in question.
Signature · 5 lines
Christopher Larson clarson at kergoth dot com Founder - BitBake, OpenEmbedded, OpenZaurus Maintainer - Tslib Senior Software Engineer, Mentor Graphics