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 setup
class 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))