Files
Anthony Tseng 73c1f6462f Move WASM crates next to their consumer components (#34574)
* Move opaque_ke WASM crate next to its consumer component

Move opaque_ke from third_party/wasm/ to
components/brave_account/resources/opaque_ke/ so it lives
next to the component that consumes it.

- Move crate directory with index.js as-is
- Add workspace key to Cargo.toml for workspace discovery
- Update .cargo/config.toml vendor path
- Add transpile_web_ui target in resources/opaque_ke/BUILD.gn
- Update workspace Cargo.toml member path
- Update update.py to use make_config_toml for relative vendor paths

* Move candle_embedding_gemma WASM crate next to its consumer component

Move candle_embedding_gemma from third_party/wasm/ to
components/local_ai/resources/candle_embedding_gemma/ so it lives
next to the component that consumes it.

- Move crate directory
- Add workspace key to Cargo.toml for workspace discovery
- Update .cargo/config.toml vendor path
- Update workspace Cargo.toml member path
- Update update.py PRESERVE_PATTERNS for new config location
2026-03-10 11:06:21 +00:00

104 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env vpython3
# Copyright (c) 2025 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
# update.py vendors the deps of the Rust workspace in `wasm`.
# Run this script via npm run update_wasm_resources
# whenever you need to add a new workspace member,
# or bump the version of an existing member's deps.
import os
from pathlib import Path
import shutil
import subprocess
import sys
import toml
import brave_chromium_utils
PRESERVE_PATTERNS = [
'vendor/.clang-format',
'vendor/*/README.chromium',
]
CLEANUP_PATTERNS = [
'vendor/**/*.o',
'vendor/**/*.dll',
]
def back_up_files(patterns):
backed_up_files = {}
for pattern in patterns:
for path in Path().glob(pattern):
print(f'Backing up: {path}')
backed_up_files[str(path)] = path.read_text(encoding='utf-8')
return backed_up_files
def restore_files(backed_up_files):
for path_str, content in backed_up_files.items():
path = Path(path_str)
print(f'Restoring: {path}')
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, 'w', encoding='utf-8', newline='\n') as f:
f.write(content)
def clean_up_files(patterns):
for pattern in patterns:
for path in Path().glob(pattern):
print(f'Removing: {path}')
path.unlink()
def make_config_toml(member):
"""Build a cargo config that points to the shared vendor dir."""
vendor_rel = os.path.relpath('vendor', member)
vendor_rel = vendor_rel.replace(os.sep, '/')
return {
'source': {
'crates-io': {
'replace-with': 'vendored-sources'
},
'vendored-sources': {
'directory': vendor_rel
}
}
}
with brave_chromium_utils.sys_path('//tools/rust'):
import update_rust
CARGO = os.path.join(update_rust.RUST_TOOLCHAIN_OUT_DIR, 'bin',
'cargo' + ('.exe' if sys.platform == 'win32' else ''))
def main():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
members = toml.load('Cargo.toml')['workspace']['members']
preserve = ([f'{m}/.cargo/config.toml'
for m in members] + PRESERVE_PATTERNS)
backed_up_files = back_up_files(preserve)
shutil.rmtree('vendor', ignore_errors=True)
for member in members:
Path(f'{member}/.cargo/config.toml').unlink(missing_ok=True)
subprocess.run([CARGO, 'vendor'], check=True)
for member in members:
Path(f'{member}/.cargo').mkdir(exist_ok=True)
with open(Path(f'{member}/.cargo/config.toml'), 'w') as f:
toml.dump(make_config_toml(member), f)
restore_files(backed_up_files)
clean_up_files(CLEANUP_PATTERNS)
if __name__ == '__main__':
main()