amalgamate.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. os.system("python3 preprocess.py")
  3. with open("src/opcodes.h", "rt", encoding='utf-8') as f:
  4. OPCODES_TEXT = f.read()
  5. pipeline = [
  6. ["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"],
  7. ["obj.h", "codeobject.h", "frame.h"],
  8. ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
  9. ["iter.h", "cffi.h", "io.h", "_generated.h", "pocketpy.h"]
  10. ]
  11. copied = set()
  12. text = ""
  13. import re
  14. import shutil
  15. import os
  16. import sys
  17. import time
  18. if os.path.exists("amalgamated"):
  19. shutil.rmtree("amalgamated")
  20. time.sleep(0.6)
  21. os.mkdir("amalgamated")
  22. def remove_copied_include(text):
  23. text = text.replace("#pragma once", "")
  24. text = re.sub(
  25. r'#include\s+"(.+)"\s*',
  26. lambda m: "" if m.group(1) in copied else m.group(0),
  27. text
  28. )
  29. text = text.replace('#include "opcodes.h"', OPCODES_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 MIT License
  41. * https://github.com/blueloveTH/pocketpy
  42. */
  43. #ifndef POCKETPY_H
  44. #define POCKETPY_H
  45. ''' + text + '\n#endif // POCKETPY_H'
  46. f.write(final_text)
  47. shutil.copy("src/main.cpp", "amalgamated/main.cpp")
  48. if sys.platform == 'linux':
  49. ok = os.system("g++ -o pocketpy amalgamated/main.cpp --std=c++17")
  50. if ok == 0:
  51. print("Test build success!")
  52. os.remove("pocketpy")
  53. # plugins sync
  54. shutil.copy("amalgamated/pocketpy.h", "plugins/flutter/src/pocketpy.h")
  55. shutil.copy("amalgamated/pocketpy.h", "plugins/macos/pocketpy/pocketpy.h")
  56. if os.path.exists("plugins/unity/PocketPyUnityPlugin"):
  57. unity_ios_header = 'plugins/unity/PocketPyUnityPlugin/Assets/PocketPy/Plugins/iOS/pocketpy.h'
  58. shutil.copy("amalgamated/pocketpy.h", unity_ios_header)
  59. if os.path.exists("plugins/godot/godot-cpp/pocketpy"):
  60. shutil.copy("amalgamated/pocketpy.h", "plugins/godot/godot-cpp/pocketpy/src/pocketpy.h")
  61. print("amalgamated/pocketpy.h")