def build_bergamot()

in inference/scripts/build-wasm.py [0:0]


def build_bergamot(args: Optional[list[str]]):
    if args.clobber and os.path.exists(BUILD_PATH):
        shutil.rmtree(BUILD_PATH)

    if not os.path.exists(BUILD_PATH):
        os.mkdir(BUILD_PATH)

    print("\nšŸ–Œļø  Applying source code patches\n")
    for repo_path, patch_path in patches:
        apply_git_patch(repo_path, patch_path)

    # These commands require the emsdk environment variables to be set up.
    def run_shell(command):
        if '"' in command or "'" in command:
            raise Exception("This run_shell utility does not support quotes.")

        return subprocess.run(
            # "source" is not available in all shells so explicitly
            f"bash -c 'source {EMSDK_ENV_PATH} && {command}'",
            cwd=BUILD_PATH,
            shell=True,
            check=True,
        )

    try:
        flags = ""
        if args.debug:
            flags = "-DCMAKE_BUILD_TYPE=RelWithDebInfo"

        print("\nšŸƒ Running CMake for Bergamot\n")
        run_shell(f"emcmake cmake -DCOMPILE_WASM=on -DWORMHOLE=off {flags} {INFERENCE_PATH}")

        if args.j:
            # If -j is specified explicitly, use it.
            cores = args.j
        elif os.getenv("HOST_OS") == "Darwin":
            # There is an issue building with multiple cores when the Linux Docker container is
            # running on a macOS host system. If the Docker container was created with HOST_OS
            # set to Darwin, we should use only 1 core to build.
            cores = 1
        else:
            # Otherwise, build with as many cores as we have.
            cores = multiprocessing.cpu_count()

        print(f"\nšŸƒ Building Bergamot with emmake using {cores} cores\n")

        try:
            run_shell(f"emmake make -j {cores}")
        except:
            print(f"āŒ Build failed with {cores} cores.")
            print("This has been known to occur on macOS AArch64.\n")
            print("Please try running again with -j 1.")
            raise

        print("\n🪚  Patching Bergamot for gemm support\n")
        subprocess.check_call(["bash", GEMM_SCRIPT, BUILD_PATH])

        print("\nāœ… Build complete\n")
        print("  " + JS_ARTIFACT)
        print("  " + WASM_ARTIFACT)

        # Get the sizes of the build artifacts.
        wasm_size = os.path.getsize(WASM_ARTIFACT)
        gzip_size = int(
            subprocess.run(
                f"gzip -c {WASM_ARTIFACT} | wc -c",
                check=True,
                shell=True,
                capture_output=True,
            ).stdout.strip()
        )
        print(f"  Uncompressed wasm size: {to_human_readable(wasm_size)}")
        print(f"  Compressed wasm size: {to_human_readable(gzip_size)}")

        prepare_js_artifact()

    finally:
        print("\nšŸ–Œļø  Reverting the source code patches\n")
        for repo_path, patch_path in patches[::-1]:
            revert_git_patch(repo_path, patch_path)