2019-06-23 05:21:30 +03: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 21:20:28 +03:00
|
|
|
module main
|
|
|
|
|
2019-08-29 01:52:32 +03:00
|
|
|
import (
|
2019-10-13 16:37:43 +03:00
|
|
|
compiler
|
2019-09-16 17:29:06 +03:00
|
|
|
benchmark
|
2019-10-30 17:07:41 +03:00
|
|
|
os
|
2019-11-09 23:49:15 +03:00
|
|
|
filepath
|
2019-10-18 20:49:34 +03:00
|
|
|
//time
|
2019-08-29 01:52:32 +03:00
|
|
|
)
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-12-15 04:51:18 +03:00
|
|
|
const (
|
|
|
|
known_commands = ['run', 'build', 'version', 'doc']
|
|
|
|
simple_tools = ['up', 'create', 'test', 'test-compiler', 'build-tools',
|
|
|
|
'build-examples', 'build-vbinaries']
|
|
|
|
)
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn main() {
|
2019-10-18 20:49:34 +03:00
|
|
|
//t := time.ticks()
|
|
|
|
//defer { println(time.ticks() - t) }
|
2019-10-13 16:37:43 +03:00
|
|
|
args := compiler.env_vflags_and_os_args()
|
2019-12-18 09:04:35 +03:00
|
|
|
options, command := compiler.get_v_options_and_main_command( args )
|
2019-12-15 04:51:18 +03:00
|
|
|
// external tool
|
|
|
|
if command in simple_tools {
|
|
|
|
compiler.launch_tool('v' + command)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// v run, v doc, etc
|
2019-12-15 05:51:00 +03:00
|
|
|
if !command.starts_with('-') && !command.ends_with('.v') && !os.exists(command) {
|
2019-12-15 04:51:18 +03:00
|
|
|
v_command(command, args)
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// Print the version and exit.
|
2019-12-15 04:51:18 +03:00
|
|
|
if '-v' in options || '--version' in options {
|
2019-10-13 16:37:43 +03:00
|
|
|
version_hash := compiler.vhash()
|
|
|
|
println('V $compiler.Version $version_hash')
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
2019-12-15 04:51:18 +03:00
|
|
|
else if '-h' in options || '--help' in options {
|
2019-11-04 05:05:26 +03:00
|
|
|
println(compiler.help_text)
|
|
|
|
return
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 15:19:04 +03:00
|
|
|
// No args? REPL
|
2019-12-15 04:51:18 +03:00
|
|
|
else if command == '' || (args.len == 2 && args[1] == '-') {
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 15:19:04 +03:00
|
|
|
compiler.launch_tool('vrepl')
|
|
|
|
return
|
|
|
|
}
|
2019-10-09 06:01:43 +03:00
|
|
|
// Construct the V object from command line arguments
|
2019-10-13 16:37:43 +03:00
|
|
|
mut v := compiler.new_v(args)
|
2019-06-30 23:44:15 +03:00
|
|
|
if v.pref.is_verbose {
|
2019-06-22 21:20:28 +03:00
|
|
|
println(args)
|
|
|
|
}
|
2019-12-15 04:51:18 +03:00
|
|
|
if command == 'run' {
|
2019-08-09 23:37:31 +03: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 22:19:37 +03:00
|
|
|
v.compile()
|
2019-08-06 19:04:55 +03:00
|
|
|
v.run_compiled_executable_and_exit()
|
|
|
|
}
|
2019-10-07 08:51:26 +03:00
|
|
|
mut tmark := benchmark.new_benchmark()
|
2019-11-19 09:53:52 +03:00
|
|
|
if v.pref.x64 {
|
|
|
|
v.compile_x64()
|
|
|
|
} else {
|
|
|
|
v.compile()
|
|
|
|
}
|
2019-10-07 08:51:26 +03:00
|
|
|
if v.pref.is_stats {
|
|
|
|
tmark.stop()
|
2019-12-15 04:51:18 +03:00
|
|
|
println('compilation took: ' + tmark.total_duration().str() + 'ms')
|
2019-10-07 08:51:26 +03:00
|
|
|
}
|
2019-08-06 19:04:55 +03:00
|
|
|
if v.pref.is_test {
|
|
|
|
v.run_compiled_executable_and_exit()
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
v.finalize_compilation()
|
2019-10-12 00:54:25 +03:00
|
|
|
}
|
2019-10-14 05:18:48 +03:00
|
|
|
|
2019-12-15 04:51:18 +03:00
|
|
|
fn v_command(command string, args []string) {
|
|
|
|
match command {
|
2019-12-16 18:54:10 +03:00
|
|
|
'', '.', 'run', 'build' { // handled later in vlib/compiler/main.v
|
2019-12-15 05:44:34 +03:00
|
|
|
return
|
2019-12-15 04:51:18 +03: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 05:44:34 +03:00
|
|
|
exit(0)
|
2019-12-15 04:51:18 +03:00
|
|
|
}
|
|
|
|
|