v/cmd/v/v.v

327 lines
7.6 KiB
V
Raw Normal View History

2020-02-09 10:08:04 +01:00
// Copyright (c) 2019-2020 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 internal.help
import os
import os.cmdline
import v.table
import v.doc
import v.pref
import v.util
import v.builder
2020-02-09 10:08:04 +01:00
const (
simple_cmd = ['fmt', 'up'
'self', 'test'
'test-fmt', 'test-compiler', 'test-fixed', 'bin2v'
'repl'
'build-tools'
'build-examples', 'build-vbinaries', 'setup-freetype'
2020-04-07 00:44:19 +02:00
]
list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags']
list_of_flags_with_param = [
'o'
'output', 'd', 'define', 'b', 'backend', 'cc', 'os', 'target-os', 'arch', 'csource'
'cf', 'cflags', 'path']
2020-02-09 10:08:04 +01:00
)
fn main() {
2020-04-16 19:41:24 +02:00
args := os.args[1..]
// args = 123
if args.len == 0 || args[0] in ['-', 'repl'] {
// Running `./v` without args launches repl
if args.len == 0 {
println('For usage information, quit V REPL using `exit` and use `v help`')
}
util.launch_tool(false, 'vrepl')
return
}
if args.len > 0 && (args[0] in ['version', '-V', '-version', '--version'] || (args[0] ==
'-v' && args.len == 1)) {
// `-v` flag is for setting verbosity, but without any args it prints the version, like Clang
println(util.full_v_version())
return
}
2020-04-16 19:41:24 +02:00
args_and_flags := util.join_env_vflags_and_os_args()[1..]
prefs, command := parse_args(args_and_flags)
2020-04-07 00:44:19 +02:00
if prefs.is_verbose {
2020-04-07 01:09:25 +02:00
println('command = "$command"')
println(util.full_v_version())
2020-02-09 10:08:04 +01:00
}
2020-04-07 00:44:19 +02:00
if prefs.is_verbose {
// println('args= ')
// println(args) // QTODO
// println('prefs= ')
// println(prefs) // QTODO
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
// Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below.
2020-02-09 10:08:04 +01:00
if command in simple_cmd {
2020-02-10 14:42:57 +01:00
// External tools
2020-04-07 00:44:19 +02:00
util.launch_tool(prefs.is_verbose, 'v' + command)
2020-02-09 10:08:04 +01:00
return
}
match command {
2020-04-02 17:46:39 +02:00
'help' {
invoke_help_and_exit(args)
}
2020-04-07 19:37:15 +02:00
'new', 'init' {
2020-04-07 00:44:19 +02:00
util.launch_tool(prefs.is_verbose, 'vcreate')
2020-03-15 11:20:12 +01:00
return
}
2020-02-09 10:08:04 +01:00
'translate' {
2020-02-19 16:12:39 +01:00
println('Translating C to V will be available in V 0.3')
2020-03-06 18:53:29 +01:00
return
2020-02-09 10:08:04 +01:00
}
'search', 'install', 'update', 'remove' {
2020-04-07 00:44:19 +02:00
util.launch_tool(prefs.is_verbose, 'vpm')
2020-03-06 18:53:29 +01:00
return
2020-02-09 10:08:04 +01:00
}
'get' {
2020-03-06 18:53:29 +01:00
println('V Error: Use `v install` to install modules from vpm.vlang.io')
exit(1)
2020-02-09 10:08:04 +01:00
}
'symlink' {
create_symlink()
2020-03-06 18:53:29 +01:00
return
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01:00
'doc' {
2020-04-02 17:46:39 +02:00
if args.len == 1 {
println('v doc [module]')
2020-03-06 18:53:29 +01:00
exit(1)
}
table := table.new_table()
2020-04-02 17:46:39 +02:00
println(doc.doc(args[1], table))
2020-03-06 18:53:29 +01:00
return
}
else {}
}
if command in ['run', 'build-module'] || command.ends_with('.v') || os.exists(command) {
// println('command')
// println(prefs.path)
2020-04-07 00:44:19 +02:00
builder.compile(command, prefs)
return
}
2020-03-06 18:53:29 +01:00
eprintln('v $command: unknown command\nRun "v help" for usage.')
exit(1)
}
2020-04-02 17:46:39 +02:00
fn parse_args(args []string) (&pref.Preferences, string) {
2020-04-23 01:16:58 +02:00
mut res := &pref.Preferences{}
mut command := ''
mut command_pos := 0
// for i, arg in args {
for i := 0; i < args.len; i++ {
2020-04-02 17:46:39 +02:00
arg := args[i]
current_args := args[i..]
match arg {
'-v' {
res.is_verbose = true
}
'-cg' {
res.is_debug = true
}
2020-04-22 01:52:56 +02:00
'-repl' {
res.is_repl = true
}
2020-05-01 19:34:27 +02:00
'-live' {
res.is_livemain = true
}
'-sharedlive' {
2020-05-01 19:34:27 +02:00
res.is_liveshared = true
res.is_shared = true
}
'-shared' {
res.is_shared = true
}
'-autofree' {
res.autofree = true
}
'-compress' {
res.compress = true
}
'-freestanding' {
res.is_bare = true
}
'-prof', '-profile' {
res.profile_file = cmdline.option(current_args, '-profile', '-')
res.is_prof = true
i++
}
'-prod' {
res.is_prod = true
}
'-stats' {
res.is_stats = true
}
'-obfuscate' {
res.obfuscate = true
}
'-translated' {
res.translated = true
}
'-showcc' {
res.show_cc = true
}
2020-04-27 14:46:25 +02:00
'-usecache' {
res.use_cache = true
}
'-keepc' {
2020-04-27 13:27:55 +02:00
res.keep_c = true
}
'-x64' {
res.backend = .x64
}
'-print_v_files' {
res.print_v_files = true
}
2020-04-07 00:44:19 +02:00
'-os' {
target_os := cmdline.option(current_args, '-os', '')
i++
target_os_kind := pref.os_from_string(target_os) or {
if target_os == 'cross' {
res.output_cross_c = true
continue
}
println('unknown operating system target `$target_os`')
exit(1)
2020-04-07 00:44:19 +02:00
}
res.os = target_os_kind
2020-04-07 00:44:19 +02:00
}
'-printfn' {
res.printfn_list << cmdline.option(current_args, '-printfn', '')
i++
}
'-cflags' {
res.cflags += ' ' + cmdline.option(current_args, '-cflags', '')
i++
}
'-define', '-d' {
if current_args.len > 1 {
define := current_args[1]
parse_define(mut res, define)
}
i++
}
2020-04-02 17:55:00 +02:00
'-cc' {
res.ccompiler = cmdline.option(current_args, '-cc', 'cc')
2020-04-02 17:46:39 +02:00
i++
}
'-o' {
res.out_name = cmdline.option(current_args, '-o', '')
2020-04-02 17:46:39 +02:00
i++
}
2020-04-15 23:16:49 +02:00
'-b' {
b := pref.backend_from_string(cmdline.option(current_args, '-b', 'c')) or {
2020-04-15 23:16:49 +02:00
continue
}
res.backend = b
i++
}
2020-04-02 17:46:39 +02:00
else {
2020-04-23 01:16:58 +02:00
mut should_continue := false
2020-04-04 12:09:34 +02:00
for flag_with_param in list_of_flags_with_param {
if '-$flag_with_param' == arg {
should_continue = true
i++
break
}
}
if should_continue {
continue
}
2020-04-02 17:46:39 +02:00
if !arg.starts_with('-') && command == '' {
command = arg
2020-04-07 03:21:32 +02:00
command_pos = i
2020-04-02 17:46:39 +02:00
}
}
}
}
2020-04-07 00:44:19 +02:00
if command.ends_with('.v') || os.exists(command) {
res.path = command
} else if command == 'run' {
2020-04-07 01:23:36 +02:00
res.is_run = true
2020-04-15 16:17:17 +02:00
if command_pos > args.len {
eprintln('v run: no v files listed')
exit(1)
}
2020-04-15 16:17:17 +02:00
res.path = args[command_pos + 1]
res.run_args = args[command_pos + 2..]
2020-04-07 01:09:25 +02:00
}
if command == 'build-module' {
2020-04-10 18:11:43 +02:00
res.build_mode = .build_module
res.path = args[command_pos + 1]
2020-04-10 18:11:43 +02:00
}
2020-04-07 01:09:25 +02:00
if res.is_verbose {
println('setting pref.path to "$res.path"')
}
2020-04-07 00:44:19 +02:00
res.fill_with_defaults()
2020-04-02 17:46:39 +02:00
return res, command
2020-03-06 18:53:29 +01:00
}
2020-03-13 20:52:49 +01:00
fn invoke_help_and_exit(remaining []string) {
match remaining.len {
0, 1 { help.print_and_exit('default') }
2 { help.print_and_exit(remaining[1]) }
2020-03-13 20:52:49 +01:00
else {}
}
println('V Error: Expected only one help topic to be provided.')
println('For usage information, use `v help`.')
exit(1)
}
fn create_symlink() {
$if windows {
return
}
vexe := pref.vexe_path()
2020-04-23 01:16:58 +02:00
mut link_path := '/usr/local/bin/v'
mut ret := os.exec('ln -sf $vexe $link_path') or {
panic(err)
}
if ret.exit_code == 0 {
println('Symlink "$link_path" has been created')
} else if os.system("uname -o | grep -q \'[A/a]ndroid\'") == 0 {
println('Failed to create symlink "$link_path". Trying again with Termux path for Android.')
link_path = '/data/data/com.termux/files/usr/bin/v'
ret = os.exec('ln -sf $vexe $link_path') or {
panic(err)
}
if ret.exit_code == 0 {
println('Symlink "$link_path" has been created')
} else {
println('Failed to create symlink "$link_path". Try again with sudo.')
}
} else {
println('Failed to create symlink "$link_path". Try again with sudo.')
}
}
fn parse_define(prefs mut pref.Preferences, define string) {
define_parts := define.split('=')
if define_parts.len == 1 {
prefs.compile_defines << define
prefs.compile_defines_all << define
return
}
if define_parts.len == 2 {
prefs.compile_defines_all << define_parts[0]
match define_parts[1] {
'0' {}
'1' {
prefs.compile_defines << define_parts[0]
}
else {
println('V error: Unknown define argument value `${define_parts[1]}` for ${define_parts[0]}.' +
'Expected `0` or `1`.')
exit(1)
}
}
return
}
println('V error: Unknown define argument: ${define}. Expected at most one `=`.')
exit(1)
}