2022-11-16 10:59:20 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
|
|
|
import shutil
|
|
|
|
import optparse
|
|
|
|
import subprocess
|
|
|
|
import configparser
|
|
|
|
|
2022-12-05 06:58:31 +00:00
|
|
|
rebase_prefix = ""
|
|
|
|
version_override = None
|
2023-01-13 05:17:39 +00:00
|
|
|
release_date = None
|
2022-12-05 06:58:31 +00:00
|
|
|
|
2022-11-16 10:59:20 +00:00
|
|
|
def check_rebase():
|
2022-12-05 06:58:31 +00:00
|
|
|
""" detect 'git rebase -x edk2-build.py master' testbuilds """
|
|
|
|
global rebase_prefix
|
|
|
|
global version_override
|
2023-03-02 11:48:27 +00:00
|
|
|
gitdir = '.git'
|
2022-12-05 06:58:31 +00:00
|
|
|
|
2023-03-02 11:48:27 +00:00
|
|
|
if os.path.isfile(gitdir):
|
|
|
|
with open(gitdir) as f:
|
|
|
|
(unused, gitdir) = f.read().split()
|
|
|
|
|
|
|
|
if not os.path.exists(f'{gitdir}/rebase-merge/msgnum'):
|
2022-11-16 10:59:20 +00:00
|
|
|
return ""
|
2023-03-02 11:48:27 +00:00
|
|
|
with open(f'{gitdir}/rebase-merge/msgnum', 'r') as f:
|
2022-11-16 10:59:20 +00:00
|
|
|
msgnum = int(f.read())
|
2023-03-02 11:48:27 +00:00
|
|
|
with open(f'{gitdir}/rebase-merge/end', 'r') as f:
|
2022-11-16 10:59:20 +00:00
|
|
|
end = int(f.read())
|
2023-03-02 11:48:27 +00:00
|
|
|
with open(f'{gitdir}/rebase-merge/head-name', 'r') as f:
|
2022-11-16 10:59:20 +00:00
|
|
|
head = f.read().strip().split('/')
|
2022-12-05 06:58:31 +00:00
|
|
|
|
|
|
|
rebase_prefix = f'[ {int(msgnum/2)} / {int(end/2)} - {head[-1]} ] '
|
2023-01-13 05:17:39 +00:00
|
|
|
if msgnum != end and not version_override:
|
2022-12-05 06:58:31 +00:00
|
|
|
# fixed version speeds up builds
|
|
|
|
version_override = "test-build-patch-series"
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
def get_coredir(cfg):
|
|
|
|
if cfg.has_option('global', 'core'):
|
|
|
|
return os.path.abspath(cfg['global']['core'])
|
|
|
|
else:
|
|
|
|
return os.getcwd()
|
|
|
|
|
|
|
|
def get_version(cfg):
|
|
|
|
coredir = get_coredir(cfg)
|
2022-12-05 06:58:31 +00:00
|
|
|
if version_override:
|
|
|
|
version = version_override
|
|
|
|
print('')
|
|
|
|
print(f'### version [override]: {version}')
|
|
|
|
return version
|
2022-11-16 10:59:20 +00:00
|
|
|
if os.environ.get('RPM_PACKAGE_NAME'):
|
|
|
|
version = os.environ.get('RPM_PACKAGE_NAME');
|
|
|
|
version += '-' + os.environ.get('RPM_PACKAGE_VERSION');
|
|
|
|
version += '-' + os.environ.get('RPM_PACKAGE_RELEASE');
|
|
|
|
print('')
|
|
|
|
print(f'### version [rpmbuild]: {version}')
|
|
|
|
return version
|
|
|
|
if os.path.exists(coredir + '/.git'):
|
|
|
|
cmdline = [ 'git', 'describe', '--tags', '--abbrev=8', '--match=edk2-stable*' ]
|
2023-01-06 12:05:30 +00:00
|
|
|
result = subprocess.run(cmdline, stdout = subprocess.PIPE, cwd = coredir)
|
2022-11-16 10:59:20 +00:00
|
|
|
version = result.stdout.decode().strip()
|
|
|
|
print('')
|
|
|
|
print(f'### version [git]: {version}')
|
|
|
|
return version
|
|
|
|
return None
|
|
|
|
|
|
|
|
def pcd_string(name, value):
|
2022-11-29 10:59:23 +00:00
|
|
|
return f'{name}=L{value}\\0'
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
def pcd_version(cfg):
|
|
|
|
version = get_version(cfg)
|
|
|
|
if version is None:
|
|
|
|
return []
|
|
|
|
return [ '--pcd', pcd_string('PcdFirmwareVersionString', version) ]
|
|
|
|
|
2023-01-13 05:17:39 +00:00
|
|
|
def pcd_release_date(cfg):
|
|
|
|
if release_date is None:
|
|
|
|
return []
|
|
|
|
return [ '--pcd', pcd_string('PcdFirmwareReleaseDateString', release_date) ]
|
|
|
|
|
|
|
|
def build_message(line, line2 = None):
|
2022-11-16 10:59:20 +00:00
|
|
|
if os.environ.get('TERM') in [ 'xterm', 'xterm-256color' ]:
|
|
|
|
# setxterm title
|
|
|
|
start = '\x1b]2;'
|
|
|
|
end = '\x07'
|
2022-12-05 06:58:31 +00:00
|
|
|
print(f'{start}{rebase_prefix}{line}{end}', end = '')
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
print('')
|
|
|
|
print('###')
|
2022-12-05 06:58:31 +00:00
|
|
|
print(f'### {rebase_prefix}{line}')
|
2023-01-13 05:17:39 +00:00
|
|
|
if line2:
|
|
|
|
print(f'### {line2}')
|
2022-11-16 10:59:20 +00:00
|
|
|
print('###')
|
|
|
|
|
2023-01-13 05:17:39 +00:00
|
|
|
def build_run(cmdline, name, section, silent = False):
|
2022-11-16 10:59:20 +00:00
|
|
|
print(cmdline)
|
2023-01-13 05:17:39 +00:00
|
|
|
if silent:
|
|
|
|
print('### building in silent mode ...', flush = True)
|
|
|
|
result = subprocess.run(cmdline,
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.STDOUT)
|
|
|
|
|
|
|
|
logfile = f'{section}.log'
|
|
|
|
print(f'### writing log to {logfile} ...')
|
|
|
|
with open(logfile, 'wb') as f:
|
|
|
|
f.write(result.stdout)
|
|
|
|
|
|
|
|
if result.returncode:
|
|
|
|
print('### BUILD FAILURE')
|
|
|
|
print('### output')
|
|
|
|
print(result.stdout.decode())
|
|
|
|
print(f'### exit code: {result.returncode}')
|
|
|
|
else:
|
|
|
|
print('### OK')
|
|
|
|
else:
|
|
|
|
result = subprocess.run(cmdline)
|
2022-11-16 10:59:20 +00:00
|
|
|
if result.returncode:
|
|
|
|
print(f'ERROR: {cmdline[0]} exited with {result.returncode} while building {name}')
|
|
|
|
sys.exit(result.returncode)
|
|
|
|
|
|
|
|
def build_copy(plat, tgt, dstdir, copy):
|
|
|
|
srcdir = f'Build/{plat}/{tgt}_GCC5'
|
|
|
|
names = copy.split()
|
|
|
|
srcfile = names[0]
|
|
|
|
if len(names) > 1:
|
|
|
|
dstfile = names[1]
|
|
|
|
else:
|
|
|
|
dstfile = os.path.basename(srcfile)
|
|
|
|
print(f'# copy: {srcdir} / {srcfile} => {dstdir} / {dstfile}')
|
|
|
|
|
2023-01-13 05:17:39 +00:00
|
|
|
src = srcdir + '/' + srcfile
|
|
|
|
dst = dstdir + '/' + dstfile
|
|
|
|
os.makedirs(os.path.dirname(dst), exist_ok = True)
|
|
|
|
shutil.copy(src, dst)
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
def pad_file(dstdir, pad):
|
|
|
|
args = pad.split()
|
|
|
|
if len(args) < 2:
|
|
|
|
raise RuntimeError(f'missing arg for pad ({args})')
|
|
|
|
name = args[0]
|
|
|
|
size = args[1]
|
|
|
|
cmdline = [
|
|
|
|
'truncate',
|
|
|
|
'--size', size,
|
|
|
|
dstdir + '/' + name,
|
|
|
|
]
|
|
|
|
print(f'# padding: {dstdir} / {name} => {size}')
|
|
|
|
subprocess.run(cmdline)
|
|
|
|
|
2023-01-13 05:17:39 +00:00
|
|
|
def build_one(cfg, build, jobs = None, silent = False):
|
2022-11-16 10:59:20 +00:00
|
|
|
cmdline = [ 'build' ]
|
|
|
|
cmdline += [ '-t', 'GCC5' ]
|
|
|
|
cmdline += [ '-p', cfg[build]['conf'] ]
|
|
|
|
|
|
|
|
if (cfg[build]['conf'].startswith('OvmfPkg/') or
|
|
|
|
cfg[build]['conf'].startswith('ArmVirtPkg/')):
|
|
|
|
cmdline += pcd_version(cfg)
|
2023-01-13 05:17:39 +00:00
|
|
|
cmdline += pcd_release_date(cfg)
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
if jobs:
|
|
|
|
cmdline += [ '-n', jobs ]
|
|
|
|
for arch in cfg[build]['arch'].split():
|
|
|
|
cmdline += [ '-a', arch ]
|
|
|
|
if 'opts' in cfg[build]:
|
|
|
|
for name in cfg[build]['opts'].split():
|
|
|
|
section = 'opts.' + name
|
|
|
|
for opt in cfg[section]:
|
2023-01-06 12:05:30 +00:00
|
|
|
cmdline += [ '-D', opt + '=' + cfg[section][opt] ]
|
|
|
|
if 'pcds' in cfg[build]:
|
|
|
|
for name in cfg[build]['pcds'].split():
|
|
|
|
section = 'pcds.' + name
|
|
|
|
for pcd in cfg[section]:
|
|
|
|
cmdline += [ '--pcd', pcd + '=' + cfg[section][pcd] ]
|
2022-11-16 10:59:20 +00:00
|
|
|
if 'tgts' in cfg[build]:
|
|
|
|
tgts = cfg[build]['tgts'].split()
|
|
|
|
else:
|
|
|
|
tgts = [ 'DEBUG' ]
|
|
|
|
for tgt in tgts:
|
2023-01-13 05:17:39 +00:00
|
|
|
desc = None
|
|
|
|
if 'desc' in cfg[build]:
|
|
|
|
desc = cfg[build]['desc']
|
|
|
|
build_message(f'building: {cfg[build]["conf"]} ({cfg[build]["arch"]}, {tgt})',
|
|
|
|
f'description: {desc}')
|
2022-11-16 10:59:20 +00:00
|
|
|
build_run(cmdline + [ '-b', tgt ],
|
2023-01-13 05:17:39 +00:00
|
|
|
cfg[build]['conf'],
|
|
|
|
build + '.' + tgt,
|
|
|
|
silent)
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
if 'plat' in cfg[build]:
|
|
|
|
# copy files
|
|
|
|
for cpy in cfg[build]:
|
|
|
|
if not cpy.startswith('cpy'):
|
|
|
|
continue
|
|
|
|
build_copy(cfg[build]['plat'],
|
|
|
|
tgt,
|
|
|
|
cfg[build]['dest'],
|
|
|
|
cfg[build][cpy])
|
|
|
|
# pad builds
|
|
|
|
for pad in cfg[build]:
|
|
|
|
if not pad.startswith('pad'):
|
|
|
|
continue
|
|
|
|
pad_file(cfg[build]['dest'],
|
|
|
|
cfg[build][pad])
|
|
|
|
|
2023-01-13 05:17:39 +00:00
|
|
|
def build_basetools(silent = False):
|
2022-11-16 10:59:20 +00:00
|
|
|
build_message(f'building: BaseTools')
|
|
|
|
basedir = os.environ['EDK_TOOLS_PATH']
|
|
|
|
cmdline = [ 'make', '-C', basedir ]
|
2023-01-13 05:17:39 +00:00
|
|
|
build_run(cmdline, 'BaseTools', 'build.basetools', silent)
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
def binary_exists(name):
|
|
|
|
for dir in os.environ['PATH'].split(':'):
|
|
|
|
if os.path.exists(dir + '/' + name):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def prepare_env(cfg):
|
|
|
|
""" mimic Conf/BuildEnv.sh """
|
|
|
|
workspace = os.getcwd()
|
|
|
|
packages = [ workspace, ]
|
|
|
|
path = os.environ['PATH'].split(':')
|
|
|
|
dirs = [
|
|
|
|
'BaseTools/Bin/Linux-x86_64',
|
|
|
|
'BaseTools/BinWrappers/PosixLike'
|
|
|
|
]
|
|
|
|
|
|
|
|
if cfg.has_option('global', 'pkgs'):
|
|
|
|
for pkgdir in cfg['global']['pkgs'].split():
|
|
|
|
packages.append(os.path.abspath(pkgdir))
|
2023-01-13 05:17:39 +00:00
|
|
|
coredir = get_coredir(cfg)
|
|
|
|
if coredir != workspace:
|
|
|
|
packages.append(coredir)
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
# add basetools to path
|
|
|
|
for dir in dirs:
|
|
|
|
p = coredir + '/' + dir
|
|
|
|
if not os.path.exists(p):
|
|
|
|
continue
|
|
|
|
if p in path:
|
|
|
|
continue
|
|
|
|
path.insert(0, p)
|
|
|
|
|
|
|
|
# run edksetup if needed
|
|
|
|
toolsdef = coredir + '/Conf/tools_def.txt';
|
|
|
|
if not os.path.exists(toolsdef):
|
2023-01-13 05:17:39 +00:00
|
|
|
os.makedirs(os.path.dirname(toolsdef), exist_ok = True)
|
|
|
|
build_message('running BaseTools/BuildEnv')
|
|
|
|
cmdline = [ 'sh', 'BaseTools/BuildEnv' ]
|
2022-11-16 10:59:20 +00:00
|
|
|
subprocess.run(cmdline, cwd = coredir)
|
|
|
|
|
|
|
|
# set variables
|
|
|
|
os.environ['PATH'] = ':'.join(path)
|
|
|
|
os.environ['PACKAGES_PATH'] = ':'.join(packages)
|
|
|
|
os.environ['WORKSPACE'] = workspace
|
|
|
|
os.environ['EDK_TOOLS_PATH'] = coredir + '/BaseTools'
|
|
|
|
os.environ['CONF_PATH'] = coredir + '/Conf'
|
|
|
|
os.environ['PYTHON_COMMAND'] = '/usr/bin/python3'
|
2023-02-17 10:38:41 +00:00
|
|
|
os.environ['PYTHONHASHSEED'] = '1'
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
# for cross builds
|
|
|
|
if binary_exists('arm-linux-gnu-gcc'):
|
|
|
|
os.environ['GCC5_ARM_PREFIX'] = 'arm-linux-gnu-'
|
2023-02-17 10:38:41 +00:00
|
|
|
if binary_exists('loongarch64-linux-gnu-gcc'):
|
|
|
|
os.environ['GCC5_LOONGARCH64_PREFIX'] = 'loongarch64-linux-gnu-'
|
2023-03-02 11:48:27 +00:00
|
|
|
|
|
|
|
hostarch = os.uname().machine
|
|
|
|
if binary_exists('aarch64-linux-gnu-gcc') and hostarch != 'aarch64':
|
|
|
|
os.environ['GCC5_AARCH64_PREFIX'] = 'aarch64-linux-gnu-'
|
|
|
|
if binary_exists('riscv64-linux-gnu-gcc') and hostarch != 'riscv64':
|
|
|
|
os.environ['GCC5_RISCV64_PREFIX'] = 'riscv64-linux-gnu-'
|
|
|
|
if binary_exists('x86_64-linux-gnu-gcc') and hostarch != 'x86_64':
|
2022-11-16 10:59:20 +00:00
|
|
|
os.environ['GCC5_IA32_PREFIX'] = 'x86_64-linux-gnu-'
|
|
|
|
os.environ['GCC5_X64_PREFIX'] = 'x86_64-linux-gnu-'
|
2023-02-17 10:38:41 +00:00
|
|
|
os.environ['GCC5_BIN'] = 'x86_64-linux-gnu-'
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
def build_list(cfg):
|
|
|
|
for build in cfg.sections():
|
|
|
|
if not build.startswith('build.'):
|
|
|
|
continue
|
|
|
|
name = build.lstrip('build.')
|
|
|
|
desc = 'no description'
|
|
|
|
if 'desc' in cfg[build]:
|
|
|
|
desc = cfg[build]['desc']
|
2022-12-05 06:58:31 +00:00
|
|
|
print(f'# {name:20s} - {desc}')
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-c', '--config', dest = 'configfile',
|
2023-03-02 11:48:27 +00:00
|
|
|
type = 'string', default = '.edk2.builds', metavar = 'FILE',
|
|
|
|
help = 'read configuration from FILE (default: .edk2.builds)')
|
|
|
|
parser.add_option('-C', '--directory', dest = 'directory', type = 'string', metavar = 'DIR',
|
|
|
|
help = 'change to DIR before building')
|
|
|
|
parser.add_option('-j', '--jobs', dest = 'jobs', type = 'string', metavar = 'JOBS'
|
|
|
|
'allow up to JOBS parallel build jobs')
|
|
|
|
parser.add_option('-m', '--match', dest = 'match', type = 'string', metavar = 'INCLUDE',
|
|
|
|
help = 'only run builds matching INCLUDE (substring)')
|
|
|
|
parser.add_option('-x', '--exclude', dest = 'exclude', type = 'string', metavar = 'EXCLUDE',
|
|
|
|
help = 'skip builds matching EXCLUDE (substring)')
|
|
|
|
parser.add_option('-l', '--list', dest = 'list', action = 'store_true', default = False,
|
|
|
|
help = 'list build configs available')
|
|
|
|
parser.add_option('--silent', dest = 'silent', action = 'store_true', default = False,
|
|
|
|
help = 'write build output to logfiles, write to console only on errors')
|
|
|
|
parser.add_option('--core', dest = 'core', type = 'string', metavar = 'DIR',
|
|
|
|
help = 'location of the core edk2 repository (i.e. where BuildTools are located)')
|
|
|
|
parser.add_option('--pkg', '--package', dest = 'pkgs', type = 'string', action = 'append', metavar = 'DIR',
|
|
|
|
help = 'location(s) of additional packages (can be specified multiple times)')
|
|
|
|
parser.add_option('--version-override', dest = 'version_override', type = 'string', metavar = 'VERSION',
|
|
|
|
help = 'set firmware build version')
|
|
|
|
parser.add_option('--release-date', dest = 'release_date', type = 'string', metavar = 'DATE',
|
|
|
|
help = 'set firmware build release date (in MM/DD/YYYY format)')
|
2022-11-16 10:59:20 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2023-02-17 10:38:41 +00:00
|
|
|
if options.directory:
|
|
|
|
os.chdir(options.directory)
|
|
|
|
|
2022-11-16 10:59:20 +00:00
|
|
|
cfg = configparser.ConfigParser()
|
2023-01-06 12:05:30 +00:00
|
|
|
cfg.optionxform = str
|
2022-11-16 10:59:20 +00:00
|
|
|
cfg.read(options.configfile)
|
|
|
|
|
|
|
|
if options.list:
|
|
|
|
build_list(cfg)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not cfg.has_section('global'):
|
|
|
|
cfg.add_section('global')
|
|
|
|
if options.core:
|
|
|
|
cfg.set('global', 'core', options.core)
|
2023-01-13 05:17:39 +00:00
|
|
|
if options.pkgs:
|
|
|
|
cfg.set('global', 'pkgs', ' '.join(options.pkgs))
|
2022-11-16 10:59:20 +00:00
|
|
|
|
2023-01-06 12:05:30 +00:00
|
|
|
global version_override
|
2023-01-13 05:17:39 +00:00
|
|
|
global release_date
|
2022-12-05 06:58:31 +00:00
|
|
|
check_rebase()
|
|
|
|
if options.version_override:
|
|
|
|
version_override = options.version_override
|
2023-01-13 05:17:39 +00:00
|
|
|
if options.release_date:
|
|
|
|
release_date = options.release_date
|
2022-12-05 06:58:31 +00:00
|
|
|
|
2022-11-16 10:59:20 +00:00
|
|
|
prepare_env(cfg)
|
2023-01-13 05:17:39 +00:00
|
|
|
build_basetools(options.silent)
|
2022-11-16 10:59:20 +00:00
|
|
|
for build in cfg.sections():
|
|
|
|
if not build.startswith('build.'):
|
|
|
|
continue
|
|
|
|
if options.match and options.match not in build:
|
|
|
|
print(f'# skipping "{build}" (not matching "{options.match}")')
|
|
|
|
continue
|
2023-02-17 10:38:41 +00:00
|
|
|
if options.exclude and options.exclude in build:
|
|
|
|
print(f'# skipping "{build}" (matching "{options.exclude}")')
|
|
|
|
continue
|
2023-01-13 05:17:39 +00:00
|
|
|
build_one(cfg, build, options.jobs, options.silent)
|
2022-11-16 10:59:20 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|