amalgamate.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 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. os.system("g++ -o pocketpy amalgamated/main.cpp --std=c++17")
  49. os.system("rm pocketpy")
  50. os.system("cp amalgamated/pocketpy.h plugins/flutter/src/pocketpy.h")
  51. os.system("cp amalgamated/pocketpy.h plugins/macos/pocketpy/pocketpy.h")
  52. unity_ios_header = 'plugins/unity/PocketPyUnityPlugin/Assets/PocketPy/Plugins/iOS/pocketpy.h'
  53. os.system(f'cp amalgamated/pocketpy.h "{unity_ios_header}"')
  54. if os.path.exists("plugins/godot/godot-cpp/pocketpy"):
  55. os.system("cp amalgamated/pocketpy.h plugins/godot/godot-cpp/pocketpy/src/pocketpy.h")