2017-09-21 21:42:57 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2020-03-31 23:52:15 +00:00
|
|
|
# Copyright 2020 Ben Vanik. All Rights Reserved.
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
"""Main build script and tooling for xenia.
|
|
|
|
|
|
|
|
Run with --help or no arguments for possible commands.
|
|
|
|
"""
|
2017-09-21 21:42:57 +00:00
|
|
|
from __future__ import print_function
|
2020-11-23 23:08:00 +00:00
|
|
|
from datetime import datetime
|
2015-08-01 06:48:24 +00:00
|
|
|
import argparse
|
2019-04-18 15:14:18 +00:00
|
|
|
import json
|
2015-08-01 06:48:24 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
__author__ = 'ben.vanik@gmail.com (Ben Vanik)'
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
self_path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
2020-11-21 11:54:57 +00:00
|
|
|
# Detect if building on Android via Termux.
|
2020-11-21 13:18:50 +00:00
|
|
|
host_linux_platform_is_android = False
|
2020-11-21 11:54:57 +00:00
|
|
|
if sys.platform == 'linux':
|
|
|
|
try:
|
2020-11-21 13:18:50 +00:00
|
|
|
host_linux_platform_is_android = subprocess.Popen(
|
2020-11-21 11:54:57 +00:00
|
|
|
['uname', '-o'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
|
|
|
universal_newlines=True).communicate()[0] == 'Android\n'
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-11-21 13:18:50 +00:00
|
|
|
def import_subprocess_environment(args):
|
|
|
|
popen = subprocess.Popen(
|
|
|
|
args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
|
|
|
variables, _ = popen.communicate()
|
|
|
|
envvars_to_save = (
|
|
|
|
'devenvdir',
|
|
|
|
'include',
|
|
|
|
'lib',
|
|
|
|
'libpath',
|
|
|
|
'path',
|
|
|
|
'pathext',
|
|
|
|
'systemroot',
|
|
|
|
'temp',
|
|
|
|
'tmp',
|
|
|
|
'windowssdkdir',
|
|
|
|
)
|
|
|
|
for line in variables.splitlines():
|
|
|
|
for envvar in envvars_to_save:
|
|
|
|
if re.match(envvar + '=', line.lower()):
|
|
|
|
var, setting = line.split('=', 1)
|
|
|
|
if envvar == 'path':
|
|
|
|
setting = os.path.dirname(sys.executable) + os.pathsep + setting
|
|
|
|
os.environ[var.upper()] = setting
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def import_vs_environment():
|
|
|
|
"""Finds the installed Visual Studio version and imports
|
|
|
|
interesting environment variables into os.environ.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A version such as 2015 or None if no installation is found.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform != 'win32':
|
|
|
|
return None
|
|
|
|
|
|
|
|
version = 0
|
|
|
|
install_path = None
|
|
|
|
env_tool_args = None
|
|
|
|
|
2021-05-15 23:50:59 +00:00
|
|
|
vswhere = subprocess.check_output(
|
|
|
|
'third_party/vswhere/vswhere.exe -version "[15,)" -latest -prerelease -format json -utf8 -products '
|
|
|
|
"Microsoft.VisualStudio.Product.Enterprise "
|
|
|
|
"Microsoft.VisualStudio.Product.Professional "
|
|
|
|
"Microsoft.VisualStudio.Product.Community "
|
|
|
|
"Microsoft.VisualStudio.Product.BuildTools",
|
|
|
|
shell=False,
|
|
|
|
universal_newlines=True,
|
|
|
|
encoding="utf-8",
|
|
|
|
)
|
2020-11-21 13:18:50 +00:00
|
|
|
if vswhere:
|
|
|
|
vswhere = json.loads(vswhere)
|
|
|
|
if vswhere and len(vswhere) > 0:
|
|
|
|
version = int(vswhere[0].get("catalog", {}).get("productLineVersion", 2017))
|
|
|
|
install_path = vswhere[0].get("installationPath", None)
|
|
|
|
|
|
|
|
if version < 2017:
|
|
|
|
if 'VS140COMNTOOLS' in os.environ:
|
|
|
|
version = 2015
|
|
|
|
vcvars_path = os.environ['VS140COMNTOOLS']
|
2021-05-15 23:12:15 +00:00
|
|
|
vcvars_path = os.path.join(vcvars_path, '..\\..\\vc\\vcvarsall.bat')
|
2020-11-21 13:18:50 +00:00
|
|
|
env_tool_args = [vcvars_path, 'x64', '&&', 'set']
|
|
|
|
else:
|
|
|
|
vsdevcmd_path = os.path.join(install_path, 'Common7\\Tools\\VsDevCmd.bat')
|
|
|
|
if os.path.isfile(vsdevcmd_path) and os.access(vsdevcmd_path, os.X_OK):
|
|
|
|
env_tool_args = [vsdevcmd_path, '-arch=amd64', '-host_arch=amd64', '&&', 'set']
|
|
|
|
else:
|
|
|
|
vcvars_path = os.path.join(install_path, 'VC\\Auxiliary\\Build\\vcvarsall.bat')
|
|
|
|
env_tool_args = [vcvars_path, 'x64', '&&', 'set']
|
|
|
|
|
|
|
|
if version == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
import_subprocess_environment(env_tool_args)
|
|
|
|
os.environ['VSVERSION'] = str(version)
|
|
|
|
return version
|
|
|
|
|
|
|
|
|
|
|
|
vs_version = import_vs_environment()
|
|
|
|
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
def main():
|
2017-09-21 21:42:57 +00:00
|
|
|
# Add self to the root search path.
|
|
|
|
sys.path.insert(0, self_path)
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
# Augment path to include our fancy things.
|
|
|
|
os.environ['PATH'] += os.pathsep + os.pathsep.join([
|
|
|
|
self_path,
|
|
|
|
os.path.abspath(os.path.join('tools', 'build')),
|
|
|
|
])
|
|
|
|
|
|
|
|
# Check git exists.
|
|
|
|
if not has_bin('git'):
|
2020-10-28 22:18:37 +00:00
|
|
|
print('WARNING: Git should be installed and on PATH. Version info will be omitted from all binaries!')
|
|
|
|
print('')
|
|
|
|
elif not git_is_repository():
|
|
|
|
print('WARNING: The source tree is unversioned. Version info will be omitted from all binaries!')
|
|
|
|
print('')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
# Check python version.
|
2020-03-30 22:37:32 +00:00
|
|
|
if not sys.version_info[:2] >= (3, 6):
|
|
|
|
print('ERROR: Python 3.6+ must be installed and on PATH')
|
2017-09-21 21:42:57 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Grab Visual Studio version and execute shell to set up environment.
|
2020-11-21 13:18:50 +00:00
|
|
|
if sys.platform == 'win32' and vs_version is None:
|
|
|
|
print('WARNING: Visual Studio not found!')
|
|
|
|
print('Building for Windows will not be supported.')
|
|
|
|
print('Please refer to the building guide:')
|
|
|
|
print('https://github.com/xenia-project/xenia/blob/master/docs/building.md')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
# Setup main argument parser and common arguments.
|
|
|
|
parser = argparse.ArgumentParser(prog='xenia-build')
|
|
|
|
|
|
|
|
# Grab all commands and populate the argument parser for each.
|
|
|
|
subparsers = parser.add_subparsers(title='subcommands',
|
|
|
|
dest='subcommand')
|
|
|
|
commands = discover_commands(subparsers)
|
|
|
|
|
|
|
|
# If the user passed no args, die nicely.
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Gather any arguments that we want to pass to child processes.
|
|
|
|
command_args = sys.argv[1:]
|
|
|
|
pass_args = []
|
|
|
|
try:
|
|
|
|
pass_index = command_args.index('--')
|
|
|
|
pass_args = command_args[pass_index + 1:]
|
|
|
|
command_args = command_args[:pass_index]
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Parse command name and dispatch.
|
|
|
|
args = vars(parser.parse_args(command_args))
|
|
|
|
command_name = args['subcommand']
|
|
|
|
try:
|
|
|
|
command = commands[command_name]
|
|
|
|
return_code = command.execute(args, pass_args, os.getcwd())
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
sys.exit(return_code)
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2020-11-08 21:28:36 +00:00
|
|
|
def print_box(msg):
|
|
|
|
"""Prints an important message inside a box
|
|
|
|
"""
|
|
|
|
print(
|
|
|
|
'┌{0:─^{2}}╖\n'
|
|
|
|
'│{1: ^{2}}║\n'
|
|
|
|
'╘{0:═^{2}}╝\n'
|
|
|
|
.format('', msg, len(msg) + 2))
|
|
|
|
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def has_bin(binary):
|
|
|
|
"""Checks whether the given binary is present.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
binary: binary name (without .exe, etc).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the binary exists.
|
|
|
|
"""
|
|
|
|
bin_path = get_bin(binary)
|
|
|
|
if not bin_path:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def get_bin(binary):
|
|
|
|
"""Checks whether the given binary is present and returns the path.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
binary: binary name (without .exe, etc).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Full path to the binary or None if not found.
|
|
|
|
"""
|
|
|
|
for path in os.environ['PATH'].split(os.pathsep):
|
|
|
|
path = path.strip('"')
|
|
|
|
exe_file = os.path.join(path, binary)
|
|
|
|
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
|
|
|
|
return exe_file
|
|
|
|
exe_file += '.exe'
|
|
|
|
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
|
|
|
|
return exe_file
|
|
|
|
return None
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2020-10-28 22:18:37 +00:00
|
|
|
def shell_call(command, throw_on_error=True, stdout_path=None, stderr_path=None, shell=False):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Executes a shell command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
command: Command to execute, as a list of parameters.
|
|
|
|
throw_on_error: Whether to throw an error or return the status code.
|
|
|
|
stdout_path: File path to write stdout output to.
|
2020-10-28 22:18:37 +00:00
|
|
|
stderr_path: File path to write stderr output to.
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
If throw_on_error is False the status code of the call will be returned.
|
|
|
|
"""
|
|
|
|
stdout_file = None
|
|
|
|
if stdout_path:
|
|
|
|
stdout_file = open(stdout_path, 'w')
|
2020-10-28 22:18:37 +00:00
|
|
|
stderr_file = None
|
|
|
|
if stderr_path:
|
|
|
|
stderr_file = open(stderr_path, 'w')
|
2017-09-21 21:42:57 +00:00
|
|
|
result = 0
|
|
|
|
try:
|
|
|
|
if throw_on_error:
|
|
|
|
result = 1
|
2020-10-28 22:18:37 +00:00
|
|
|
subprocess.check_call(command, shell=shell, stdout=stdout_file, stderr=stderr_file)
|
2017-09-21 21:42:57 +00:00
|
|
|
result = 0
|
|
|
|
else:
|
2020-10-28 22:18:37 +00:00
|
|
|
result = subprocess.call(command, shell=shell, stdout=stdout_file, stderr=stderr_file)
|
2017-09-21 21:42:57 +00:00
|
|
|
finally:
|
|
|
|
if stdout_file:
|
|
|
|
stdout_file.close()
|
2020-10-28 22:18:37 +00:00
|
|
|
if stderr_file:
|
|
|
|
stderr_file.close()
|
2017-09-21 21:42:57 +00:00
|
|
|
return result
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2020-10-28 22:18:37 +00:00
|
|
|
def generate_version_h():
|
|
|
|
"""Generates a build/version.h file that contains current git info.
|
|
|
|
"""
|
2021-05-03 20:26:05 +00:00
|
|
|
header_file = 'build/version.h'
|
2021-08-08 15:20:51 +00:00
|
|
|
pr_number = 0
|
|
|
|
pr_repo_name = ""
|
|
|
|
pr_branch_name = ""
|
|
|
|
pr_commit = ""
|
|
|
|
pr_commit_short = ""
|
|
|
|
if os.getenv('APPVEYOR') == 'True':
|
|
|
|
branch_name = os.getenv('APPVEYOR_REPO_BRANCH')
|
|
|
|
commit = os.getenv('APPVEYOR_REPO_COMMIT')
|
|
|
|
commit_short = commit[:9]
|
|
|
|
pr_number = os.getenv('APPVEYOR_PULL_REQUEST_NUMBER')
|
|
|
|
if not pr_number:
|
|
|
|
pr_number = 0
|
|
|
|
else:
|
|
|
|
pr_repo_name = os.getenv('APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME')
|
|
|
|
pr_branch_name = os.getenv('APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH')
|
|
|
|
pr_commit = os.getenv('APPVEYOR_PULL_REQUEST_HEAD_COMMIT')
|
|
|
|
pr_commit_short = pr_commit[:9]
|
|
|
|
elif git_is_repository():
|
2020-10-28 22:18:37 +00:00
|
|
|
(branch_name, commit, commit_short) = git_get_head_info()
|
|
|
|
else:
|
|
|
|
branch_name = 'tarball'
|
|
|
|
commit = ':(-dont-do-this'
|
|
|
|
commit_short = ':('
|
|
|
|
|
2021-08-08 15:20:51 +00:00
|
|
|
# header
|
2021-05-03 20:26:05 +00:00
|
|
|
contents_new = '''// Autogenerated by `xb premake`.
|
|
|
|
#ifndef GENERATED_VERSION_H_
|
|
|
|
#define GENERATED_VERSION_H_
|
|
|
|
#define XE_BUILD_BRANCH "{}"
|
|
|
|
#define XE_BUILD_COMMIT "{}"
|
|
|
|
#define XE_BUILD_COMMIT_SHORT "{}"
|
|
|
|
#define XE_BUILD_DATE __DATE__
|
|
|
|
'''.format(branch_name, commit, commit_short)
|
|
|
|
|
2021-08-08 15:20:51 +00:00
|
|
|
# PR info (if available)
|
|
|
|
if pr_number != 0:
|
|
|
|
contents_new += '''#define XE_BUILD_IS_PR
|
|
|
|
#define XE_BUILD_PR_NUMBER "{}"
|
|
|
|
#define XE_BUILD_PR_REPO "{}"
|
|
|
|
#define XE_BUILD_PR_BRANCH "{}"
|
|
|
|
#define XE_BUILD_PR_COMMIT "{}"
|
|
|
|
#define XE_BUILD_PR_COMMIT_SHORT "{}"
|
|
|
|
'''.format(pr_number, pr_repo_name, pr_branch_name, pr_commit, pr_commit_short)
|
|
|
|
|
|
|
|
# footer
|
|
|
|
contents_new += '''#endif // GENERATED_VERSION_H_
|
|
|
|
'''
|
|
|
|
|
2021-05-03 20:26:05 +00:00
|
|
|
contents_old = None
|
|
|
|
if os.path.exists(header_file) and os.path.getsize(header_file) < 1024:
|
|
|
|
with open(header_file, 'r') as f:
|
|
|
|
contents_old = f.read()
|
|
|
|
|
|
|
|
if contents_old != contents_new:
|
|
|
|
with open(header_file, 'w') as f:
|
|
|
|
f.write(contents_new)
|
|
|
|
|
2020-10-28 22:18:37 +00:00
|
|
|
|
2020-11-23 23:08:00 +00:00
|
|
|
def generate_source_class(path):
|
|
|
|
header_path = '{}.h'.format(path)
|
|
|
|
source_path = '{}.cc'.format(path)
|
|
|
|
|
|
|
|
if os.path.isfile(header_path) or os.path.isfile(source_path):
|
|
|
|
print('ERROR: Target file already exists')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if generate_source_file(header_path) > 0:
|
|
|
|
return 1
|
|
|
|
if generate_source_file(source_path) > 0:
|
|
|
|
# remove header if source file generation failed
|
|
|
|
os.remove(os.path.join(source_root, header_path))
|
|
|
|
return 1
|
2021-05-03 20:26:26 +00:00
|
|
|
|
2020-11-23 23:08:00 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
def generate_source_file(path):
|
|
|
|
"""Generates a source file at the specified path containing copyright notice
|
|
|
|
"""
|
|
|
|
copyright = '''/**
|
|
|
|
******************************************************************************
|
|
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright {} Ben Vanik. All rights reserved. *
|
|
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
|
|
******************************************************************************
|
|
|
|
*/'''.format(datetime.now().year)
|
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
print('ERROR: Target file already exists')
|
|
|
|
return 1
|
|
|
|
try:
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
f.write(copyright)
|
|
|
|
except Exception as e:
|
|
|
|
print('ERROR: Could not write to file [path {}]'.format(path))
|
|
|
|
return 1
|
2021-05-03 20:26:26 +00:00
|
|
|
|
2020-11-23 23:08:00 +00:00
|
|
|
return 0
|
|
|
|
|
2021-05-03 20:26:26 +00:00
|
|
|
|
2020-10-28 22:18:37 +00:00
|
|
|
|
|
|
|
def git_get_head_info():
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Queries the current branch and commit checksum from git.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
(branch_name, commit, commit_short)
|
|
|
|
If the user is not on any branch the name will be 'detached'.
|
|
|
|
"""
|
|
|
|
p = subprocess.Popen([
|
|
|
|
'git',
|
|
|
|
'symbolic-ref',
|
|
|
|
'--short',
|
|
|
|
'-q',
|
|
|
|
'HEAD',
|
|
|
|
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = p.communicate()
|
2017-12-16 05:43:27 +00:00
|
|
|
branch_name = stdout.decode('ascii').strip() or 'detached'
|
2017-09-21 21:42:57 +00:00
|
|
|
p = subprocess.Popen([
|
|
|
|
'git',
|
|
|
|
'rev-parse',
|
|
|
|
'HEAD',
|
|
|
|
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = p.communicate()
|
2017-12-16 05:43:27 +00:00
|
|
|
commit = stdout.decode('ascii').strip() or 'unknown'
|
2017-09-21 21:42:57 +00:00
|
|
|
p = subprocess.Popen([
|
|
|
|
'git',
|
|
|
|
'rev-parse',
|
|
|
|
'--short',
|
|
|
|
'HEAD',
|
|
|
|
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(stdout, stderr) = p.communicate()
|
2017-12-16 05:43:27 +00:00
|
|
|
commit_short = stdout.decode('ascii').strip() or 'unknown'
|
2017-09-21 21:42:57 +00:00
|
|
|
return branch_name, commit, commit_short
|
2015-12-27 19:53:37 +00:00
|
|
|
|
|
|
|
|
2020-10-28 22:18:37 +00:00
|
|
|
def git_is_repository():
|
|
|
|
"""Checks if git is available and this source tree is versioned.
|
2017-09-21 21:42:57 +00:00
|
|
|
"""
|
2020-10-28 22:18:37 +00:00
|
|
|
if not has_bin('git'):
|
|
|
|
return False
|
|
|
|
return shell_call([
|
|
|
|
'git',
|
|
|
|
'rev-parse',
|
|
|
|
'--is-inside-work-tree',
|
|
|
|
], throw_on_error=False, stdout_path=os.devnull, stderr_path=os.devnull) == 0
|
2015-12-27 19:53:37 +00:00
|
|
|
|
|
|
|
|
2015-08-01 07:05:55 +00:00
|
|
|
def git_submodule_update():
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Runs a full recursive git submodule init and update.
|
|
|
|
"""
|
2020-10-28 22:18:37 +00:00
|
|
|
shell_call([
|
|
|
|
'git',
|
|
|
|
'submodule',
|
|
|
|
'update',
|
|
|
|
'--init',
|
|
|
|
'--recursive',
|
|
|
|
])
|
2015-08-01 07:05:55 +00:00
|
|
|
|
|
|
|
|
2015-08-01 07:41:46 +00:00
|
|
|
def get_clang_format_binary():
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Finds a clang-format binary. Aborts if none is found.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A path to the clang-format executable.
|
|
|
|
"""
|
|
|
|
attempts = [
|
|
|
|
'C:\\Program Files\\LLVM\\bin\\clang-format.exe',
|
|
|
|
'C:\\Program Files (x86)\\LLVM\\bin\\clang-format.exe',
|
2021-09-23 13:41:12 +00:00
|
|
|
'clang-format-13',
|
2017-09-21 21:42:57 +00:00
|
|
|
'clang-format',
|
|
|
|
]
|
|
|
|
for binary in attempts:
|
|
|
|
if has_bin(binary):
|
|
|
|
return binary
|
|
|
|
print('ERROR: clang-format is not on PATH')
|
2018-11-22 15:20:09 +00:00
|
|
|
print('LLVM is available from https://llvm.org/releases/download.html')
|
2021-09-23 13:41:12 +00:00
|
|
|
print('At least version 13 is required.')
|
2017-09-21 21:42:57 +00:00
|
|
|
print('See docs/style_guide.md for instructions on how to get it.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-08-01 07:41:46 +00:00
|
|
|
|
2020-11-21 13:18:50 +00:00
|
|
|
def get_premake_target_os(target_os_override=None):
|
|
|
|
"""Gets the target --os to pass to premake, either for the current platform
|
|
|
|
or for the user-specified cross-compilation target.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_os_override: override specified by the user for cross-compilation,
|
|
|
|
or None to target the host platform.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Target --os to pass to premake. If a return value of this function valid
|
2020-11-21 13:32:07 +00:00
|
|
|
for the current configuration is passed to it again, the same value will
|
|
|
|
be returned.
|
2020-11-21 13:18:50 +00:00
|
|
|
"""
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
target_os = 'macosx'
|
|
|
|
elif sys.platform == 'win32':
|
|
|
|
target_os = 'windows'
|
|
|
|
elif host_linux_platform_is_android:
|
|
|
|
target_os = 'android'
|
|
|
|
else:
|
|
|
|
target_os = 'linux'
|
|
|
|
if target_os_override is not None and target_os_override != target_os:
|
|
|
|
if target_os_override == 'android':
|
|
|
|
target_os = target_os_override
|
|
|
|
else:
|
|
|
|
print(
|
|
|
|
'ERROR: cross-compilation is only supported for Android target')
|
|
|
|
sys.exit(0)
|
|
|
|
return target_os
|
|
|
|
|
|
|
|
|
2018-04-04 00:02:49 +00:00
|
|
|
def run_premake(target_os, action, cc=None):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Runs premake on the main project with the given format.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_os: target --os to pass to premake.
|
|
|
|
action: action to preform.
|
|
|
|
"""
|
2019-04-18 10:26:20 +00:00
|
|
|
args = [
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
os.path.join('tools', 'build', 'premake'),
|
|
|
|
'--file=premake5.lua',
|
|
|
|
'--os=%s' % target_os,
|
|
|
|
'--test-suite-mode=combined',
|
|
|
|
'--verbose',
|
|
|
|
action,
|
2019-04-18 10:26:20 +00:00
|
|
|
]
|
|
|
|
if cc:
|
|
|
|
args.insert(4, '--cc=%s' % cc)
|
|
|
|
|
|
|
|
ret = subprocess.call(args, shell=False)
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
if ret == 0:
|
|
|
|
generate_version_h()
|
|
|
|
|
|
|
|
return ret
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2020-11-21 13:18:50 +00:00
|
|
|
def run_platform_premake(target_os_override=None, cc='clang', devenv=None):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Runs all gyp configurations.
|
|
|
|
"""
|
2020-11-21 13:18:50 +00:00
|
|
|
target_os = get_premake_target_os(target_os_override)
|
|
|
|
if devenv is None:
|
|
|
|
if target_os == 'macosx':
|
|
|
|
devenv = 'xcode4'
|
|
|
|
elif target_os == 'windows':
|
|
|
|
vs_version = '2015'
|
|
|
|
if 'VSVERSION' in os.environ:
|
|
|
|
vs_version = os.environ['VSVERSION']
|
|
|
|
devenv = 'vs' + vs_version
|
|
|
|
elif target_os == 'android':
|
2021-10-29 21:01:27 +00:00
|
|
|
devenv = 'androidndk'
|
2020-11-21 13:18:50 +00:00
|
|
|
else:
|
|
|
|
devenv = 'gmake2'
|
|
|
|
if target_os != 'linux':
|
|
|
|
cc = None
|
|
|
|
return run_premake(target_os=target_os, action=devenv, cc=cc)
|
2015-12-31 08:37:13 +00:00
|
|
|
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
def get_build_bin_path(args):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Returns the path of the bin/ path with build results based on the
|
|
|
|
configuration specified in the parsed arguments.
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
Args:
|
|
|
|
args: Parsed arguments.
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
Returns:
|
|
|
|
A full path for the bin folder.
|
|
|
|
"""
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
platform = 'macosx'
|
|
|
|
elif sys.platform == 'win32':
|
|
|
|
platform = 'windows'
|
|
|
|
else:
|
|
|
|
platform = 'linux'
|
2018-02-15 04:57:45 +00:00
|
|
|
return os.path.join(self_path, 'build', 'bin', platform.capitalize(), args['config'].capitalize())
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2020-11-08 21:28:36 +00:00
|
|
|
def create_clion_workspace():
|
|
|
|
"""Creates some basic workspace information inside the .idea directory for first start.
|
|
|
|
"""
|
|
|
|
if os.path.exists('.idea'):
|
|
|
|
# No first start
|
|
|
|
return False
|
|
|
|
print('Generating CLion workspace files...')
|
|
|
|
# Might become easier in the future: https://youtrack.jetbrains.com/issue/CPP-7911
|
|
|
|
|
|
|
|
# Set the location of the CMakeLists.txt
|
|
|
|
os.mkdir('.idea')
|
|
|
|
with open(os.path.join('.idea', 'misc.xml'), 'w') as f:
|
|
|
|
f.write("""<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<project version="4">
|
|
|
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/build">
|
|
|
|
<contentRoot DIR="$PROJECT_DIR$" />
|
|
|
|
</component>
|
|
|
|
</project>
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Set available configurations
|
|
|
|
# TODO Find a way to trigger a cmake reload
|
|
|
|
with open(os.path.join('.idea', 'workspace.xml'), 'w') as f:
|
|
|
|
f.write("""<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<project version="4">
|
|
|
|
<component name="CMakeSettings">
|
|
|
|
<configurations>
|
|
|
|
<configuration PROFILE_NAME="Checked" CONFIG_NAME="Checked" />
|
|
|
|
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
|
|
|
|
<configuration PROFILE_NAME="Release" CONFIG_NAME="Release" />
|
|
|
|
</configurations>
|
|
|
|
</component>
|
|
|
|
</project>""")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
def discover_commands(subparsers):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Looks for all commands and returns a dictionary of them.
|
|
|
|
In the future commands could be discovered on disk.
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
subparsers: Argument subparsers parent used to add command parsers.
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
Returns:
|
|
|
|
A dictionary containing name-to-Command mappings.
|
|
|
|
"""
|
|
|
|
commands = {
|
|
|
|
'setup': SetupCommand(subparsers),
|
|
|
|
'pull': PullCommand(subparsers),
|
|
|
|
'premake': PremakeCommand(subparsers),
|
|
|
|
'build': BuildCommand(subparsers),
|
2021-06-05 15:53:53 +00:00
|
|
|
'buildshaders': BuildShadersCommand(subparsers),
|
2020-03-31 23:52:15 +00:00
|
|
|
'devenv': DevenvCommand(subparsers),
|
2017-09-21 21:42:57 +00:00
|
|
|
'genspirv': GenSpirvCommand(subparsers),
|
|
|
|
'gentests': GenTestsCommand(subparsers),
|
|
|
|
'test': TestCommand(subparsers),
|
|
|
|
'gputest': GpuTestCommand(subparsers),
|
|
|
|
'clean': CleanCommand(subparsers),
|
|
|
|
'nuke': NukeCommand(subparsers),
|
|
|
|
'lint': LintCommand(subparsers),
|
|
|
|
'format': FormatCommand(subparsers),
|
|
|
|
'style': StyleCommand(subparsers),
|
|
|
|
'tidy': TidyCommand(subparsers),
|
2020-11-23 23:08:00 +00:00
|
|
|
'stub': StubCommand(subparsers),
|
2017-09-21 21:42:57 +00:00
|
|
|
}
|
|
|
|
return commands
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
class Command(object):
|
|
|
|
"""Base type for commands.
|
2015-08-01 06:48:24 +00:00
|
|
|
"""
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
def __init__(self, subparsers, name, help_short=None, help_long=None,
|
|
|
|
*args, **kwargs):
|
|
|
|
"""Initializes a command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
subparsers: Argument subparsers parent used to add command parsers.
|
|
|
|
name: The name of the command exposed to the management script.
|
|
|
|
help_short: Help text printed alongside the command when queried.
|
|
|
|
help_long: Extended help text when viewing command help.
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.help_short = help_short
|
|
|
|
self.help_long = help_long
|
|
|
|
|
|
|
|
self.parser = subparsers.add_parser(name,
|
|
|
|
help=help_short,
|
|
|
|
description=help_long)
|
|
|
|
self.parser.set_defaults(command_handler=self)
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
"""Executes the command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args: Arguments hash for the command.
|
|
|
|
pass_args: Arguments list to pass to child commands.
|
|
|
|
cwd: Current working directory.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Return code of the command.
|
|
|
|
"""
|
|
|
|
return 1
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SetupCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'setup' command."""
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(SetupCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='setup',
|
|
|
|
help_short='Setup the build environment.',
|
|
|
|
*args, **kwargs)
|
2020-11-21 13:18:50 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Setting up the build environment...')
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
# Setup submodules.
|
|
|
|
print('- git submodule init / update...')
|
2020-10-28 22:18:37 +00:00
|
|
|
if git_is_repository():
|
|
|
|
git_submodule_update()
|
|
|
|
else:
|
|
|
|
print('WARNING: Git not available or not a repository. Dependencies may be missing.')
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- running premake...')
|
2020-11-21 13:18:50 +00:00
|
|
|
if run_platform_premake(target_os_override=args['target_os']) == 0:
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
|
|
|
print('Success!')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PullCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'pull' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(PullCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='pull',
|
|
|
|
help_short='Pulls the repo and all dependencies and rebases changes.',
|
|
|
|
*args, **kwargs)
|
2020-11-21 13:18:50 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--merge', action='store_true',
|
|
|
|
help='Merges on master instead of rebasing.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Pulling...')
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- switching to master...')
|
|
|
|
shell_call([
|
|
|
|
'git',
|
|
|
|
'checkout',
|
|
|
|
'master',
|
|
|
|
])
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- pulling self...')
|
|
|
|
if args['merge']:
|
|
|
|
shell_call([
|
|
|
|
'git',
|
|
|
|
'pull',
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
shell_call([
|
|
|
|
'git',
|
|
|
|
'pull',
|
|
|
|
'--rebase',
|
|
|
|
])
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- pulling dependencies...')
|
|
|
|
git_submodule_update()
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- running premake...')
|
2020-11-21 13:18:50 +00:00
|
|
|
if run_platform_premake(target_os_override=args['target_os']) == 0:
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
|
|
|
print('Success!')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
class PremakeCommand(Command):
|
|
|
|
"""'premake' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(PremakeCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='premake',
|
|
|
|
help_short='Runs premake to update all projects.',
|
|
|
|
*args, **kwargs)
|
2018-04-04 00:02:49 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--cc', default='clang', help='Compiler toolchain passed to premake')
|
2020-03-31 23:52:15 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--devenv', default=None, help='Development environment')
|
2020-11-21 13:18:50 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
# Update premake. If no binary found, it will be built from source.
|
|
|
|
print('Running premake...')
|
|
|
|
print('')
|
2020-11-21 13:18:50 +00:00
|
|
|
if run_platform_premake(target_os_override=args['target_os'],
|
|
|
|
cc=args['cc'], devenv=args['devenv']) == 0:
|
2017-09-21 21:42:57 +00:00
|
|
|
print('Success!')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseBuildCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Base command for things that require building."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(BaseBuildCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
*args, **kwargs)
|
2018-04-04 00:02:49 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--cc', default='clang', help='Compiler toolchain passed to premake')
|
2017-09-21 21:42:57 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--config', choices=['checked', 'debug', 'release'], default='debug',
|
|
|
|
type=str.lower, help='Chooses the build configuration.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--target', action='append', default=[],
|
|
|
|
help='Builds only the given target(s).')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--force', action='store_true',
|
|
|
|
help='Forces a full rebuild.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--no_premake', action='store_true',
|
|
|
|
help='Skips running premake before building.')
|
2017-12-20 01:25:25 +00:00
|
|
|
self.parser.add_argument(
|
2019-05-08 17:11:09 +00:00
|
|
|
'-j', default=4, type=int, help='Number of parallel threads')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
if not args['no_premake']:
|
|
|
|
print('- running premake...')
|
2020-11-21 13:18:50 +00:00
|
|
|
run_platform_premake(cc=args['cc'])
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
|
|
|
|
2017-12-20 01:25:25 +00:00
|
|
|
threads = args['j']
|
|
|
|
if threads < 0:
|
|
|
|
threads = 0
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- building (%s):%s...' % (
|
|
|
|
'all' if not len(args['target']) else ', '.join(args['target']),
|
|
|
|
args['config']))
|
|
|
|
if sys.platform == 'win32':
|
2020-11-21 13:18:50 +00:00
|
|
|
if vs_version is None:
|
|
|
|
print('ERROR: Visual Studio is not installed.');
|
|
|
|
result = 1
|
2017-09-21 21:42:57 +00:00
|
|
|
else:
|
2020-11-21 13:18:50 +00:00
|
|
|
targets = None
|
|
|
|
if len(args['target']):
|
|
|
|
targets = '/t:' + ';'.join(
|
|
|
|
target + (':Rebuild' if args['force'] else '')
|
|
|
|
for target in args['target'])
|
|
|
|
else:
|
|
|
|
targets = '/t:Rebuild' if args['force'] else None
|
|
|
|
|
|
|
|
result = subprocess.call([
|
|
|
|
'msbuild',
|
|
|
|
'build/xenia.sln',
|
|
|
|
'/nologo',
|
|
|
|
'/m',
|
|
|
|
'/v:m',
|
|
|
|
'/p:Configuration=' + args['config'],
|
|
|
|
] + ([targets] if targets is not None else []) + pass_args,
|
|
|
|
shell=False)
|
2017-09-21 21:42:57 +00:00
|
|
|
elif sys.platform == 'darwin':
|
2021-10-06 21:38:06 +00:00
|
|
|
schemes = args['target'] if len(args['target']) else ['xenia-app']
|
|
|
|
nested_args = [['-scheme', scheme] for scheme in schemes]
|
|
|
|
scheme_args = [arg for pair in nested_args for arg in pair]
|
|
|
|
result = subprocess.call([
|
|
|
|
'xcodebuild',
|
|
|
|
'-workspace',
|
|
|
|
'build/xenia.xcworkspace',
|
|
|
|
'-configuration',
|
|
|
|
args['config']
|
|
|
|
] + scheme_args + pass_args, shell=False, env=dict(os.environ))
|
2017-09-21 21:42:57 +00:00
|
|
|
else:
|
|
|
|
result = subprocess.call([
|
|
|
|
'make',
|
2020-02-08 22:25:13 +00:00
|
|
|
'-j' if threads == 0 else '-j%d' % threads,
|
2017-09-21 21:42:57 +00:00
|
|
|
'-Cbuild/',
|
|
|
|
'config=%s_linux' % (args['config']),
|
2017-12-20 01:25:25 +00:00
|
|
|
] + pass_args + args['target'], shell=False, env=dict(os.environ))
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
|
|
|
if result != 0:
|
|
|
|
print('ERROR: build failed with one or more errors.')
|
|
|
|
return result
|
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BuildCommand(BaseBuildCommand):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'build' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(BuildCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='build',
|
2020-03-31 23:52:15 +00:00
|
|
|
help_short='Builds the project with the default toolchain.',
|
2017-09-21 21:42:57 +00:00
|
|
|
*args, **kwargs)
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Building %s...' % (args['config']))
|
|
|
|
print('')
|
|
|
|
|
|
|
|
result = super(BuildCommand, self).execute(args, pass_args, cwd)
|
|
|
|
if not result:
|
|
|
|
print('Success!')
|
|
|
|
return result
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2021-06-05 15:53:53 +00:00
|
|
|
class BuildShadersCommand(Command):
|
|
|
|
"""'buildshaders' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(BuildShadersCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='buildshaders',
|
|
|
|
help_short='Generates shader binaries for inclusion in C++ files.',
|
|
|
|
help_long='''
|
|
|
|
Generates the shader binaries under src/*/shaders/bytecode/.
|
|
|
|
Run after modifying any .hs/vs/ds/gs/ps/cs.hlsl files.
|
|
|
|
Direct3D shaders can be built only on a Windows host.
|
|
|
|
''',
|
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--target', action='append', choices=['dxbc'], default=[],
|
|
|
|
help='Builds only the given target(s).')
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
src_paths = [os.path.join(root, name)
|
|
|
|
for root, dirs, files in os.walk('src')
|
|
|
|
for name in files
|
|
|
|
if (name.endswith('.hs.hlsl') or
|
|
|
|
name.endswith('.vs.hlsl') or
|
|
|
|
name.endswith('.ds.hlsl') or
|
|
|
|
name.endswith('.gs.hlsl') or
|
|
|
|
name.endswith('.ps.hlsl') or
|
|
|
|
name.endswith('.cs.hlsl'))]
|
|
|
|
targets = args['target']
|
|
|
|
all_targets = len(targets) == 0
|
|
|
|
|
|
|
|
# Direct3D DXBC.
|
|
|
|
if all_targets or 'dxbc' in targets:
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
print('Building Direct3D 12 Shader Model 5.1 DXBC shaders...')
|
|
|
|
windows_sdk_bin_path = os.path.join(
|
|
|
|
os.environ['ProgramFiles(x86)'], 'Windows Kits/10/bin/x64')
|
|
|
|
fxc = os.path.join(windows_sdk_bin_path, 'fxc')
|
|
|
|
# Ensure we have the tools.
|
|
|
|
if not os.path.exists(windows_sdk_bin_path):
|
|
|
|
print('ERROR: could not find Windows 10 SDK binaries')
|
|
|
|
return 1
|
|
|
|
elif not has_bin(fxc):
|
|
|
|
print('ERROR: could not find fxc')
|
|
|
|
return 1
|
|
|
|
# Build DXBC.
|
|
|
|
for src_path in src_paths:
|
|
|
|
print('- %s > d3d12_5_1' % (src_path))
|
|
|
|
dxbc_identifier = \
|
|
|
|
os.path.basename(src_path)[:-5].replace('.', '_')
|
|
|
|
dxbc_dir_path = os.path.join(os.path.dirname(src_path),
|
|
|
|
'bytecode/d3d12_5_1')
|
|
|
|
os.makedirs(dxbc_dir_path, exist_ok=True)
|
|
|
|
dxbc_file_path_base = os.path.join(dxbc_dir_path,
|
|
|
|
dxbc_identifier)
|
|
|
|
if subprocess.call([
|
|
|
|
fxc,
|
|
|
|
'/Fh', dxbc_file_path_base + '.h',
|
|
|
|
'/T', dxbc_identifier[-2:] + '_5_1',
|
|
|
|
'/Vn', dxbc_identifier,
|
|
|
|
'/WX',
|
|
|
|
'/nologo',
|
|
|
|
src_path
|
|
|
|
], stdout=subprocess.DEVNULL):
|
|
|
|
print('ERROR: failed to build a DXBC shader')
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
if all_targets:
|
|
|
|
print('WARNING: Direct3D DXBC shader building is supported '
|
|
|
|
'only on Windows')
|
|
|
|
else:
|
|
|
|
print('ERROR: Direct3D DXBC shader building is supported '
|
|
|
|
'only on Windows')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2016-02-21 00:24:42 +00:00
|
|
|
class GenSpirvCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'genspirv' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(GenSpirvCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='genspirv',
|
|
|
|
help_short='Generates SPIR-V binaries and header files.',
|
|
|
|
help_long='''
|
|
|
|
Generates the .spv/.h binaries under src/xenia/*/vulkan/shaders/bin/).
|
|
|
|
Run after modifying any .vert/.geom/.frag files.
|
|
|
|
''',
|
|
|
|
*args, **kwargs)
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Generating SPIR-V binaries...')
|
|
|
|
print('')
|
2016-02-21 00:24:42 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
vulkan_sdk_path = os.environ['VULKAN_SDK']
|
|
|
|
vulkan_bin_path = os.path.join(vulkan_sdk_path, 'bin')
|
|
|
|
glslang = os.path.join(vulkan_bin_path, 'glslangValidator')
|
|
|
|
spirv_dis = os.path.join(vulkan_bin_path, 'spirv-dis')
|
|
|
|
spirv_remap = os.path.join(vulkan_bin_path, 'spirv-remap')
|
|
|
|
|
|
|
|
# Ensure we have the tools.
|
|
|
|
if not os.path.exists(vulkan_sdk_path):
|
|
|
|
print('ERROR: could not find the Vulkan SDK')
|
|
|
|
return 1
|
|
|
|
elif not has_bin(glslang):
|
|
|
|
print('ERROR: could not find glslangValidator')
|
|
|
|
return 1
|
|
|
|
elif not has_bin(spirv_dis):
|
|
|
|
print('ERROR: could not find spirv-dis')
|
|
|
|
return 1
|
|
|
|
elif not has_bin(spirv_remap):
|
|
|
|
print('ERROR: could not find spirv-remap')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
src_files = [os.path.join(root, name)
|
|
|
|
for root, dirs, files in os.walk('src')
|
|
|
|
for name in files
|
|
|
|
if (name.endswith('.vert') or name.endswith('.geom') or
|
|
|
|
name.endswith('.frag'))]
|
|
|
|
|
|
|
|
any_errors = False
|
|
|
|
for src_file in src_files:
|
|
|
|
print('- %s' % (src_file))
|
|
|
|
src_name = os.path.splitext(os.path.basename(src_file))[0]
|
|
|
|
identifier = os.path.basename(src_file).replace('.', '_')
|
|
|
|
|
|
|
|
bin_path = os.path.join(os.path.dirname(src_file), 'bin')
|
|
|
|
spv_file = os.path.join(bin_path, identifier) + '.spv'
|
|
|
|
txt_file = os.path.join(bin_path, identifier) + '.txt'
|
|
|
|
h_file = os.path.join(bin_path, identifier) + '.h'
|
|
|
|
|
|
|
|
# GLSL source -> .spv binary
|
|
|
|
shell_call([
|
|
|
|
glslang,
|
2018-11-26 17:46:09 +00:00
|
|
|
'-Os',
|
2017-09-21 21:42:57 +00:00
|
|
|
'-V', src_file,
|
|
|
|
'-o', spv_file,
|
|
|
|
])
|
|
|
|
|
|
|
|
# Disassemble binary into human-readable text.
|
|
|
|
shell_call([
|
|
|
|
spirv_dis,
|
|
|
|
'-o', txt_file,
|
|
|
|
spv_file,
|
|
|
|
])
|
|
|
|
|
|
|
|
# TODO(benvanik): remap?
|
|
|
|
|
|
|
|
# bin2c so we get a header file we can compile in.
|
2017-12-21 22:11:35 +00:00
|
|
|
with open(h_file, 'w') as out_file:
|
2017-09-21 21:42:57 +00:00
|
|
|
out_file.write('// generated from `xb genspirv`\n')
|
|
|
|
out_file.write('// source: %s\n' % os.path.basename(src_file))
|
|
|
|
out_file.write('const uint8_t %s[] = {' % (identifier))
|
|
|
|
with open(spv_file, 'rb') as in_file:
|
|
|
|
index = 0
|
|
|
|
c = in_file.read(1)
|
2017-12-21 22:11:35 +00:00
|
|
|
while len(c) != 0:
|
2017-09-21 21:42:57 +00:00
|
|
|
if index % 12 == 0:
|
|
|
|
out_file.write('\n ')
|
|
|
|
else:
|
|
|
|
out_file.write(' ')
|
|
|
|
index += 1
|
|
|
|
out_file.write('0x%02X,' % ord(c))
|
|
|
|
c = in_file.read(1)
|
|
|
|
out_file.write('\n};\n')
|
|
|
|
|
|
|
|
if any_errors:
|
|
|
|
print('ERROR: failed to build one or more SPIR-V files.')
|
|
|
|
return 1
|
2016-02-21 00:24:42 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
return 0
|
2016-02-21 00:24:42 +00:00
|
|
|
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
class TestCommand(BaseBuildCommand):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'test' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(TestCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='test',
|
|
|
|
help_short='Runs automated tests that have been built with `xb build`.',
|
|
|
|
help_long='''
|
|
|
|
To pass arguments to the test executables separate them with `--`.
|
|
|
|
For example, you can run only the instr_foo.s tests with:
|
|
|
|
$ xb test -- instr_foo
|
|
|
|
''',
|
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--no_build', action='store_true',
|
|
|
|
help='Don\'t build before running tests.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--continue', action='store_true',
|
|
|
|
help='Don\'t stop when a test errors, but continue running all.')
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Testing...')
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
# The test executables that will be built and run.
|
|
|
|
test_targets = args['target'] or [
|
2020-02-28 20:09:50 +00:00
|
|
|
'xenia-base-tests',
|
|
|
|
'xenia-cpu-ppc-tests'
|
2017-09-21 21:42:57 +00:00
|
|
|
]
|
|
|
|
args['target'] = test_targets
|
|
|
|
|
|
|
|
# Build all targets (if desired).
|
|
|
|
if not args['no_build']:
|
|
|
|
result = super(TestCommand, self).execute(args, [], cwd)
|
|
|
|
if result:
|
|
|
|
print('Failed to build, aborting test run.')
|
|
|
|
return result
|
|
|
|
|
|
|
|
# Ensure all targets exist before we run.
|
|
|
|
test_executables = [
|
|
|
|
get_bin(os.path.join(get_build_bin_path(args), test_target))
|
|
|
|
for test_target in test_targets]
|
2020-02-28 20:09:50 +00:00
|
|
|
for i in range(0, len(test_targets)):
|
|
|
|
if test_executables[i] is None:
|
|
|
|
print('ERROR: Unable to find %s - build it.' % (test_targets[i]))
|
2017-09-21 21:42:57 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
# Run tests.
|
|
|
|
any_failed = False
|
|
|
|
for test_executable in test_executables:
|
|
|
|
print('- %s' % test_executable)
|
|
|
|
result = shell_call([test_executable] + pass_args,
|
|
|
|
throw_on_error=False)
|
|
|
|
if result:
|
|
|
|
any_failed = True
|
|
|
|
if args['continue']:
|
|
|
|
print('ERROR: test failed but continuing due to --continue.')
|
|
|
|
else:
|
|
|
|
print('ERROR: test failed, aborting, use --continue to keep going.')
|
|
|
|
return result
|
|
|
|
|
|
|
|
if any_failed:
|
|
|
|
print('ERROR: one or more tests failed.')
|
|
|
|
result = 1
|
2015-08-01 06:48:24 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
class GenTestsCommand(Command):
|
|
|
|
"""'gentests' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(GenTestsCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='gentests',
|
|
|
|
help_short='Generates test binaries.',
|
|
|
|
help_long='''
|
|
|
|
Generates test binaries (under src/xenia/cpu/ppc/testing/bin/).
|
|
|
|
Run after modifying test .s files.
|
|
|
|
''',
|
|
|
|
*args, **kwargs)
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Generating test binaries...')
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
binutils_path = os.path.join('third_party', 'binutils-ppc-cygwin')
|
|
|
|
ppc_as = os.path.join(binutils_path, 'powerpc-none-elf-as')
|
|
|
|
ppc_ld = os.path.join(binutils_path, 'powerpc-none-elf-ld')
|
|
|
|
ppc_objdump = os.path.join(binutils_path, 'powerpc-none-elf-objdump')
|
|
|
|
ppc_nm = os.path.join(binutils_path, 'powerpc-none-elf-nm')
|
|
|
|
|
|
|
|
test_src = os.path.join('src', 'xenia', 'cpu', 'ppc', 'testing')
|
|
|
|
test_bin = os.path.join(test_src, 'bin')
|
|
|
|
|
|
|
|
# Ensure the test output path exists.
|
|
|
|
if not os.path.exists(test_bin):
|
|
|
|
os.mkdir(test_bin)
|
|
|
|
|
|
|
|
src_files = [os.path.join(root, name)
|
|
|
|
for root, dirs, files in os.walk('src')
|
|
|
|
for name in files
|
|
|
|
if (name.startswith('instr_') or name.startswith('seq_'))
|
|
|
|
and name.endswith(('.s'))]
|
|
|
|
|
|
|
|
def make_unix_path(p):
|
|
|
|
"""Forces a unix path separator style, as required by binutils.
|
|
|
|
"""
|
|
|
|
return p.replace(os.sep, '/')
|
|
|
|
|
|
|
|
any_errors = False
|
|
|
|
for src_file in src_files:
|
|
|
|
print('- %s' % src_file)
|
|
|
|
src_name = os.path.splitext(os.path.basename(src_file))[0]
|
|
|
|
obj_file = os.path.join(test_bin, src_name) + '.o'
|
|
|
|
shell_call([
|
|
|
|
ppc_as,
|
|
|
|
'-a32',
|
|
|
|
'-be',
|
|
|
|
'-mregnames',
|
|
|
|
'-mpower7',
|
|
|
|
'-maltivec',
|
|
|
|
'-mvsx',
|
|
|
|
'-mvmx128',
|
|
|
|
'-R',
|
|
|
|
'-o%s' % (make_unix_path(obj_file)),
|
|
|
|
make_unix_path(src_file),
|
|
|
|
])
|
|
|
|
dis_file = os.path.join(test_bin, src_name) + '.dis'
|
|
|
|
shell_call([
|
|
|
|
ppc_objdump,
|
|
|
|
'--adjust-vma=0x100000',
|
|
|
|
'-Mpower7',
|
|
|
|
'-Mvmx128',
|
|
|
|
'-D',
|
|
|
|
'-EB',
|
|
|
|
make_unix_path(obj_file),
|
|
|
|
], stdout_path=dis_file)
|
|
|
|
# Eat the first 4 lines to kill the file path that'll differ across machines.
|
|
|
|
with open(dis_file) as f:
|
|
|
|
dis_file_lines = f.readlines()
|
|
|
|
with open(dis_file, 'w') as f:
|
|
|
|
f.writelines(dis_file_lines[4:])
|
|
|
|
shell_call([
|
|
|
|
ppc_ld,
|
|
|
|
'-A powerpc:common32',
|
|
|
|
'-melf32ppc',
|
|
|
|
'-EB',
|
|
|
|
'-nostdlib',
|
|
|
|
'--oformat=binary',
|
|
|
|
'-Ttext=0x80000000',
|
|
|
|
'-e0x80000000',
|
|
|
|
'-o%s' % (make_unix_path(os.path.join(test_bin, src_name) + '.bin')),
|
|
|
|
make_unix_path(obj_file),
|
|
|
|
])
|
|
|
|
shell_call([
|
|
|
|
ppc_nm,
|
|
|
|
'--numeric-sort',
|
|
|
|
make_unix_path(obj_file),
|
|
|
|
], stdout_path=os.path.join(test_bin, src_name) + '.map')
|
|
|
|
|
|
|
|
if any_errors:
|
|
|
|
print('ERROR: failed to build one or more tests.')
|
|
|
|
return 1
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2015-12-31 20:27:26 +00:00
|
|
|
class GpuTestCommand(BaseBuildCommand):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'gputest' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(GpuTestCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='gputest',
|
|
|
|
help_short='Runs automated GPU diff tests against reference imagery.',
|
|
|
|
help_long='''
|
|
|
|
To pass arguments to the test executables separate them with `--`.
|
|
|
|
''',
|
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--no_build', action='store_true',
|
|
|
|
help='Don\'t build before running tests.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--update_reference_files', action='store_true',
|
|
|
|
help='Update all reference imagery.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--generate_missing_reference_files', action='store_true',
|
|
|
|
help='Create reference files for new traces.')
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Testinging...')
|
|
|
|
print('')
|
2015-12-31 20:27:26 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
# The test executables that will be built and run.
|
|
|
|
test_targets = args['target'] or [
|
2019-05-08 15:31:04 +00:00
|
|
|
'xenia-gpu-vulkan-trace-dump',
|
2017-09-21 21:42:57 +00:00
|
|
|
]
|
|
|
|
args['target'] = test_targets
|
|
|
|
|
|
|
|
# Build all targets (if desired).
|
|
|
|
if not args['no_build']:
|
|
|
|
result = super(GpuTestCommand, self).execute(args, [], cwd)
|
|
|
|
if result:
|
|
|
|
print('Failed to build, aborting test run.')
|
|
|
|
return result
|
|
|
|
|
|
|
|
# Ensure all targets exist before we run.
|
|
|
|
test_executables = [
|
|
|
|
get_bin(os.path.join(get_build_bin_path(args), test_target))
|
|
|
|
for test_target in test_targets]
|
2020-02-28 20:09:50 +00:00
|
|
|
for i in range(0, len(test_targets)):
|
|
|
|
if test_executables[i] is None:
|
|
|
|
print('ERROR: Unable to find %s - build it.' % (test_targets[i]))
|
2017-09-21 21:42:57 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
output_path = os.path.join(self_path, 'build', 'gputest')
|
|
|
|
if os.path.isdir(output_path):
|
|
|
|
shutil.rmtree(output_path)
|
|
|
|
os.makedirs(output_path)
|
|
|
|
print('Running tests and outputting to %s...' % (output_path))
|
|
|
|
|
|
|
|
reference_trace_root = os.path.join(self_path, 'testdata',
|
|
|
|
'reference-gpu-traces')
|
|
|
|
|
|
|
|
# Run tests.
|
|
|
|
any_failed = False
|
|
|
|
result = shell_call([
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
os.path.join(self_path, 'tools', 'gpu-trace-diff'),
|
|
|
|
'--executable=' + test_executables[0],
|
|
|
|
'--trace_path=' + os.path.join(reference_trace_root, 'traces'),
|
|
|
|
'--output_path=' + output_path,
|
|
|
|
'--reference_path=' + os.path.join(reference_trace_root, 'references'),
|
|
|
|
] + (['--generate_missing_reference_files'] if args['generate_missing_reference_files'] else []) +
|
|
|
|
(['--update_reference_files'] if args['update_reference_files'] else []) +
|
|
|
|
pass_args,
|
|
|
|
throw_on_error=False)
|
|
|
|
if result:
|
|
|
|
any_failed = True
|
|
|
|
|
|
|
|
if any_failed:
|
|
|
|
print('ERROR: one or more tests failed.')
|
|
|
|
result = 1
|
|
|
|
print('Check %s/results.html for more details.' % (output_path))
|
2015-12-31 20:27:26 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
class CleanCommand(Command):
|
|
|
|
"""'clean' command."""
|
2015-12-31 20:27:26 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(CleanCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='clean',
|
|
|
|
help_short='Removes intermediate files and build outputs.',
|
|
|
|
*args, **kwargs)
|
2020-11-21 13:18:50 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
2015-12-31 20:27:26 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Cleaning build artifacts...')
|
|
|
|
print('')
|
2015-12-31 20:27:26 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- premake clean...')
|
2020-11-21 13:18:50 +00:00
|
|
|
run_premake(get_premake_target_os(args['target_os']), 'clean')
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
2015-12-31 20:27:26 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('Success!')
|
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
class NukeCommand(Command):
|
|
|
|
"""'nuke' command."""
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(NukeCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='nuke',
|
|
|
|
help_short='Removes all build/ output.',
|
|
|
|
*args, **kwargs)
|
2020-11-21 13:18:50 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
print('Cleaning build artifacts...')
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- removing build/...')
|
|
|
|
if os.path.isdir('build/'):
|
|
|
|
shutil.rmtree('build/')
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- git reset to master...')
|
|
|
|
shell_call([
|
|
|
|
'git',
|
|
|
|
'reset',
|
|
|
|
'--hard',
|
|
|
|
'master',
|
|
|
|
])
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- running premake...')
|
2020-11-21 13:18:50 +00:00
|
|
|
run_platform_premake(target_os_override=args['target_os'])
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('Success!')
|
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2015-08-01 17:49:58 +00:00
|
|
|
def find_xenia_source_files():
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Gets all xenia source files in the project.
|
2015-08-01 09:51:49 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
Returns:
|
|
|
|
A list of file paths.
|
|
|
|
"""
|
|
|
|
return [os.path.join(root, name)
|
|
|
|
for root, dirs, files in os.walk('src')
|
|
|
|
for name in files
|
2021-06-29 00:36:43 +00:00
|
|
|
if name.endswith(('.cc', '.c', '.h', '.inl', '.inc'))]
|
2015-08-01 17:49:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def find_all_source_files():
|
2017-09-21 21:42:57 +00:00
|
|
|
"""Gets all interesting source files in the project.
|
2015-08-01 17:49:58 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
Returns:
|
|
|
|
A list of file paths.
|
|
|
|
"""
|
|
|
|
return find_xenia_source_files()
|
2015-08-01 09:51:49 +00:00
|
|
|
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
class LintCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'lint' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(LintCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='lint',
|
|
|
|
help_short='Checks for lint errors with clang-format.',
|
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--all', action='store_true',
|
|
|
|
help='Lint all files, not just those changed.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--origin', action='store_true',
|
|
|
|
help='Lints all files changed relative to origin/master.')
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
clang_format_binary = get_clang_format_binary()
|
|
|
|
|
|
|
|
difftemp = '.difftemp.txt'
|
|
|
|
|
|
|
|
if args['all']:
|
|
|
|
all_files = find_all_source_files()
|
2020-02-22 18:24:41 +00:00
|
|
|
all_files.sort()
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- linting %d files' % (len(all_files)))
|
|
|
|
any_errors = False
|
|
|
|
for file_path in all_files:
|
|
|
|
if os.path.exists(difftemp): os.remove(difftemp)
|
|
|
|
ret = shell_call([
|
|
|
|
clang_format_binary,
|
|
|
|
'-output-replacements-xml',
|
|
|
|
'-style=file',
|
|
|
|
file_path,
|
|
|
|
], throw_on_error=False, stdout_path=difftemp)
|
|
|
|
with open(difftemp) as f:
|
|
|
|
had_errors = '<replacement ' in f.read()
|
|
|
|
if os.path.exists(difftemp): os.remove(difftemp)
|
|
|
|
if had_errors:
|
|
|
|
any_errors = True
|
|
|
|
print('')
|
|
|
|
print(file_path)
|
|
|
|
shell_call([
|
|
|
|
clang_format_binary,
|
|
|
|
'-style=file',
|
|
|
|
file_path,
|
|
|
|
], throw_on_error=False, stdout_path=difftemp)
|
|
|
|
shell_call([
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
'tools/diff.py',
|
|
|
|
file_path,
|
|
|
|
difftemp,
|
|
|
|
difftemp,
|
|
|
|
])
|
|
|
|
shell_call([
|
|
|
|
'type' if sys.platform == 'win32' else 'cat',
|
|
|
|
difftemp,
|
2020-02-22 18:10:18 +00:00
|
|
|
], shell=True if sys.platform == 'win32' else False)
|
2017-09-21 21:42:57 +00:00
|
|
|
if os.path.exists(difftemp): os.remove(difftemp)
|
|
|
|
print('')
|
|
|
|
print('')
|
|
|
|
if any_errors:
|
|
|
|
print('ERROR: 1+ diffs. Stage changes and run \'xb format\' to fix.')
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
print('Linting completed successfully.')
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
print('- git-clang-format --diff')
|
|
|
|
if os.path.exists(difftemp): os.remove(difftemp)
|
|
|
|
ret = shell_call([
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
'third_party/clang-format/git-clang-format',
|
|
|
|
'--binary=%s' % (clang_format_binary),
|
|
|
|
'--commit=%s' % ('origin/master' if args['origin'] else 'HEAD'),
|
2020-02-22 19:23:54 +00:00
|
|
|
'--style=file',
|
2017-09-21 21:42:57 +00:00
|
|
|
'--diff',
|
|
|
|
], throw_on_error=False, stdout_path=difftemp)
|
|
|
|
with open(difftemp) as f:
|
|
|
|
contents = f.read()
|
|
|
|
not_modified = 'no modified files' in contents
|
|
|
|
not_modified = not_modified or 'did not modify' in contents
|
|
|
|
f.close()
|
|
|
|
if os.path.exists(difftemp): os.remove(difftemp)
|
|
|
|
if not not_modified:
|
|
|
|
any_errors = True
|
|
|
|
print('')
|
|
|
|
shell_call([
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
'third_party/clang-format/git-clang-format',
|
|
|
|
'--binary=%s' % (clang_format_binary),
|
|
|
|
'--commit=%s' % ('origin/master' if args['origin'] else 'HEAD'),
|
2020-02-22 19:23:54 +00:00
|
|
|
'--style=file',
|
2017-09-21 21:42:57 +00:00
|
|
|
'--diff',
|
|
|
|
])
|
|
|
|
print('ERROR: 1+ diffs. Stage changes and run \'xb format\' to fix.')
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
print('Linting completed successfully.')
|
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FormatCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'format' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(FormatCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='format',
|
|
|
|
help_short='Reformats staged code with clang-format.',
|
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--all', action='store_true',
|
|
|
|
help='Format all files, not just those changed.')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--origin', action='store_true',
|
|
|
|
help='Formats all files changed relative to origin/master.')
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
clang_format_binary = get_clang_format_binary()
|
|
|
|
|
|
|
|
if args['all']:
|
|
|
|
all_files = find_all_source_files()
|
2020-02-22 18:24:41 +00:00
|
|
|
all_files.sort()
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- clang-format [%d files]' % (len(all_files)))
|
|
|
|
any_errors = False
|
|
|
|
for file_path in all_files:
|
|
|
|
ret = shell_call([
|
|
|
|
clang_format_binary,
|
|
|
|
'-i',
|
|
|
|
'-style=file',
|
|
|
|
file_path,
|
|
|
|
], throw_on_error=False)
|
|
|
|
if ret:
|
|
|
|
any_errors = True
|
|
|
|
print('')
|
|
|
|
if any_errors:
|
|
|
|
print('ERROR: 1+ clang-format calls failed.')
|
|
|
|
print('Ensure all files are staged.')
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
print('Formatting completed successfully.')
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
print('- git-clang-format')
|
|
|
|
shell_call([
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
'third_party/clang-format/git-clang-format',
|
|
|
|
'--binary=%s' % (clang_format_binary),
|
|
|
|
'--commit=%s' % ('origin/master' if args['origin'] else 'HEAD'),
|
|
|
|
])
|
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
2015-08-01 17:49:58 +00:00
|
|
|
# TODO(benvanik): merge into linter, or as lint --anal?
|
|
|
|
class StyleCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'style' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(StyleCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='style',
|
|
|
|
help_short='Runs the style checker on all code.',
|
|
|
|
*args, **kwargs)
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
all_files = [file_path for file_path in find_all_source_files()
|
|
|
|
if not file_path.endswith('_test.cc')]
|
|
|
|
print('- cpplint [%d files]' % (len(all_files)))
|
|
|
|
ret = shell_call([
|
2018-04-29 01:15:16 +00:00
|
|
|
sys.executable,
|
2017-09-21 21:42:57 +00:00
|
|
|
'third_party/google-styleguide/cpplint/cpplint.py',
|
|
|
|
'--output=vs7',
|
|
|
|
'--linelength=80',
|
|
|
|
'--filter=-build/c++11,+build/include_alpha',
|
|
|
|
'--root=src',
|
|
|
|
] + all_files, throw_on_error=False)
|
|
|
|
print('')
|
|
|
|
if ret:
|
|
|
|
print('ERROR: 1+ cpplint calls failed.')
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
print('Style linting completed successfully.')
|
|
|
|
return 0
|
2015-08-01 17:49:58 +00:00
|
|
|
|
|
|
|
|
2016-01-01 19:15:07 +00:00
|
|
|
# TODO(benvanik): merge into linter, or as lint --anal?
|
|
|
|
class TidyCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'tidy' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(TidyCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='tidy',
|
2019-07-18 00:52:01 +00:00
|
|
|
help_short='Runs the clang-tidy checker on all code.',
|
2017-09-21 21:42:57 +00:00
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--fix', action='store_true',
|
|
|
|
help='Applies suggested fixes, where possible.')
|
2020-11-21 13:18:50 +00:00
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
# Run premake to generate our compile_commands.json file for clang to use.
|
2020-11-21 13:18:50 +00:00
|
|
|
# TODO(benvanik): only do linux? whatever clang-tidy is ok with.
|
|
|
|
run_premake(get_premake_target_os(args['target_os']),
|
|
|
|
'export-compile-commands')
|
2017-09-21 21:42:57 +00:00
|
|
|
|
|
|
|
platform_name = ''
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
platform_name = 'darwin'
|
|
|
|
elif sys.platform == 'win32':
|
|
|
|
platform_name = 'windows'
|
|
|
|
else:
|
|
|
|
platform_name = 'linux'
|
|
|
|
tool_root = 'build/llvm_tools/debug_%s' % platform_name
|
|
|
|
|
|
|
|
all_files = [file_path for file_path in find_all_source_files()
|
|
|
|
if not file_path.endswith('_test.cc')]
|
|
|
|
# Tidy only likes .cc files.
|
|
|
|
all_files = [file_path for file_path in all_files
|
|
|
|
if file_path.endswith('.cc')]
|
|
|
|
|
|
|
|
any_errors = False
|
|
|
|
for file in all_files:
|
|
|
|
print('- clang-tidy %s' % (file))
|
|
|
|
ret = shell_call([
|
|
|
|
'clang-tidy',
|
|
|
|
'-p', tool_root,
|
|
|
|
'-checks=' + ','.join([
|
|
|
|
'clang-analyzer-*',
|
|
|
|
'google-*',
|
|
|
|
'misc-*',
|
|
|
|
'modernize-*'
|
|
|
|
# TODO(benvanik): pick the ones we want - some are silly.
|
|
|
|
# 'readability-*',
|
|
|
|
]),
|
|
|
|
] + (['-fix'] if args['fix'] else []) + [
|
|
|
|
file,
|
|
|
|
], throw_on_error=False)
|
|
|
|
if ret:
|
|
|
|
any_errors = True
|
2016-01-01 19:15:07 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
|
|
|
if any_errors:
|
|
|
|
print('ERROR: 1+ clang-tidy calls failed.')
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
print('Tidy completed successfully.')
|
|
|
|
return 0
|
2016-01-01 19:15:07 +00:00
|
|
|
|
2020-11-23 23:08:00 +00:00
|
|
|
class StubCommand(Command):
|
|
|
|
"""'stub' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(StubCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='stub',
|
|
|
|
help_short='Create new file(s) in the xenia source tree and run premake',
|
|
|
|
*args, **kwargs)
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--file', default=None,
|
|
|
|
help='Generate a source file at the provided location in the source tree')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--class', default=None,
|
|
|
|
help='Generate a class pair (.cc/.h) at the provided location in the source tree')
|
|
|
|
self.parser.add_argument(
|
|
|
|
'--target_os', default=None,
|
|
|
|
help='Target OS passed to premake, for cross-compilation')
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
|
|
|
root = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
source_root = os.path.join(root, os.path.normpath('src/xenia'))
|
|
|
|
|
|
|
|
if args['class']:
|
|
|
|
path = os.path.normpath(os.path.join(source_root, args['class']))
|
|
|
|
target_dir = os.path.dirname(path)
|
|
|
|
class_name = os.path.basename(path)
|
|
|
|
|
|
|
|
status = generate_source_class(path)
|
|
|
|
if status > 0:
|
|
|
|
return status
|
2021-05-03 20:26:26 +00:00
|
|
|
|
2020-11-23 23:08:00 +00:00
|
|
|
print('Created class \'{0}\' at {1}'.format(class_name, target_dir))
|
|
|
|
|
|
|
|
elif args['file']:
|
|
|
|
path = os.path.normpath(os.path.join(source_root, args['file']))
|
|
|
|
target_dir = os.path.dirname(path)
|
|
|
|
file_name = os.path.basename(path)
|
|
|
|
|
|
|
|
status = generate_source_file(path)
|
|
|
|
if status > 0:
|
|
|
|
return status
|
2021-05-03 20:26:26 +00:00
|
|
|
|
2020-11-23 23:08:00 +00:00
|
|
|
print('Created file \'{0}\' at {1}'.format(file_name, target_dir))
|
|
|
|
|
|
|
|
else:
|
|
|
|
print('ERROR: Please specify a file/class to generate')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
run_platform_premake(target_os_override=args['target_os'])
|
|
|
|
return 0
|
2016-01-01 19:15:07 +00:00
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
class DevenvCommand(Command):
|
2017-09-21 21:42:57 +00:00
|
|
|
"""'devenv' command."""
|
|
|
|
|
|
|
|
def __init__(self, subparsers, *args, **kwargs):
|
|
|
|
super(DevenvCommand, self).__init__(
|
|
|
|
subparsers,
|
|
|
|
name='devenv',
|
2020-03-31 23:52:15 +00:00
|
|
|
help_short='Launches the development environment.',
|
2017-09-21 21:42:57 +00:00
|
|
|
*args, **kwargs)
|
|
|
|
|
|
|
|
def execute(self, args, pass_args, cwd):
|
2020-03-31 23:52:15 +00:00
|
|
|
devenv = None
|
2020-11-08 21:28:36 +00:00
|
|
|
show_reload_prompt = False
|
2020-03-31 23:52:15 +00:00
|
|
|
if sys.platform == 'win32':
|
2020-11-21 13:18:50 +00:00
|
|
|
if vs_version is None:
|
|
|
|
print('ERROR: Visual Studio is not installed.');
|
|
|
|
return 1
|
2020-03-31 23:52:15 +00:00
|
|
|
print('Launching Visual Studio...')
|
2021-10-06 21:38:06 +00:00
|
|
|
elif sys.platform == 'darwin':
|
|
|
|
print('Launching Xcode...')
|
|
|
|
devenv = 'xcode4'
|
2020-11-08 21:28:36 +00:00
|
|
|
elif has_bin('clion') or has_bin('clion.sh'):
|
|
|
|
print('Launching CLion...')
|
|
|
|
show_reload_prompt = create_clion_workspace()
|
|
|
|
devenv = 'cmake'
|
2020-03-31 23:52:15 +00:00
|
|
|
else:
|
|
|
|
print('Launching CodeLite...')
|
|
|
|
devenv = 'codelite'
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
|
|
|
|
|
|
|
print('- running premake...')
|
2020-03-31 23:52:15 +00:00
|
|
|
run_platform_premake(devenv=devenv)
|
2017-09-21 21:42:57 +00:00
|
|
|
print('')
|
2015-08-01 06:48:24 +00:00
|
|
|
|
2017-09-21 21:42:57 +00:00
|
|
|
print('- launching devenv...')
|
2020-11-08 21:28:36 +00:00
|
|
|
if show_reload_prompt:
|
|
|
|
print_box('Please run "File ⇒ ↺ Reload CMake Project" from inside the IDE!')
|
2020-03-31 23:52:15 +00:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
shell_call([
|
|
|
|
'devenv',
|
|
|
|
'build\\xenia.sln',
|
|
|
|
])
|
2021-10-06 21:38:06 +00:00
|
|
|
elif sys.platform == 'darwin':
|
|
|
|
shell_call([
|
|
|
|
'xed',
|
|
|
|
'build/xenia.xcworkspace',
|
|
|
|
])
|
2020-11-08 21:28:36 +00:00
|
|
|
elif has_bin('clion'):
|
|
|
|
shell_call([
|
|
|
|
'clion',
|
|
|
|
'.',
|
|
|
|
])
|
|
|
|
elif has_bin('clion.sh'):
|
|
|
|
shell_call([
|
|
|
|
'clion.sh',
|
|
|
|
'.',
|
|
|
|
])
|
2020-03-31 23:52:15 +00:00
|
|
|
else:
|
|
|
|
shell_call([
|
|
|
|
'codelite',
|
|
|
|
'build/xenia.workspace',
|
2017-09-21 21:42:57 +00:00
|
|
|
])
|
|
|
|
print('')
|
|
|
|
|
|
|
|
return 0
|
2015-08-01 06:48:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-09-21 21:42:57 +00:00
|
|
|
main()
|
|
|
|
|