amalgamate.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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", "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("clang++ -o pocketpy amalgamated/main.cpp --std=c++17 -stdlib=libc++")
  50. if ok == 0:
  51. print("Test build success!")
  52. os.remove("pocketpy")
  53. print("amalgamated/pocketpy.h")
  54. shutil.copy("amalgamated/pocketpy.h", "plugins/flutter/src/pocketpy.h")
  55. shutil.copy("amalgamated/pocketpy.h", "plugins/macos/pocketpy/pocketpy.h")
  56. # my custom things...
  57. if os.path.exists("/mnt/e/PainterEngine/project/pocketpy.h"):
  58. shutil.copy("amalgamated/pocketpy.h", "/mnt/e/PainterEngine/project/pocketpy.h")
  59. shutil.copy("src/easing.pyi", "/mnt/e/PainterEngine/game/pype/easing.pyi")
  60. shutil.copy("src/linalg.pyi", "/mnt/e/PainterEngine/game/pype/linalg.pyi")
  61. shutil.copy("src/c.pyi", "/mnt/e/PainterEngine/game/pype/c.pyi")