2021-12-12 20:10:43 +01:00
|
|
|
module cbuilder
|
2020-04-15 23:16:49 +02:00
|
|
|
|
2020-04-25 17:49:16 +02:00
|
|
|
import os
|
|
|
|
import v.pref
|
2021-02-05 15:34:56 +01:00
|
|
|
import v.util
|
2021-12-12 20:10:43 +01:00
|
|
|
import v.builder
|
2021-02-02 15:41:51 +01:00
|
|
|
import v.gen.c
|
2020-04-15 23:16:49 +02:00
|
|
|
|
2021-12-12 20:10:43 +01:00
|
|
|
pub fn start() {
|
|
|
|
mut args_and_flags := util.join_env_vflags_and_os_args()[1..]
|
|
|
|
prefs, _ := pref.parse_args([], args_and_flags)
|
|
|
|
builder.compile('build', prefs, compile_c)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compile_c(mut b builder.Builder) {
|
2020-04-17 21:17:19 +02:00
|
|
|
if b.pref.is_verbose {
|
2020-04-15 23:16:49 +02:00
|
|
|
println('all .v files before:')
|
|
|
|
}
|
2020-06-19 12:54:56 +02:00
|
|
|
$if windows {
|
2021-12-12 20:10:43 +01:00
|
|
|
b.find_win_cc() or { builder.verror(builder.no_compiler_error) }
|
2020-06-19 12:54:56 +02:00
|
|
|
}
|
2020-04-17 21:17:19 +02:00
|
|
|
mut files := b.get_builtin_files()
|
2020-04-15 23:16:49 +02:00
|
|
|
files << b.get_user_files()
|
|
|
|
b.set_module_lookup_paths()
|
2020-04-17 21:17:19 +02:00
|
|
|
if b.pref.is_verbose {
|
2020-04-15 23:16:49 +02:00
|
|
|
println('all .v files:')
|
|
|
|
println(files)
|
|
|
|
}
|
2020-10-24 19:29:24 +02:00
|
|
|
mut out_name_c := b.get_vtmp_filename(b.pref.out_name, '.tmp.c')
|
2020-04-18 17:46:23 +02:00
|
|
|
if b.pref.is_shared {
|
2020-10-24 19:29:24 +02:00
|
|
|
out_name_c = b.get_vtmp_filename(b.pref.out_name, '.tmp.so.c')
|
2020-04-15 23:16:49 +02:00
|
|
|
}
|
2021-12-12 20:10:43 +01:00
|
|
|
build_c(mut b, files, out_name_c)
|
2020-04-15 23:16:49 +02:00
|
|
|
b.cc()
|
|
|
|
}
|
2021-12-11 22:51:42 +01:00
|
|
|
|
2021-12-12 20:10:43 +01:00
|
|
|
pub fn gen_c(mut b builder.Builder, v_files []string) string {
|
2021-12-11 22:51:42 +01:00
|
|
|
b.front_and_middle_stages(v_files) or {
|
2022-02-11 14:52:33 +01:00
|
|
|
if err.code() != 9999 {
|
|
|
|
builder.verror(err.msg())
|
2021-12-11 22:51:42 +01:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
2022-02-11 14:52:33 +01:00
|
|
|
|
2021-12-11 22:51:42 +01:00
|
|
|
util.timing_start('C GEN')
|
|
|
|
res := c.gen(b.parsed_files, b.table, b.pref)
|
|
|
|
util.timing_measure('C GEN')
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-12 20:10:43 +01:00
|
|
|
pub fn build_c(mut b builder.Builder, v_files []string, out_file string) {
|
2021-12-11 22:51:42 +01:00
|
|
|
b.out_name_c = out_file
|
|
|
|
b.pref.out_name_c = os.real_path(out_file)
|
|
|
|
b.info('build_c($out_file)')
|
2021-12-12 20:10:43 +01:00
|
|
|
output2 := gen_c(mut b, v_files)
|
2021-12-11 22:51:42 +01:00
|
|
|
os.write_file(out_file, output2) or { panic(err) }
|
|
|
|
if b.pref.is_stats {
|
|
|
|
b.stats_lines = output2.count('\n') + 1
|
|
|
|
b.stats_bytes = output2.len
|
|
|
|
}
|
|
|
|
}
|