From 3c1427a4e86e45658064cde32580dcbd333d1111 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 25 Jul 2020 01:37:52 +0200 Subject: [PATCH] all: fix current module in build-module mode --- vlib/v/builder/cc.v | 22 ++++++++++++++++------ vlib/v/gen/cgen.v | 3 ++- vlib/v/gen/fn.v | 8 +++++--- vlib/v/parser/module.v | 1 + vlib/v/parser/parser.v | 15 ++++++++++++++- vlib/v/table/table.v | 1 + 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index d3dd18c87c..8b9f3d885f 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -176,8 +176,8 @@ fn (mut v Builder) cc() { verror('-fast is only supported on Linux right now') } } - if !v.pref.is_shared && v.pref.build_mode != .build_module && - os.user_os() == 'windows' && !v.pref.out_name.ends_with('.exe') { + if !v.pref.is_shared && v.pref.build_mode != .build_module && os.user_os() == 'windows' && + !v.pref.out_name.ends_with('.exe') { v.pref.out_name += '.exe' } // linux_host := os.user_os() == 'linux' @@ -199,13 +199,15 @@ fn (mut v Builder) cc() { } if v.pref.build_mode == .build_module { // Create the modules & out directory if it's not there. - mut out_dir := if v.pref.path.starts_with('vlib') { '$pref.default_module_path${os.path_separator}cache$os.path_separator$v.pref.path' } else { '$pref.default_module_path$os.path_separator$v.pref.path' } + mut out_dir := if v.pref.path.starts_with('vlib') { '$pref.default_module_path${os.path_separator}cache$os.path_separator$v.pref.path' } else { '$pref.default_module_path${os.path_separator}cache/$v.pref.path' } pdir := out_dir.all_before_last(os.path_separator) if !os.is_dir(pdir) { os.mkdir_all(pdir) } v.pref.out_name = '${out_dir}.o' // v.out_name println('Building ${v.pref.out_name}...') + // println('v.table.imports:') + // println(v.table.imports) } debug_mode := v.pref.is_debug mut debug_options := '-g3' @@ -290,7 +292,14 @@ fn (mut v Builder) cc() { os.system('$vexe build module vlib${os.path_separator}builtin') } */ + // TODO add `.unique()` to V arrays + mut unique_imports := []string{cap: v.table.imports.len} for imp in v.table.imports { + if imp !in unique_imports { + unique_imports << imp + } + } + for imp in unique_imports { if imp.contains('vweb') { continue } @@ -385,8 +394,8 @@ fn (mut v Builder) cc() { } // Without these libs compilation will fail on Linux // || os.user_os() == 'linux' - if !v.pref.is_bare && v.pref.build_mode != .build_module && - v.pref.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] { + if !v.pref.is_bare && v.pref.build_mode != .build_module && v.pref.os in + [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] { linker_flags << '-lm' linker_flags << '-lpthread' // -ldl is a Linux only thing. BSDs have it in libc. @@ -529,7 +538,8 @@ fn (mut v Builder) cc() { println('install upx with `brew install upx`') } $if linux { - println('install upx\n' + 'for example, on Debian/Ubuntu run `sudo apt install upx`') + println('install upx\n' + + 'for example, on Debian/Ubuntu run `sudo apt install upx`') } $if windows { // :) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 7c0e6a83e8..cf1f13bd75 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -132,7 +132,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string fn_decl: 0 autofree: true indent: -1 - module_built: pref.path.after('vlib/') + module_built: pref.path.after('vlib/').replace('/', '.') } for mod in g.table.modules { g.inits[mod] = strings.new_builder(100) @@ -740,6 +740,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { if !node.name.starts_with(g.module_built + '.') && node.mod != g.module_built.after('/') { // Skip functions that don't have to be generated // for this module. + println('skip bm $node.name mode=$node.mod module_built=$g.module_built') skip = true } if g.is_builtin_mod && g.module_built == 'builtin' { diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 0e4c8f380e..1e81adbd02 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -7,6 +7,9 @@ import v.ast import v.table import v.util +pub fn kek_cheburek() { +} + fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) { if it.language == .c { // || it.no_body { @@ -277,9 +280,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { g.write('$styp $tmp_opt = ') } if node.is_method && !node.is_field { - if node.name == 'writeln' && g.pref.experimental && - node.args.len > 0 && node.args[0].expr is ast.StringInterLiteral && - g.table.get_type_symbol(node.receiver_type).name == 'strings.Builder' { + if node.name == 'writeln' && g.pref.experimental && node.args.len > 0 && node.args[0].expr is + ast.StringInterLiteral && g.table.get_type_symbol(node.receiver_type).name == 'strings.Builder' { g.string_inter_literal_sb_optimized(node) } else { g.method_call(node) diff --git a/vlib/v/parser/module.v b/vlib/v/parser/module.v index 3f13650aa8..5621be6cda 100644 --- a/vlib/v/parser/module.v +++ b/vlib/v/parser/module.v @@ -9,6 +9,7 @@ pub fn (p &Parser) known_import(mod string) bool { } fn (p &Parser) prepend_mod(name string) string { + // println('prepend_mod() name=$name p.mod=$p.mod expr_mod=$p.expr_mod') if p.expr_mod != '' { return p.expr_mod + '.' + name } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a98f621632..95f187d5b3 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1342,7 +1342,20 @@ fn (mut p Parser) module_decl() ast.Module { } module_pos = module_pos.extend(pos) } - full_mod := p.table.qualify_module(name, p.file_name) + mut full_mod := p.table.qualify_module(name, p.file_name) + if p.pref.build_mode == .build_module && !full_mod.contains('.') { + // A hack to make building vlib modules work + // `v build-module v.gen` will result in `full_mod = "gen"`, not "v.gen", + // because the module being built + // is not imported. + // So here we fetch the name of the module by looking at the path that's being built. + word := p.pref.path.after('/') + if full_mod == word { + full_mod = p.pref.path.after('vlib/').replace('/', '.') + // println('new full mod =$full_mod') + } + // println('file_name=$p.file_name path=$p.pref.path') + } p.mod = full_mod p.builtin_mod = p.mod == 'builtin' return ast.Module{ diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index db98b18d24..909aeeeaca 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -489,6 +489,7 @@ pub fn (t &Table) mktyp(typ Type) Type { // this is not optimal pub fn (table &Table) qualify_module(mod, file_path string) string { for m in table.imports { + //if m.contains('gen') { println('qm=$m') } if m.contains('.') && m.contains(mod) { m_parts := m.split('.') m_path := m_parts.join(os.path_separator)