mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: remove duplication & slight cleanup. share prefs with v1
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
filepath
|
||||
v.ast
|
||||
v.table
|
||||
v.pref
|
||||
v.checker
|
||||
v.parser
|
||||
v.gen
|
||||
@@ -14,22 +15,24 @@ import (
|
||||
|
||||
pub struct Builder {
|
||||
pub:
|
||||
table &table.Table
|
||||
checker checker.Checker
|
||||
pref &pref.Preferences
|
||||
table &table.Table
|
||||
checker checker.Checker
|
||||
os pref.OS // the OS to build for
|
||||
compiled_dir string // contains os.realpath() of the dir of the final file beeing compiled, or the dir itself when doing `v .`
|
||||
module_path string
|
||||
module_search_paths []string
|
||||
mut:
|
||||
prefs Preferences
|
||||
parsed_files []ast.File
|
||||
parsed_files []ast.File
|
||||
}
|
||||
|
||||
pub fn new_builder(prefs Preferences) Builder {
|
||||
pub fn new_builder(pref &pref.Preferences) Builder {
|
||||
table := table.new_table()
|
||||
mut b:= Builder{
|
||||
prefs: prefs
|
||||
return Builder{
|
||||
pref: pref
|
||||
table: table
|
||||
checker: checker.new_checker(table)
|
||||
}
|
||||
b.set_module_search_paths()
|
||||
return b
|
||||
}
|
||||
|
||||
pub fn (b mut Builder) gen_c(v_files []string) string {
|
||||
@@ -54,11 +57,6 @@ pub fn (b mut Builder) build_x64(v_files []string, out_file string) {
|
||||
println('x64 GEN: ${time.ticks() - ticks}ms')
|
||||
}
|
||||
|
||||
|
||||
fn (b &Builder) parse_module_files() {
|
||||
|
||||
}
|
||||
|
||||
// parse all deps from already parsed files
|
||||
pub fn (b mut Builder) parse_imports() {
|
||||
mut done_imports := []string
|
||||
@@ -108,7 +106,7 @@ pub fn (b &Builder) v_files_from_dir(dir string) []string {
|
||||
mut files := os.ls(dir)or{
|
||||
panic(err)
|
||||
}
|
||||
if b.prefs.is_verbose {
|
||||
if b.pref.is_verbose {
|
||||
println('v_files_from_dir ("$dir")')
|
||||
}
|
||||
files.sort()
|
||||
@@ -119,22 +117,22 @@ pub fn (b &Builder) v_files_from_dir(dir string) []string {
|
||||
if file.ends_with('_test.v') {
|
||||
continue
|
||||
}
|
||||
if (file.ends_with('_win.v') || file.ends_with('_windows.v')) && b.prefs.os != .windows {
|
||||
if (file.ends_with('_win.v') || file.ends_with('_windows.v')) && b.os != .windows {
|
||||
continue
|
||||
}
|
||||
if (file.ends_with('_lin.v') || file.ends_with('_linux.v')) && b.prefs.os != .linux {
|
||||
if (file.ends_with('_lin.v') || file.ends_with('_linux.v')) && b.os != .linux {
|
||||
continue
|
||||
}
|
||||
if (file.ends_with('_mac.v') || file.ends_with('_darwin.v')) && b.prefs.os != .mac {
|
||||
if (file.ends_with('_mac.v') || file.ends_with('_darwin.v')) && b.os != .mac {
|
||||
continue
|
||||
}
|
||||
if file.ends_with('_nix.v') && b.prefs.os == .windows {
|
||||
if file.ends_with('_nix.v') && b.os == .windows {
|
||||
continue
|
||||
}
|
||||
if file.ends_with('_js.v') && b.prefs.os != .js {
|
||||
if file.ends_with('_js.v') && b.os != .js {
|
||||
continue
|
||||
}
|
||||
if file.ends_with('_c.v') && b.prefs.os == .js {
|
||||
if file.ends_with('_c.v') && b.os == .js {
|
||||
continue
|
||||
}
|
||||
/*
|
||||
@@ -162,7 +160,7 @@ fn verror(err string) {
|
||||
}
|
||||
|
||||
pub fn (b &Builder) log(s string) {
|
||||
if b.prefs.is_verbose {
|
||||
if b.pref.is_verbose {
|
||||
println(s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,36 +5,6 @@ import (
|
||||
filepath
|
||||
)
|
||||
|
||||
fn (b mut Builder) set_module_search_paths() {
|
||||
msearch_path := if b.prefs.vpath.len > 0 { b.prefs.vpath } else { b.prefs.mod_path }
|
||||
// Module search order:
|
||||
// 0) V test files are very commonly located right inside the folder of the
|
||||
// module, which they test. Adding the parent folder of the module folder
|
||||
// with the _test.v files, *guarantees* that the tested module can be found
|
||||
// without needing to set custom options/flags.
|
||||
// 1) search in the *same* directory, as the compiled final v program source
|
||||
// (i.e. the . in `v .` or file.v in `v file.v`)
|
||||
// 2) search in the modules/ in the same directory.
|
||||
// 3) search in vlib/
|
||||
// 4.1) search in -vpath (if given)
|
||||
// 4.2) search in ~/.vmodules/ (i.e. modules installed with vpm) (no -vpath)
|
||||
b.prefs.module_search_paths = []
|
||||
if b.prefs.is_test {
|
||||
b.prefs.module_search_paths << filepath.basedir(b.prefs.compile_dir) // pdir of _test.v
|
||||
}
|
||||
b.prefs.module_search_paths << b.prefs.compile_dir
|
||||
b.prefs.module_search_paths << filepath.join(b.prefs.compile_dir,'modules')
|
||||
b.prefs.module_search_paths << b.prefs.vlib_path
|
||||
b.prefs.module_search_paths << msearch_path
|
||||
if b.prefs.user_mod_path.len > 0 {
|
||||
b.prefs.module_search_paths << b.prefs.user_mod_path
|
||||
}
|
||||
if b.prefs.is_verbose {
|
||||
b.log('b.prefs.module_search_paths: $b.prefs.module_search_paths')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[inline]
|
||||
fn module_path(mod string) string {
|
||||
// submodule support
|
||||
@@ -43,13 +13,13 @@ fn module_path(mod string) string {
|
||||
|
||||
fn (b &Builder) find_module_path(mod string) ?string {
|
||||
mod_path := module_path(mod)
|
||||
for search_path in b.prefs.module_search_paths {
|
||||
for search_path in b.module_search_paths {
|
||||
try_path := filepath.join(search_path,mod_path)
|
||||
if b.prefs.is_verbose {
|
||||
if b.pref.is_verbose {
|
||||
println(' >> trying to find $mod in $try_path ..')
|
||||
}
|
||||
if os.is_dir(try_path) {
|
||||
if b.prefs.is_verbose {
|
||||
if b.pref.is_verbose {
|
||||
println(' << found $try_path .')
|
||||
}
|
||||
return try_path
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
module builder
|
||||
|
||||
pub struct Preferences {
|
||||
pub mut:
|
||||
// paths
|
||||
vpath string
|
||||
vlib_path string
|
||||
compile_dir string // contains os.realpath() of the dir of the final file beeing compiled, or the dir itself when doing `v .`
|
||||
mod_path string
|
||||
user_mod_path string
|
||||
module_search_paths []string
|
||||
// settings
|
||||
os OS // the OS to build for
|
||||
is_test bool
|
||||
is_verbose bool
|
||||
}
|
||||
|
||||
pub enum OS {
|
||||
mac
|
||||
linux
|
||||
windows
|
||||
freebsd
|
||||
openbsd
|
||||
netbsd
|
||||
dragonfly
|
||||
js // TODO
|
||||
android
|
||||
solaris
|
||||
haiku
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import (
|
||||
os
|
||||
filepath
|
||||
v.pref
|
||||
v.builder
|
||||
term
|
||||
)
|
||||
@@ -23,7 +24,7 @@ fn test_c_files() {
|
||||
ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or {
|
||||
panic(err)
|
||||
}
|
||||
mut b := builder.new_builder(builder.Preferences{})
|
||||
mut b := builder.new_builder(pref.Preferences{})
|
||||
res := b.gen_c([path])
|
||||
if compare_texts(res, ctext) {
|
||||
eprintln('${term_ok} ${i}')
|
||||
|
||||
@@ -12,6 +12,19 @@ pub enum BuildMode {
|
||||
build_module
|
||||
}
|
||||
|
||||
pub enum OS {
|
||||
mac
|
||||
linux
|
||||
windows
|
||||
freebsd
|
||||
openbsd
|
||||
netbsd
|
||||
dragonfly
|
||||
js // TODO
|
||||
android
|
||||
solaris
|
||||
haiku
|
||||
}
|
||||
|
||||
pub struct Preferences {
|
||||
pub mut:
|
||||
@@ -65,4 +78,3 @@ pub mut:
|
||||
prealloc bool
|
||||
v2 bool
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user