1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/v/pref/default.v

97 lines
2.5 KiB
V
Raw Normal View History

2020-02-09 12:08:04 +03: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 pref
2020-04-25 18:49:16 +03:00
import os
import term
2020-02-09 12:08:04 +03:00
pub const (
default_module_path = mpath()
2020-02-09 12:08:04 +03:00
)
fn mpath() string {
return os.home_dir() + '.vmodules'
}
pub fn new_preferences() Preferences {
p := Preferences{}
p.fill_with_defaults()
return p
}
2020-04-25 18:49:16 +03:00
pub fn (mut p Preferences) fill_with_defaults() {
2020-02-09 12:08:04 +03:00
if p.vroot == '' {
// Location of all vlib files
2020-03-08 00:26:26 +03:00
p.vroot = os.dir(vexe_path())
2020-02-09 12:08:04 +03:00
}
2020-03-09 04:23:34 +03:00
vlib_path := os.join_path(p.vroot, 'vlib')
2020-03-06 20:53:29 +03:00
if p.lookup_path.len == 0 {
p.lookup_path = ['@vlib', '@vmodules']
2020-02-09 12:08:04 +03:00
}
2020-03-06 20:53:29 +03:00
for i, path in p.lookup_path {
p.lookup_path[i] = path.replace('@vlib', vlib_path).replace('@vmodules', default_module_path)
2020-02-09 12:08:04 +03:00
}
2020-03-20 18:41:18 +03:00
rpath := os.real_path(p.path)
if p.out_name == '' {
2020-03-19 17:49:07 +03:00
filename := os.file_name(rpath).trim_space()
2020-02-09 12:08:04 +03:00
mut base := filename.all_before_last('.')
if base == '' {
// The file name is just `.v` or `.vsh` or `.*`
base = filename
}
2020-03-08 00:26:26 +03:00
target_dir := if os.is_dir(rpath) { rpath } else { os.dir(rpath) }
2020-03-09 04:23:34 +03:00
p.out_name = os.join_path(target_dir, base)
2020-02-09 12:08:04 +03:00
if rpath == '$p.vroot/cmd/v' && os.is_dir('vlib/compiler') {
// Building V? Use v2, since we can't overwrite a running
// executable on Windows + the precompiled V is more
// optimized.
println('Saving the resulting V executable in `./v2`')
println('Use `v -o v cmd/v` if you want to replace current ' + 'V executable.')
p.out_name = 'v2'
}
}
2020-03-19 17:49:07 +03:00
rpath_name := os.file_name(rpath)
2020-03-06 20:53:29 +03:00
p.building_v = !p.is_repl && (rpath_name == 'v' || rpath_name == 'vfmt.v')
2020-02-09 12:08:04 +03:00
if p.os == ._auto {
// No OS specifed? Use current system
p.os = get_host_os()
}
if p.ccompiler == '' {
p.ccompiler = default_c_compiler()
}
p.is_test = p.path.ends_with('_test.v') || p.path.ends_with('.vv')
2020-02-09 12:08:04 +03:00
p.is_script = p.path.ends_with('.v') || p.path.ends_with('.vsh')
2020-03-06 20:53:29 +03:00
if p.third_party_option == '' {
p.third_party_option = p.cflags
$if !windows {
if !p.third_party_option.contains('-fPIC') {
p.third_party_option += ' -fPIC'
}
}
}
p.enable_globals = true
2020-02-09 12:08:04 +03:00
}
fn default_c_compiler() string {
// fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
// if os.exists(fast_clang) {
// return fast_clang
// }
// TODO fix $if after 'string'
$if windows {
return 'gcc'
}
return 'cc'
}
2020-02-20 13:33:01 +03:00
pub fn vexe_path() string {
2020-02-09 12:08:04 +03:00
vexe := os.getenv('VEXE')
if vexe != '' {
return vexe
}
2020-03-20 18:41:18 +03:00
real_vexe_path := os.real_path(os.executable())
2020-02-09 12:08:04 +03:00
os.setenv('VEXE', real_vexe_path, true)
return real_vexe_path
}