amalgamate.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. with open("src/opcodes.h", "rt", encoding='utf-8') as f:
  2. OPCODES_TEXT = f.read()
  3. pipeline = [
  4. ["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"],
  5. ["obj.h", "iter.h", "parser.h", "ref.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) 2023 blueloveTH
  37. * Distributed Under The MIT License
  38. * https://github.com/blueloveTH/pocketpy
  39. */
  40. #ifndef POCKETPY_H
  41. #define POCKETPY_H
  42. ''' + text + '\n#endif // POCKETPY_H'
  43. f.write(final_text)
  44. shutil.copy("src/main.cpp", "amalgamated/main.cpp")
  45. os.system("g++ -o pocketpy amalgamated/main.cpp --std=c++17")
  46. os.system("rm pocketpy")
  47. os.system("cp amalgamated/pocketpy.h plugins/flutter/src/pocketpy.h")
  48. os.system("cp amalgamated/pocketpy.h plugins/macos/pocketpy/pocketpy.h")
  49. unity_ios_header = 'plugins/unity/PocketPyUnityPlugin/Assets/PocketPy/Plugins/iOS/pocketpy.h'
  50. os.system(f'cp amalgamated/pocketpy.h "{unity_ios_header}"')
  51. if os.path.exists("plugins/godot/godot-cpp/pocketpy"):
  52. os.system("cp amalgamated/pocketpy.h plugins/godot/godot-cpp/pocketpy/src/pocketpy.h")