From 846ddfd728d672ff38d339bb0d1af1e9d374e58a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 1 Jun 2022 09:08:18 +0300 Subject: [PATCH] v: always embed file data of \$embed_file(file) constructs, even without -prod, unless `-d embed_only_metadata` is given. --- CONTRIBUTING.md | 2 +- vlib/v/gen/c/cgen.v | 7 +------ vlib/v/gen/c/embed.v | 25 +++++++++++++++++-------- vlib/v/parser/comptime.v | 3 +-- vlib/v/pref/pref.v | 7 +++++++ 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 626ca9a26e..fb2fb56dee 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -203,4 +203,4 @@ to create a copy of the compiler rather than replacing it with `v self`. | `trace_thirdparty_obj_files` | Prints details about built thirdparty obj files | | `trace_usecache` | Prints details when -usecache is used | | `trace_embed_file` | Prints details when $embed_file is used | -| `force_embed_file` | Force embedding of file(s) with `$embed_file('somefile')` | +| `embed_only_metadata` | Embed only the metadata for the file(s) with `$embed_file('somefile')`; faster; for development, *not* distribution | diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a7a0176ff3..a88e42ac0f 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -801,12 +801,7 @@ pub fn (mut g Gen) finish() { if g.pref.is_livemain || g.pref.is_liveshared { g.generate_hotcode_reloader_code() } - if g.embedded_files.len > 0 { - if g.embed_file_is_prod_mode() { - g.gen_embedded_data() - } - g.gen_embedded_metadata() - } + g.handle_embedded_files_finish() if g.pref.is_test { g.gen_c_main_for_tests() } else { diff --git a/vlib/v/gen/c/embed.v b/vlib/v/gen/c/embed.v index 5fb8fbb410..57eea65984 100644 --- a/vlib/v/gen/c/embed.v +++ b/vlib/v/gen/c/embed.v @@ -5,11 +5,20 @@ import rand import v.ast import v.pref -fn (mut g Gen) embed_file_is_prod_mode() bool { - if g.pref.is_prod || 'force_embed_file' in g.pref.compile_defines { - return true +fn (mut g Gen) should_really_embed_file() bool { + if 'embed_only_metadata' in g.pref.compile_defines { + return false + } + return true +} + +fn (mut g Gen) handle_embedded_files_finish() { + if g.embedded_files.len > 0 { + if g.should_really_embed_file() { + g.gen_embedded_data() + } + g.gen_embedded_metadata() } - return false } // gen_embed_file_struct generates C code for `$embed_file('...')` calls. @@ -17,7 +26,7 @@ fn (mut g Gen) gen_embed_file_init(mut node ast.ComptimeCall) { $if trace_embed_file ? { eprintln('> gen_embed_file_init $node.embed_file.apath') } - if g.embed_file_is_prod_mode() { + if g.should_really_embed_file() { file_bytes := os.read_bytes(node.embed_file.apath) or { panic('unable to read file: "$node.embed_file.rpath') } @@ -84,13 +93,13 @@ fn (mut g Gen) gen_embedded_metadata() { ef_idx := emfile.hash() g.embedded_data.writeln('\t\tcase ${ef_idx}U: {') g.embedded_data.writeln('\t\t\tres.path = ${ctoslit(emfile.rpath)};') - if g.embed_file_is_prod_mode() { + if g.should_really_embed_file() { // apath is not needed in production and may leak information g.embedded_data.writeln('\t\t\tres.apath = ${ctoslit('')};') } else { g.embedded_data.writeln('\t\t\tres.apath = ${ctoslit(emfile.apath)};') } - if g.embed_file_is_prod_mode() { + if g.should_really_embed_file() { // use function generated in Gen.gen_embedded_data() if emfile.is_compressed { g.embedded_data.writeln('\t\t\tres.compression_type = ${ctoslit(emfile.compression_type)};') @@ -104,7 +113,7 @@ fn (mut g Gen) gen_embedded_metadata() { } g.embedded_data.writeln('\t\t\tres.free_compressed = 0;') g.embedded_data.writeln('\t\t\tres.free_uncompressed = 0;') - if g.embed_file_is_prod_mode() { + if g.should_really_embed_file() { g.embedded_data.writeln('\t\t\tres.len = $emfile.len;') } else { file_size := os.file_size(emfile.apath) diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 6f2a9f8ef9..335c1c3607 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -164,8 +164,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { } } p.register_auto_import('v.preludes.embed_file') - if embed_compression_type == 'zlib' - && (p.pref.is_prod || 'force_embed_file' in p.pref.compile_defines) { + if embed_compression_type == 'zlib' { p.register_auto_import('v.preludes.embed_file.zlib') } return ast.ComptimeCall{ diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 0af3de4319..fedff0fd34 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -965,6 +965,7 @@ pub fn get_host_arch() Arch { fn (mut prefs Preferences) parse_define(define string) { define_parts := define.split('=') + prefs.diagnose_deprecated_defines(define_parts) if !(prefs.is_debug && define == 'debug') { prefs.build_options << '-d $define' } @@ -992,3 +993,9 @@ fn (mut prefs Preferences) parse_define(define string) { println('V error: Unknown define argument: ${define}. Expected at most one `=`.') exit(1) } + +fn (mut prefs Preferences) diagnose_deprecated_defines(define_parts []string) { + if define_parts[0] == 'force_embed_file' { + eprintln('-d force_embed_file was deprecated in 2022/06/01. Now \$embed_file(file) always embeds the file, unless you pass `-d embed_only_metadata`.') + } +}