generate_gles2_embedded_data.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2013 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """generates files to embed the gles2 conformance test data in executable."""
  6. from __future__ import print_function
  7. import os
  8. import sys
  9. import typing
  10. class GenerateEmbeddedFiles():
  11. """generates files to embed the gles2 conform test data in executable"""
  12. paths_to_ignore = set([
  13. ".",
  14. "..",
  15. ".svn",
  16. ".git",
  17. ".hg",
  18. ])
  19. extensions_to_include = set([
  20. ".vert",
  21. ".frag",
  22. ".test",
  23. ".run",
  24. ])
  25. def __init__(self, scan_dir: str, base_dir: typing.Optional[str]):
  26. self.scan_dir = scan_dir
  27. self.base_dir = base_dir
  28. self.count = 0;
  29. if self.base_dir != None:
  30. self.files_data_h = open(os.path.join(base_dir, "FilesDATA.h"), "wb")
  31. self.files_data_c = open(os.path.join(base_dir, "FilesDATA.c"), "wb")
  32. self.files_toc_c = open(os.path.join(base_dir, "FilesTOC.c"), "wb")
  33. self.files_data_h.write(b"#ifndef FilesDATA_h\n\n")
  34. self.files_data_h.write(b"#define FilesDATA_h\n\n");
  35. self.files_data_c.write(b"#include \"FilesDATA.h\"\n\n")
  36. self.files_toc_c.write(b"#include \"FilesTOC.h\"\n\n");
  37. self.files_toc_c.write(b"struct GTFVectorFileEntry tempFiles;\n\n");
  38. self.files_toc_c.write(b"struct FileEntry files[] = {\n");
  39. self.AddFiles(scan_dir)
  40. if self.base_dir != None:
  41. self.files_toc_c.write(b"\n};\n\n");
  42. self.files_toc_c.write(
  43. b"int numFileEntrys = sizeof(files) / sizeof(struct FileEntry);\n");
  44. self.files_data_h.write(b"\n\n#endif // FilesDATA_h\n");
  45. self.files_data_c.close()
  46. self.files_data_h.close()
  47. self.files_toc_c.close()
  48. def AddFiles(self, scan_dir: str) -> None:
  49. """Scan a folder and embed the contents of files."""
  50. files_to_embed = os.listdir(scan_dir)
  51. sub_dirs = []
  52. for file_to_embed in files_to_embed:
  53. full_path = os.path.join(scan_dir, file_to_embed)
  54. ext = os.path.splitext(file_to_embed)[1]
  55. base_path = full_path[len(self.scan_dir) + 1:]
  56. if os.path.isdir(full_path):
  57. if not file_to_embed in GenerateEmbeddedFiles.paths_to_ignore:
  58. sub_dirs.append(full_path)
  59. elif ext in GenerateEmbeddedFiles.extensions_to_include:
  60. if self.base_dir == None:
  61. print(full_path.replace("\\", "/"))
  62. else:
  63. self.count += 1
  64. name = "_FILE_%s_%d" % (ext.upper(), self.count)
  65. name = name.replace(".", "_")
  66. self.files_data_h.write(
  67. ("extern const char %s[];\n" % name).encode("utf8"))
  68. self.files_data_c.write(
  69. ("const char %s[] = \n" % name).encode("utf8"))
  70. data = open(full_path, "r")
  71. lines = data.readlines();
  72. data.close()
  73. for line in lines:
  74. line = line.replace("\n", "")
  75. line = line.replace("\r", "")
  76. line = line.replace("\\", "\\\\")
  77. line = line.replace("\"", "\\\"")
  78. self.files_data_c.write(('"%s\\n"\n' % line).encode("utf8"))
  79. self.files_data_c.write(b";\n")
  80. self.files_toc_c.write(("\t{ \"%s\", %s, 0 },\n" % (
  81. base_path.replace("\\", "/"), name)).encode("utf8"))
  82. for sub_dir in sub_dirs:
  83. self.AddFiles(sub_dir)
  84. def main(argv: typing.List[str]) -> int:
  85. """This is the main function."""
  86. if len(argv) >= 1:
  87. scan_dir = argv[0]
  88. else:
  89. scan_dir = '.'
  90. if len(argv) >= 2:
  91. base_dir = argv[1]
  92. else:
  93. base_dir = None
  94. GenerateEmbeddedFiles(scan_dir, base_dir)
  95. return 0
  96. if __name__ == '__main__':
  97. sys.exit(main(sys.argv[1:]))