import os
import sys
import time
import shutil
import subprocess
import platform
from sys import argv
UniProton_home = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, "%s/cmake/common/build_auxiliary_script"%UniProton_home)
from make_buildef import make_buildef
sys.path.insert(0, "%s/build/uniproton_ci_lib"%UniProton_home)
import globle
from logs import BuilderNolog, log_msg
from get_config_info import *
class Compile:
def get_config(self, cpu_type, cpu_plat):
self.compile_mode = get_compile_mode()
self.lib_type, self.plam_type, self.hcc_path, self.kconf_dir, self.system, self.core = get_cpu_info(cpu_type, cpu_plat, self.build_machine_platform)
if platform.uname()[-1] == 'riscv64':
self.hcc_path = os.path.dirname(
subprocess.check_output(['which', 'gcc'], text=True).strip()
)
if not self.compile_mode and self.lib_type and self.plam_type and self.hcc_path and self.kconf_dir:
log_msg('error', 'load config.xml env error')
sys.exit(0)
self.config_file_path = '%s/build/uniproton_config/config_%s'%(self.home_path, self.kconf_dir)
self.objcopy_path = self.hcc_path
def setCmdEnv(self):
self.build_time_tag = time.strftime('%Y-%m-%d_%H:%M:00')
self.log_dir = '%s/logs/%s' % (self.build_dir, self.cpu_type)
self.log_file = '%s.log' % self.kconf_dir
def SetCMakeEnviron(self):
os.environ["CPU_TYPE"] = self.cpu_type
os.environ["PLAM_TYPE"] = self.plam_type
os.environ["LIB_TYPE"] = self.lib_type
os.environ["COMPILE_OPTION"] = self.compile_option
os.environ["HCC_PATH"] = self.hcc_path
os.environ["UNIPROTON_PACKING_PATH"] = self.UniProton_packing_path
os.environ["CONFIG_FILE_PATH"] = self.config_file_path
os.environ["LIB_RUN_TYPE"] = self.lib_run_type
os.environ["HOME_PATH"] = self.home_path
os.environ["COMPILE_MODE"] = self.compile_mode
os.environ["BUILD_MACHINE_PLATFORM"] = self.build_machine_platform
os.environ["SYSTEM"] = self.system
os.environ["CORE"] = self.core
os.environ["OBJCOPY_PATH"] = self.objcopy_path
os.environ['PATH'] = '%s:%s' % (self.hcc_path, os.getenv('PATH'))
def prepare_env(self, cpu_type, choice):
self.get_config(cpu_type, choice)
self.setCmdEnv()
self.SetCMakeEnviron()
def getOsPlatform(self):
self.cmake_env_path = get_tool_info('cmake', 'tool_path')
if platform.uname()[-1] == 'aarch64':
self.build_machine_platform = 'arm64'
elif platform.uname()[-1] == 'x86_64':
self.build_machine_platform = 'x86'
elif platform.uname()[-1] == 'arm':
self.build_machine_platform = 'arm'
else:
self.build_machine_platform = 'riscv64'
self.cmake_env_path = os.path.dirname(
subprocess.check_output(['which', 'cmake'], text=True).strip()
)
def __init__(self, cpu_type: str, make_option="normal", lib_run_type="FPGA", choice="ALL", make_phase="ALL",
UniProton_packing_path=""):
self.system = ""
self.objcopy_path = ""
self.builder = None
self.compile_mode = ""
self.core = ""
self.plam_type = ""
self.kconf_dir = ""
self.build_tmp_dir = ""
self.log_dir = ""
self.lib_type = ""
self.hcc_path = ""
self.log_file = ""
self.config_file_path = ""
self.build_time_tag = ""
self.build_dir = globle.build_dir
self.home_path = globle.home_path
self.kbuild_path = globle.kbuild_path
self.cpu_type = cpu_type
self.compile_option = make_option
self.lib_run_type = lib_run_type
self.make_choice = choice.lower()
self.make_phase = make_phase
self.UniProton_packing_path = UniProton_packing_path if make_phase == "CREATE_CMAKE_FILE" else '%s/output'%self.home_path
self.UniProton_binary_dir = os.getenv('RPROTON_BINARY_DIR')
self.UniProton_install_file_option = os.getenv('RPROTON_INSTALL_FILE_OPTION')
self.UniProton_make_jx = 'VERBOSE=1' if self.UniProton_install_file_option == 'SUPER_BUILD' else 'VERBOSE=1 -j' + str(os.cpu_count())
self.getOsPlatform()
def CMake(self):
if self.UniProton_binary_dir:
self.build_tmp_dir = '%s/output/tmp/%s' % (self.UniProton_binary_dir, self.kconf_dir)
else:
self.build_tmp_dir = '%s/output/tmp/%s' % (self.build_dir, self.kconf_dir)
os.environ['BUILD_TMP_DIR'] = self.build_tmp_dir
if not os.path.exists(self.build_tmp_dir):
os.makedirs(self.build_tmp_dir)
if not os.path.exists(self.log_dir):
os.makedirs(self.log_dir)
log_msg('info', 'BUILD_TIME_TAG %s' % self.build_time_tag)
self.builder = BuilderNolog(os.path.join(self.log_dir, self.log_file))
if self.make_phase in ['CREATE_CMAKE_FILE', 'ALL']:
real_path = os.path.realpath(self.build_tmp_dir)
if os.path.exists(real_path):
shutil.rmtree(real_path)
os.makedirs(self.build_tmp_dir)
if self.compile_option == 'fortify':
cmd = '%s/cmake %s -DCMAKE_TOOLCHAIN_FILE=%s/cmake/tool_chain/uniproton_tool_chain.cmake ' \
'-DCMAKE_C_COMPILER_LAUNCHER="sourceanalyzer;-b;%sproject" ' \
'-DCMAKE_INSTALL_PREFIX=%s &> %s/%s' % (
self.cmake_env_path, self.home_path, self.home_path, self.cpu_type,
self.UniProton_packing_path, self.log_dir, self.log_file)
elif self.compile_option == 'hllt':
cmd = '%s/cmake %s -DCMAKE_TOOLCHAIN_FILE=%s/cmake/tool_chain/uniproton_tool_chain.cmake ' \
'-DCMAKE_C_COMPILER_LAUNCHER="lltwrapper" -DCMAKE_INSTALL_PREFIX=%s &> %s/%s' % (
self.cmake_env_path, self.home_path, self.home_path, self.UniProton_packing_path, self.log_dir, self.log_file)
else:
cmd = '%s/cmake %s -DCMAKE_TOOLCHAIN_FILE=%s/cmake/tool_chain/uniproton_tool_chain.cmake ' \
'-DCMAKE_INSTALL_PREFIX=%s &> %s/%s' % (
self.cmake_env_path, self.home_path, self.home_path, self.UniProton_packing_path, self.log_dir, self.log_file)
if self.builder.run(cmd, cwd=self.build_tmp_dir, env=None):
log_msg('error', 'generate makefile failed!')
return False
log_msg('info', 'generate makefile succeed.')
return True
def UniProton_clean(self):
for foldername,subfoldernames,filenames in os.walk(self.build_dir):
for subfoldername in subfoldernames:
if subfoldername in ['logs','output','__pycache__']:
folder_path = os.path.join(foldername,subfoldername)
shutil.rmtree(os.path.relpath(folder_path))
for filename in filenames:
if filename == 'prt_buildef.h':
file_dir = os.path.join(foldername,filename)
os.remove(os.path.relpath(file_dir))
if os.path.exists('%s/cmake/common/build_auxiliary_script/__pycache__'%self.home_path):
shutil.rmtree('%s/cmake/common/build_auxiliary_script/__pycache__'%self.home_path)
if os.path.exists('%s/output'%self.home_path):
shutil.rmtree('%s/output'%self.home_path)
if os.path.exists('%s/tools/SRE/x86-win32/sp_makepatch/makepatch'%self.home_path):
os.remove('%s/tools/SRE/x86-win32/sp_makepatch/makepatch'%self.home_path)
if os.path.exists('%s/build/prepare/__pycache__'%self.home_path):
shutil.rmtree('%s/build/prepare/__pycache__'%self.home_path)
return True
def make(self):
if self.make_phase in ['EXECUTING_MAKE', 'ALL']:
self.builder.run('make clean', cwd=self.build_tmp_dir, env=None)
tmp = sys.argv
if self.builder.run(
'make all %s &>> %s/%s' % (
self.UniProton_make_jx, self.log_dir, self.log_file), cwd=self.build_tmp_dir, env=None):
log_msg('error', 'make %s %s failed!' % (self.cpu_type, self.plam_type))
return False
sys.argv = tmp
if self.compile_option in ['normal', 'coverity', 'single']:
if self.builder.run('make install %s &>> %s/%s' % (self.UniProton_make_jx, self.log_dir, self.log_file), cwd=self.build_tmp_dir, env=None):
log_msg('error', 'make install failed!')
return False
if os.path.exists('%s/%s' % (self.log_dir, self.log_file)):
self.builder.log_format()
log_msg('info', 'make %s %s succeed.' % (self.cpu_type, self.plam_type))
return True
def SdkCompaile(self)->bool:
if self.hcc_path == 'None':
return True
self.MakeBuildef()
if self.CMake() and self.make():
log_msg('info', 'make %s %s lib succeed!' % (self.cpu_type, self.make_choice))
return True
else:
log_msg('info', 'make %s %s lib failed!' % (self.cpu_type, self.make_choice))
return False
def UniProtonCompile(self):
if self.cpu_type == 'clean':
log_msg('info', 'UniProton clean')
return self.UniProton_clean()
if self.make_choice == "all":
for make_choice in globle.cpu_plat[self.cpu_type]:
self.prepare_env(self.cpu_type, make_choice)
if not self.SdkCompaile():
return False
else:
self.prepare_env(self.cpu_type, self.make_choice)
if not self.SdkCompaile():
return False
return True
def MakeBuildef(self):
if not make_buildef(globle.home_path,self.kconf_dir,"CREATE"):
sys.exit(1)
log_msg('info', 'make_buildef_file.sh %s successfully.' % self.kconf_dir)
if __name__ == "__main__":
default_para = ("all", "normal", "FPGA", "ALL", "ALL", "")
if len(argv) == 1:
para = [default_para[i] for i in range(0, len(default_para))]
else:
para = [argv[i+1] if i < len(argv) - 1 else default_para[i] for i in range(0,len(default_para))]
cur_cpu_type = para[0].lower()
cur_compile_option = para[1].lower()
cur_lib_run_type = para[2]
cur_make_choice = para[3]
cur_make_phase = para[4]
cur_UniProton_packing_path = para[5]
for plat in globle.cpus_[cur_cpu_type]:
UniProton_build = Compile(plat, cur_compile_option, cur_lib_run_type, cur_make_choice, cur_make_phase, cur_UniProton_packing_path)
if not UniProton_build.UniProtonCompile():
sys.exit(1)
sys.exit(0)