v/v.v

132 lines
3.2 KiB
V
Raw Normal View History

2019-06-23 04:21:30 +02:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 20:20:28 +02:00
module main
2019-08-29 00:52:32 +02:00
import (
compiler
benchmark
2019-10-30 15:07:41 +01:00
os
filepath
2019-10-18 19:49:34 +02:00
//time
2019-08-29 00:52:32 +02:00
)
2019-06-22 20:20:28 +02:00
2019-12-15 02:51:18 +01:00
const (
known_commands = ['run', 'build', 'version', 'doc']
simple_tools = ['up', 'create', 'test', 'test-compiler', 'build-tools',
'build-examples', 'build-vbinaries']
)
2019-06-22 20:20:28 +02:00
fn main() {
2019-10-18 19:49:34 +02:00
//t := time.ticks()
//defer { println(time.ticks() - t) }
2019-07-03 13:20:43 +02:00
// There's no `flags` module yet, so args have to be parsed manually
args := compiler.env_vflags_and_os_args()
options := args.filter(it.starts_with('-'))
//NB: commands should be explicitly set by the command line (os.args)
// NOT passed through VFLAGS, otherwise the naked `v` invocation for
// the repl does not work when you have VFLAGS with -cc or -cflags set
// which may be surprising to v users.
2019-12-15 02:51:18 +01:00
command := if os.args.len > 1 { os.args[1] } else { '' }
// external tool
if command in simple_tools {
compiler.launch_tool('v' + command)
return
}
// v run, v doc, etc
if !command.starts_with('-') && !os.exists(command) {
v_command(command, args)
2019-12-01 10:50:13 +01:00
}
2019-06-22 20:20:28 +02:00
// Print the version and exit.
2019-12-15 02:51:18 +01:00
if '-v' in options || '--version' in options {
version_hash := compiler.vhash()
println('V $compiler.Version $version_hash')
2019-06-22 20:20:28 +02:00
return
}
2019-12-15 02:51:18 +01:00
else if '-h' in options || '--help' in options {
println(compiler.help_text)
return
}
// No args? REPL
2019-12-15 02:51:18 +01:00
else if command == '' || (args.len == 2 && args[1] == '-') {
compiler.launch_tool('vrepl')
return
}
// Construct the V object from command line arguments
mut v := compiler.new_v(args)
if v.pref.is_verbose {
2019-06-22 20:20:28 +02:00
println(args)
}
2019-12-15 02:51:18 +01:00
if command == 'run' {
2019-08-09 22:37:31 +02:00
// always recompile for now, too error prone to skip recompilation otherwise
// for example for -repl usage, especially when piping lines to v
2019-08-17 21:19:37 +02:00
v.compile()
v.run_compiled_executable_and_exit()
}
mut tmark := benchmark.new_benchmark()
2019-11-19 07:53:52 +01:00
if v.pref.x64 {
v.compile_x64()
} else {
v.compile()
}
if v.pref.is_stats {
tmark.stop()
2019-12-15 02:51:18 +01:00
println('compilation took: ' + tmark.total_duration().str() + 'ms')
}
if v.pref.is_test {
v.run_compiled_executable_and_exit()
}
v.finalize_compilation()
}
2019-10-14 04:18:48 +02:00
2019-12-15 02:51:18 +01:00
fn v_command(command string, args []string) {
match command {
'', '.', 'run' {
2019-12-15 03:44:34 +01:00
return
2019-12-15 02:51:18 +01:00
}
'version' {
println('V $compiler.Version $compiler.vhash()')
}
'help' {
println(compiler.help_text)
}
'translate' {
println('Translating C to V will be available in V 0.3 (January)')
}
'search', 'install', 'update' {
compiler.launch_tool('vpm')
}
'get' {
println('use `v install` to install modules from vpm.vlang.io ')
}
'symlink' {
compiler.create_symlink()
}
'fmt' {
compiler.vfmt(args)
}
'runrepl' {
compiler.launch_tool('vrepl')
}
'doc' {
vexe := os.executable()
vdir := os.dir(os.executable())
os.chdir(vdir)
mod := args.last()
os.system('$vexe build module vlib$os.path_separator' + args.last())
txt := os.read_file(filepath.join(compiler.v_modules_path, 'vlib', '${mod}.vh')) or {
panic(err)
}
println(txt)
// v.gen_doc_html_for_module(args.last())
}
else {
println('v $command: unknown command')
println('Run "v help" for usage.')
}
}
2019-12-15 03:44:34 +01:00
exit(0)
2019-12-15 02:51:18 +01:00
}