From 1763dc45a8c0fe7885bfe1a9e5fce6ca3ee28e24 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 17 Dec 2024 19:39:23 +0200 Subject: [PATCH] v.builder: integrate more compile/linker options into parallel_cc.v (#23190) --- vlib/v/builder/cbuilder/parallel_cc.v | 28 +++++++++++----- vlib/v/builder/cc.v | 47 ++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/vlib/v/builder/cbuilder/parallel_cc.v b/vlib/v/builder/cbuilder/parallel_cc.v index c2985ec5e5..9c19853f3d 100644 --- a/vlib/v/builder/cbuilder/parallel_cc.v +++ b/vlib/v/builder/cbuilder/parallel_cc.v @@ -8,7 +8,6 @@ import sync.pool import v.gen.c const cc_compiler = os.getenv_opt('CC') or { 'cc' } -const cc = os.quoted_path(cc_compiler) const cc_ldflags = os.getenv_opt('LDFLAGS') or { '' } const cc_cflags = os.getenv_opt('CFLAGS') or { '' } const cc_cflags_opt = os.getenv_opt('CFLAGS_OPT') or { '' } // '-O3' } @@ -70,25 +69,38 @@ fn parallel_cc(mut b builder.Builder, result c.GenOutput) { out_files[i].close() } - // cc := os.quoted_path(cc_compiler) + mut cc_path := cc_compiler + explicit_cc_flag_passed := b.pref.build_options.any(it.starts_with('-cc ')) + if explicit_cc_flag_passed { + // do not guess, just use the user's preference + cc_path = b.pref.ccompiler + } + cc := os.quoted_path(cc_path) + mut compile_args := b.get_compile_args() + mut linker_args := b.get_linker_args() + if !explicit_cc_flag_passed { + compile_args = compile_args.filter(it != '-bt25') + linker_args = linker_args.filter(it != '-bt25') + } + scompile_args := compile_args.join(' ') + slinker_args := linker_args.join(' ') + mut o_postfixes := ['0', 'x'] mut cmds := []string{} for i in 0 .. c_files { o_postfixes << (i + 1).str() } - str_args := b.str_args.replace('-flto', '') // remove link time optimization, slows down linking 10x for postfix in o_postfixes { - cmds << '${cc} ${cc_cflags} ${cc_cflags_opt} ${str_args} -c -w -o ${tmp_dir}/out_${postfix}.o ${tmp_dir}/out_${postfix}.c' + cmds << '${cc} ${cc_cflags} ${cc_cflags_opt} ${scompile_args} -w -o ${tmp_dir}/out_${postfix}.o -c ${tmp_dir}/out_${postfix}.c' } sw := time.new_stopwatch() mut pp := pool.new_pool_processor(callback: build_parallel_o_cb) pp.set_max_jobs(util.nr_jobs) pp.work_on_items(cmds) - eprintln('> ${sw.elapsed().milliseconds():5} ms, C compilation on ${util.nr_jobs} thread(s), processing ${cmds.len} commands') - gc_flag := if b.pref.gc_mode != .no_gc { '-lgc ' } else { '' } + eprint_time(sw, 'C compilation on ${util.nr_jobs} thread(s), processing ${cmds.len} commands') + obj_files := fnames.map(it.replace('.c', '.o')).join(' ') - ld_flags := '${gc_flag}${cc_ldflags}' - link_cmd := '${cc} -o ${os.quoted_path(b.pref.out_name)} ${tmp_dir}/out_0.o ${obj_files} ${tmp_dir}/out_x.o -lpthread ${ld_flags}' + link_cmd := '${cc} ${scompile_args} -o ${os.quoted_path(b.pref.out_name)} ${tmp_dir}/out_0.o ${obj_files} ${tmp_dir}/out_x.o ${slinker_args} ${cc_ldflags}' sw_link := time.new_stopwatch() link_res := os.execute(link_cmd) eprint_result_time(sw_link, 'link_cmd', link_cmd, link_res) diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 644aa286b4..0ff9f5b0d5 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -100,7 +100,7 @@ fn (mut v Builder) show_cc(cmd string, response_file string, response_file_conte } } -enum CC { +pub enum CC { tcc gcc icc @@ -109,8 +109,8 @@ enum CC { unknown } -struct CcompilerOptions { -mut: +pub struct CcompilerOptions { +pub mut: guessed_compiler string shared_postfix string // .so, .dll @@ -235,6 +235,9 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) { $if windows { have_flto = false } + if v.pref.parallel_cc { + have_flto = false + } if have_flto { optimization_options << '-flto' } @@ -252,7 +255,14 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) { debug_options << '-no-pie' } } - optimization_options = ['-O3', '-flto'] + optimization_options = ['-O3'] + mut have_flto := true + if v.pref.parallel_cc { + have_flto = false + } + if have_flto { + optimization_options << '-flto' + } } if ccoptions.cc == .icc { if ccoptions.debug_mode { @@ -461,6 +471,17 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) { } fn (v &Builder) all_args(ccoptions CcompilerOptions) []string { + mut all := []string{} + all << v.only_compile_args(ccoptions) + all << v.only_linker_args(ccoptions) + return all +} + +pub fn (v &Builder) get_compile_args() []string { + return v.only_compile_args(v.ccoptions) +} + +fn (v &Builder) only_compile_args(ccoptions CcompilerOptions) []string { mut all := []string{} all << ccoptions.env_cflags if v.pref.is_cstrict { @@ -488,6 +509,15 @@ fn (v &Builder) all_args(ccoptions CcompilerOptions) []string { all << ccoptions.pre_args all << ccoptions.source_args all << ccoptions.post_args + return all +} + +pub fn (v &Builder) get_linker_args() []string { + return v.only_linker_args(v.ccoptions) +} + +fn (v &Builder) only_linker_args(ccoptions CcompilerOptions) []string { + mut all := []string{} // in `build-mode`, we do not need -lxyz flags, since we are // building an (.o) object file, that will be linked later. if v.pref.build_mode != .build_module { @@ -1014,7 +1044,14 @@ fn (mut c Builder) cc_windows_cross() { mut debug_options := []string{} if c.pref.is_prod { if c.pref.ccompiler != 'msvc' { - optimization_options = ['-O3', '-flto'] + optimization_options = ['-O3'] + mut have_flto := true + if c.pref.parallel_cc { + have_flto = false + } + if have_flto { + optimization_options << '-flto' + } } } if c.pref.is_debug {