import os
import subprocess
import sys
import argparse
source_code_path = "fio"
class OPOperator():
def __init__(self, mode, filename, compiletype):
self.mode = mode
self.filename = filename
self.local_dir = os.getcwd()
self.compiletype = compiletype.split('|')
def folder_parser(self):
ls_cmd = 'cd %s; ls ' % (self.local_dir)
file_str = os.popen(ls_cmd).read()
file_list = file_str.split('\n')
for pre_str in file_list:
if pre_str.find('.tar.gz') != -1:
source_code = pre_str.split(".tar.gz", 1)
break
print(source_code[0])
return source_code[0]
def patch_parser(self):
patch_list = []
ls_cmd = 'cd %s; ls ' % (self.local_dir)
file_str = os.popen(ls_cmd).read()
file_list = file_str.split('\n')
for pre_str in file_list:
if pre_str.find('patch') != -1:
patch_list.append(pre_str)
return patch_list
def build_mode(self):
if self.mode == 'build':
self.build_component()
elif self.mode == 'all':
self.build_all()
elif self.mode == 'shrink':
self.shrink_component()
elif self.mode == 'clean':
self.clean_component()
elif self.mode == 'dist':
self.dist_component()
else:
print("[ERROR] Unrecognized build parameters, assert!")
assert False
def error_handler(self, ret, cmd):
if ret:
print("[ERROR] Invalid return code(%d), exited and cmd is: %s" %(ret, cmd))
assert False
def build_all(self):
self.build_component()
self.shrink_component()
self.dist_component()
self.clean_component()
def build_component(self):
uzip_cmd = 'cd %s; mkdir -p %s; tar xvf %s -C %s --strip-components 1' % \
(self.local_dir, source_code_path, self.filename, source_code_path)
ret = self.exe_cmd(uzip_cmd)
self.error_handler(ret, uzip_cmd)
for line in open('./patch_list','r').readlines():
pathName = line.split()[1]
patch_cmd = 'cd %s/%s; patch -p1 < ../%s' % (self.local_dir, source_code_path, pathName)
ret = self.exe_cmd(patch_cmd)
self.error_handler(ret, patch_cmd)
for c_type in self.compiletype:
if c_type == 'release':
print ("[WARNING] Not supported build type")
elif c_type == 'llt':
print ("[WARNING] Not supported build type")
elif c_type == 'debug':
print ("[WARNING] Not supported build type")
elif c_type == 'comm':
prepare_cmd = 'rm -rf %s/install_comm; mkdir -p %s/install_comm' % (self.local_dir, self.local_dir)
ret = self.exe_cmd(prepare_cmd)
self.error_handler(ret, prepare_cmd)
fix_cmd = "cd %s/%s; sed -i 's/OPTFLAGS= -g -ffast-math/OPTFLAGS= -ffast-math/g' Makefile" % (self.local_dir, source_code_path)
fix_cmd += "; sed -i 's/$(LDFLAGS) -o $@ $(FIO_OBJS)/$(LDFLAGS) $(BUILD_CFLAGS) -o $@ $(FIO_OBJS)/g' Makefile"
ret = self.exe_cmd(fix_cmd)
self.error_handler(ret, fix_cmd)
config_cmd = "cd %s/%s; ./configure --disable-native --prefix=%s/install_comm --extra-cflags='-fstack-protector-strong -fPIE -pie -Wl,-z,relro,-z,now -z,noexecstack'" % (self.local_dir, source_code_path, self.local_dir)
ret = self.exe_cmd(config_cmd)
self.error_handler(ret, config_cmd)
make_cmd = 'cd %s/%s; make && make install ' % (self.local_dir, source_code_path)
ret = self.exe_cmd(make_cmd)
self.error_handler(ret, make_cmd)
else:
print ("[WARNING] Not supported build type")
print ("[INFO] Build component finished")
def shrink_component(self):
for c_type in self.compiletype:
if c_type == 'release':
print ("[WARNING] Not supported build type")
elif c_type == 'llt':
print ("[WARNING] Not supported build type")
elif c_type == 'debug':
print ("[WARNING] Not supported build type")
elif c_type == 'comm':
install_dist_cmd = 'mkdir -p %s/install_comm_dist' % (self.local_dir)
ret = self.exe_cmd(install_dist_cmd)
self.error_handler(ret, install_dist_cmd)
cp_cmd = 'mkdir -p %s/install_comm_dist/bin; cp %s/install_comm/bin/fio %s/install_comm_dist/bin' % (self.local_dir, self.local_dir, self.local_dir)
ret = self.exe_cmd(cp_cmd)
self.error_handler(ret, cp_cmd)
else:
print ("[WARNING] Not supported build type")
print ("[INFO] Shrink component finished")
def dist_component(self):
install_path = '%s/../../output/kernel/dependency/fio' % (self.local_dir)
for c_type in self.compiletype:
if c_type == 'release':
print ("[WARNING] Not supported build type")
elif c_type == 'llt':
print ("[WARNING] Not supported build type")
elif c_type == 'debug':
print ("[WARNING] Not supported build type")
elif c_type == 'comm':
rm_cmd = 'rm -rf %s/comm; mkdir -p %s/comm' % (install_path, install_path)
ret = self.exe_cmd(rm_cmd)
self.error_handler(ret, rm_cmd)
cp_cmd = 'cp -r %s/install_comm_dist/* %s/comm/' % (self.local_dir, install_path)
ret = self.exe_cmd(cp_cmd)
self.error_handler(ret, cp_cmd)
else:
print ("[WARNING] Not supported build type")
print ("[INFO] Dist component finished")
def clean_component(self):
clean_cmd = 'cd %s/%s; make clean' % (self.local_dir, source_code_path)
ret = self.exe_cmd(clean_cmd)
self.error_handler(ret, clean_cmd)
rm_cmd = 'cd %s; rm -rf %s; rm -rf install_*' % (self.local_dir, source_code_path)
ret = self.exe_cmd(rm_cmd)
self.error_handler(ret, rm_cmd)
print ("[INFO] Clean component finished")
def exe_cmd(self, cmd):
if sys.version_info < (3, 5):
ret = subprocess.call(cmd, shell = True)
else:
run_tsk = subprocess.run(cmd, shell = True, check = True)
ret = run_tsk.returncode
return ret
def parse_args():
parser = argparse.ArgumentParser(description='GaussDB Kernel open source software build script')
parser.add_argument('-m', '--mode', type=str, required=True,
help='build mode set, all|build|shrink|dist|clean')
parser.add_argument('-f', '--filename', type=str, required=True,
help='file name to compile')
parser.add_argument('-t', '--compiletype', type=str, required=True,
help='compile type set "comm|llt|release|debug"')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
Operator = OPOperator(mode = args.mode, filename = args.filename, compiletype = args.compiletype)
cmd_str='python $(pwd)/../../build/pull_open_source.py "fio" "fio-fio-3.8.tar.gz" "05832WND"'
print("cwd is :",os.getcwd())
ret=Operator.exe_cmd(cmd_str)
print("ret is :",ret)
Operator.build_mode()