Skip to content

Commit 735046a

Browse files
authored
v.builder: cleanup quoted path handling (followup to #23686) (#23688)
1 parent b94da8a commit 735046a

4 files changed

Lines changed: 99 additions & 41 deletions

File tree

‎vlib/v/builder/cbuilder/parallel_cc.v‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,26 @@ fn parallel_cc(mut b builder.Builder, result c.GenOutput) ! {
107107
return error_with_code('failed parallel C compilation', failed)
108108
}
109109

110-
obj_files := fnames.map(it.replace('.c', '.o')).join(' ')
111-
link_cmd := '${cc} ${scompile_args_for_linker} -o ${os.quoted_path(b.pref.out_name)} ${tmp_dir}/out_0.o ${obj_files} ${tmp_dir}/out_x.o ${slinker_args} ${cc_ldflags}'
110+
mut ofiles := []string{}
111+
for f in fnames {
112+
fo := f.replace('.c', '.o')
113+
ofiles << os.quoted_path(fo)
114+
}
115+
obj_files := ofiles.join(' ')
116+
117+
alink := [
118+
cc,
119+
scompile_args_for_linker,
120+
'-o',
121+
os.quoted_path(b.pref.out_name),
122+
os.quoted_path('${tmp_dir}/out_0.o'),
123+
obj_files,
124+
os.quoted_path('${tmp_dir}/out_x.o'),
125+
slinker_args,
126+
cc_ldflags,
127+
]
128+
link_cmd := alink.join(' ')
129+
112130
sw_link := time.new_stopwatch()
113131
link_res := os.execute(link_cmd)
114132
eprint_result_time(sw_link, 'link_cmd', link_cmd, link_res)

‎vlib/v/builder/cc.v‎

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) {
380380
}
381381
// The C file we are compiling
382382
if !v.pref.parallel_cc { // parallel_cc uses its own split up c files
383-
ccoptions.source_args << '"${v.out_name_c}"'
383+
ccoptions.source_args << v.tcc_quoted_path(v.out_name_c)
384384
}
385385
// Min macos version is mandatory I think?
386386
if v.pref.os == .macos {
@@ -573,7 +573,11 @@ fn (mut v Builder) thirdparty_object_args(ccoptions CcompilerOptions, middle []s
573573
// all << ccoptions.env_ldflags
574574
// all << ccoptions.ldflags
575575
if cross_compiling_from_macos_to_linux {
576-
all << '-I${os.quoted_path(sysroot)}/include' // add the system include/ folder after everything else, so that local folders like thirdparty/mbedtls have a chance to supply their own headers
576+
// add the system include/ folder after everything else,
577+
// so that local folders like thirdparty/mbedtls have a
578+
// chance to supply their own headers
579+
all << '-I'
580+
all << os.quoted_path('${sysroot}/include')
577581
}
578582
return all
579583
}
@@ -603,20 +607,28 @@ fn (mut v Builder) setup_output_name() {
603607
// println(v.ast.imports)
604608
}
605609
if os.is_dir(v.pref.out_name) {
606-
verror("'${v.pref.out_name}' is a directory")
610+
verror('${os.quoted_path(v.pref.out_name)} is a directory')
607611
}
608612
if !v.pref.parallel_cc {
609613
// parallel_cc sets its own `-o out_n.o`
610-
v.ccoptions.o_args << '-o "${v.pref.out_name}"'
614+
v.ccoptions.o_args << '-o ${v.tcc_quoted_path(v.pref.out_name)}'
611615
}
612616
}
613617

618+
pub fn (mut v Builder) tcc_quoted_path(p string) string {
619+
if v.ccoptions.cc == .tcc && !v.pref.no_rsp {
620+
// tcc has a bug, that prevents it from being able to parse names quoted with ' in .rsp files :-|
621+
return '"${p}"'
622+
}
623+
return os.quoted_path(p)
624+
}
625+
614626
pub fn (mut v Builder) cc() {
615627
if os.executable().contains('vfmt') {
616628
return
617629
}
618630
if v.pref.is_verbose {
619-
println('builder.cc() pref.out_name="${v.pref.out_name}"')
631+
println('builder.cc() pref.out_name=${os.quoted_path(v.pref.out_name)}')
620632
}
621633
if v.pref.only_check_syntax {
622634
if v.pref.is_verbose {
@@ -642,7 +654,7 @@ pub fn (mut v Builder) cc() {
642654
ends_with_js := v.pref.out_name.ends_with('.js')
643655
if ends_with_c || ends_with_js {
644656
v.pref.skip_running = true
645-
msg_mv := 'os.mv_by_cp ${v.out_name_c} => ${v.pref.out_name}'
657+
msg_mv := 'os.mv_by_cp ${os.quoted_path(v.out_name_c)} => ${os.quoted_path(v.pref.out_name)}'
646658
util.timing_start(msg_mv)
647659
// v.out_name_c may be on a different partition than v.out_name
648660
os.mv_by_cp(v.out_name_c, v.pref.out_name) or { panic(err) }
@@ -851,7 +863,7 @@ fn (mut b Builder) ensure_linuxroot_exists(sysroot string) {
851863
}
852864
if !os.is_dir(sysroot) {
853865
println('Downloading files for Linux cross compilation (~77MB) ...')
854-
os.system('git clone ${crossrepo_url} ${sysroot}')
866+
os.system('git clone "${crossrepo_url}" ${os.quoted_path(sysroot)}')
855867
if !os.exists(sysroot_git_config_path) {
856868
verror('Failed to clone `${crossrepo_url}` to `${sysroot}`')
857869
}
@@ -868,7 +880,7 @@ fn (mut b Builder) ensure_freebsdroot_exists(sysroot string) {
868880
}
869881
if !os.is_dir(sysroot) {
870882
println('Downloading files for FreeBSD cross compilation (~458MB) ...')
871-
os.system('git clone ${crossrepo_url} ${sysroot}')
883+
os.system('git clone "${crossrepo_url}" ${os.quoted_path(sysroot)}')
872884
if !os.exists(sysroot_git_config_path) {
873885
verror('Failed to clone `${crossrepo_url}` to `${sysroot}`')
874886
}
@@ -899,13 +911,12 @@ fn (mut b Builder) cc_linux_cross() {
899911
mut cc_args := []string{cap: 20}
900912
cc_args << '-w'
901913
cc_args << '-fPIC'
902-
cc_args << '-c'
903914
cc_args << '-target x86_64-linux-gnu'
904915
cc_args << defines
905-
cc_args << '-I ${sysroot}/include '
916+
cc_args << '-I ${os.quoted_path('${sysroot}/include')} '
906917
cc_args << others
907-
cc_args << '-o "${obj_file}"'
908-
cc_args << '-c "${b.out_name_c}"'
918+
cc_args << '-o ${os.quoted_path(obj_file)}'
919+
cc_args << '-c ${os.quoted_path(b.out_name_c)}'
909920
cc_args << libs
910921
b.dump_c_options(cc_args)
911922
mut cc_name := 'cc'
@@ -924,11 +935,29 @@ fn (mut b Builder) cc_linux_cross() {
924935
verror(cc_res.output)
925936
return
926937
}
927-
mut linker_args := ['-L${sysroot}/usr/lib/x86_64-linux-gnu/',
928-
'-L${sysroot}/lib/x86_64-linux-gnu', '--sysroot=${sysroot}', '-v', '-o ${out_name}',
929-
'-m elf_x86_64', '-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2',
930-
'${sysroot}/crt1.o ${sysroot}/crti.o ${obj_file}', '-lc', '-lcrypto', '-lssl', '-lpthread',
931-
'${sysroot}/crtn.o', '-lm', '-ldl']
938+
mut linker_args := [
939+
'-L',
940+
os.quoted_path('${sysroot}/usr/lib/x86_64-linux-gnu/'),
941+
'-L',
942+
os.quoted_path('${sysroot}/lib/x86_64-linux-gnu'),
943+
'--sysroot=' + os.quoted_path(sysroot),
944+
'-v',
945+
'-o',
946+
os.quoted_path(out_name),
947+
'-m elf_x86_64',
948+
'-dynamic-linker',
949+
os.quoted_path('/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2'),
950+
os.quoted_path('${sysroot}/crt1.o'),
951+
os.quoted_path('${sysroot}/crti.o'),
952+
os.quoted_path(obj_file),
953+
'-lc',
954+
'-lcrypto',
955+
'-lssl',
956+
'-lpthread',
957+
os.quoted_path('${sysroot}/crtn.o'),
958+
'-lm',
959+
'-ldl',
960+
]
932961
linker_args << cflags.c_options_only_object_files()
933962
// -ldl
934963
b.dump_c_options(linker_args)
@@ -937,8 +966,6 @@ fn (mut b Builder) cc_linux_cross() {
937966
ldlld = 'ld.lld.exe'
938967
}
939968
linker_cmd := '${b.quote_compiler_name(ldlld)} ' + linker_args.join(' ')
940-
// s = s.replace('SYSROOT', sysroot) // TODO: $ inter bug
941-
// s = s.replace('-o hi', '-o ' + c.pref.out_name)
942969
if b.pref.show_cc {
943970
println(linker_cmd)
944971
}
@@ -967,14 +994,17 @@ fn (mut b Builder) cc_freebsd_cross() {
967994
mut cc_args := []string{cap: 20}
968995
cc_args << '-w'
969996
cc_args << '-fPIC'
970-
cc_args << '-c'
971997
cc_args << '-target x86_64-unknown-freebsd14.0' // TODO custom freebsd versions
972998
cc_args << defines
973-
cc_args << '-I ${sysroot}/include '
974-
cc_args << '-I ${sysroot}/usr/include '
999+
cc_args << '-I'
1000+
cc_args << os.quoted_path('${sysroot}/include')
1001+
cc_args << '-I'
1002+
cc_args << os.quoted_path('${sysroot}/usr/include')
9751003
cc_args << others
976-
cc_args << '-o "${obj_file}"'
977-
cc_args << '-c "${b.out_name_c}"'
1004+
cc_args << '-o'
1005+
cc_args << os.quoted_path(obj_file)
1006+
cc_args << '-c'
1007+
cc_args << os.quoted_path(b.out_name_c)
9781008
cc_args << libs
9791009
b.dump_c_options(cc_args)
9801010
mut cc_name := b.pref.vcross_compiler_name()
@@ -993,10 +1023,22 @@ fn (mut b Builder) cc_freebsd_cross() {
9931023
verror(cc_res.output)
9941024
return
9951025
}
996-
mut linker_args := ['-L${sysroot}/lib/', '-L${sysroot}/usr/lib/', '--sysroot=${sysroot}', '-v',
997-
'-o ${out_name}', '-m elf_x86_64', '-dynamic-linker /libexec/ld-elf.so.1',
998-
'${sysroot}/usr/lib/crt1.o ${sysroot}/usr/lib/crti.o ${obj_file}',
999-
'${sysroot}/usr/lib/crtn.o']
1026+
mut linker_args := [
1027+
'-L',
1028+
os.quoted_path('${sysroot}/lib/'),
1029+
'-L',
1030+
os.quoted_path('${sysroot}/usr/lib/'),
1031+
'--sysroot=' + os.quoted_path(sysroot),
1032+
'-v',
1033+
'-o',
1034+
os.quoted_path(out_name),
1035+
'-m elf_x86_64',
1036+
'-dynamic-linker /libexec/ld-elf.so.1',
1037+
os.quoted_path('${sysroot}/usr/lib/crt1.o'),
1038+
os.quoted_path('${sysroot}/usr/lib/crti.o'),
1039+
os.quoted_path(obj_file),
1040+
os.quoted_path('${sysroot}/usr/lib/crtn.o'),
1041+
]
10001042
linker_args << '-lc' // needed for fwrite, strlen etc
10011043
linker_args << '-lexecinfo' // needed for backtrace
10021044
linker_args << cflags.c_options_only_object_files() // support custom module defined linker flags
@@ -1006,8 +1048,6 @@ fn (mut b Builder) cc_freebsd_cross() {
10061048
// mut ldlld := '${sysroot}/ld.lld'
10071049
mut ldlld := b.pref.vcross_linker_name()
10081050
linker_cmd := '${b.quote_compiler_name(ldlld)} ' + linker_args.join(' ')
1009-
// s = s.replace('SYSROOT', sysroot) // TODO: $ inter bug
1010-
// s = s.replace('-o hi', '-o ' + c.pref.out_name)
10111051
if b.pref.show_cc {
10121052
println(linker_cmd)
10131053
}
@@ -1065,13 +1105,13 @@ fn (mut c Builder) cc_windows_cross() {
10651105
}
10661106
mut libs := []string{}
10671107
if false && c.pref.build_mode == .default_mode {
1068-
builtin_o := '"${pref.default_module_path}/vlib/builtin.o"'
1069-
libs << builtin_o
1108+
builtin_o := '${pref.default_module_path}/vlib/builtin.o'
1109+
libs << os.quoted_path(builtin_o)
10701110
if !os.exists(builtin_o) {
10711111
verror('${builtin_o} not found')
10721112
}
10731113
for imp in c.table.imports {
1074-
libs << '"${pref.default_module_path}/vlib/${imp}.o"'
1114+
libs << os.quoted_path('${pref.default_module_path}/vlib/${imp}.o')
10751115
}
10761116
}
10771117
// add the thirdparty .o files, produced by all the #flag directives:
@@ -1143,10 +1183,10 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag
11431183
cpp_file = true
11441184
}
11451185
opath := v.pref.cache_manager.mod_postfix_with_key2cpath(mod, '.o', obj_path)
1146-
mut rebuild_reason_message := '${obj_path} not found, building it in ${opath} ...'
1186+
mut rebuild_reason_message := '${os.quoted_path(obj_path)} not found, building it in ${os.quoted_path(opath)} ...'
11471187
if os.exists(opath) {
11481188
if os.exists(cfile) && os.file_last_mod_unix(opath) < os.file_last_mod_unix(cfile) {
1149-
rebuild_reason_message = '${opath} is older than ${cfile}, rebuilding ...'
1189+
rebuild_reason_message = '${os.quoted_path(opath)} is older than ${os.quoted_path(cfile)}, rebuilding ...'
11501190
} else {
11511191
return
11521192
}
@@ -1169,8 +1209,8 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag
11691209
mut all_options := []string{}
11701210
all_options << v.pref.third_party_option
11711211
all_options << moduleflags.c_options_before_target()
1172-
all_options << '-o ${os.quoted_path(opath)}'
1173-
all_options << '-c ${os.quoted_path(cfile)}'
1212+
all_options << '-o ${v.tcc_quoted_path(opath)}'
1213+
all_options << '-c ${v.tcc_quoted_path(cfile)}'
11741214
cc_options := v.thirdparty_object_args(v.ccoptions, all_options, cpp_file).join(' ')
11751215

11761216
// If the third party object file requires a CPP file compilation, switch to a CPP compiler

‎vlib/v/builder/compile.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn (mut b Builder) run_compiled_executable_and_exit() {
139139
}
140140
mut ret := 0
141141
if b.pref.use_os_system_to_run {
142-
command_to_run := run_file + ' ' + run_args.join(' ')
142+
command_to_run := os.quoted_path(run_file) + ' ' + run_args.join(' ')
143143
ret = os.system(command_to_run)
144144
// eprintln('> ret: ${ret:5} | command_to_run: ${command_to_run}')
145145
} else {

‎vlib/v/builder/msvc_windows.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub fn (mut v Builder) cc_msvc() {
338338
a << real_libs.join(' ')
339339
a << '/link'
340340
a << '/nologo' // NOTE: /NOLOGO is explicitly not recognised!
341-
a << '/OUT:"${v.pref.out_name}"'
341+
a << '/OUT:${os.quoted_path(v.pref.out_name)}'
342342
a << r.library_paths()
343343
if !all_cflags.contains('/DEBUG') {
344344
// only use /DEBUG, if the user *did not* provide its own:

0 commit comments

Comments
 (0)