build_web.py 4.13 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import argparse
import os
import shutil
import subprocess
import sys
from os.path import abspath, dirname

CURRENT_FOLDER = abspath(dirname(__file__))
ROOT_DIR = os.path.normpath(os.path.join(CURRENT_FOLDER, "../../"))

BUILD_SCRIPT = "./build.sh"
if sys.platform.startswith("win"):
    BUILD_SCRIPT = "build.bat"

DEFAULT_FLAGS = " --build_wasm --skip_tests --parallel "
BINARIES_DIR = os.path.normpath(os.path.join(ROOT_DIR, "./build/"))
DIST_PATH = os.path.normpath(os.path.join(ROOT_DIR, "./js/web/dist/"))
BINDING_PATH = os.path.normpath(os.path.join(ROOT_DIR, "./js/web/lib/wasm/binding/"))

BUILDS = [
    {"dir": "wasm", "wasm_flags": "", "wasm_file_name": "ort-wasm.wasm"},
    {"dir": "wasm_SIMD", "wasm_flags": "--enable_wasm_simd", "wasm_file_name": "ort-wasm-simd.wasm"},
    {"dir": "wasm_threaded", "wasm_flags": "--enable_wasm_threads", "wasm_file_name": "ort-wasm-threaded.wasm"},
    {
        "dir": "wasm_SIMD_threaded",
        "wasm_flags": "--enable_wasm_simd --enable_wasm_threads ",
        "wasm_file_name": "ort-wasm-simd-threaded.wasm",
    },
]

JS_FILES = [
    {"dir": "wasm", "file_name": "ort-wasm.js"},
    {"dir": "wasm_SIMD_threaded", "file_name": "ort-wasm-simd-threaded.worker.js"},
    {"dir": "wasm_SIMD_threaded", "file_name": "ort-wasm-simd-threaded.js"},
]

NPM_BUILD_DIR = [
    {"command": "npm ci", "path": os.path.normpath(os.path.join(CURRENT_FOLDER, "../"))},
    {"command": "npm ci", "path": os.path.normpath(os.path.join(CURRENT_FOLDER, "../common"))},
    {"command": "npm ci", "path": os.path.normpath(os.path.join(CURRENT_FOLDER, "../web"))},
    {"command": "npm run build", "path": os.path.normpath(os.path.join(CURRENT_FOLDER, "../web"))},
]

# Generating the build folders
if not os.path.isdir(BINARIES_DIR):
    os.mkdir(BINARIES_DIR)

for entry in BUILDS:
    dirctory_name = os.path.join(BINARIES_DIR, entry["dir"])
    if not os.path.isdir(dirctory_name):
        os.mkdir(dirctory_name)

if not os.path.isdir(DIST_PATH):
    os.mkdir(DIST_PATH)

if not os.path.isdir(BINDING_PATH):
    os.mkdir(BINDING_PATH)

# Running the WASM build commands
parser = argparse.ArgumentParser()
parser.add_argument(
    "--config",
    choices=["Release", "Debug", "RelWithDebInfo"],
    default="Release",
    help="build WASM artifactsfor the configuration - {Release,Debug,RelWithDebInfo}",
)

args = parser.parse_args()
CONFIGURATION = args.config

for entry in BUILDS:
    command = (
        BUILD_SCRIPT
        + " --config "
        + CONFIGURATION
        + " "
        + DEFAULT_FLAGS
        + " --build_dir "
        + os.path.join('"' + BINARIES_DIR, entry["dir"] + '"')
        + " "
        + entry["wasm_flags"]
    )

    print(command)

    with subprocess.Popen(command, shell=True, cwd=ROOT_DIR) as p:
        p.wait()
        if not os.path.exists(os.path.join(BINARIES_DIR, entry["dir"], CONFIGURATION, entry["wasm_file_name"])) and (
            p.returncode != 0
        ):
            print("Error - can find " + entry["wasm_file_name"] + " there might be an issue with the build\n")
            sys.exit()

# Copying WASM artifacts
for entry in BUILDS:
    if not os.path.exists(os.path.join(BINARIES_DIR, entry["dir"], CONFIGURATION, entry["wasm_file_name"])):
        print("Error - can find " + entry["wasm_file_name"] + " there might be an issue with the build\n")
        sys.exit()
    shutil.copyfile(
        os.path.join(BINARIES_DIR, entry["dir"], CONFIGURATION, entry["wasm_file_name"]),
        os.path.join(DIST_PATH, entry["wasm_file_name"]),
    )

# Copying JS binding files
for entry in JS_FILES:
    if not os.path.exists(os.path.join(BINARIES_DIR, entry["dir"], CONFIGURATION, entry["file_name"])):
        print("Error - can find " + entry["file_name"] + " there might be an issue with the build\n")
        sys.exit()
    shutil.copyfile(
        os.path.join(BINARIES_DIR, entry["dir"], CONFIGURATION, entry["file_name"]),
        os.path.join(BINDING_PATH, entry["file_name"]),
    )

# Build NPM package
for entry in NPM_BUILD_DIR:
    with subprocess.Popen(entry["command"], shell=True, cwd=entry["path"]) as p:
        p.wait()