ssg_kernel_headers.py 4.5 KB

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