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 (
|
2019-10-13 15:37:43 +02:00
|
|
|
compiler
|
2019-09-16 16:29:06 +02:00
|
|
|
benchmark
|
2019-10-30 15:07:41 +01:00
|
|
|
os
|
2019-11-09 21:49:15 +01:00
|
|
|
filepath
|
2020-01-17 14:09:26 +01:00
|
|
|
// v.types
|
2019-12-19 22:29:37 +01: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']
|
2020-01-17 14:09:26 +01:00
|
|
|
simple_tools = ['fmt',
|
|
|
|
'up',
|
|
|
|
'create',
|
|
|
|
'test', 'test-fmt', 'test-compiler',
|
|
|
|
'bin2v',
|
|
|
|
'repl',
|
|
|
|
'build-tools', 'build-examples', 'build-vbinaries']
|
2019-12-15 02:51:18 +01:00
|
|
|
)
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn main() {
|
2019-12-30 09:23:17 +01:00
|
|
|
is_verbose := '-verbose' in os.args || '--verbose' in os.args
|
2019-12-19 22:29:37 +01:00
|
|
|
// t := time.ticks()
|
|
|
|
// defer { println(time.ticks() - t) }
|
2019-10-13 15:37:43 +02:00
|
|
|
args := compiler.env_vflags_and_os_args()
|
2019-12-30 09:23:17 +01:00
|
|
|
options,command := compiler.get_v_options_and_main_command(args)
|
|
|
|
if is_verbose {
|
|
|
|
eprintln('v args: $args')
|
|
|
|
eprintln('v command: $command')
|
|
|
|
eprintln('v options: $options')
|
|
|
|
}
|
2019-12-15 02:51:18 +01:00
|
|
|
// external tool
|
|
|
|
if command in simple_tools {
|
2020-01-08 17:15:05 +01:00
|
|
|
compiler.launch_tool('v' + command, command)
|
2019-12-15 02:51:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// v run, v doc, etc
|
2019-12-15 03:51:00 +01:00
|
|
|
if !command.starts_with('-') && !command.ends_with('.v') && !os.exists(command) {
|
2019-12-15 02:51:18 +01:00
|
|
|
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 {
|
2019-10-13 15:37:43 +02:00
|
|
|
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 {
|
2019-11-04 03:05:26 +01: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 13:19:04 +01:00
|
|
|
// No args? REPL
|
2019-12-15 02:51:18 +01:00
|
|
|
else if command == '' || (args.len == 2 && args[1] == '-') {
|
2020-01-08 17:15:05 +01:00
|
|
|
compiler.launch_tool('vrepl', '')
|
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 13:19:04 +01:00
|
|
|
return
|
|
|
|
}
|
2019-10-09 05:01:43 +02:00
|
|
|
// Construct the V object from command line arguments
|
2019-10-13 15:37:43 +02:00
|
|
|
mut v := compiler.new_v(args)
|
2019-06-30 22:44:15 +02:00
|
|
|
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()
|
2019-08-06 18:04:55 +02:00
|
|
|
v.run_compiled_executable_and_exit()
|
|
|
|
}
|
2019-10-07 07:51:26 +02:00
|
|
|
mut tmark := benchmark.new_benchmark()
|
2019-11-19 07:53:52 +01:00
|
|
|
if v.pref.x64 {
|
|
|
|
v.compile_x64()
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
2019-12-31 19:42:16 +01:00
|
|
|
else if v.pref.v2 {
|
|
|
|
v.compile2()
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
else {
|
2019-11-19 07:53:52 +01:00
|
|
|
v.compile()
|
|
|
|
}
|
2019-10-07 07:51:26 +02:00
|
|
|
if v.pref.is_stats {
|
|
|
|
tmark.stop()
|
2019-12-15 02:51:18 +01:00
|
|
|
println('compilation took: ' + tmark.total_duration().str() + 'ms')
|
2019-10-07 07:51:26 +02:00
|
|
|
}
|
2019-08-06 18:04:55 +02:00
|
|
|
if v.pref.is_test {
|
|
|
|
v.run_compiled_executable_and_exit()
|
|
|
|
}
|
2019-10-13 15:37:43 +02:00
|
|
|
v.finalize_compilation()
|
2019-10-11 23:54:25 +02:00
|
|
|
}
|
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 {
|
2019-12-19 22:29:37 +01:00
|
|
|
'', '.', 'run', 'build' {
|
|
|
|
// handled later in vlib/compiler/main.v
|
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)')
|
|
|
|
}
|
2019-12-23 11:02:50 +01:00
|
|
|
'search', 'install', 'update', 'remove' {
|
2020-01-08 17:15:05 +01:00
|
|
|
compiler.launch_tool('vpm', command)
|
2019-12-15 02:51:18 +01:00
|
|
|
}
|
|
|
|
'get' {
|
|
|
|
println('use `v install` to install modules from vpm.vlang.io ')
|
|
|
|
}
|
|
|
|
'symlink' {
|
|
|
|
compiler.create_symlink()
|
|
|
|
}
|
|
|
|
'runrepl' {
|
2020-01-20 06:16:04 +01:00
|
|
|
// TODO: remove this after 2020/02/01
|
2020-01-17 14:09:26 +01:00
|
|
|
eprintln('`v runrepl` has been deprecated. Please use `v repl` instead.')
|
2020-01-08 17:15:05 +01:00
|
|
|
compiler.launch_tool('vrepl', 'runrepl')
|
2019-12-15 02:51:18 +01:00
|
|
|
}
|
|
|
|
'doc' {
|
|
|
|
vexe := os.executable()
|
2019-12-23 11:09:22 +01:00
|
|
|
vdir := filepath.dir(os.executable())
|
2019-12-15 02:51:18 +01:00
|
|
|
os.chdir(vdir)
|
|
|
|
mod := args.last()
|
|
|
|
os.system('$vexe build module vlib$os.path_separator' + args.last())
|
2019-12-23 11:02:50 +01:00
|
|
|
vhfile := filepath.join(compiler.v_modules_path,'vlib','${mod}.vh')
|
2019-12-30 09:23:17 +01:00
|
|
|
txt := os.read_file(vhfile) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-15 02:51:18 +01:00
|
|
|
println(txt)
|
|
|
|
// v.gen_doc_html_for_module(args.last())
|
|
|
|
}
|
|
|
|
else {
|
2020-01-20 06:16:04 +01:00
|
|
|
eprintln('v $command: unknown command\nRun "v help" for usage.')
|
|
|
|
exit(1)
|
2019-12-23 11:02:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 03:44:34 +01:00
|
|
|
exit(0)
|
2019-12-15 02:51:18 +01:00
|
|
|
}
|