Skip to content

Commit 90c638e

Browse files
authored
cgen,v.builder: cleanup const generation/checks; fix cross compilation of .dll files from linux->windows (#23971)
1 parent d44d9fa commit 90c638e

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

vlib/v/builder/cc.v

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,20 @@ fn (mut c Builder) cc_windows_cross() {
11251125
// add the thirdparty .o files, produced by all the #flag directives:
11261126
args << cflags.c_options_only_object_files()
11271127
args << os.quoted_path(c.out_name_c)
1128+
1129+
mut c_options_after_target := []string{}
11281130
if c.pref.ccompiler == 'msvc' {
1129-
args << cflags.c_options_after_target_msvc()
1131+
c_options_after_target << cflags.c_options_after_target_msvc()
11301132
} else {
1131-
args << cflags.c_options_after_target()
1133+
c_options_after_target << cflags.c_options_after_target()
1134+
}
1135+
for lf in c.ccoptions.linker_flags {
1136+
if lf in c_options_after_target {
1137+
continue
1138+
}
1139+
c_options_after_target << lf
11321140
}
1141+
args << c_options_after_target
11331142

11341143
if current_os !in ['macos', 'linux', 'termux'] {
11351144
println(current_os)
@@ -1145,9 +1154,7 @@ fn (mut c Builder) cc_windows_cross() {
11451154

11461155
all_args << args
11471156
all_args << c.get_subsystem_flag()
1148-
// Note: c.ccoptions.linker_flags was already in cflags.c_options_after_target(), which is in args, so no need to add it again here
11491157
all_args << c.pref.ldflags
1150-
11511158
c.dump_c_options(all_args)
11521159
mut cmd := cross_compiler_name_path + ' ' + all_args.join(' ')
11531160
// cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 ${pref.default_module_path}/$c.out_name_c'

vlib/v/gen/c/consts_and_globals.v

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
3636
mut const_name := '_const_' + name
3737
if g.pref.translated && !g.is_builtin_mod
3838
&& !util.module_is_builtin(field.name.all_before_last('.')) {
39-
mut x := util.no_dots(field.name)
40-
if x.starts_with('main__') {
41-
const_name = x['main__'.len..]
39+
if name.starts_with('main__') {
40+
const_name = name.all_after_first('main__')
4241
}
4342
}
4443
if !g.is_builtin_mod {
@@ -55,7 +54,8 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
5554
&& (!g.is_cc_msvc || field.expr.elem_type != ast.string_type) && elems_are_const {
5655
styp := g.styp(field.expr.typ)
5756
val := g.expr_string(field.expr)
58-
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
57+
// eprintln('> const_name: ${const_name} | name: ${name} | styp: ${styp} | val: ${val}')
58+
g.global_const_defs[name] = GlobalConstDef{
5959
mod: field.mod
6060
def: '${g.static_non_parallel}${styp} ${const_name} = ${val}; // fixed array const'
6161
dep_names: g.table.dependent_names_in_expr(field_expr)
@@ -72,7 +72,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
7272
ast.StringLiteral {
7373
val := g.expr_string(field.expr)
7474
typ := if field.expr.language == .c { 'char*' } else { 'string' }
75-
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
75+
g.global_const_defs[name] = GlobalConstDef{
7676
mod: field.mod
7777
def: '${typ} ${const_name}; // a string literal, inited later'
7878
init: '\t${const_name} = ${val};'
@@ -121,7 +121,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
121121
if field.expr.expr.is_fixed && g.pref.build_mode != .build_module {
122122
styp := g.styp(field.expr.typ)
123123
val := g.expr_string(field.expr.expr)
124-
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
124+
g.global_const_defs[name] = GlobalConstDef{
125125
mod: field.mod
126126
def: '${g.static_non_parallel}${styp} ${const_name} = ${val}; // fixed array const'
127127
dep_names: g.table.dependent_names_in_expr(field_expr)
@@ -435,6 +435,7 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
435435
attributes += '__attribute__ ((section ("${attr.arg}"))) '
436436
}
437437
for field in node.fields {
438+
name := c_name(field.name)
438439
if g.pref.skip_unused {
439440
if field.name !in g.table.used_features.used_globals {
440441
$if trace_skip_unused_globals ? {
@@ -462,7 +463,7 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
462463
def_builder.write_string('${extern}${visibility_kw}${modifier}${styp} ${attributes}${field.name}')
463464
if cextern {
464465
def_builder.writeln('; // global 2')
465-
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
466+
g.global_const_defs[name] = GlobalConstDef{
466467
mod: node.mod
467468
def: def_builder.str()
468469
order: -1
@@ -515,7 +516,7 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
515516
}
516517
}
517518
def_builder.writeln('; // global 6')
518-
g.global_const_defs[util.no_dots(field.name)] = GlobalConstDef{
519+
g.global_const_defs[name] = GlobalConstDef{
519520
mod: node.mod
520521
def: def_builder.str()
521522
init: init

0 commit comments

Comments
 (0)