v/vlib/v/pref/default.v

97 lines
2.5 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 pref
2020-04-25 17:49:16 +02:00
import os
2020-02-09 10:08:04 +01:00
pub const (
default_module_path = mpath()
2020-02-09 10:08:04 +01:00
)
fn mpath() string {
return os.join_path(os.home_dir(), '.vmodules')
}
pub fn new_preferences() Preferences {
mut p := Preferences{}
p.fill_with_defaults()
return p
}
2020-04-25 17:49:16 +02:00
pub fn (mut p Preferences) fill_with_defaults() {
2020-02-09 10:08:04 +01:00
if p.vroot == '' {
// Location of all vlib files
2020-03-07 22:26:26 +01:00
p.vroot = os.dir(vexe_path())
2020-02-09 10:08:04 +01:00
}
2020-03-09 02:23:34 +01:00
vlib_path := os.join_path(p.vroot, 'vlib')
2020-03-06 18:53:29 +01:00
if p.lookup_path.len == 0 {
p.lookup_path = ['@vlib', '@vmodules']
2020-02-09 10:08:04 +01:00
}
2020-03-06 18:53:29 +01: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 10:08:04 +01:00
}
2020-03-20 16:41:18 +01:00
rpath := os.real_path(p.path)
if p.out_name == '' {
2020-03-19 15:49:07 +01:00
filename := os.file_name(rpath).trim_space()
2020-02-09 10:08:04 +01:00
mut base := filename.all_before_last('.')
if base == '' {
// The file name is just `.v` or `.vsh` or `.*`
base = filename
}
2020-03-07 22:26:26 +01:00
target_dir := if os.is_dir(rpath) { rpath } else { os.dir(rpath) }
2020-03-09 02:23:34 +01:00
p.out_name = os.join_path(target_dir, base)
2020-02-09 10:08:04 +01: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 15:49:07 +01:00
rpath_name := os.file_name(rpath)
2020-03-06 18:53:29 +01:00
p.building_v = !p.is_repl && (rpath_name == 'v' || rpath_name == 'vfmt.v')
2020-02-09 10:08:04 +01: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.ccompiler_type = cc_from_string(p.ccompiler)
p.is_test = p.path.ends_with('_test.v')
p.is_vsh = p.path.ends_with('.vsh')
p.is_script = p.is_vsh || p.path.ends_with('.v') || p.path.ends_with('.vv')
2020-03-06 18:53:29 +01: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'
}
}
}
2020-02-09 10:08:04 +01: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 11:33:01 +01:00
pub fn vexe_path() string {
2020-02-09 10:08:04 +01:00
vexe := os.getenv('VEXE')
if vexe != '' {
return vexe
}
2020-03-20 16:41:18 +01:00
real_vexe_path := os.real_path(os.executable())
2020-02-09 10:08:04 +01:00
os.setenv('VEXE', real_vexe_path, true)
return real_vexe_path
}