Skip to content

Commit 85973b9

Browse files
committed
v.builder,v.cflags: improve the cross compilation support for programs using libraries like raylib, that have specific linking order needs, by supporting -lraylib@START_LIBS
1 parent d970a8f commit 85973b9

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

vlib/v/builder/cc.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ fn (mut c Builder) cc_windows_cross() {
11301130
} else {
11311131
args << cflags.c_options_after_target()
11321132
}
1133+
11331134
if current_os !in ['macos', 'linux', 'termux'] {
11341135
println(current_os)
11351136
panic('your platform is not supported yet')
@@ -1144,8 +1145,9 @@ fn (mut c Builder) cc_windows_cross() {
11441145

11451146
all_args << args
11461147
all_args << c.get_subsystem_flag()
1147-
all_args << c.ccoptions.linker_flags
1148-
all_args << '${c.pref.ldflags}'
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
1149+
all_args << c.pref.ldflags
1150+
11491151
c.dump_c_options(all_args)
11501152
mut cmd := cross_compiler_name_path + ' ' + all_args.join(' ')
11511153
// cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 ${pref.default_module_path}/$c.out_name_c'

vlib/v/cflag/cflags.v

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn (cflags []CFlag) c_options_before_target() []string {
100100
mut args := []string{cap: defines.len + others.len}
101101
args << defines
102102
args << others
103-
return args
103+
return uniq_non_empty(args)
104104
}
105105

106106
pub fn (cflags []CFlag) c_options_after_target() []string {
@@ -116,7 +116,7 @@ pub fn (cflags []CFlag) c_options_without_object_files() []string {
116116
}
117117
args << flag.format() or { continue }
118118
}
119-
return args
119+
return uniq_non_empty(args)
120120
}
121121

122122
pub fn (cflags []CFlag) c_options_only_object_files() []string {
@@ -128,7 +128,7 @@ pub fn (cflags []CFlag) c_options_only_object_files() []string {
128128
args << flag.format() or { continue }
129129
}
130130
}
131-
return args.filter(it != '')
131+
return uniq_non_empty(args)
132132
}
133133

134134
pub fn (cflags []CFlag) defines_others_libs() ([]string, []string, []string) {
@@ -137,6 +137,10 @@ pub fn (cflags []CFlag) defines_others_libs() ([]string, []string, []string) {
137137
mut others := []string{}
138138
mut libs := []string{}
139139
for copt in copts_without_obj_files {
140+
if copt.ends_with('@START_LIBS') {
141+
libs.insert(0, copt.all_before('@START_LIBS'))
142+
continue
143+
}
140144
if copt.starts_with('-l') {
141145
libs << copt
142146
continue
@@ -145,11 +149,34 @@ pub fn (cflags []CFlag) defines_others_libs() ([]string, []string, []string) {
145149
libs << '"${copt}"'
146150
continue
147151
}
152+
153+
if copt.ends_with('@START_DEFINES') {
154+
defines.insert(0, copt.all_before('@START_DEFINES'))
155+
continue
156+
}
148157
if copt.starts_with('-D') {
149158
defines << copt
150159
continue
151160
}
161+
162+
if copt.ends_with('@START_OTHERS') {
163+
others.insert(0, copt.all_before('@START_OTHERS'))
164+
continue
165+
}
152166
others << copt
153167
}
154-
return defines, others, libs
168+
return uniq_non_empty(defines), uniq_non_empty(others), uniq_non_empty(libs)
169+
}
170+
171+
fn uniq_non_empty(args []string) []string {
172+
mut uniq_args := []string{}
173+
for a in args {
174+
if a == '' {
175+
continue
176+
}
177+
if a !in uniq_args {
178+
uniq_args << a
179+
}
180+
}
181+
return uniq_args
155182
}

0 commit comments

Comments
 (0)