Use marshalled code, enable optimization (.pyc -> .pyo)

This commit is contained in:
Oleksii Shevchuk 2016-10-20 23:16:53 +03:00
parent 6149aad2d4
commit 9202f365a1
2 changed files with 18 additions and 15 deletions

View File

@ -3,6 +3,7 @@ CC ?= gcc
CFLAGS := $(shell pkg-config --cflags python-2.7) -fPIC $(CFLAGS_EXTRA)
LDFLAGS := -pthread -ldl -fPIC $(LDFLAGS_EXTRA) -Wl,-Bstatic -lz -Wl,-Bdynamic
PFLAGS := -O
PIE ?= -pie
LIBPYTHON ?= $(shell ldconfig -p | awk '/libpython2.7.so/{print $$4}' | head -n 1)
@ -44,7 +45,7 @@ endif
COMMON_OBJS += linux-inject/ptrace.o
COMMON_OBJS += linux-inject/utils.o
ZLIB := $(shell $(PYTHON) -c 'import zlib; print zlib.__file__ if "__file__" in zlib.__dict__ else "built-in"')
ZLIB := $(shell $(PYTHON) $(PFLAGS) -c 'import zlib; print zlib.__file__ if "__file__" in zlib.__dict__ else "built-in"')
ifneq ($(ZLIB),built-in)
COMMON_OBJS += resources_zlib_so.o
@ -58,26 +59,26 @@ resources/zlib.so: $(ZLIB)
$(GZIP) -9 -c $< >$@
resources_zlib_so.c: gen_resource_header.py resources/zlib.so
$(PYTHON) $+
$(PYTHON) $(PFLAGS) $+
endif
import-tab.c import-tab.h: mktab.py
$(PYTHON) $<
$(PYTHON) $(PFLAGS) $<
Python-dynload.o: Python-dynload.c import-tab.c import-tab.h
$(CC) -c -o $@ $< $(CFLAGS)
resources/library_compressed_string.txt: gen_library_compressed_string.py resources/library.zip
$(PYTHON) gen_library_compressed_string.py
$(PYTHON) $(PFLAGS) gen_library_compressed_string.py
resources_library_compressed_string_txt.c: gen_resource_header.py resources/library_compressed_string.txt resources/library.zip
$(PYTHON) gen_resource_header.py resources/library_compressed_string.txt
$(PYTHON) $(PFLAGS) gen_resource_header.py resources/library_compressed_string.txt
resources/bootloader.pyc: gen_python_bootloader.py ../../pupy/packages/all/pupyimporter.py ../../pupy/pp.py
$(PYTHON) gen_python_bootloader.py $(DEBUG_ADD)
$(PYTHON) $(PFLAGS) gen_python_bootloader.py $(DEBUG_ADD)
resources_bootloader_pyc.c: gen_resource_header.py resources/bootloader.pyc
$(PYTHON) $+
$(PYTHON) $(PFLAGS) $+
linux-inject/%.o: linux-inject/%.c
$(CC) -c $(LINUX_INJECT_CFLAGS) $(CFLAGS) -o $@ $<
@ -89,10 +90,10 @@ resources/python27.so: $(LIBPYTHON)
rm -f $@.tmp
resources/library.zip: build_library_zip.py additional_imports.py
$(PYTHON) $<
$(PYTHON) $(PFLAGS) $<
resources_python27_so.c: gen_resource_header.py resources/python27.so
$(PYTHON) $+
$(PYTHON) $(PFLAGS) $+
$(TEMPLATE_OUTPUT_PATH)/pupyx$(NAME).lin: main_exe.o $(PYOBJS) $(COMMON_OBJS)
$(CC) $(PIE) $+ -o $@ $(LDFLAGS)
@ -103,13 +104,12 @@ $(TEMPLATE_OUTPUT_PATH)/pupyx$(NAME).so: main_so.o $(PYOBJS) $(COMMON_OBJS)
.PHONY: clean all
clean:
rm -f *.o
rm -f *.pyc
find -name "*.pyc" | xargs rm -f
find -name "*.pyo" | xargs rm -f
find -name "*.o" | xargs rm -f
rm -f pupy pupy.so
rm -f linux-inject/*.o
rm -f resources/library.zip
rm -f resources/*.so
rm -f resources/*.pyc resources/library_patches/*.pyc
rm -f resources/*.txt
rm -f resources_*.c
rm -f import-tab.c

View File

@ -27,11 +27,11 @@ if len(sys.argv)==2 and sys.argv[1].strip().lower()=="debug":
def get_load_module_code(code, modulename):
loader="""
import imp, sys
import marshal, imp, sys
fullname={}
mod = imp.new_module(fullname)
mod.__file__ = "<bootloader>\\%s" % fullname
exec {} in mod.__dict__
exec marshal.loads({}) in mod.__dict__
sys.modules[fullname]=mod
""".format(repr(modulename),repr(code))
return loader
@ -44,6 +44,9 @@ if __name__=="__main__":
code_bytes.append(compile("import sys; sys.path = [];", "<string>", "exec"))
with open(os.path.join("..", "..", "pupy", "packages","all", "pupyimporter.py")) as f:
code=f.read()
code=marshal.dumps(compile(code, '<string>', 'exec'))
code_bytes.append(compile(get_load_module_code(code,"pupyimporter")+"\n", "<string>", "exec"))
code_bytes.append(compile("import pupyimporter;pupyimporter.install();\n", "<string>", "exec"))
code_bytes.append(compile("import encodings;\n", "<string>", "exec"))