build-android 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import common
  5. import shutil
  6. from shell_helpers import LF
  7. class Main(common.BuildCliFunction):
  8. def __init__(self):
  9. super().__init__(
  10. description='''\
  11. Download and build Android AOSP.
  12. https://cirosantilli.com/linux-kernel-module-cheat#android
  13. '''
  14. )
  15. self.add_argument(
  16. '--extra-args',
  17. default='',
  18. )
  19. self.add_argument(
  20. 'targets',
  21. default=['build'],
  22. nargs='*',
  23. )
  24. def build(self):
  25. if 'download' in self.env['targets']:
  26. os.makedirs(self.env['android_dir'], exist_ok=True)
  27. # Can only download base64. I kid you not:
  28. # https://github.com/google/gitiles/issues/7
  29. self.sh.wget(
  30. 'https://android.googlesource.com/tools/repo/+/v2.8/repo?format=TEXT',
  31. self.env['repo_path_base64'],
  32. )
  33. with open(self.env['repo_path_base64'], 'r') as input, \
  34. open(self.env['repo_path'], 'w') as output:
  35. output.write(self.sh.base64_decode(input.read()))
  36. self.sh.chmod(self.env['repo_path'])
  37. self.sh.run_cmd(
  38. [
  39. self.env['repo_path'], LF,
  40. 'init', LF,
  41. '-b', 'android-{}'.format(self.env['android_version']), LF,
  42. '--depth', '1', LF,
  43. '-u', 'https://android.googlesource.com/platform/manifest', LF,
  44. ],
  45. cwd=self.env['android_dir'],
  46. )
  47. self.sh.run_cmd(
  48. [
  49. self.env['repo_path'], LF,
  50. 'sync', LF,
  51. '-c', LF,
  52. '-j', str(self.env['nproc']), LF,
  53. '--no-tags', LF,
  54. '--no-clone-bundle', LF,
  55. ],
  56. cwd=self.env['android_dir'],
  57. )
  58. if 'build' in self.env['targets']:
  59. # The crappy android build system requires
  60. # https://stackoverflow.com/questions/7040592/calling-the-source-command-from-subprocess-popen
  61. self.sh.run_cmd('{}USE_CCACHE=1 make -j {} {}'.format(
  62. self.env['android_shell_setup'],
  63. self.env['nproc'],
  64. self.env['extra_args']
  65. ),
  66. cwd=self.env['android_dir'],
  67. executable=shutil.which('bash'),
  68. shell=True,
  69. )
  70. def get_build_dir(self):
  71. return self.env['android_build_dir']
  72. if __name__ == '__main__':
  73. Main().cli()