ssg_kernel_headers.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  2. # Copyright (c) 2022-2023 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_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 is combining the relative path to the header file (made in gen_smcinvoke_headers()) to the gen_dir out/soong/.temp/sbox/<temp hash value>/out/
  26. # ex. out/soong/.temp/sbox/<temp hash value>/out/linux/smcinvoke.h
  27. # After the build is complete, you can find the headers that you exposed located in the following gen path:
  28. # out/soong/.intermediates/.../qti_generate_smcinvoke_kernel_headers/gen/
  29. if 'include/uapi' in h:
  30. out_h = os.path.join(gen_dir,'include', h[len(prefix):])
  31. else:
  32. out_h = os.path.join(gen_dir, h[len(prefix):])
  33. (out_h_dirname, out_h_basename) = os.path.split(out_h)
  34. env = os.environ.copy()
  35. env["LOC_UNIFDEF"] = unifdef
  36. cmd = ["sh", headers_install, h, out_h]
  37. if verbose:
  38. print('run_headers_install: cmd is %s' % cmd)
  39. result = subprocess.call(cmd, env=env)
  40. if result != 0:
  41. print('error: run_headers_install: cmd %s failed %d' % (cmd, result))
  42. return False
  43. return True
  44. def gen_smcinvoke_headers(verbose, gen_dir, headers_install, unifdef, smcinvoke_headers_to_expose):
  45. error_count = 0
  46. # smcinvoke_headers_to_expose is a string list of individual paths to headers to expose
  47. # They are passed using Android.bp variable substition: $(locations <label>) ex. $(locations linux/*.h)
  48. # Note <label> has to be a rule to find the file, it cannot be the file itself.
  49. for h in smcinvoke_headers_to_expose:
  50. # h will be the relative path from the repo root directory securemsm-kernel ex. <parent directory structure>/securemsm-kernel/linux/smcinvoke.h
  51. # So we need to split the string and keep the directory structure we want to expose i.e. just linux/smcinvoke.h
  52. topDirectory = 'securemsm-kernel'
  53. if 'include/uapi' in h:
  54. directorySplitLocation = '/'+ topDirectory +'/'
  55. smcinvoke_headers_to_expose_prefix = os.path.join(h.split(directorySplitLocation)[0], topDirectory, 'include', 'uapi') + os.sep
  56. if not run_headers_install(verbose, gen_dir, headers_install, unifdef, smcinvoke_headers_to_expose_prefix, h):
  57. error_count += 1
  58. else:
  59. directorySplitLocation = '/'+ topDirectory +'/'
  60. smcinvoke_headers_to_expose_prefix = os.path.join(h.split(directorySplitLocation)[0], topDirectory) + os.sep
  61. if not run_headers_install(verbose, gen_dir, headers_install, unifdef, smcinvoke_headers_to_expose_prefix, h):
  62. error_count += 1
  63. return error_count
  64. def main():
  65. """Parse command line arguments and perform top level control."""
  66. parser = argparse.ArgumentParser(
  67. description=__doc__,
  68. formatter_class=argparse.RawDescriptionHelpFormatter)
  69. # Arguments that apply to every invocation of this script.
  70. parser.add_argument(
  71. '--verbose', action='store_true',
  72. help='Print output that describes the workings of this script.')
  73. parser.add_argument(
  74. '--header_arch', required=True,
  75. help='The arch for which to generate headers.')
  76. parser.add_argument(
  77. '--gen_dir', required=True,
  78. help='Where to place the generated files.')
  79. parser.add_argument(
  80. '--smcinvoke_headers_to_expose', required=True, nargs='*',
  81. help='The list of smcinvoke header files.')
  82. parser.add_argument(
  83. '--headers_install', required=True,
  84. help='The headers_install tool to process input headers.')
  85. parser.add_argument(
  86. '--unifdef',
  87. required=True,
  88. help='The unifdef tool used by headers_install.')
  89. args = parser.parse_args()
  90. if args.verbose:
  91. print('header_arch [%s]' % args.header_arch)
  92. print('gen_dir [%s]' % args.gen_dir)
  93. print('smcinvoke_headers_to_expose [%s]' % args.smcinvoke_headers_to_expose)
  94. print('headers_install [%s]' % args.headers_install)
  95. print('unifdef [%s]' % args.unifdef)
  96. return gen_smcinvoke_headers(args.verbose, args.gen_dir,
  97. args.headers_install, args.unifdef, args.smcinvoke_headers_to_expose)
  98. if __name__ == '__main__':
  99. sys.exit(main())