pocketpy/prebuild.py
2024-04-04 00:12:01 +08:00

35 lines
1.1 KiB
Python

import os
def generate_python_sources():
sources = {}
for file in sorted(os.listdir("python")):
if not file.endswith(".py"):
continue
key = file.split(".")[0]
const_char_array = []
with open("python/" + file) as f:
# convert to char array (signed)
for c in f.read().encode('utf-8'):
if c < 128:
const_char_array.append(str(c))
else:
const_char_array.append(str(c - 256))
const_char_array.append('0')
const_char_array = ','.join(const_char_array)
sources[key] = '{' + const_char_array + '}'
header = '''#pragma once
// generated by prebuild.py
namespace pkpy{
'''
for key in sorted(sources.keys()):
value = sources[key]
header += f' inline const char kPythonLibs_{key}[] = {value};\n'
header += '}\n'
return header
# use LF line endings instead of CRLF
with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
f.write(generate_python_sources())