Merge branch 'master' into pubsync

This commit is contained in:
Eric Froemling 2019-10-07 05:40:07 -07:00
commit 6f85856616
2 changed files with 277 additions and 266 deletions

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2019 Eric Froemling
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -31,22 +31,6 @@ CLRRED = '\033[91m' # Red.
CLREND = '\033[0m' # End. CLREND = '\033[0m' # End.
def _find_files(scan_dir: str) -> Tuple[List[str], List[str]]:
src_files = set()
header_files = set()
exts = ['.c', '.cc', '.cpp', '.cxx', '.m', '.mm']
header_exts = ['.h']
# Gather all sources and headers.
for root, _dirs, files in os.walk(scan_dir):
for ftst in files:
if any(ftst.endswith(ext) for ext in exts):
src_files.add(os.path.join(root, ftst)[len(scan_dir):])
if any(ftst.endswith(ext) for ext in header_exts):
header_files.add(os.path.join(root, ftst)[len(scan_dir):])
return sorted(src_files), sorted(header_files)
def _check_files(src_files: Sequence[str]) -> None: def _check_files(src_files: Sequence[str]) -> None:
# A bit of sanity/lint-testing while we're here. # A bit of sanity/lint-testing while we're here.
@ -109,289 +93,297 @@ def _check_headers(header_files: Sequence[str], fixable_header_errors: Dict,
sys.exit(255) sys.exit(255)
def _update_cmake_file(fname: str, src_files: List[str], class App:
header_files: List[str], """Context for an app run."""
files_to_write: Dict[str, str]) -> None:
with open(fname) as infile: def __init__(self) -> None:
lines = infile.read().splitlines() self._check = ('--check' in sys.argv)
auto_start = lines.index(' #AUTOGENERATED_BEGIN (this section' self._fix = ('--fix' in sys.argv)
' is managed by the "update_project" tool)') self._checkarg = ' --check' if self._check else ''
auto_end = lines.index(' #AUTOGENERATED_END')
our_lines = [
' ${BA_SRC_ROOT}/ballistica' + f
for f in sorted(src_files + header_files)
if not f.endswith('.mm') and not f.endswith('.m')
]
filtered = lines[:auto_start + 1] + our_lines + lines[auto_end:]
files_to_write[fname] = '\n'.join(filtered) + '\n'
self._files_to_write: Dict[str, str] = {}
self._src_files: List[str] = []
self._header_files: List[str] = []
self._fixable_header_errors: Dict[str, List[Tuple[int, str]]] = {}
def _update_visual_studio_project(fname: str, src_root: str, def run(self) -> None:
src_files: List[str], """Do the thing."""
header_files: List[str],
files_to_write: Dict[str, str]) -> None:
# pylint: disable=too-many-locals
with open(fname) as infile:
lines = infile.read().splitlines()
# Hmm can we include headers in the project for easy access? # pylint: disable=too-many-branches
# Seems VS attempts to compile them if we do so here. # pylint: disable=too-many-statements
# all_files = sorted(src_files + header_files)
# del header_files # Unused.
all_files = sorted([
f for f in (src_files + header_files) if not f.endswith('.m')
and not f.endswith('.mm') and not f.endswith('.c')
])
# Find the ItemGroup containing stdafx.cpp. This is where we'll dump # Make sure we're operating from a project root.
# our stuff. if not os.path.isdir('config') or not os.path.isdir('tools'):
index = lines.index(' <ClCompile Include="stdafx.cpp">') raise Exception('This must be run from a project root.')
begin_index = end_index = index
while lines[begin_index] != ' <ItemGroup>':
begin_index -= 1
while lines[end_index] != ' </ItemGroup>':
end_index += 1
group_lines = lines[begin_index + 1:end_index]
# Strip out any existing files from src/ballistica. # NOTE: Do py-enums before updating asset deps since it is an asset.
group_lines = [ self._update_python_enums_module()
l for l in group_lines if src_root + '\\ballistica\\' not in l self._update_resources_makefile()
] self._update_generated_code_makefile()
self._update_assets_makefile()
# Now add in our own. self._check_python_files()
# Note: we can't use C files in this build at the moment; breaks self._check_sync_states()
# precompiled header stuff. (shouldn't be a problem though).
group_lines = [
' <' +
('ClInclude' if src.endswith('.h') else 'ClCompile') + ' Include="' +
src_root + '\\ballistica' + src.replace('/', '\\') + '" />'
for src in all_files
] + group_lines
filtered = lines[:begin_index + 1] + group_lines + lines[end_index:]
files_to_write[fname] = '\r\n'.join(filtered) + '\r\n'
filterpaths: Set[str] = set() # Now go through and update our various build files
filterlines: List[str] = [ # (MSVC, CMake, Android NDK, etc) as well as sanity-testing/fixing
'<?xml version="1.0" encoding="utf-8"?>', # various other files such as headers while we're at it.
'<Project ToolsVersion="4.0"'
' xmlns="http://schemas.microsoft.com/developer/msbuild/2003">',
' <ItemGroup>',
]
sourcelines = [l for l in filtered if 'Include="' + src_root in l]
for line in sourcelines:
entrytype = line.strip().split()[0][1:]
path = line.split('"')[1]
filterlines.append(' <' + entrytype + ' Include="' + path + '">')
# If we have a dir foo/bar/eep we need to create filters for # Grab sources/headers.
# each of foo, foo/bar, and foo/bar/eep self._find_sources_and_headers('src/ballistica')
splits = path[len(src_root):].split('\\')
splits = [s for s in splits if s != '']
splits = splits[:-1]
for i in range(len(splits)):
filterpaths.add('\\'.join(splits[:(i + 1)]))
filterlines.append(' <Filter>' + '\\'.join(splits) + '</Filter>')
filterlines.append(' </' + entrytype + '>')
filterlines += [
' </ItemGroup>',
' <ItemGroup>',
]
for filterpath in sorted(filterpaths):
filterlines.append(' <Filter Include="' + filterpath + '" />')
filterlines += [
' </ItemGroup>',
'</Project>',
]
files_to_write[fname + '.filters'] = '\r\n'.join(filterlines) + '\r\n' # Run some checks on them.
_check_files(self._src_files)
_check_headers(self._header_files, self._fixable_header_errors,
self._fix)
self._update_cmake_files()
self._update_visual_studio_projects()
def main() -> None: # If we're all good to here, do the actual writes.
"""Main script entry point."""
# pylint: disable=too-many-locals # First, write any header fixes.
# pylint: disable=too-many-branches if self._fixable_header_errors:
# pylint: disable=too-many-statements for filename, fixes in self._fixable_header_errors.items():
with open(filename, 'r') as infile:
lines = infile.read().splitlines()
for fix_line, fix_str in fixes:
lines[fix_line] = fix_str
with open(filename, 'w') as outfile:
outfile.write('\n'.join(lines) + '\n')
print(CLRBLU + 'Writing header: ' + filename + CLREND)
else:
print(f'No issues found in {len(self._header_files)} headers.')
# Make sure we're operating from dist root. # Now write out any project files that have changed
os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '..')) # (or error if we're in check mode).
unchanged_project_count = 0
for fname, fcode in self._files_to_write.items():
f_orig: Optional[str]
if os.path.exists(fname):
with open(fname, 'r') as infile:
f_orig = infile.read()
else:
f_orig = None
if f_orig == fcode.replace('\r\n', '\n'):
unchanged_project_count += 1
else:
if self._check:
print(f'{CLRRED}ERROR: found out-of-date'
f' project file: {fname}{CLREND}')
sys.exit(255)
check = ('--check' in sys.argv) print(f'{CLRBLU}Writing project file: {fname}{CLREND}')
fix = ('--fix' in sys.argv) with open(fname, 'w') as outfile:
outfile.write(fcode)
if unchanged_project_count > 0:
print(f'All {unchanged_project_count} project files up to date.')
checkarg = ' --check' if check else '' # Update our local compile-commands file based on any changes to
# our cmake stuff. Do this at end so cmake changes already happened.
# Update our python enums module. if not self._check and os.path.exists('ballisticacore-cmake'):
# Should do this before updating asset deps since this is an asset. if os.system('make .irony/compile_commands.json') != 0:
if os.path.exists('tools/update_python_enums_module'): print(CLRRED + 'Error updating compile-commands.' + CLREND)
if os.system('tools/update_python_enums_module' + checkarg) != 0:
print(CLRRED + 'Error checking/updating python enums module' +
CLREND)
sys.exit(255)
# Update our resources Makefile.
if os.path.exists('tools/update_resources_makefile'):
if os.system('tools/update_resources_makefile' + checkarg) != 0:
print(CLRRED + 'Error checking/updating resources Makefile' +
CLREND)
sys.exit(255)
# Update our generated-code Makefile.
if os.path.exists('tools/update_generated_code_makefile'):
if os.system('tools/update_generated_code_makefile' + checkarg) != 0:
print(CLRRED + 'Error checking/updating generated-code Makefile' +
CLREND)
sys.exit(255)
# Update our assets Makefile.
if os.path.exists('tools/update_assets_makefile'):
if os.system('tools/update_assets_makefile' + checkarg) != 0:
print(CLRRED + 'Error checking/updating assets Makefile' + CLREND)
sys.exit(255)
# Make sure all module dirs in python scripts contain an __init__.py
scripts_dir = 'assets/src/data/scripts'
for root, _dirs, files in os.walk(scripts_dir):
if (root != scripts_dir and '__pycache__' not in root
and os.path.basename(root) != '.vscode'):
if '__init__.py' not in files:
print(CLRRED + 'Error: no __init__.py in package dir: ' +
root + CLREND)
sys.exit(255) sys.exit(255)
# Make sure none of our sync targets have been mucked with since # Lastly update our dummy _ba module.
# their last sync. # We need to do this very last because it may run the cmake build
if os.system('tools/snippets sync check') != 0: # so its success may depend on the cmake build files having already
print(CLRRED + 'Sync check failed; you may need to run "sync".' + # been updated.
CLREND) if os.path.exists('tools/gendummymodule.py'):
sys.exit(255) if os.system('tools/gendummymodule.py' + self._checkarg) != 0:
print(CLRRED + 'Error checking/updating dummy module' + CLREND)
sys.exit(255)
# Now go through and update our various build files if self._check:
# (MSVC, CMake, Android NDK, etc) as well as sanity-testing/fixing print('Check-Builds: Everything up to date.')
# various other files such as headers while we're at it. else:
print('Update-Builds: SUCCESS!')
files_to_write: Dict[str, str] = {} def _update_visual_studio_project(self, fname: str, src_root: str) -> None:
# pylint: disable=too-many-locals
with open(fname) as infile:
lines = infile.read().splitlines()
# Grab sources/headers. # Hmm can we include headers in the project for easy access?
src_files, header_files = _find_files('src/ballistica') # Seems VS attempts to compile them if we do so here.
# all_files = sorted(src_files + header_files)
# del header_files # Unused.
all_files = sorted([
f for f in (self._src_files + self._header_files)
if not f.endswith('.m') and not f.endswith('.mm')
and not f.endswith('.c')
])
# Run some checks on them. # Find the ItemGroup containing stdafx.cpp. This is where we'll dump
_check_files(src_files) # our stuff.
fixable_header_errors: Dict[str, List[Tuple[int, str]]] = {} index = lines.index(' <ClCompile Include="stdafx.cpp">')
_check_headers(header_files, fixable_header_errors, fix) begin_index = end_index = index
while lines[begin_index] != ' <ItemGroup>':
begin_index -= 1
while lines[end_index] != ' </ItemGroup>':
end_index += 1
group_lines = lines[begin_index + 1:end_index]
# Now update various builds. # Strip out any existing files from src/ballistica.
group_lines = [
l for l in group_lines if src_root + '\\ballistica\\' not in l
]
# CMake # Now add in our own.
fname = 'ballisticacore-cmake/CMakeLists.txt' # Note: we can't use C files in this build at the moment; breaks
if os.path.exists(fname): # precompiled header stuff. (shouldn't be a problem though).
_update_cmake_file( group_lines = [
fname, ' <' +
src_files, ('ClInclude' if src.endswith('.h') else 'ClCompile') + ' Include="'
header_files, + src_root + '\\ballistica' + src.replace('/', '\\') + '" />'
files_to_write, for src in all_files
) ] + group_lines
fname = 'ballisticacore-android/BallisticaCore/src/main/cpp/CMakeLists.txt' filtered = lines[:begin_index + 1] + group_lines + lines[end_index:]
if os.path.exists(fname): self._files_to_write[fname] = '\r\n'.join(filtered) + '\r\n'
_update_cmake_file(
fname,
src_files,
header_files,
files_to_write,
)
# Visual Studio filterpaths: Set[str] = set()
fname = 'ballisticacore-windows/BallisticaCore/BallisticaCore.vcxproj' filterlines: List[str] = [
if os.path.exists(fname): '<?xml version="1.0" encoding="utf-8"?>',
_update_visual_studio_project( '<Project ToolsVersion="4.0"'
fname, ' xmlns="http://schemas.microsoft.com/developer/msbuild/2003">',
'..\\..\\src', ' <ItemGroup>',
src_files, ]
header_files, sourcelines = [l for l in filtered if 'Include="' + src_root in l]
files_to_write, for line in sourcelines:
) entrytype = line.strip().split()[0][1:]
fname = ('ballisticacore-windows/BallisticaCoreHeadless/' path = line.split('"')[1]
'BallisticaCoreHeadless.vcxproj') filterlines.append(' <' + entrytype + ' Include="' + path +
if os.path.exists(fname): '">')
_update_visual_studio_project(
fname,
'..\\..\\src',
src_files,
header_files,
files_to_write,
)
fname = ('ballisticacore-windows/BallisticaCoreOculus'
'/BallisticaCoreOculus.vcxproj')
if os.path.exists(fname):
_update_visual_studio_project(
fname,
'..\\..\\src',
src_files,
header_files,
files_to_write,
)
# If we're all good to here, do the actual writes. # If we have a dir foo/bar/eep we need to create filters for
# each of foo, foo/bar, and foo/bar/eep
splits = path[len(src_root):].split('\\')
splits = [s for s in splits if s != '']
splits = splits[:-1]
for i in range(len(splits)):
filterpaths.add('\\'.join(splits[:(i + 1)]))
filterlines.append(' <Filter>' + '\\'.join(splits) +
'</Filter>')
filterlines.append(' </' + entrytype + '>')
filterlines += [
' </ItemGroup>',
' <ItemGroup>',
]
for filterpath in sorted(filterpaths):
filterlines.append(' <Filter Include="' + filterpath + '" />')
filterlines += [
' </ItemGroup>',
'</Project>',
]
# First, write any header fixes. self._files_to_write[fname +
if fixable_header_errors: '.filters'] = '\r\n'.join(filterlines) + '\r\n'
for filename, fixes in fixable_header_errors.items():
with open(filename, 'r') as infile:
lines = infile.read().splitlines()
for fix_line, fix_str in fixes:
lines[fix_line] = fix_str
with open(filename, 'w') as outfile:
outfile.write('\n'.join(lines) + '\n')
print(CLRBLU + 'Writing header: ' + filename + CLREND)
else:
print(f'No issues found in {len(header_files)} headers.')
# Now write out any project files that have changed def _update_visual_studio_projects(self) -> None:
# (or error if we're in check mode). fname = 'ballisticacore-windows/BallisticaCore/BallisticaCore.vcxproj'
unchanged_project_count = 0
for fname, fcode in files_to_write.items():
f_orig: Optional[str]
if os.path.exists(fname): if os.path.exists(fname):
with open(fname, 'r') as infile: self._update_visual_studio_project(fname, '..\\..\\src')
f_orig = infile.read() fname = ('ballisticacore-windows/BallisticaCoreHeadless/'
else: 'BallisticaCoreHeadless.vcxproj')
f_orig = None if os.path.exists(fname):
if f_orig == fcode.replace('\r\n', '\n'): self._update_visual_studio_project(fname, '..\\..\\src')
unchanged_project_count += 1 fname = ('ballisticacore-windows/BallisticaCoreOculus'
else: '/BallisticaCoreOculus.vcxproj')
if check: if os.path.exists(fname):
print(f'{CLRRED}ERROR: found out-of-date' self._update_visual_studio_project(fname, '..\\..\\src')
f' project file: {fname}{CLREND}')
def _update_cmake_file(self, fname: str) -> None:
with open(fname) as infile:
lines = infile.read().splitlines()
auto_start = lines.index(' #AUTOGENERATED_BEGIN (this section'
' is managed by the "update_project" tool)')
auto_end = lines.index(' #AUTOGENERATED_END')
our_lines = [
' ${BA_SRC_ROOT}/ballistica' + f
for f in sorted(self._src_files + self._header_files)
if not f.endswith('.mm') and not f.endswith('.m')
]
filtered = lines[:auto_start + 1] + our_lines + lines[auto_end:]
self._files_to_write[fname] = '\n'.join(filtered) + '\n'
def _update_cmake_files(self) -> None:
fname = 'ballisticacore-cmake/CMakeLists.txt'
if os.path.exists(fname):
self._update_cmake_file(fname)
fname = ('ballisticacore-android/BallisticaCore'
'/src/main/cpp/CMakeLists.txt')
if os.path.exists(fname):
self._update_cmake_file(fname)
def _find_sources_and_headers(self, scan_dir: str) -> None:
src_files = set()
header_files = set()
exts = ['.c', '.cc', '.cpp', '.cxx', '.m', '.mm']
header_exts = ['.h']
# Gather all sources and headers.
for root, _dirs, files in os.walk(scan_dir):
for ftst in files:
if any(ftst.endswith(ext) for ext in exts):
src_files.add(os.path.join(root, ftst)[len(scan_dir):])
if any(ftst.endswith(ext) for ext in header_exts):
header_files.add(os.path.join(root, ftst)[len(scan_dir):])
self._src_files = sorted(src_files)
self._header_files = sorted(header_files)
def _check_python_files(self) -> None:
# Make sure all module dirs in python scripts contain an __init__.py
scripts_dir = 'assets/src/data/scripts'
for root, _dirs, files in os.walk(scripts_dir):
if (root != scripts_dir and '__pycache__' not in root
and os.path.basename(root) != '.vscode'):
if '__init__.py' not in files:
print(CLRRED + 'Error: no __init__.py in package dir: ' +
root + CLREND)
sys.exit(255)
def _check_sync_states(self) -> None:
# Make sure none of our sync targets have been mucked with since
# their last sync.
if os.system('tools/snippets sync check') != 0:
print(CLRRED + 'Sync check failed; you may need to run "sync".' +
CLREND)
sys.exit(255)
def _update_assets_makefile(self) -> None:
if os.path.exists('tools/update_assets_makefile'):
if os.system('tools/update_assets_makefile' + self._checkarg) != 0:
print(CLRRED + 'Error checking/updating assets Makefile' +
CLREND)
sys.exit(255) sys.exit(255)
print(f'{CLRBLU}Writing project file: {fname}{CLREND}') def _update_generated_code_makefile(self) -> None:
with open(fname, 'w') as outfile: if os.path.exists('tools/update_generated_code_makefile'):
outfile.write(fcode) if os.system('tools/update_generated_code_makefile' +
if unchanged_project_count > 0: self._checkarg) != 0:
print(f'All {unchanged_project_count} project files up to date.') print(CLRRED +
'Error checking/updating generated-code Makefile' +
CLREND)
sys.exit(255)
# Update our local compile-commands file based on any changes to def _update_resources_makefile(self) -> None:
# our cmake stuff. Do this at end so cmake changes already happened. if os.path.exists('tools/update_resources_makefile'):
if not check and os.path.exists('ballisticacore-cmake'): if os.system('tools/update_resources_makefile' +
if os.system('make .irony/compile_commands.json') != 0: self._checkarg) != 0:
print(CLRRED + 'Error updating compile-commands.' + CLREND) print(CLRRED + 'Error checking/updating resources Makefile' +
sys.exit(255) CLREND)
sys.exit(255)
# Lastly update our dummy _ba module. def _update_python_enums_module(self) -> None:
# We need to do this very last because it may run the cmake build if os.path.exists('tools/update_python_enums_module'):
# so its success may depend on the cmake build files having already if os.system('tools/update_python_enums_module' +
# been updated. self._checkarg) != 0:
if os.path.exists('tools/gendummymodule.py'): print(CLRRED + 'Error checking/updating python enums module' +
if os.system('tools/gendummymodule.py' + checkarg) != 0: CLREND)
print(CLRRED + 'Error checking/updating dummy module' + CLREND) sys.exit(255)
sys.exit(255)
if check:
print('Check-Builds: Everything up to date.')
else:
print('Update-Builds: SUCCESS!')
if __name__ == '__main__': if __name__ == '__main__':
main() App().run()