From b00f2d7a9feee667e7bfa1b87f2bbe89f6fb8202 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 14 Oct 2024 21:59:21 +0300 Subject: [PATCH] v.builder: support compiling resource files (allow setting an icon for V program executables on windows) --- vlib/v/builder/cc.v | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 1590ae23223045..9bc8cb20266154 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -1064,11 +1064,16 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag obj_path := os.real_path(path) mut cfile := '${obj_path[..obj_path.len - 2]}.c' mut cpp_file := false + mut rc_file := false if !os.exists(cfile) { // Guessed C file does not exist, so it may be a CPP file cfile += 'pp' cpp_file = true } + if !os.exists(cfile) { + cfile = '${obj_path[..obj_path.len - 2]}.rc' + rc_file = true + } opath := v.pref.cache_manager.mod_postfix_with_key2cpath(mod, '.o', obj_path) mut rebuild_reason_message := '${obj_path} not found, building it in ${opath} ...' if os.exists(opath) { @@ -1108,6 +1113,41 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag } ccompiler = v.pref.cppcompiler } + + if rc_file { + if v.pref.os != .windows { + eprintln('error: building an .rc file is not supported, when the target OS is not windows. RC file: `${cfile}` .') + // exit(1) + } + // TODO: add pref setting for the resource compiler + mut windres_path := '' + for rc_name in ['windres', 'x86_64-w64-mingw32-windres'] { + windres_path = os.find_abs_path_of_executable(rc_name) or { continue } + break + } + println('> found windres_path: ${windres_path}') + rc_cmd := '${windres_path} -i ${os.quoted_path(cfile)} -o ${os.quoted_path(opath)}' + rc_res := os.execute(rc_cmd) + os.chdir(current_folder) or {} + if rc_res.exit_code != 0 { + eprintln('failed building resource file cmd:\n${rc_cmd}') + verror(rc_res.output) + return + } + v.pref.cache_manager.mod_save(mod, '.description.txt', obj_path, '${obj_path:-30} @ ${rc_cmd}\n') or { + panic(err) + } + if v.pref.show_cc { + println('>> OBJECT FILE compilation cmd: ${rc_cmd}') + } + $if trace_thirdparty_obj_files ? { + if rc_res.output != '' { + println(rc_res.output) + } + } + return + } + cmd := '${v.quote_compiler_name(ccompiler)} ${cc_options}' $if trace_thirdparty_obj_files ? { println('>>> build_thirdparty_obj_files cmd: ${cmd}')