builder.py 4.0 KB

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