Skip to content

Commit e97dfaa

Browse files
authored
v.builder: support compiling asm .S files to .o files, mentioned in #flag directives (needed for #26185) (#26211)
1 parent 2e983cf commit e97dfaa

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

‎vlib/v/builder/cc.v‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,25 +1175,17 @@ fn (mut b Builder) build_thirdparty_obj_files() {
11751175
}
11761176
}
11771177

1178+
enum SourceKind {
1179+
c
1180+
cpp
1181+
asm
1182+
unknown
1183+
}
1184+
11781185
fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflags []cflag.CFlag) {
11791186
trace_thirdparty_obj_files := 'trace_thirdparty_obj_files' in v.pref.compile_defines
11801187
obj_path := os.real_path(path)
1181-
mut cfile := '${obj_path[..obj_path.len - 2]}.c'
1182-
mut cpp_file := false
1183-
if !os.exists(cfile) {
1184-
// Guessed C file does not exist, so it may be a CPP file
1185-
cfile += 'pp'
1186-
cpp_file = true
1187-
}
11881188
opath := v.pref.cache_manager.mod_postfix_with_key2cpath(mod, '.o', obj_path)
1189-
mut rebuild_reason_message := '${os.quoted_path(obj_path)} not found, building it in ${os.quoted_path(opath)} ...'
1190-
if os.exists(opath) {
1191-
if os.exists(cfile) && os.file_last_mod_unix(opath) < os.file_last_mod_unix(cfile) {
1192-
rebuild_reason_message = '${os.quoted_path(opath)} is older than ${os.quoted_path(cfile)}, rebuilding ...'
1193-
} else {
1194-
return
1195-
}
1196-
}
11971189
if os.exists(obj_path) {
11981190
// Some .o files are distributed with no source
11991191
// for example thirdparty\tcc\lib\openlibm.o
@@ -1202,6 +1194,29 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag
12021194
os.cp(obj_path, opath) or { panic(err) }
12031195
return
12041196
}
1197+
base := obj_path[..obj_path.len - 2]
1198+
1199+
source_kind, source_file := if os.exists(base + '.c') {
1200+
SourceKind.c, base + '.c'
1201+
} else if os.exists(base + '.cpp') {
1202+
SourceKind.cpp, base + '.cpp'
1203+
} else if os.exists(base + '.S') {
1204+
SourceKind.asm, base + '.S'
1205+
} else {
1206+
SourceKind.unknown, ''
1207+
}
1208+
if source_kind == .unknown {
1209+
eprintln('> File not found: ${base}{.c,.cpp,.S}')
1210+
verror('build_thirdparty_obj_file only support .c, .cpp, and .S source file.')
1211+
}
1212+
mut rebuild_reason_message := '${os.quoted_path(obj_path)} not found, building it in ${os.quoted_path(opath)} ...'
1213+
if os.exists(opath) {
1214+
if os.file_last_mod_unix(opath) < os.file_last_mod_unix(source_file) {
1215+
rebuild_reason_message = '${os.quoted_path(opath)} is older than ${os.quoted_path(source_file)}, rebuilding ...'
1216+
} else {
1217+
return
1218+
}
1219+
}
12051220
if v.pref.is_verbose {
12061221
println(rebuild_reason_message)
12071222
}
@@ -1210,15 +1225,22 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag
12101225
os.chdir(v.pref.vroot) or {}
12111226

12121227
mut all_options := []string{}
1213-
all_options << v.pref.third_party_option
1214-
all_options << moduleflags.c_options_before_target()
1228+
if source_kind != .asm {
1229+
all_options << v.pref.third_party_option
1230+
all_options << moduleflags.c_options_before_target()
1231+
}
12151232
all_options << '-o ${v.tcc_quoted_path(opath)}'
1216-
all_options << '-c ${v.tcc_quoted_path(cfile)}'
1217-
cc_options := v.thirdparty_object_args(v.ccoptions, all_options, cpp_file).join(' ')
1233+
all_options << '-c ${v.tcc_quoted_path(source_file)}'
1234+
cc_options := if source_kind == .asm {
1235+
''
1236+
} else {
1237+
cpp_file := source_kind == .cpp
1238+
v.thirdparty_object_args(v.ccoptions, all_options, cpp_file).join(' ')
1239+
}
12181240

12191241
// If the third party object file requires a CPP file compilation, switch to a CPP compiler
12201242
mut ccompiler := v.pref.ccompiler
1221-
if cpp_file {
1243+
if source_kind == .cpp {
12221244
if trace_thirdparty_obj_files {
12231245
println('>>> build_thirdparty_obj_files switched from compiler "${ccompiler}" to "${v.pref.cppcompiler}"')
12241246
}
@@ -1234,7 +1256,7 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag
12341256
eprintln('> Failed build_thirdparty_obj_file cmd')
12351257
eprintln('> mod: ${mod}')
12361258
eprintln('> path: ${path}')
1237-
eprintln('> cfile: ${cfile}')
1259+
eprintln('> source_file: ${source_file}')
12381260
eprintln('> wd before cmd: ${current_folder}')
12391261
eprintln('> getwd for cmd: ${v.pref.vroot}')
12401262
eprintln('> cmd: ${cmd}')

0 commit comments

Comments
 (0)