builder.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import shutil
  3. import subprocess
  4. import infra
  5. class Builder(object):
  6. def __init__(self, config, builddir, logtofile):
  7. self.config = '\n'.join([line.lstrip() for line in
  8. config.splitlines()]) + '\n'
  9. self.builddir = builddir
  10. self.logfile = infra.open_log_file(builddir, "build", logtofile)
  11. def configure(self, make_extra_opts=[], make_extra_env={}):
  12. """Configure the build.
  13. make_extra_opts: a list of arguments to be passed to the make
  14. command.
  15. e.g. make_extra_opts=["BR2_EXTERNAL=/path"]
  16. make_extra_env: a dict of variables to be appended (or replaced)
  17. in the environment that calls make.
  18. e.g. make_extra_env={"BR2_DL_DIR": "/path"}
  19. """
  20. if not os.path.isdir(self.builddir):
  21. os.makedirs(self.builddir)
  22. config_file = os.path.join(self.builddir, ".config")
  23. with open(config_file, "w+") as cf:
  24. cf.write(self.config)
  25. # dump the defconfig to the logfile for easy debugging
  26. self.logfile.write("> start defconfig\n" + self.config +
  27. "> end defconfig\n")
  28. self.logfile.flush()
  29. env = {"PATH": os.environ["PATH"]}
  30. env.update(make_extra_env)
  31. cmd = ["make",
  32. "O={}".format(self.builddir)]
  33. cmd += make_extra_opts
  34. cmd += ["olddefconfig"]
  35. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  36. env=env)
  37. if ret != 0:
  38. raise SystemError("Cannot olddefconfig")
  39. def build(self, make_extra_opts=[], make_extra_env={}):
  40. """Perform the build.
  41. make_extra_opts: a list of arguments to be passed to the make
  42. command. It can include a make target.
  43. e.g. make_extra_opts=["foo-source"]
  44. make_extra_env: a dict of variables to be appended (or replaced)
  45. in the environment that calls make.
  46. e.g. make_extra_env={"BR2_DL_DIR": "/path"}
  47. """
  48. env = {"PATH": os.environ["PATH"]}
  49. if "http_proxy" in os.environ:
  50. self.logfile.write("Using system proxy: " +
  51. os.environ["http_proxy"] + "\n")
  52. env['http_proxy'] = os.environ["http_proxy"]
  53. env['https_proxy'] = os.environ["http_proxy"]
  54. env.update(make_extra_env)
  55. cmd = ["make", "-C", self.builddir]
  56. cmd += make_extra_opts
  57. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  58. env=env)
  59. if ret != 0:
  60. raise SystemError("Build failed")
  61. open(self.stamp_path(), 'a').close()
  62. def stamp_path(self):
  63. return os.path.join(self.builddir, "build-done")
  64. def is_finished(self):
  65. return os.path.exists(self.stamp_path())
  66. def delete(self):
  67. if os.path.exists(self.builddir):
  68. shutil.rmtree(self.builddir)