create_kernel.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Recipe creation tool - kernel support plugin
  2. #
  3. # Copyright (C) 2016 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import re
  18. import logging
  19. from recipetool.create import RecipeHandler, read_pkgconfig_provides, validate_pv
  20. logger = logging.getLogger('recipetool')
  21. tinfoil = None
  22. def tinfoil_init(instance):
  23. global tinfoil
  24. tinfoil = instance
  25. class KernelRecipeHandler(RecipeHandler):
  26. def process(self, srctree, classes, lines_before, lines_after, handled, extravalues):
  27. import bb.process
  28. if 'buildsystem' in handled:
  29. return False
  30. for tell in ['arch', 'firmware', 'Kbuild', 'Kconfig']:
  31. if not os.path.exists(os.path.join(srctree, tell)):
  32. return False
  33. handled.append('buildsystem')
  34. del lines_after[:]
  35. del classes[:]
  36. template = os.path.join(tinfoil.config_data.getVar('COREBASE', True), 'meta-skeleton', 'recipes-kernel', 'linux', 'linux-yocto-custom.bb')
  37. def handle_var(varname, origvalue, op, newlines):
  38. if varname in ['SRCREV', 'SRCREV_machine']:
  39. while newlines[-1].startswith('#'):
  40. del newlines[-1]
  41. try:
  42. stdout, _ = bb.process.run('git rev-parse HEAD', cwd=srctree, shell=True)
  43. except bb.process.ExecutionError as e:
  44. stdout = None
  45. if stdout:
  46. return stdout.strip(), op, 0, True
  47. elif varname == 'LINUX_VERSION':
  48. makefile = os.path.join(srctree, 'Makefile')
  49. if os.path.exists(makefile):
  50. kversion = -1
  51. kpatchlevel = -1
  52. ksublevel = -1
  53. kextraversion = ''
  54. with open(makefile, 'r', errors='surrogateescape') as f:
  55. for i, line in enumerate(f):
  56. if i > 10:
  57. break
  58. if line.startswith('VERSION ='):
  59. kversion = int(line.split('=')[1].strip())
  60. elif line.startswith('PATCHLEVEL ='):
  61. kpatchlevel = int(line.split('=')[1].strip())
  62. elif line.startswith('SUBLEVEL ='):
  63. ksublevel = int(line.split('=')[1].strip())
  64. elif line.startswith('EXTRAVERSION ='):
  65. kextraversion = line.split('=')[1].strip()
  66. version = ''
  67. if kversion > -1 and kpatchlevel > -1:
  68. version = '%d.%d' % (kversion, kpatchlevel)
  69. if ksublevel > -1:
  70. version += '.%d' % ksublevel
  71. version += kextraversion
  72. if version:
  73. return version, op, 0, True
  74. elif varname == 'SRC_URI':
  75. while newlines[-1].startswith('#'):
  76. del newlines[-1]
  77. elif varname == 'COMPATIBLE_MACHINE':
  78. while newlines[-1].startswith('#'):
  79. del newlines[-1]
  80. machine = tinfoil.config_data.getVar('MACHINE', True)
  81. return machine, op, 0, True
  82. return origvalue, op, 0, True
  83. with open(template, 'r') as f:
  84. varlist = ['SRCREV', 'SRCREV_machine', 'SRC_URI', 'LINUX_VERSION', 'COMPATIBLE_MACHINE']
  85. (_, newlines) = bb.utils.edit_metadata(f, varlist, handle_var)
  86. lines_before[:] = [line.rstrip('\n') for line in newlines]
  87. return True
  88. def register_recipe_handlers(handlers):
  89. handlers.append((KernelRecipeHandler(), 100))