v / cmd / tools / install_binaryen.vsh
74 lines · 61 sloc · 1.89 KB · 387af74302fbfdd3d8ad980e334fc20e9372b3fe
Raw
1#!/usr/bin/env -S v -raw-vsh-tmp-prefix tmp
2
3import os
4import net.http
5
6const github_job = os.getenv('GITHUB_JOB')
7
8struct JQ {
9 tag_name string
10}
11
12fn main() {
13 root := os.real_path(os.dir(os.getenv_opt('VEXE') or { @VEXE }))
14 os.chdir(root)! // make sure that the workfolder is stable
15
16 tloc := os.join_path(root, 'thirdparty')
17 loc := os.join_path(tloc, 'binaryen')
18
19 if os.exists(loc) {
20 eprintln('thirdparty/binaryen exists, will not overwrite')
21 eprintln('delete the folder, and execute again')
22 exit(1)
23 }
24
25 /*
26 // TODO: add retries here, github requests can fail
27 jq := http.get_text('https://api.github.com/repos/WebAssembly/binaryen/releases/latest')
28 tag := json.decode(JQ, jq)!.tag_name
29 if github_job != '' {
30 dump(jq)
31 dump(tag)
32 }
33 */
34 tag := 'version_112'
35
36 name := $if windows {
37 'x86_64-windows'
38 } $else $if macos {
39 $if arm64 {
40 'arm64-macos'
41 } $else {
42 'x86_64-macos'
43 }
44 } $else $if linux {
45 'x86_64-linux'
46 } $else {
47 eprintln('A premade binary library is not available for your system.')
48 eprintln('Build it from source, following the documentation here: https://github.com/WebAssembly/binaryen/#building')
49 exit(1)
50 }
51
52 fname := 'binaryen-${tag}'
53 url := 'https://github.com/WebAssembly/binaryen/releases/download/${tag}/${fname}-${name}.tar.gz'
54
55 saveloc := os.join_path(tloc, '${fname}.tar.gz')
56 if !os.exists(saveloc) {
57 println('Downloading archive: ${saveloc}, from url: ${url} ...')
58 http.download_file(url, saveloc)!
59 // defer { os.rm(saveloc) or {}! }
60 }
61
62 println('Extracting `${tloc}/${fname}` to `${tloc}/binaryen` ...')
63 cmd := 'tar -xvf ${saveloc} --directory ${tloc}'
64 if os.system(cmd) != 0 {
65 eprintln('`${cmd}` exited with a non zero exit code')
66 exit(1)
67 }
68
69 println(cmd)
70 println('Moving `${tloc}/${fname}` to `${tloc}/binaryen` ...')
71
72 os.rename_dir('${tloc}/${fname}', loc)!
73 println('Done. You can now use `v -b wasm file.v` .')
74}
75