amalgamate.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os
  2. os.system("python3 prebuild.py")
  3. with open("include/pocketpy/opcodes.h", "rt", encoding='utf-8') as f:
  4. OPCODES_TEXT = '\n' + f.read() + '\n'
  5. pipeline = [
  6. ["config.h", "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", "random.h", "re.h", "linalg.h", "easing.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.5)
  22. os.mkdir("amalgamated")
  23. def remove_copied_include(text):
  24. text = text.replace("#pragma once", "")
  25. def _replace(m):
  26. key = m.group(1)
  27. if key.startswith("pocketpy/"):
  28. key = key[9:]
  29. if key == "user_config.h":
  30. return m.group(0)
  31. if key == "opcodes.h":
  32. return OPCODES_TEXT
  33. assert key in copied, f"include {key} not found"
  34. return ""
  35. text = re.sub(
  36. r'#include\s+"(.+)"\s*',
  37. _replace,
  38. text
  39. )
  40. return text
  41. for seq in pipeline:
  42. for j in seq:
  43. print(j)
  44. with open("include/pocketpy/"+j, "rt", encoding='utf-8') as f:
  45. text += remove_copied_include(f.read()) + '\n'
  46. copied.add(j)
  47. j = j.replace(".h", ".cpp")
  48. if os.path.exists("src/"+j):
  49. with open("src/"+j, "rt", encoding='utf-8') as f:
  50. text += remove_copied_include(f.read()) + '\n'
  51. copied.add(j)
  52. with open("amalgamated/pocketpy.h", "wt", encoding='utf-8') as f:
  53. final_text = \
  54. r'''/*
  55. * Copyright (c) 2023 blueloveTH
  56. * Distributed Under The MIT License
  57. * https://github.com/blueloveTH/pocketpy
  58. */
  59. #ifndef POCKETPY_H
  60. #define POCKETPY_H
  61. ''' + text + '\n#endif // POCKETPY_H'
  62. f.write(final_text)
  63. shutil.copy("src2/main.cpp", "amalgamated/main.cpp")
  64. with open("amalgamated/main.cpp", "rt", encoding='utf-8') as f:
  65. text = f.read()
  66. text = text.replace('#include "pocketpy/pocketpy.h"', '#include "pocketpy.h"')
  67. with open("amalgamated/main.cpp", "wt", encoding='utf-8') as f:
  68. f.write(text)
  69. if sys.platform == 'linux':
  70. ok = os.system("clang++ -o pocketpy amalgamated/main.cpp --std=c++17 -stdlib=libc++")
  71. if ok == 0:
  72. print("Test build success!")
  73. os.remove("pocketpy")
  74. print("amalgamated/pocketpy.h")
  75. content = []
  76. for i in ["include/pocketpy/export.h", "c_bindings/pocketpy_c.h", "c_bindings/pocketpy_c.cpp"]:
  77. with open(i, "rt", encoding='utf-8') as g:
  78. content.append(g.read())
  79. with open("amalgamated/pocketpy.cpp", "wt", encoding='utf-8') as f:
  80. content = '\n\n'.join(content)
  81. content = content.replace('#include "export.h"', '')
  82. content = content.replace('#include "pocketpy_c.h"', '')
  83. f.write(content)
  84. shutil.copy("amalgamated/pocketpy.h", "plugins/flutter/src/pocketpy.h")
  85. shutil.copy("amalgamated/pocketpy.cpp", "plugins/flutter/src/pocketpy.cpp")
  86. shutil.copy("amalgamated/pocketpy.h", "plugins/macos/pocketpy/pocketpy.h")
  87. shutil.copy("amalgamated/pocketpy.cpp", "plugins/macos/pocketpy/pocketpy.cpp")
  88. # unity plugin
  89. unity_ios_root = 'plugins/unity/PocketPyUnityPlugin/Assets/PocketPython/Plugins/iOS'
  90. if os.path.exists(unity_ios_root):
  91. shutil.copy("amalgamated/pocketpy.h", unity_ios_root)
  92. shutil.copy("amalgamated/pocketpy.cpp", unity_ios_root)