amalgamate.py 2.0 KB

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