build: Add script to install/upgrade websocketpp

This commit is contained in:
John Colvin committed 2023-06-26 14:56:55 +00:00
1 parent b27dc4bc67
commit 9a41237fc0
2 files changed
+123 -2

No files matched your search

-2
View File
@@ -1,2 +0,0 @@
init_target("websocketpp")
final_target ()
+123
View File
@@ -0,0 +1,123 @@
from argparse import ArgumentParser
from pathlib import Path
import subprocess
from zipfile import ZipFile
import platform
import requests
import shutil
def _get_arg_parser():
parser = ArgumentParser(description='Installs CLI11')
parser.add_argument(
'--version',
type=str,
nargs='?',
help='The version of CLI11 to install',
default='Debug')
parser.set_defaults(version='0.8.2')
return parser
def _rm(path: Path):
if not path.exists():
return
if path.is_dir():
shutil.rmtree(path)
else:
path.unlink()
def _mk_dir(path: Path, clean=False):
if clean:
_rm(path)
path.mkdir(parents=True, exist_ok=True)
def _find_ancestor_dir(name, path=Path(__file__).parent):
def is_match():
return path.is_dir() and path.name == name
if is_match():
return path
while path != path.parent:
path = path.parent
if is_match():
return path
return None
def _download_source_files(version: str, dest: Path):
zip_file_name = f'{version}.zip'
url = f'https://github.com/zaphoyd/websocketpp/archive/refs/tags/{zip_file_name}'
print(f"Downloading source files from '{url}'")
res = requests.get(url)
if res.status_code != 200:
print('Download failed')
return False
open(zip_file_name, 'wb').write(res.content)
_mk_dir(dest, clean=True)
with ZipFile(zip_file_name, 'r') as zip_ref:
zip_ref.extractall(dest)
Path(zip_file_name).unlink()
return True
def _run(cmd: str, echo=True, check_res=True):
if echo:
print(f'cmd: {cmd}')
res = subprocess.run(cmd, shell=True)
if check_res:
res.check_returncode()
return res
def _configure_cmd(cmake_dir: Path, build_dir: Path, install_dir: Path):
cmd_parts = [
f'cmake',
f'-DCMAKE_INSTALL_PREFIX={install_dir}',
f'-DCMAKE_CXX_STANDARD=20',
f'-B {build_dir}',
f'-S {cmake_dir}',
]
return ' '.join(cmd_parts)
def _build_cmd(build_dir: Path):
cmd_parts = [
f'cmake',
f'--build {build_dir}',
f'--target install',
]
return ' '.join(cmd_parts)
def _install(cmake_dir: Path, include_out_dir: Path):
install_dir = cmake_dir.joinpath('install')
build_dir = cmake_dir.joinpath('build')
_run(_configure_cmd(cmake_dir, build_dir, install_dir))
_run(_build_cmd(build_dir))
_rm(include_out_dir)
include_dir = install_dir.joinpath('include', 'websocketpp')
include_dir.rename(include_out_dir)
_rm(install_dir)
_rm(build_dir)
def _main():
parser = _get_arg_parser()
args = parser.parse_args()
work_dir = Path(__file__).parent.joinpath('work').absolute()
if not _download_source_files(args.version, work_dir):
return
cmake_dir = work_dir.joinpath(f'websocketpp-{args.version}')
include_out_dir = _find_ancestor_dir('externals').joinpath(
platform.system().lower(), 'include', 'websocketpp')
_rm(include_out_dir)
_install(cmake_dir, include_out_dir)
_rm(work_dir)
if __name__ == '__main__':
_main()