ubwcp_kernel_headers.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  2. # Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. #
  4. # This program is free software; you can redistribute it and/or modify it
  5. # under the terms of the GNU General Public License version 2 as published by
  6. # the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. # more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program. If not, see <http://www.gnu.org/licenses/>.
  15. import argparse
  16. import filecmp
  17. import os
  18. import re
  19. import subprocess
  20. import sys
  21. def run_uapi_headers_install(verbose, gen_dir, headers_install, unifdef, prefix, h):
  22. if not h.startswith(prefix):
  23. print('error: expected prefix [%s] on header [%s]' % (prefix, h))
  24. return False
  25. out_h = os.path.join(gen_dir, h[len(prefix):])
  26. (out_h_dirname, out_h_basename) = os.path.split(out_h)
  27. env = os.environ.copy()
  28. env["LOC_UNIFDEF"] = unifdef
  29. cmd = ["sh", headers_install, h, out_h]
  30. if verbose:
  31. print('run_uapi_headers_install: cmd is %s' % cmd)
  32. result = subprocess.call(cmd, env=env)
  33. if result != 0:
  34. print('error: run_uapi_headers_install: cmd %s failed %d' % (cmd, result))
  35. return False
  36. return True
  37. def run_kernel_headers_install(verbose, gen_dir, headers_install, unifdef, prefix, h):
  38. if not h.startswith(prefix):
  39. print('error: expected prefix [%s] on header [%s]' % (prefix, h))
  40. return False
  41. out_h = os.path.join(gen_dir, h[len(prefix):])
  42. (out_h_dirname, out_h_basename) = os.path.split(out_h)
  43. env = os.environ.copy()
  44. cmd = ["cp", h, out_h]
  45. if verbose:
  46. print('run_kernel_headers_install: cmd is %s' % cmd)
  47. result = subprocess.call(cmd, env=env)
  48. if result != 0:
  49. print('error: run_kernel_headers_install: cmd %s failed %d' % (cmd, result))
  50. return False
  51. return True
  52. def gen_ubwcp_headers(verbose, gen_dir, headers_install, unifdef, ubwcp_include):
  53. error_count = 0
  54. for h in ubwcp_include:
  55. if 'include/uapi' in h:
  56. ubwcp_include_prefix = os.path.join(h.split('/include/uapi')[0], 'include', 'uapi') + os.sep
  57. if not run_uapi_headers_install(
  58. verbose, gen_dir, headers_install, unifdef,
  59. ubwcp_include_prefix, h): error_count += 1
  60. elif 'include/kernel' in h:
  61. ubwcp_include_prefix = os.path.join(h.split('/include/kernel')[0], 'include', 'kernel') + os.sep
  62. if not run_kernel_headers_install(
  63. verbose, gen_dir, headers_install, unifdef,
  64. ubwcp_include_prefix, h): error_count += 1
  65. return error_count
  66. def main():
  67. """Parse command line arguments and perform top level control."""
  68. parser = argparse.ArgumentParser(
  69. description=__doc__,
  70. formatter_class=argparse.RawDescriptionHelpFormatter)
  71. # Arguments that apply to every invocation of this script.
  72. parser.add_argument(
  73. '--verbose', action='store_true',
  74. help='Print output that describes the workings of this script.')
  75. parser.add_argument(
  76. '--header_arch', required=True,
  77. help='The arch for which to generate headers.')
  78. parser.add_argument(
  79. '--gen_dir', required=True,
  80. help='Where to place the generated files.')
  81. parser.add_argument(
  82. '--ubwcp_include', required=True, nargs='*',
  83. help='The list of header files.')
  84. parser.add_argument(
  85. '--headers_install', required=True,
  86. help='The headers_install tool to process input headers.')
  87. parser.add_argument(
  88. '--unifdef',
  89. required=True,
  90. help='The unifdef tool used by headers_install.')
  91. args = parser.parse_args()
  92. if args.verbose:
  93. print('header_arch [%s]' % args.header_arch)
  94. print('gen_dir [%s]' % args.gen_dir)
  95. print('ubwcp_include [%s]' % args.ubwcp_include)
  96. print('headers_install [%s]' % args.headers_install)
  97. print('unifdef [%s]' % args.unifdef)
  98. return gen_ubwcp_headers(args.verbose, args.gen_dir,
  99. args.headers_install, args.unifdef, args.ubwcp_include)
  100. if __name__ == '__main__':
  101. sys.exit(main())