amalgamate.py 1.6 KB

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