2
1

builder.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 is_defconfig_valid(self, configfile, defconfig):
  12. """Check if the .config is contains all lines present in the defconfig."""
  13. with open(configfile) as configf:
  14. configlines = configf.readlines()
  15. defconfiglines = defconfig.split("\n")
  16. # Check that all the defconfig lines are still present
  17. for defconfigline in defconfiglines:
  18. if defconfigline + "\n" not in configlines:
  19. self.logfile.write("WARN: defconfig can't be used\n")
  20. self.logfile.write(" Missing: %s\n" % defconfigline.strip())
  21. self.logfile.flush()
  22. return False
  23. return True
  24. def configure(self, make_extra_opts=[], make_extra_env={}):
  25. """Configure the build.
  26. make_extra_opts: a list of arguments to be passed to the make
  27. command.
  28. e.g. make_extra_opts=["BR2_EXTERNAL=/path"]
  29. make_extra_env: a dict of variables to be appended (or replaced)
  30. in the environment that calls make.
  31. e.g. make_extra_env={"BR2_DL_DIR": "/path"}
  32. """
  33. if not os.path.isdir(self.builddir):
  34. os.makedirs(self.builddir)
  35. config_file = os.path.join(self.builddir, ".config")
  36. with open(config_file, "w+") as cf:
  37. cf.write(self.config)
  38. # dump the defconfig to the logfile for easy debugging
  39. self.logfile.write("> start defconfig\n" + self.config +
  40. "> end defconfig\n")
  41. self.logfile.flush()
  42. env = {"PATH": os.environ["PATH"]}
  43. env.update(make_extra_env)
  44. cmd = ["make",
  45. "O={}".format(self.builddir)]
  46. cmd += make_extra_opts
  47. cmd += ["olddefconfig"]
  48. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  49. cwd=infra.basepath(), env=env)
  50. if ret != 0:
  51. raise SystemError("Cannot olddefconfig")
  52. if not self.is_defconfig_valid(config_file, self.config):
  53. raise SystemError("The defconfig is not valid")
  54. def build(self, make_extra_opts=[], make_extra_env={}):
  55. """Perform the build.
  56. make_extra_opts: a list of arguments to be passed to the make
  57. command. It can include a make target.
  58. e.g. make_extra_opts=["foo-source"]
  59. make_extra_env: a dict of variables to be appended (or replaced)
  60. in the environment that calls make.
  61. e.g. make_extra_env={"BR2_DL_DIR": "/path"}
  62. """
  63. env = {"PATH": os.environ["PATH"]}
  64. if "http_proxy" in os.environ:
  65. self.logfile.write("Using system proxy: " +
  66. os.environ["http_proxy"] + "\n")
  67. env['http_proxy'] = os.environ["http_proxy"]
  68. env['https_proxy'] = os.environ["http_proxy"]
  69. env.update(make_extra_env)
  70. cmd = ["make", "-C", self.builddir]
  71. cmd += make_extra_opts
  72. ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
  73. env=env)
  74. if ret != 0:
  75. raise SystemError("Build failed")
  76. open(self.stamp_path(), 'a').close()
  77. def stamp_path(self):
  78. return os.path.join(self.builddir, "build-done")
  79. def is_finished(self):
  80. return os.path.exists(self.stamp_path())
  81. def delete(self):
  82. if os.path.exists(self.builddir):
  83. shutil.rmtree(self.builddir)