2022-06-19 15:52:42 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
import v.util
|
|
|
|
|
2022-06-23 03:31:05 +03:00
|
|
|
const vexe = os.getenv('VEXE')
|
|
|
|
|
2022-06-19 15:52:42 +03:00
|
|
|
fn main() {
|
|
|
|
vmodules := os.vmodules_dir()
|
2022-06-22 22:10:46 +03:00
|
|
|
c2v_dir := os.join_path(vmodules, 'c2v')
|
2022-06-23 03:31:05 +03:00
|
|
|
mut c2v_bin := os.join_path(c2v_dir, 'c2v')
|
|
|
|
$if windows {
|
|
|
|
c2v_bin += '.exe'
|
|
|
|
}
|
2022-06-19 15:52:42 +03:00
|
|
|
// Git clone c2v
|
|
|
|
if !os.exists(c2v_dir) {
|
2022-06-19 16:19:56 +03:00
|
|
|
println('C2V is not installed. Cloning C2V to $c2v_dir ...')
|
2022-06-19 15:52:42 +03:00
|
|
|
os.chdir(vmodules)?
|
2022-06-22 22:49:29 +03:00
|
|
|
res := os.execute('git clone https://github.com/vlang/c2v')
|
2022-06-19 15:52:42 +03:00
|
|
|
if res.exit_code != 0 {
|
2022-06-22 22:10:46 +03:00
|
|
|
eprintln('Failed to download C2V.')
|
2022-06-19 16:19:56 +03:00
|
|
|
exit(1)
|
2022-06-19 15:52:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compile c2v
|
|
|
|
if !os.exists(c2v_bin) {
|
|
|
|
os.chdir(c2v_dir)?
|
2022-06-19 16:19:56 +03:00
|
|
|
println('Compiling c2v ...')
|
2022-06-23 03:31:05 +03:00
|
|
|
res2 := os.execute('${os.quoted_path(vexe)} -o ${os.quoted_path(c2v_bin)} -keepc -g -experimental .')
|
2022-06-19 15:52:42 +03:00
|
|
|
if res2.exit_code != 0 {
|
2022-06-19 17:08:21 +03:00
|
|
|
eprintln(res2.output)
|
2022-06-23 03:31:05 +03:00
|
|
|
eprintln('Failed to compile C2V. This should not happen. Please report it via GitHub.')
|
2022-06-19 16:19:56 +03:00
|
|
|
exit(2)
|
2022-06-19 15:52:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if os.args.len < 3 {
|
2022-06-23 03:31:05 +03:00
|
|
|
eprintln('Wrong number of arguments. Use `v translate file.c` .')
|
2022-06-19 16:19:56 +03:00
|
|
|
exit(3)
|
2022-06-19 15:52:42 +03:00
|
|
|
}
|
|
|
|
passed_args := util.args_quote_paths(os.args[2..])
|
|
|
|
// println(passed_args)
|
2022-06-19 16:19:56 +03:00
|
|
|
os.chdir(os.wd_at_startup)?
|
2022-06-20 15:50:38 +03:00
|
|
|
res := os.system('$c2v_bin $passed_args')
|
|
|
|
if res != 0 {
|
|
|
|
eprintln('C2V failed to translate the C files. Please report it via GitHub.')
|
2022-06-19 16:19:56 +03:00
|
|
|
exit(4)
|
2022-06-19 15:52:42 +03:00
|
|
|
}
|
|
|
|
}
|