amalgamate.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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", "dict.h", "codeobject.h", "frame.h"],
  8. ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
  9. ["_generated.h", "cffi.h", "iter.h", "base64.h", "linalg.h", "easing.h", "requests.h", "io.h"],
  10. ["export.h", "pocketpy.h"]
  11. ]
  12. copied = set()
  13. text = ""
  14. import re
  15. import shutil
  16. import os
  17. import sys
  18. import time
  19. if os.path.exists("amalgamated"):
  20. shutil.rmtree("amalgamated")
  21. time.sleep(0.6)
  22. os.mkdir("amalgamated")
  23. def remove_copied_include(text):
  24. text = text.replace("#pragma once", "")
  25. text = re.sub(
  26. r'#include\s+"(.+)"\s*',
  27. lambda m: "" if m.group(1) in copied else m.group(0),
  28. text
  29. )
  30. text = text.replace('#include "opcodes.h"', OPCODES_TEXT)
  31. return text
  32. for seq in pipeline:
  33. for j in seq:
  34. with open("src/"+j, "rt", encoding='utf-8') as f:
  35. text += remove_copied_include(f.read()) + '\n'
  36. copied.add(j)
  37. with open("amalgamated/pocketpy.h", "wt", encoding='utf-8') as f:
  38. final_text = \
  39. r'''/*
  40. * Copyright (c) 2023 blueloveTH
  41. * Distributed Under The MIT License
  42. * https://github.com/blueloveTH/pocketpy
  43. */
  44. #ifndef POCKETPY_H
  45. #define POCKETPY_H
  46. ''' + text + '\n#endif // POCKETPY_H'
  47. f.write(final_text)
  48. shutil.copy("src/main.cpp", "amalgamated/main.cpp")
  49. if sys.platform == 'linux':
  50. ok = os.system("clang++ -o pocketpy amalgamated/main.cpp --std=c++17 -stdlib=libc++")
  51. if ok == 0:
  52. print("Test build success!")
  53. os.remove("pocketpy")
  54. print("amalgamated/pocketpy.h")
  55. content = []
  56. for i in ["src/export.h", "c_bindings/pocketpy_c.h", "c_bindings/pocketpy_c.cpp"]:
  57. with open(i, "rt", encoding='utf-8') as g:
  58. content.append(g.read())
  59. with open("amalgamated/pocketpy.cpp", "wt", encoding='utf-8') as f:
  60. content = '\n\n'.join(content)
  61. content = content.replace('#include "export.h"', '')
  62. content = content.replace('#include "pocketpy_c.h"', '')
  63. f.write(content)
  64. shutil.copy("amalgamated/pocketpy.h", "plugins/flutter/src/pocketpy.h")
  65. shutil.copy("amalgamated/pocketpy.cpp", "plugins/flutter/src/pocketpy.cpp")
  66. shutil.copy("amalgamated/pocketpy.h", "plugins/macos/pocketpy/pocketpy.h")
  67. shutil.copy("amalgamated/pocketpy.cpp", "plugins/macos/pocketpy/pocketpy.cpp")
  68. # unity plugin
  69. unity_ios_root = 'plugins/unity/PocketPyUnityPlugin/Assets/PocketPy/Plugins/iOS'
  70. if os.path.exists(unity_ios_root):
  71. shutil.copy("amalgamated/pocketpy.h", unity_ios_root)
  72. shutil.copy("amalgamated/pocketpy.cpp", unity_ios_root)
  73. # my custom things...
  74. if os.path.exists("/mnt/e/PainterEngine/project/pocketpy.h"):
  75. shutil.copy("amalgamated/pocketpy.h", "/mnt/e/PainterEngine/project/pocketpy.h")
  76. shutil.copy("src/easing.pyi", "/mnt/e/PainterEngine/game/pype/easing.pyi")
  77. shutil.copy("src/linalg.pyi", "/mnt/e/PainterEngine/game/pype/linalg.pyi")
  78. shutil.copy("src/c.pyi", "/mnt/e/PainterEngine/game/pype/c.pyi")