kernel_headers.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. # Copyright 2019 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Generates gen_headers_<arch>.bp or generates/checks kernel headers."""
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import argparse
  19. import filecmp
  20. import os
  21. import re
  22. import subprocess
  23. import sys
  24. def gen_version_h(verbose, gen_dir, version_makefile):
  25. """Generate linux/version.h
  26. Scan the version_makefile for the version info, and then generate
  27. linux/version.h in the gen_dir as done in kernel Makefile function
  28. filechk_version.h
  29. Args:
  30. verbose: Set True to print progress messages.
  31. gen_dir: Where to place the generated files.
  32. version_makefile: The makefile that contains version info.
  33. Return:
  34. If version info not found, False. Otherwise, True.
  35. """
  36. version_re = re.compile(r'VERSION\s*=\s*(\d+)')
  37. patchlevel_re = re.compile(r'PATCHLEVEL\s*=\s*(\d+)')
  38. sublevel_re = re.compile(r'SUBLEVEL\s*=\s*(\d+)')
  39. version_str = None
  40. patchlevel_str = None
  41. sublevel_str = None
  42. if verbose:
  43. print('gen_version_h: processing [%s]' % version_makefile)
  44. with open(version_makefile, 'r') as f:
  45. while not version_str or not patchlevel_str or not sublevel_str:
  46. line = f.readline()
  47. if not line:
  48. print(
  49. 'error: gen_version_h: failed to parse kernel version from %s' %
  50. version_makefile)
  51. return False
  52. line = line.rstrip()
  53. if verbose:
  54. print('gen_version_h: line is %s' % line)
  55. if not version_str:
  56. match = version_re.match(line)
  57. if match:
  58. if verbose:
  59. print('gen_version_h: matched version [%s]' % line)
  60. version_str = match.group(1)
  61. continue
  62. if not patchlevel_str:
  63. match = patchlevel_re.match(line)
  64. if match:
  65. if verbose:
  66. print('gen_version_h: matched patchlevel [%s]' % line)
  67. patchlevel_str = match.group(1)
  68. continue
  69. if not sublevel_str:
  70. match = sublevel_re.match(line)
  71. if match:
  72. if verbose:
  73. print('gen_version_h: matched sublevel [%s]' % line)
  74. sublevel_str = match.group(1)
  75. continue
  76. version = int(version_str)
  77. patchlevel = int(patchlevel_str)
  78. sublevel = int(sublevel_str)
  79. if verbose:
  80. print(
  81. 'gen_version_h: found kernel version %d.%d.%d' %
  82. (version, patchlevel, sublevel))
  83. version_h = os.path.join(gen_dir, 'linux', 'version.h')
  84. with open(version_h, 'w') as f:
  85. # This code must match the code in Makefile in the make function
  86. # filechk_version.h
  87. version_code = (version << 16) + (patchlevel << 8) + sublevel
  88. f.write('#define LINUX_VERSION_CODE %d\n' % version_code)
  89. f.write(
  90. '#define KERNEL_VERSION(a,b,c) ' +
  91. '(((a) << 16) + ((b) << 8) + (c))\n')
  92. return True
  93. def scan_arch_kbuild(verbose, arch_asm_kbuild, asm_generic_kbuild, arch_include_uapi):
  94. """Scan arch_asm_kbuild for generated headers.
  95. This function processes the Kbuild file to scan for three types of files that
  96. need to be generated. The first type are syscall generated headers, which are
  97. identified by adding to the generated-y make variable. The second type are
  98. generic headers, which are arch-specific headers that simply wrap the
  99. asm-generic counterpart, and are identified by adding to the generic-y make
  100. variable. The third type are mandatory headers that should be present in the
  101. /usr/include/asm folder.
  102. Args:
  103. verbose: Set True to print progress messages.
  104. arch_asm_kbuild: The Kbuild file containing lists of headers to generate.
  105. asm_generic_kbuild: The Kbuild file containing lists of mandatory headers.
  106. arch_include_uapi: Headers in /arch/<arch>/include/uapi directory
  107. Return:
  108. Two lists of discovered headers, one for generated and one for generic.
  109. """
  110. generated_y_re = re.compile(r'generated-y\s*\+=\s*(\S+)')
  111. generic_y_re = re.compile(r'generic-y\s*\+=\s*(\S+)')
  112. mandatory_y_re = re.compile(r'mandatory-y\s*\+=\s*(\S+)')
  113. # This loop parses arch_asm_kbuild for various kinds of headers to generate.
  114. if verbose:
  115. print('scan_arch_kbuild: processing [%s]' % arch_asm_kbuild)
  116. generated_list = []
  117. generic_list = []
  118. arch_include_uapi_list = [os.path.basename(x) for x in arch_include_uapi]
  119. mandatory_pre_list = []
  120. mandatory_list = []
  121. with open(arch_asm_kbuild, 'r') as f:
  122. while True:
  123. line = f.readline()
  124. if not line:
  125. break
  126. line = line.rstrip()
  127. if verbose:
  128. print('scan_arch_kbuild: line is %s' % line)
  129. match = generated_y_re.match(line)
  130. if match:
  131. if verbose:
  132. print('scan_arch_kbuild: matched [%s]' % line)
  133. generated_list.append(match.group(1))
  134. continue
  135. match = generic_y_re.match(line)
  136. if match:
  137. if verbose:
  138. print('scan_arch_kbuild: matched [%s]' % line)
  139. generic_list.append(match.group(1))
  140. continue
  141. # This loop parses asm_generic_kbuild for various kinds of headers to generate.
  142. if verbose:
  143. print('scan_arch_kbuild: processing [%s]' % asm_generic_kbuild)
  144. with open(asm_generic_kbuild, 'r') as f:
  145. while True:
  146. line = f.readline()
  147. if not line:
  148. break
  149. line = line.rstrip()
  150. if verbose:
  151. print('scan_arch_kbuild: line is %s' % line)
  152. match = mandatory_y_re.match(line)
  153. if match:
  154. if verbose:
  155. print('scan_arch_kbuild: matched [%s]' % line)
  156. mandatory_pre_list.append(match.group(1))
  157. continue
  158. comb_list = generic_list + generated_list + arch_include_uapi_list
  159. mandatory_list = [ x for x in mandatory_pre_list if x not in comb_list]
  160. if verbose:
  161. print("generic")
  162. for x in generic_list:
  163. print(x)
  164. print("generated")
  165. for x in generated_list:
  166. print(x)
  167. print("mandatory")
  168. for x in mandatory_list:
  169. print(x)
  170. print("arch_include_uapi_list")
  171. for x in arch_include_uapi_list:
  172. print(x)
  173. return (generated_list, generic_list, mandatory_list)
  174. def gen_arch_headers(
  175. verbose, gen_dir, arch_asm_kbuild, asm_generic_kbuild, arch_syscall_tool, arch_syscall_tbl, arch_include_uapi):
  176. """Process arch-specific and asm-generic uapi/asm/Kbuild to generate headers.
  177. The function consists of a call to scan_arch_kbuild followed by three loops.
  178. The first loop generates headers found and placed in the generated_list by
  179. scan_arch_kbuild. The second loop generates headers found and placed in the
  180. generic_list by the scan_arch_kbuild. The third loop generates headers found
  181. in mandatory_list by scan_arch_kbuild.
  182. The function does some parsing of file names and tool invocations. If that
  183. parsing fails for some reason (e.g., we don't know how to generate the
  184. header) or a tool invocation fails, then this function will count that as
  185. an error but keep processing. In the end, the function returns the number of
  186. errors encountered.
  187. Args:
  188. verbose: Set True to print progress messages.
  189. gen_dir: Where to place the generated files.
  190. arch_asm_kbuild: The Kbuild file containing lists of headers to generate.
  191. asm_generic_kbuild: The Kbuild file containing lists of mandatory headers.
  192. arch_syscall_tool: The arch script that generates syscall headers, or None.
  193. arch_syscall_tbl: The arch script that defines syscall vectors, or None.
  194. arch_include_uapi: Headers in arch/<arch>/include/uapi directory.
  195. Return:
  196. The number of parsing errors encountered.
  197. """
  198. error_count = 0
  199. # First generate the lists
  200. (generated_list, generic_list, mandatory_list) = scan_arch_kbuild(verbose, arch_asm_kbuild, asm_generic_kbuild ,arch_include_uapi)
  201. # Now we're at the first loop, which is able to generate syscall headers
  202. # found in the first loop, and placed in generated_list. It's okay for this
  203. # list to be empty. In that case, of course, the loop does nothing.
  204. abi_re = re.compile(r'unistd-(\S+)\.h')
  205. for generated in generated_list:
  206. gen_h = os.path.join(gen_dir, 'asm', generated)
  207. match = abi_re.match(generated)
  208. if match:
  209. abi = match.group(1)
  210. cmd = [
  211. '/bin/bash',
  212. arch_syscall_tool,
  213. arch_syscall_tbl,
  214. gen_h,
  215. abi,
  216. '',
  217. '__NR_SYSCALL_BASE',
  218. ]
  219. if verbose:
  220. print('gen_arch_headers: cmd is %s' % cmd)
  221. result = subprocess.call(cmd)
  222. if result != 0:
  223. print('error: gen_arch_headers: cmd %s failed %d' % (cmd, result))
  224. error_count += 1
  225. else:
  226. print('error: gen_arch_headers: syscall header has bad filename: %s' % generated)
  227. error_count += 1
  228. # Now we're at the second loop, which generates wrappers from arch-specific
  229. # headers listed in generic_list to the corresponding asm-generic header.
  230. for generic in generic_list:
  231. wrap_h = os.path.join(gen_dir, 'asm', generic)
  232. with open(wrap_h, 'w') as f:
  233. f.write('#include <asm-generic/%s>\n' % generic)
  234. # Now we're at the third loop, which generates wrappers from asm
  235. # headers listed in mandatory_list to the corresponding asm-generic header.
  236. for mandatory in mandatory_list:
  237. wrap_h = os.path.join(gen_dir, 'asm', mandatory)
  238. with open(wrap_h, 'w') as f:
  239. f.write('#include <asm-generic/%s>\n' % mandatory)
  240. return error_count
  241. def run_headers_install(verbose, gen_dir, headers_install, prefix, h):
  242. """Process a header through the headers_install script.
  243. The headers_install script does some processing of a header so that it is
  244. appropriate for inclusion in a userland program. This function invokes that
  245. script for one header file.
  246. The input file is a header file found in the directory named by prefix. This
  247. function stips the prefix from the header to generate the name of the
  248. processed header.
  249. Args:
  250. verbose: Set True to print progress messages.
  251. gen_dir: Where to place the generated files.
  252. headers_install: The script that munges the header.
  253. prefix: The prefix to strip from h to generate the output filename.
  254. h: The input header to process.
  255. Return:
  256. If parsing or the tool fails, False. Otherwise, True
  257. """
  258. if not h.startswith(prefix):
  259. print('error: expected prefix [%s] on header [%s]' % (prefix, h))
  260. return False
  261. out_h = os.path.join(gen_dir, h[len(prefix):])
  262. (out_h_dirname, out_h_basename) = os.path.split(out_h)
  263. h_dirname = os.path.dirname(h)
  264. cmd = [headers_install, h, out_h]
  265. if verbose:
  266. print('run_headers_install: cmd is %s' % cmd)
  267. result = subprocess.call(cmd)
  268. if result != 0:
  269. print('error: run_headers_install: cmd %s failed %d' % (cmd, result))
  270. return False
  271. return True
  272. def glob_headers(prefix, rel_glob, excludes):
  273. """Recursively scan the a directory for headers.
  274. This function recursively scans the directory identified by prefix for
  275. headers. We don't yet have a new enough version of python3 to use the
  276. better glob function, so right now we assume the glob is '**/*.h'.
  277. The function filters out any files that match the items in excludes.
  278. Args:
  279. prefix: The directory to recursively scan for headers.
  280. rel_glob: The shell-style glob that identifies the header pattern.
  281. excludes: A list of headers to exclude from the glob.
  282. Return:
  283. A list of headers discovered with excludes excluded.
  284. """
  285. # If we had python 3.5+, we could use the fancy new glob.glob.
  286. # full_glob = os.path.join(prefix, rel_glob)
  287. # full_srcs = glob.glob(full_glob, recursive=True)
  288. full_dirs = [prefix]
  289. full_srcs = []
  290. while full_dirs:
  291. full_dir = full_dirs.pop(0)
  292. items = sorted(os.listdir(full_dir))
  293. for item in items:
  294. full_item = os.path.join(full_dir, item)
  295. if os.path.isdir(full_item):
  296. full_dirs.append(full_item)
  297. continue
  298. if full_item in excludes:
  299. continue
  300. if full_item.endswith('.h'):
  301. full_srcs.append(full_item)
  302. return full_srcs
  303. def find_out(verbose, module_dir, prefix, rel_glob, excludes, outs):
  304. """Build a list of outputs for the genrule that creates kernel headers.
  305. This function scans for headers in the source tree and produces a list of
  306. output (generated) headers.
  307. Args:
  308. verbose: Set True to print progress messages.
  309. module_dir: The root directory of the kernel source.
  310. prefix: The prefix with in the kernel source tree to search for headers.
  311. rel_glob: The pattern to use when matching headers under prefix.
  312. excludes: A list of files to exclude from the glob.
  313. outs: The list to populdate with the headers that will be generated.
  314. Return:
  315. The number of errors encountered.
  316. """
  317. # Turn prefix, which is relative to the soong module, to a full prefix that
  318. # is relative to the Android source tree.
  319. full_prefix = os.path.join(module_dir, prefix)
  320. # Convert the list of excludes, which are relative to the soong module, to a
  321. # set of excludes (for easy hashing), relative to the Android source tree.
  322. full_excludes = set()
  323. if excludes:
  324. for exclude in excludes:
  325. full_exclude = os.path.join(full_prefix, exclude)
  326. full_excludes.add(full_exclude)
  327. # Glob those headers.
  328. full_srcs = glob_headers(full_prefix, rel_glob, full_excludes)
  329. # Now convert the file names, which are relative to the Android source tree,
  330. # to be relative to the gen dir. This means stripping off the module prefix
  331. # and the directory within this module.
  332. module_dir_sep = module_dir + os.sep
  333. prefix_sep = prefix + os.sep
  334. if verbose:
  335. print('find_out: module_dir_sep [%s]' % module_dir_sep)
  336. print('find_out: prefix_sep [%s]' % prefix_sep)
  337. error_count = 0
  338. for full_src in full_srcs:
  339. if verbose:
  340. print('find_out: full_src [%s]' % full_src)
  341. if not full_src.startswith(module_dir_sep):
  342. print('error: expected %s to start with %s' % (full_src, module_dir_sep))
  343. error_count += 1
  344. continue
  345. local_src = full_src[len(module_dir_sep):]
  346. if verbose:
  347. print('find_out: local_src [%s]' % local_src)
  348. if not local_src.startswith(prefix_sep):
  349. print('error: expected %s to start with %s' % (local_src, prefix_sep))
  350. error_count += 1
  351. continue
  352. # After stripping the module directory and the prefix, we're left with the
  353. # name of a header that we'll generate, relative to the base of of a the
  354. # the include path.
  355. local_out = local_src[len(prefix_sep):]
  356. if verbose:
  357. print('find_out: local_out [%s]' % local_out)
  358. outs.append(local_out)
  359. return error_count
  360. def gen_blueprints(
  361. verbose, header_arch, gen_dir, arch_asm_kbuild, asm_generic_kbuild, module_dir,
  362. rel_arch_asm_kbuild, rel_asm_generic_kbuild, arch_include_uapi, techpack_include_uapi):
  363. """Generate a blueprints file containing modules that invoke this script.
  364. This function generates a blueprints file that contains modules that
  365. invoke this script to generate kernel headers. We generate the blueprints
  366. file as needed, but we don't actually use the generated file. The blueprints
  367. file that we generate ends up in the out directory, and we can use it to
  368. detect if the checked-in version of the file (in the source directory) is out
  369. of date. This pattern occurs in the Android source tree in several places.
  370. Args:
  371. verbose: Set True to print progress messages.
  372. header_arch: The arch for which to generate headers.
  373. gen_dir: Where to place the generated files.
  374. arch_asm_kbuild: The Kbuild file containing lists of headers to generate.
  375. asm_generic_kbuild: The Kbuild file containing lists of mandatory headers.
  376. module_dir: The root directory of the kernel source.
  377. rel_arch_asm_kbuild: arch_asm_kbuild relative to module_dir.
  378. Return:
  379. The number of errors encountered.
  380. """
  381. error_count = 0
  382. # The old and new blueprints files. We generate the new one, but we need to
  383. # refer to the old one in the modules that we generate.
  384. old_gen_headers_bp = 'gen_headers_%s.bp' % header_arch
  385. new_gen_headers_bp = os.path.join(gen_dir, old_gen_headers_bp)
  386. # Tools and tool files.
  387. headers_install_sh = 'headers_install.sh'
  388. kernel_headers_py = 'kernel_headers.py'
  389. arm_syscall_tool = 'arch/arm/tools/syscallhdr.sh'
  390. # Sources
  391. makefile = 'Makefile'
  392. arm_syscall_tbl = 'arch/arm/tools/syscall.tbl'
  393. rel_glob = '**/*.h'
  394. generic_prefix = 'include/uapi'
  395. arch_prefix = os.path.join('arch', header_arch, generic_prefix)
  396. generic_src = os.path.join(generic_prefix, rel_glob)
  397. arch_src = os.path.join(arch_prefix, rel_glob)
  398. techpack_src = os.path.join('techpack/*',generic_prefix, '*',rel_glob)
  399. # Excluded sources, architecture specific.
  400. exclude_srcs = []
  401. if header_arch == "arm":
  402. exclude_srcs = ['linux/a.out.h']
  403. if header_arch == "arm64":
  404. exclude_srcs = ['linux/a.out.h']
  405. # Scan the arch_asm_kbuild file for files that need to be generated and those
  406. # that are generic (i.e., need to be wrapped).
  407. (generated_list, generic_list, mandatory_list) = scan_arch_kbuild(verbose,
  408. arch_asm_kbuild, asm_generic_kbuild, arch_include_uapi)
  409. generic_out = []
  410. error_count += find_out(
  411. verbose, module_dir, generic_prefix, rel_glob, exclude_srcs, generic_out)
  412. arch_out = []
  413. error_count += find_out(
  414. verbose, module_dir, arch_prefix, rel_glob, None, arch_out)
  415. techpack_out = [x.split('include/uapi/')[1] for x in techpack_include_uapi]
  416. if error_count != 0:
  417. return error_count
  418. # Generate the blueprints file.
  419. if verbose:
  420. print('gen_blueprints: generating %s' % new_gen_headers_bp)
  421. with open(new_gen_headers_bp, 'w') as f:
  422. f.write('// ***** DO NOT EDIT *****\n')
  423. f.write('// This file is generated by %s\n' % kernel_headers_py)
  424. f.write('\n')
  425. f.write('gen_headers_srcs_%s = [\n' % header_arch)
  426. f.write(' "%s",\n' % rel_arch_asm_kbuild)
  427. f.write(' "%s",\n' % rel_asm_generic_kbuild)
  428. f.write(' "%s",\n' % makefile)
  429. if header_arch == "arm":
  430. f.write(' "%s",\n' % arm_syscall_tbl)
  431. f.write(' "%s",\n' % generic_src)
  432. f.write(' "%s",\n' % arch_src)
  433. f.write(' "%s",\n' % techpack_src)
  434. f.write(']\n')
  435. f.write('\n')
  436. if exclude_srcs:
  437. f.write('gen_headers_exclude_srcs_%s = [\n' % header_arch)
  438. for h in exclude_srcs:
  439. f.write(' "%s",\n' % os.path.join(generic_prefix, h))
  440. f.write(']\n')
  441. f.write('\n')
  442. f.write('gen_headers_out_%s = [\n' % header_arch)
  443. if generated_list:
  444. f.write('\n')
  445. f.write(' // Matching generated-y:\n')
  446. f.write('\n')
  447. for h in generated_list:
  448. f.write(' "asm/%s",\n' % h)
  449. if generic_list:
  450. f.write('\n')
  451. f.write(' // Matching generic-y:\n')
  452. f.write('\n')
  453. for h in generic_list:
  454. f.write(' "asm/%s",\n' % h)
  455. if mandatory_list:
  456. f.write('\n')
  457. f.write(' // Matching mandatory-y:\n')
  458. f.write('\n')
  459. for h in mandatory_list:
  460. f.write(' "asm/%s",\n' % h)
  461. if generic_out:
  462. f.write('\n')
  463. f.write(' // From %s\n' % generic_src)
  464. f.write('\n')
  465. for h in generic_out:
  466. f.write(' "%s",\n' % h)
  467. if arch_out:
  468. f.write('\n')
  469. f.write(' // From %s\n' % arch_src)
  470. f.write('\n')
  471. for h in arch_out:
  472. f.write(' "%s",\n' % h)
  473. if techpack_out:
  474. f.write('\n')
  475. f.write(' // From %s\n' % techpack_src)
  476. f.write('\n')
  477. for h in techpack_out:
  478. f.write(' "%s",\n' % h)
  479. f.write(']\n')
  480. f.write('\n')
  481. gen_blueprints_module_name = 'qti_generate_gen_headers_%s' % header_arch
  482. f.write('genrule {\n')
  483. f.write(' // This module generates the gen_headers_<arch>.bp file\n')
  484. f.write(' // (i.e., a new version of this file) so that it can be\n')
  485. f.write(' // checked later to ensure that it matches the checked-\n')
  486. f.write(' // in version (this file).\n')
  487. f.write(' name: "%s",\n' % gen_blueprints_module_name)
  488. f.write(' srcs: gen_headers_srcs_%s,\n' % header_arch)
  489. if exclude_srcs:
  490. f.write(' exclude_srcs: gen_headers_exclude_srcs_%s,\n' % header_arch)
  491. f.write(' tool_files: ["kernel_headers.py"],\n')
  492. f.write(' cmd: "python3 $(location kernel_headers.py) " +\n')
  493. f.write(' kernel_headers_verbose +\n')
  494. f.write(' "--header_arch %s " +\n' % header_arch)
  495. f.write(' "--gen_dir $(genDir) " +\n')
  496. f.write(' "--arch_asm_kbuild $(location %s) " +\n' % rel_arch_asm_kbuild)
  497. f.write(' "--arch_include_uapi $(locations %s) " +\n' % arch_src)
  498. f.write(' "--techpack_include_uapi $(locations %s) " +\n' % techpack_src)
  499. f.write(' "--asm_generic_kbuild $(location %s) " +\n' % rel_asm_generic_kbuild)
  500. f.write(' "blueprints " +\n')
  501. f.write(' "# $(in)",\n')
  502. f.write(' out: ["gen_headers_%s.bp"],\n' % header_arch)
  503. f.write('}\n')
  504. f.write('\n')
  505. f.write('genrule {\n')
  506. f.write(' name: "qti_generate_kernel_headers_%s",\n' % header_arch)
  507. f.write(' tools: ["%s"],\n' % headers_install_sh)
  508. f.write(' tool_files: [\n')
  509. f.write(' "%s",\n' % kernel_headers_py)
  510. if header_arch == "arm":
  511. f.write(' "%s",\n' % arm_syscall_tool)
  512. f.write(' ],\n')
  513. f.write(' srcs: gen_headers_srcs_%s +[\n' % header_arch)
  514. f.write(' "%s",\n' % old_gen_headers_bp)
  515. f.write(' ":%s",\n' % gen_blueprints_module_name)
  516. f.write(' ],\n')
  517. if exclude_srcs:
  518. f.write(' exclude_srcs: gen_headers_exclude_srcs_%s,\n' % header_arch)
  519. f.write(' cmd: "python3 $(location %s) " +\n' % kernel_headers_py)
  520. f.write(' kernel_headers_verbose +\n')
  521. f.write(' "--header_arch %s " +\n' % header_arch)
  522. f.write(' "--gen_dir $(genDir) " +\n')
  523. f.write(' "--arch_asm_kbuild $(location %s) " +\n' % rel_arch_asm_kbuild)
  524. f.write(' "--arch_include_uapi $(locations %s) " +\n' % arch_src)
  525. f.write(' "--techpack_include_uapi $(locations %s) " +\n' % techpack_src)
  526. f.write(' "--asm_generic_kbuild $(location %s) " +\n' % rel_asm_generic_kbuild)
  527. f.write(' "headers " +\n')
  528. f.write(' "--old_gen_headers_bp $(location %s) " +\n' % old_gen_headers_bp)
  529. f.write(' "--new_gen_headers_bp $(location :%s) " +\n' % gen_blueprints_module_name)
  530. f.write(' "--version_makefile $(location %s) " +\n' % makefile)
  531. if header_arch == "arm":
  532. f.write(' "--arch_syscall_tool $(location %s) " +\n' % arm_syscall_tool)
  533. f.write(' "--arch_syscall_tbl $(location %s) " +\n' % arm_syscall_tbl)
  534. f.write(' "--headers_install $(location %s) " +\n' % headers_install_sh)
  535. f.write(' "--include_uapi $(locations %s)",\n' % generic_src)
  536. f.write(' out: ["linux/version.h"] + gen_headers_out_%s,\n' % header_arch)
  537. f.write('}\n')
  538. return 0
  539. def parse_bp_for_headers(file_name, headers):
  540. parsing_headers = False
  541. pattern = re.compile("gen_headers_out_[a-zA-Z0-9]+\s*=\s*\[\s*")
  542. with open(file_name, 'r') as f:
  543. for line in f:
  544. line = line.strip()
  545. if pattern.match(line):
  546. parsing_headers = True
  547. continue
  548. if line.find("]") != -1 and parsing_headers:
  549. break
  550. if not parsing_headers:
  551. continue
  552. if line.find("//") == 0:
  553. continue
  554. headers.add(line[1:-2])
  555. def headers_diff(old_file, new_file):
  556. old_headers = set()
  557. new_headers = set()
  558. diff_detected = False
  559. parse_bp_for_headers(old_file, old_headers)
  560. parse_bp_for_headers(new_file, new_headers)
  561. diff = old_headers - new_headers
  562. if len(diff):
  563. diff_detected = True
  564. print("Headers to remove:")
  565. for x in diff:
  566. print("\t{}".format(x))
  567. diff = new_headers - old_headers
  568. if len(diff):
  569. diff_detected = True
  570. print("Headers to add:")
  571. for x in diff:
  572. print("\t{}".format(x))
  573. return diff_detected
  574. def gen_headers(
  575. verbose, header_arch, gen_dir, arch_asm_kbuild, asm_generic_kbuild, module_dir,
  576. old_gen_headers_bp, new_gen_headers_bp, version_makefile,
  577. arch_syscall_tool, arch_syscall_tbl, headers_install, include_uapi,
  578. arch_include_uapi, techpack_include_uapi):
  579. """Generate the kernel headers.
  580. This script generates the version.h file, the arch-specific headers including
  581. syscall-related generated files and wrappers around generic files, and uses
  582. the headers_install tool to process other generic uapi and arch-specifc uapi
  583. files.
  584. Args:
  585. verbose: Set True to print progress messages.
  586. header_arch: The arch for which to generate headers.
  587. gen_dir: Where to place the generated files.
  588. arch_asm_kbuild: The Kbuild file containing lists of headers to generate.
  589. asm_generic_kbuild: The Kbuild file containing mandatory headers.
  590. module_dir: The root directory of the kernel source.
  591. old_gen_headers_bp: The old gen_headers_<arch>.bp file to check.
  592. new_gen_headers_bp: The new gen_headers_<arch>.bp file to check.
  593. version_makefile: The kernel Makefile that contains version info.
  594. arch_syscall_tool: The arch script that generates syscall headers.
  595. arch_syscall_tbl: The arch script that defines syscall vectors.
  596. headers_install: The headers_install tool to process input headers.
  597. include_uapi: The list of include/uapi header files.
  598. arch_include_uapi: The list of arch/<arch>/include/uapi header files.
  599. Return:
  600. The number of errors encountered.
  601. """
  602. if headers_diff(old_gen_headers_bp, new_gen_headers_bp):
  603. print('error: gen_headers blueprints file is out of date, suggested fix:')
  604. print('#######Please add or remove the above mentioned headers from %s' % (old_gen_headers_bp))
  605. print('then re-run the build')
  606. #return 1
  607. error_count = 0
  608. if not gen_version_h(verbose, gen_dir, version_makefile):
  609. error_count += 1
  610. error_count += gen_arch_headers(
  611. verbose, gen_dir, arch_asm_kbuild, asm_generic_kbuild, arch_syscall_tool, arch_syscall_tbl ,arch_include_uapi)
  612. uapi_include_prefix = os.path.join(module_dir, 'include', 'uapi') + os.sep
  613. arch_uapi_include_prefix = os.path.join(
  614. module_dir, 'arch', header_arch, 'include', 'uapi') + os.sep
  615. for h in include_uapi:
  616. if not run_headers_install(
  617. verbose, gen_dir, headers_install,
  618. uapi_include_prefix, h):
  619. error_count += 1
  620. for h in arch_include_uapi:
  621. if not run_headers_install(
  622. verbose, gen_dir, headers_install,
  623. arch_uapi_include_prefix, h):
  624. error_count += 1
  625. for h in techpack_include_uapi:
  626. techpack_uapi_include_prefix = os.path.join(h.split('/include/uapi')[0], 'include', 'uapi') + os.sep
  627. if not run_headers_install(
  628. verbose, gen_dir, headers_install,
  629. techpack_uapi_include_prefix, h):
  630. error_count += 1
  631. return error_count
  632. def extract_techpack_uapi_headers(verbose, module_dir):
  633. """EXtract list of uapi headers from techpack/* directories. We need to export
  634. these headers to userspace.
  635. Args:
  636. verbose: Verbose option is provided to script
  637. module_dir: Base directory
  638. Returs:
  639. List of uapi headers
  640. """
  641. techpack_subdir = []
  642. techpack_dir = os.path.join(module_dir,'techpack')
  643. techpack_uapi = []
  644. techpack_uapi_sub = []
  645. #get list of techpack directories under techpack/
  646. if os.path.isdir(techpack_dir):
  647. items = sorted(os.listdir(techpack_dir))
  648. for x in items:
  649. p = os.path.join(techpack_dir, x)
  650. if os.path.isdir(p):
  651. techpack_subdir.append(p)
  652. #Print list of subdirs obtained
  653. if (verbose):
  654. for x in techpack_subdir:
  655. print(x)
  656. #For every subdirectory get list of .h files under include/uapi and append to techpack_uapi list
  657. for x in techpack_subdir:
  658. techpack_uapi_path = os.path.join(x, 'include/uapi')
  659. if (os.path.isdir(techpack_uapi_path)):
  660. techpack_uapi_sub = []
  661. find_out(verbose, x, 'include/uapi', '**/*.h', None, techpack_uapi_sub)
  662. tmp = [os.path.join(techpack_uapi_path, y) for y in techpack_uapi_sub]
  663. techpack_uapi = techpack_uapi + tmp
  664. if (verbose):
  665. for x in techpack_uapi:
  666. print(x)
  667. return techpack_uapi
  668. def main():
  669. """Parse command line arguments and perform top level control."""
  670. parser = argparse.ArgumentParser(
  671. description=__doc__,
  672. formatter_class=argparse.RawDescriptionHelpFormatter)
  673. # Arguments that apply to every invocation of this script.
  674. parser.add_argument(
  675. '--verbose',
  676. action='store_true',
  677. help='Print output that describes the workings of this script.')
  678. parser.add_argument(
  679. '--header_arch',
  680. required=True,
  681. help='The arch for which to generate headers.')
  682. parser.add_argument(
  683. '--gen_dir',
  684. required=True,
  685. help='Where to place the generated files.')
  686. parser.add_argument(
  687. '--arch_asm_kbuild',
  688. required=True,
  689. help='The Kbuild file containing lists of headers to generate.')
  690. parser.add_argument(
  691. '--asm_generic_kbuild',
  692. required=True,
  693. help='The Kbuild file containing lists of mandatory headers.')
  694. parser.add_argument(
  695. '--arch_include_uapi',
  696. required=True,
  697. nargs='*',
  698. help='The list of arch/<arch>/include/uapi header files.')
  699. parser.add_argument(
  700. '--techpack_include_uapi',
  701. required=True,
  702. nargs='*',
  703. help='The list of techpack/*/include/uapi header files.')
  704. # The modes.
  705. subparsers = parser.add_subparsers(
  706. dest='mode',
  707. help='Select mode')
  708. parser_blueprints = subparsers.add_parser(
  709. 'blueprints',
  710. help='Generate the gen_headers_<arch>.bp file.')
  711. parser_headers = subparsers.add_parser(
  712. 'headers',
  713. help='Check blueprints, then generate kernel headers.')
  714. # Arguments that apply to headers mode.
  715. parser_headers.add_argument(
  716. '--old_gen_headers_bp',
  717. required=True,
  718. help='The old gen_headers_<arch>.bp file to check.')
  719. parser_headers.add_argument(
  720. '--new_gen_headers_bp',
  721. required=True,
  722. help='The new gen_headers_<arch>.bp file to check.')
  723. parser_headers.add_argument(
  724. '--version_makefile',
  725. required=True,
  726. help='The kernel Makefile that contains version info.')
  727. parser_headers.add_argument(
  728. '--arch_syscall_tool',
  729. help='The arch script that generates syscall headers, if applicable.')
  730. parser_headers.add_argument(
  731. '--arch_syscall_tbl',
  732. help='The arch script that defines syscall vectors, if applicable.')
  733. parser_headers.add_argument(
  734. '--headers_install',
  735. required=True,
  736. help='The headers_install tool to process input headers.')
  737. parser_headers.add_argument(
  738. '--include_uapi',
  739. required=True,
  740. nargs='*',
  741. help='The list of include/uapi header files.')
  742. args = parser.parse_args()
  743. if args.verbose:
  744. print('mode [%s]' % args.mode)
  745. print('header_arch [%s]' % args.header_arch)
  746. print('gen_dir [%s]' % args.gen_dir)
  747. print('arch_asm_kbuild [%s]' % args.arch_asm_kbuild)
  748. print('asm_generic_kbuild [%s]' % args.asm_generic_kbuild)
  749. # Extract the module_dir from args.arch_asm_kbuild and rel_arch_asm_kbuild.
  750. rel_arch_asm_kbuild = os.path.join(
  751. 'arch', args.header_arch, 'include/uapi/asm/Kbuild')
  752. suffix = os.sep + rel_arch_asm_kbuild
  753. if not args.arch_asm_kbuild.endswith(suffix):
  754. print('error: expected %s to end with %s' % (args.arch_asm_kbuild, suffix))
  755. return 1
  756. module_dir = args.arch_asm_kbuild[:-len(suffix)]
  757. rel_asm_generic_kbuild = os.path.join('include/uapi/asm-generic', os.path.basename(args.asm_generic_kbuild))
  758. if args.verbose:
  759. print('module_dir [%s]' % module_dir)
  760. if args.mode == 'blueprints':
  761. return gen_blueprints(
  762. args.verbose, args.header_arch, args.gen_dir, args.arch_asm_kbuild,
  763. args.asm_generic_kbuild, module_dir, rel_arch_asm_kbuild, rel_asm_generic_kbuild, args.arch_include_uapi, args.techpack_include_uapi)
  764. if args.mode == 'headers':
  765. if args.verbose:
  766. print('old_gen_headers_bp [%s]' % args.old_gen_headers_bp)
  767. print('new_gen_headers_bp [%s]' % args.new_gen_headers_bp)
  768. print('version_makefile [%s]' % args.version_makefile)
  769. print('arch_syscall_tool [%s]' % args.arch_syscall_tool)
  770. print('arch_syscall_tbl [%s]' % args.arch_syscall_tbl)
  771. print('headers_install [%s]' % args.headers_install)
  772. return gen_headers(
  773. args.verbose, args.header_arch, args.gen_dir, args.arch_asm_kbuild,
  774. args.asm_generic_kbuild, module_dir, args.old_gen_headers_bp, args.new_gen_headers_bp,
  775. args.version_makefile, args.arch_syscall_tool, args.arch_syscall_tbl,
  776. args.headers_install, args.include_uapi, args.arch_include_uapi, args.techpack_include_uapi)
  777. print('error: unknown mode: %s' % args.mode)
  778. return 1
  779. if __name__ == '__main__':
  780. sys.exit(main())