python程序在發布時,往往會打包為cpython的庫,并且根據應用服務器的不同架構(x86/aarch64),以及python的不同版本,封裝的輸出類型也是非常多。本文介紹不同架構指定python下的代碼打包方式:
首先,了解應用服務器的結構,linux在終端輸入
uname -m
確定是x86還是aarch64
以編譯目標為aarch64+python3.8為例
from Cython.Build import cythonize
from Cython.Distutils import build_ext
from distutils.core import setupclass BuildExtWithNewSuffix(build_ext):def get_ext_filename(self, ext_name):filename = super().get_ext_filename(ext_name)return filename.replace(self.oldSuffix, self.newSuffix)def main(compileType, deviceType):if compileType == 'aarch64':BuildExtWithNewSuffix.newSuffix = 'cpython-38-aarch64-linux-gnu'# os.environ['CC'] = '/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc'# os.environ['LDSHARED'] = '/opt/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc -shared'print("aarch64 compile ")setup(ext_modules=cythonize(module_list, nthreads=20,compiler_directives={'language_level': "3"}, build_dir=build_code),script_args=["build_ext", "-j10", "-b", build_target, "-t", build_tmp_dir],include_dirs=['/opt/cross_compile/rk/include/python3.8'],cmdclass={'build_ext': BuildExtWithNewSuffix})if __name__ == "__main__":compileType = 'aarch64'if (len(sys.argv) > 1):compileType = sys.argv[1]deviceType = 'atlas'if (len(sys.argv) > 2):deviceType = sys.argv[2]sys.exit(main(compileType, deviceType))