build_all_headers.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
  4. #
  5. # SPDX-License-Identifier: BSD-3-Clause
  6. #
  7. #
  8. # Little script to build a header file including every other header file in the SDK!
  9. # (also checks we don't have "conflicting" header-filenames)
  10. # Edit the IGNORE_DIRS variable to filter out which directories get looked in.
  11. #
  12. # Usage:
  13. #
  14. # tools/build_all_headers.py <root of source tree> <output file>
  15. import os
  16. import sys
  17. IGNORE_DIRS = set(['host', 'boards'])
  18. IGNORE_DIRS.add('common/boot_picoboot')
  19. IGNORE_DIRS.add('common/boot_uf2')
  20. IGNORE_DIRS.add('common/pico_usb_reset_interface')
  21. IGNORE_DIRS.add('rp2_common/cmsis')
  22. IGNORE_DIRS.add('rp2_common/pico_async_context')
  23. IGNORE_DIRS.add('rp2_common/pico_btstack')
  24. IGNORE_DIRS.add('rp2_common/pico_cyw43_arch')
  25. IGNORE_DIRS.add('rp2_common/pico_cyw43_driver')
  26. IGNORE_DIRS.add('rp2_common/pico_lwip')
  27. IGNORE_DIRS.add('rp2_common/pico_stdio_semihosting')
  28. IGNORE_DIRS.add('rp2_common/pico_stdio_usb')
  29. if len(sys.argv) != 3:
  30. print("Usage: {} top_dir output_header".format(os.path.basename(sys.argv[0])))
  31. sys.exit(1)
  32. top_dir = os.path.join(sys.argv[1], 'src')
  33. output_header = sys.argv[2]
  34. if not os.path.isdir(top_dir):
  35. print("{} doesn't exist!".format(top_dir))
  36. sys.exit(1)
  37. include_dirs = set()
  38. for root, dirs, files in os.walk(top_dir):
  39. prune_dirs = []
  40. for d in dirs:
  41. if os.path.relpath(os.path.join(root, d), top_dir) in IGNORE_DIRS:
  42. prune_dirs.append(d)
  43. for d in prune_dirs:
  44. dirs.remove(d)
  45. if 'include' in dirs:
  46. include_dirs.add(os.path.join(root, 'include'))
  47. dirs.remove('include')
  48. include_files = list()
  49. include_locations = dict()
  50. for d in sorted(include_dirs):
  51. for root, dirs, files in os.walk(d):
  52. for f in sorted(files):
  53. if f.endswith('.h'):
  54. include_file = os.path.relpath(os.path.join(root, f), d)
  55. include_path = os.path.relpath(d, top_dir)
  56. if include_file in include_files:
  57. raise Exception("Duplicate include file '{}' (found in both {} and {})".format(include_file, include_locations[include_file], include_path))
  58. include_files.append(include_file)
  59. include_locations[include_file] = include_path
  60. with open(output_header, 'w') as fh:
  61. fh.write('''/*
  62. * Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
  63. *
  64. * SPDX-License-Identifier: BSD-3-Clause
  65. */
  66. // This file is autogenerated, do not edit by hand
  67. ''')
  68. last_location = ''
  69. for f in include_files:
  70. if include_locations[f] != last_location:
  71. fh.write('\n// {}\n'.format(include_locations[f]))
  72. fh.write('#include "{}"\n'.format(f))
  73. last_location = include_locations[f]
  74. fh.write('\n')