amalgamate.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. with open("src/opcodes.h", "rt", encoding='utf-8') as f:
  2. OPCODES_TEXT = f.read()
  3. with open("src/_bindings.h", "rt", encoding='utf-8') as f:
  4. _BINDINGS_TEXT = f.read()
  5. pipeline = [
  6. ["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"],
  7. ["obj.h", "iter.h", "parser.h", "ref.h", "codeobject.h"],
  8. ["vm.h", "compiler.h", "repl.h"],
  9. ["pocketpy.h"]
  10. ]
  11. copied = set()
  12. text = ""
  13. import re
  14. import shutil
  15. import os
  16. import time
  17. if os.path.exists("amalgamated"):
  18. shutil.rmtree("amalgamated")
  19. time.sleep(1)
  20. os.mkdir("amalgamated")
  21. def remove_copied_include(text):
  22. text = text.replace("#pragma once", "")
  23. text = re.sub(
  24. r'#include\s+"(.+)"\s*',
  25. lambda m: "" if m.group(1) in copied else m.group(0),
  26. text
  27. )
  28. text = text.replace('#include "opcodes.h"', OPCODES_TEXT)
  29. text = text.replace('#include "_bindings.h"', _BINDINGS_TEXT)
  30. return text
  31. for seq in pipeline:
  32. for j in seq:
  33. with open("src/"+j, "rt", encoding='utf-8') as f:
  34. text += remove_copied_include(f.read()) + '\n'
  35. copied.add(j)
  36. with open("amalgamated/pocketpy.h", "wt", encoding='utf-8') as f:
  37. final_text = \
  38. r'''/*
  39. * Copyright (c) 2023 blueloveTH
  40. * Distributed Under The LGPLv3 License
  41. */
  42. #ifndef POCKETPY_H
  43. #define POCKETPY_H
  44. ''' + text + '\n#endif // POCKETPY_H'
  45. f.write(final_text)
  46. shutil.copy("src/main.cpp", "amalgamated/main.cpp")
  47. os.system("g++ -o pocketpy amalgamated/main.cpp --std=c++17")
  48. os.system("rm pocketpy")
  49. os.system("cp amalgamated/pocketpy.h plugins/flutter/src/pocketpy.h")
  50. unity_ios_header = 'plugins/unity/My project/Assets/com.bl.pocketpy/Plugins/iOS/pocketpy.h'
  51. os.system(f'cp amalgamated/pocketpy.h "{unity_ios_header}"')
  52. if os.path.exists("plugins/unity"):
  53. with open(unity_ios_header, "rt", encoding='utf-8') as f:
  54. text = f.read()
  55. text = text.replace("Distributed Under The LGPLv3 License", "Distributed Under The PocketPy Unity Exclusive License")
  56. with open(unity_ios_header, "wt", encoding='utf-8') as f:
  57. f.write(text)
  58. if os.path.exists("plugins/godot/godot-cpp/pocketpy"):
  59. os.system("cp amalgamated/pocketpy.h plugins/godot/godot-cpp/pocketpy/src/pocketpy.h")