amalgamate.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import os
  2. assert os.system("python prebuild.py") == 0
  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", "export.h", "_generated.h", "common.h", "memory.h", "any.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"],
  7. ["obj.h", "dict.h", "codeobject.h", "frame.h", "profiler.h"],
  8. ["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h"],
  9. ["cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"],
  10. ["pocketpy.h", "pocketpy_c.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 in ["user_config.h", "cJSONw.hpp"]:
  30. return m.group(0)
  31. if "opcodes.h" in key:
  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. # use LF line endings instead of CRLF
  53. with open("amalgamated/pocketpy.h", "wt", encoding='utf-8', newline='\n') as f:
  54. final_text = \
  55. r'''/*
  56. * Copyright (c) 2024 blueloveTH
  57. * Distributed Under The MIT License
  58. * https://github.com/pocketpy/pocketpy
  59. */
  60. #ifndef POCKETPY_H
  61. #define POCKETPY_H
  62. ''' + text + '\n#endif // POCKETPY_H'
  63. f.write(final_text)
  64. shutil.copy("src2/main.cpp", "amalgamated/main.cpp")
  65. with open("amalgamated/main.cpp", "rt", encoding='utf-8') as f:
  66. text = f.read()
  67. text = text.replace('#include "pocketpy/pocketpy.h"', '#include "pocketpy.h"')
  68. with open("amalgamated/main.cpp", "wt", encoding='utf-8', newline='\n') as f:
  69. f.write(text)
  70. if sys.platform in ['linux', 'darwin']:
  71. ok = os.system("clang++ -o main amalgamated/main.cpp -O1 --std=c++17 -frtti -stdlib=libc++")
  72. if ok == 0:
  73. print("Test build success!")
  74. print("amalgamated/pocketpy.h")
  75. def sync(path):
  76. shutil.copy("amalgamated/pocketpy.h", os.path.join(path, "pocketpy.h"))
  77. with open(os.path.join(path, "pocketpy.cpp"), "wt", encoding='utf-8', newline='\n') as f:
  78. f.write("#include \"pocketpy.h\"\n")
  79. sync("plugins/macos/pocketpy")