| 1 | module cbuilder |
| 2 | |
| 3 | import os |
| 4 | import strings |
| 5 | import v.pref |
| 6 | import v.util |
| 7 | import v.builder |
| 8 | import v.gen.c |
| 9 | |
| 10 | pub fn start() { |
| 11 | mut args_and_flags := util.join_env_vflags_and_os_args()[1..] |
| 12 | prefs, _ := pref.parse_args([], args_and_flags) |
| 13 | builder.compile('build', prefs, compile_c) |
| 14 | } |
| 15 | |
| 16 | pub fn compile_c(mut b builder.Builder) { |
| 17 | if b.pref.is_verbose { |
| 18 | println('all .v files before:') |
| 19 | } |
| 20 | $if windows { |
| 21 | if builder.should_find_windows_host_c_compiler(b.pref) { |
| 22 | b.find_win_cc() or { |
| 23 | if b.pref.ccompiler_set_by_flag && b.pref.ccompiler != 'msvc' { |
| 24 | builder.verror(err.msg()) |
| 25 | } |
| 26 | builder.verror(' |
| 27 | ================== |
| 28 | Error: no C compiler detected. |
| 29 | |
| 30 | You can find instructions on how to install one in the V wiki: |
| 31 | https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows |
| 32 | |
| 33 | If you think you have one installed, make sure it is in your PATH. |
| 34 | If you do have one in your PATH, please raise an issue on GitHub: |
| 35 | https://github.com/vlang/v/issues/new/choose |
| 36 | |
| 37 | You can also use `v doctor`, to see what V knows about your current environment. |
| 38 | |
| 39 | You can also seek #help on Discord: https://discord.gg/vlang |
| 40 | ') |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | mut files := b.get_builtin_files() |
| 45 | files << b.get_user_files() |
| 46 | b.set_module_lookup_paths() |
| 47 | if b.pref.is_verbose { |
| 48 | println('all .v files:') |
| 49 | println(files) |
| 50 | } |
| 51 | mut out_name_c := b.get_vtmp_filename(b.pref.out_name, '.tmp.c') |
| 52 | if b.pref.is_shared { |
| 53 | out_name_c = b.get_vtmp_filename(b.pref.out_name, '.tmp.so.c') |
| 54 | } |
| 55 | build_c(mut b, files, out_name_c) |
| 56 | if !b.pref.parallel_cc { |
| 57 | b.cc() |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn build_c(mut b builder.Builder, v_files []string, out_file string) { |
| 62 | b.out_name_c = out_file |
| 63 | b.pref.out_name_c = os.real_path(out_file) |
| 64 | b.info('build_c(${out_file})') |
| 65 | mut output2 := gen_c(mut b, v_files) |
| 66 | if b.pref.is_vlines { |
| 67 | output2 = c.fix_reset_dbg_line(output2, out_file) |
| 68 | } |
| 69 | os.write_file_array(out_file, output2) or { panic(err) } |
| 70 | if b.pref.is_stats { |
| 71 | b.stats_lines = output2.count(it == `\n`) + 1 |
| 72 | b.stats_bytes = output2.len |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn gen_c(mut b builder.Builder, v_files []string) strings.Builder { |
| 77 | b.front_and_middle_stages(v_files) or { |
| 78 | if err.code() > 7000 { |
| 79 | return []u8{} |
| 80 | } |
| 81 | builder.verror(err.msg()) |
| 82 | } |
| 83 | |
| 84 | util.timing_start('C GEN') |
| 85 | result := c.gen(b.parsed_files, mut b.table, b.pref) |
| 86 | util.timing_measure('C GEN') |
| 87 | |
| 88 | if b.pref.parallel_cc { |
| 89 | b.cc() // Call it just to gen b.str_args |
| 90 | util.timing_start('Parallel C compilation') |
| 91 | parallel_cc(mut b, result) or { builder.verror(err.msg()) } |
| 92 | util.timing_measure('Parallel C compilation') |
| 93 | } |
| 94 | |
| 95 | return result.res_builder |
| 96 | } |
| 97 | |