From fd3617bfef29aeb9578ad5883e9388e2d367e06a Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Mon, 15 May 2023 04:56:32 +0900 Subject: [PATCH] subprojects: Add Microsoft WebView2 SDK Add WebView2 NuGet package downloader and meson file Part-of: --- subprojects/webview2/.gitignore | 2 + subprojects/webview2/download-binary.py | 77 +++++++++++++++++++++++++ subprojects/webview2/meson.build | 36 ++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 subprojects/webview2/.gitignore create mode 100644 subprojects/webview2/download-binary.py create mode 100644 subprojects/webview2/meson.build diff --git a/subprojects/webview2/.gitignore b/subprojects/webview2/.gitignore new file mode 100644 index 0000000000..9efdfd56c3 --- /dev/null +++ b/subprojects/webview2/.gitignore @@ -0,0 +1,2 @@ +webview2-*/ +webview2-*.zip diff --git a/subprojects/webview2/download-binary.py b/subprojects/webview2/download-binary.py new file mode 100644 index 0000000000..7a9804787c --- /dev/null +++ b/subprojects/webview2/download-binary.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +import os +import sys +import ssl +import zipfile +import hashlib +import urllib.request +import urllib.error + +# Disable certificate checking because it always fails on Windows +# We verify the checksum anyway. +ctx = ssl.create_default_context() +ctx.check_hostname = False +ctx.verify_mode = ssl.CERT_NONE + +EXTRACTDIR = 'webview2-{}' +BASENAME = '{}.zip'.format(EXTRACTDIR) +BASE_URL = 'https://www.nuget.org/api/v2/package/Microsoft.Web.WebView2/{}' + +version = sys.argv[1] +zip_sha256 = sys.argv[2] +source_dir = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR']) +dest = BASENAME.format(version) +dest_path = os.path.join(source_dir, dest) +extract_path = EXTRACTDIR.format(version) + +def get_sha256(zipf): + hasher = hashlib.sha256() + with open(zipf, 'rb') as f: + hasher.update(f.read()) + return hasher.hexdigest() + +def download(): + for url in (BASE_URL.format(version),): + print('Downloading {} to {}'.format(url, dest), file=sys.stderr) + try: + with open(dest_path, 'wb') as d: + f = urllib.request.urlopen(url, context=ctx) + d.write(f.read()) + break + except urllib.error.URLError as ex: + print(ex, file=sys.stderr) + print('Failed to download from {!r}, trying mirror...'.format(url), file=sys.stderr) + continue + else: + curdir = os.path.dirname(sys.argv[0]) + print('Couldn\'t download {!r}! Try downloading it manually and ' + 'placing it into {!r}'.format(dest, curdir), file=sys.stderr) + +def print_extract_dir(): + 'Print the extracted directory name' + print(extract_path, end='', file=sys.stderr) + +if os.path.isfile(dest_path): + found_sha256 = get_sha256(dest_path) + if found_sha256 == zip_sha256: + if os.path.isdir(os.path.join(source_dir, extract_path)): + print('{} already downloaded and extracted'.format(dest), file=sys.stderr) + print_extract_dir() + sys.exit(0) + else: + print('{} checksum mismatch, redownloading'.format(dest), file=sys.stderr) + download() +else: + download() + +found_sha256 = get_sha256(dest_path) +if found_sha256 != zip_sha256: + print('SHA256 of downloaded file {} was {} instead of {}' + ''.format(dest, found_sha256, zip_sha256), file=sys.stderr) + sys.exit(1) + +print('Extracting {}', file=sys.stderr) +zf = zipfile.ZipFile(dest_path, "r") +zf.extractall(path=os.path.join(source_dir, extract_path)) +print_extract_dir() diff --git a/subprojects/webview2/meson.build b/subprojects/webview2/meson.build new file mode 100644 index 0000000000..bc7e5b5018 --- /dev/null +++ b/subprojects/webview2/meson.build @@ -0,0 +1,36 @@ +project('webview2', version : '1.0.2420.47') + +py3 = import('python3').find_python() + +if host_machine.system() != 'windows' + error('Can only download webview2 for Windows') +endif + +message('Downloading and extracting webview2 nuget package') + +arch = target_machine.cpu_family() +if arch != 'x86_64' and arch != 'x86' and arch != 'aarch64' + error('Unexpected architecture' + arch) +endif + +zip_hash = '8e5a7307d71507edbbe02cac27215d71058bbd82cd256cef60f06b945907610a' +version = meson.project_version() +ret = run_command(py3, files('download-binary.py'), meson.project_version(), zip_hash, + check: true) + +base_path = join_paths(meson.current_source_dir(), 'webview2-@0@'.format(version), + 'build', 'native') +inc_dir = include_directories(join_paths(base_path, 'include')) +lib_path = '' +if arch == 'x86_64' + lib_path = join_paths(base_path, 'x64') +elif arch == 'x86' + lib_path = join_paths(base_path, 'x86') +else + lib_path = join_paths(base_path, 'arm64') +endif + +cc = meson.get_compiler('c') +loader_static = cc.find_library('WebView2LoaderStatic', dirs: [lib_path]) +webview2_dep = declare_dependency(include_directories: inc_dir, + dependencies: loader_static)