v / cmd / tools
Raw file | 53 loc (50 sloc) | 1.47 KB | Latest commit hash 017ace6ea
1// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
3module main
4
5import os
6import v.util
7
8const vexe = os.getenv('VEXE')
9
10fn main() {
11 vmodules := os.vmodules_dir()
12 c2v_dir := os.join_path(vmodules, 'c2v')
13 mut c2v_bin := os.join_path(c2v_dir, 'c2v')
14 $if windows {
15 c2v_bin += '.exe'
16 }
17 // Git clone c2v
18 if !os.exists(c2v_dir) {
19 os.mkdir_all(vmodules)!
20 println('C2V is not installed. Cloning C2V to ${c2v_dir} ...')
21 os.chdir(vmodules)!
22 res := os.execute('git clone --filter=blob:none https://github.com/vlang/c2v')
23 if res.exit_code != 0 {
24 eprintln('Failed to download C2V.')
25 exit(1)
26 }
27 }
28 // Compile c2v
29 if !os.exists(c2v_bin) {
30 os.chdir(c2v_dir)!
31 println('Compiling c2v ...')
32 res2 := os.execute('${os.quoted_path(vexe)} -o ${os.quoted_path(c2v_bin)} -keepc -g -experimental .')
33 if res2.exit_code != 0 {
34 eprintln(res2.output)
35 eprintln('Failed to compile C2V. This should not happen. Please report it via GitHub.')
36 exit(2)
37 }
38 }
39 if os.args.len < 3 {
40 eprintln('Wrong number of arguments. Use `v translate file.c` .')
41 exit(3)
42 }
43 passed_args := util.args_quote_paths(os.args[2..])
44 // println(passed_args)
45 os.chdir(os.wd_at_startup)!
46 c2v_cmd := '${os.quoted_path(c2v_bin)} ${passed_args}'
47 res := os.system(c2v_cmd)
48 if res != 0 {
49 eprintln('C2V command: ${c2v_cmd}')
50 eprintln('C2V failed to translate the C files. Please report it via GitHub.')
51 exit(4)
52 }
53}