builder/cgen: get -usecache working for self cimpilation

pull/6031/head
joe-conigliaro 2020-07-31 20:10:19 +10:00
parent eb47ce1e83
commit 1ea511b530
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 62 additions and 50 deletions

View File

@ -292,47 +292,58 @@ fn (mut v Builder) cc() {
if v.pref.build_mode == .build_module { if v.pref.build_mode == .build_module {
args << '-c' args << '-c'
} else if v.pref.use_cache { } else if v.pref.use_cache {
/* mut built_modules := []string{}
QTODO builtin_obj_path := pref.default_module_path + os.path_separator + 'cache' + os.path_separator + 'vlib' + os.path_separator + 'builtin.o'
builtin_o_path := os.join_path(pref.default_module_path, 'cache', 'vlib', 'builtin.o') if !os.exists(builtin_obj_path) {
args << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed os.system('$vexe build-module vlib/builtin')
if os.exists(builtin_o_path) {
libs = builtin_o_path
} }
else { libs += ' ' + builtin_obj_path
println('$builtin_o_path not found... building module builtin') for ast_file in v.parsed_files {
os.system('$vexe build module vlib${os.path_separator}builtin') for imp_stmt in ast_file.imports {
} imp := imp_stmt.mod
*/ if imp in built_modules {
// TODO add `.unique()` to V arrays continue
mut unique_imports := []string{cap: v.table.imports.len} }
for imp in v.table.imports { // not working
if imp !in unique_imports { if imp == 'webview' {
unique_imports << imp continue
} }
} // println('cache: import "$imp"')
for imp in unique_imports { mod_path := imp.replace('.', os.path_separator)
if imp.contains('vweb') {
continue // TODO: to get import path all imports (even relative) we can use:
} // import_path := v.find_module_path(imp, ast_file.path) or {
// not working // verror('cannot import module "$imp" (not found)')
if imp == 'webview' { // break
continue // }
}
// println('cache: import "$imp"') // The problem is cmd/v is in module main and imports
imp_path := imp.replace('.', os.path_separator) // the relative module named help, which is built as cmd.v.help not help
path := '$pref.default_module_path${os.path_separator}cache${os.path_separator}vlib$os.path_separator${imp_path}.o' // currently this got this workign by building into main, see ast.FnDecl in cgen
// println('adding ${imp_path}.o') if imp == 'help' {
if os.exists(path) { continue
libs += ' ' + path }
} else { // we are skipping help manually above, this code will skip all relative imports
println('$path not found... building module $imp') // if os.is_dir(af_base_dir + os.path_separator + mod_path) {
os.system('$vexe build-module vlib$os.path_separator$imp_path') // continue
} // }
if path.ends_with('vlib/ui.o') {
args << '-framework Cocoa -framework Carbon' imp_path := 'vlib' + os.path_separator + mod_path
cache_path := pref.default_module_path + os.path_separator + 'cache'
obj_path := cache_path + os.path_separator + '${imp_path}.o'
if os.exists(obj_path) {
libs += ' ' + obj_path
} else {
println('$obj_path not found... building module $imp')
os.system('$vexe build-module $imp_path')
}
if obj_path.ends_with('vlib/ui.o') {
args << '-framework Cocoa -framework Carbon'
}
built_modules << imp
} }
} }
} }
if v.pref.sanitize { if v.pref.sanitize {
args << '-fsanitize=leak' args << '-fsanitize=leak'
@ -386,19 +397,20 @@ fn (mut v Builder) cc() {
args << '-fpermissive' args << '-fpermissive'
args << '-w' args << '-w'
} }
// TODO: why is this duplicated from above?
if v.pref.use_cache { if v.pref.use_cache {
// vexe := pref.vexe_path() // vexe := pref.vexe_path()
cached_modules := ['builtin', 'os', 'math', 'strconv', 'strings', 'hash'] // , 'strconv.ftoa'] // cached_modules := ['builtin', 'os', 'math', 'strconv', 'strings', 'hash'], // , 'strconv.ftoa']
for cfile in cached_modules { // for cfile in cached_modules {
ofile := os.join_path(pref.default_module_path, 'cache', 'vlib', cfile.replace('.', '/') + // ofile := os.join_path(pref.default_module_path, 'cache', 'vlib', cfile.replace('.', '/') +
'.o') // '.o')
if !os.exists(ofile) { // if !os.exists(ofile) {
println('${cfile}.o is missing. Building...') // println('${cfile}.o is missing. Building...')
println('$vexe build-module vlib/$cfile') // println('$vexe build-module vlib/$cfile')
os.system('$vexe build-module vlib/$cfile') // os.system('$vexe build-module vlib/$cfile')
} // }
args << ofile // args << ofile
} // }
if !is_cc_tcc { if !is_cc_tcc {
$if linux { $if linux {
linker_flags << '-Xlinker -z' linker_flags << '-Xlinker -z'

View File

@ -755,7 +755,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
if g.pref.use_cache { if g.pref.use_cache {
// We are using prebuilt modules, we do not need to generate // We are using prebuilt modules, we do not need to generate
// their functions in main.c. // their functions in main.c.
if node.mod != 'main' { if node.mod != 'main' && node.mod != 'help' {
skip = true skip = true
} }
} }