import os
import sys
import subprocess
import shutil
import re
import getpass
import glob

def has_build(module_name):
    build_module = ['openssl']
    if module_name in build_module:
        return True
    else:
        return False

def build(module_name, build_utils, src_dir, bin_dir, install_dir, job_n):
    # 设置环境变量
    os.environ['ARCH'] = 'arm'
    root_dir = os.environ["PRODUCT_ROOT_DIR"]
    cross_compile = f'{root_dir}/buildtools/toolchains/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-'
    os.environ['CROSS_COMPILE'] = cross_compile
    if module_name == 'openssl':
        build_utils.copy_files_ignoring_hidden(src_dir, bin_dir)
        build_utils.exec_command(['./config', 'no-asm', f'--prefix={install_dir}', f'--cross-compile-prefix={cross_compile}', f'--openssldir={install_dir}'], "config", bin_dir, exit=False)
        build_utils.exec_command(['sed', '-i', 's/-m64//g', f'{bin_dir}/Makefile'], "sed", bin_dir, exit=True)
        return build_utils.exec_command(['make', f'--directory={bin_dir}', f'--jobs={job_n}'], "make", bin_dir, exit=False)
    # subprocess.check_call(['make', f'--directory={root_dir}/system/quectel-cm', f'--jobs={job_n}'])


def build_install(module_name, build_utils, bin_dir, install_dir, job_n):
    if module_name == 'openssl':
        build_utils.exec_command(['make', 'install', f'--directory={bin_dir}', f'--jobs={job_n}'], "make", bin_dir, exit=False)
        return True
