v: always embed file data of \$embed_file(file) constructs, even without -prod, unless `-d embed_only_metadata` is given.

master
Delyan Angelov 2022-06-01 09:08:18 +03:00
parent f40c30c3dc
commit 846ddfd728
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 27 additions and 17 deletions

View File

@ -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_thirdparty_obj_files` | Prints details about built thirdparty obj files |
| `trace_usecache` | Prints details when -usecache is used | | `trace_usecache` | Prints details when -usecache is used |
| `trace_embed_file` | Prints details when $embed_file 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 |

View File

@ -801,12 +801,7 @@ pub fn (mut g Gen) finish() {
if g.pref.is_livemain || g.pref.is_liveshared { if g.pref.is_livemain || g.pref.is_liveshared {
g.generate_hotcode_reloader_code() g.generate_hotcode_reloader_code()
} }
if g.embedded_files.len > 0 { g.handle_embedded_files_finish()
if g.embed_file_is_prod_mode() {
g.gen_embedded_data()
}
g.gen_embedded_metadata()
}
if g.pref.is_test { if g.pref.is_test {
g.gen_c_main_for_tests() g.gen_c_main_for_tests()
} else { } else {

View File

@ -5,11 +5,20 @@ import rand
import v.ast import v.ast
import v.pref import v.pref
fn (mut g Gen) embed_file_is_prod_mode() bool { fn (mut g Gen) should_really_embed_file() bool {
if g.pref.is_prod || 'force_embed_file' in g.pref.compile_defines { if 'embed_only_metadata' in g.pref.compile_defines {
return true 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. // 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 ? { $if trace_embed_file ? {
eprintln('> gen_embed_file_init $node.embed_file.apath') 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 { file_bytes := os.read_bytes(node.embed_file.apath) or {
panic('unable to read file: "$node.embed_file.rpath') panic('unable to read file: "$node.embed_file.rpath')
} }
@ -84,13 +93,13 @@ fn (mut g Gen) gen_embedded_metadata() {
ef_idx := emfile.hash() ef_idx := emfile.hash()
g.embedded_data.writeln('\t\tcase ${ef_idx}U: {') g.embedded_data.writeln('\t\tcase ${ef_idx}U: {')
g.embedded_data.writeln('\t\t\tres.path = ${ctoslit(emfile.rpath)};') 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 // apath is not needed in production and may leak information
g.embedded_data.writeln('\t\t\tres.apath = ${ctoslit('')};') g.embedded_data.writeln('\t\t\tres.apath = ${ctoslit('')};')
} else { } else {
g.embedded_data.writeln('\t\t\tres.apath = ${ctoslit(emfile.apath)};') 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() // use function generated in Gen.gen_embedded_data()
if emfile.is_compressed { if emfile.is_compressed {
g.embedded_data.writeln('\t\t\tres.compression_type = ${ctoslit(emfile.compression_type)};') 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_compressed = 0;')
g.embedded_data.writeln('\t\t\tres.free_uncompressed = 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;') g.embedded_data.writeln('\t\t\tres.len = $emfile.len;')
} else { } else {
file_size := os.file_size(emfile.apath) file_size := os.file_size(emfile.apath)

View File

@ -164,8 +164,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
} }
} }
p.register_auto_import('v.preludes.embed_file') p.register_auto_import('v.preludes.embed_file')
if embed_compression_type == 'zlib' if embed_compression_type == 'zlib' {
&& (p.pref.is_prod || 'force_embed_file' in p.pref.compile_defines) {
p.register_auto_import('v.preludes.embed_file.zlib') p.register_auto_import('v.preludes.embed_file.zlib')
} }
return ast.ComptimeCall{ return ast.ComptimeCall{

View File

@ -965,6 +965,7 @@ pub fn get_host_arch() Arch {
fn (mut prefs Preferences) parse_define(define string) { fn (mut prefs Preferences) parse_define(define string) {
define_parts := define.split('=') define_parts := define.split('=')
prefs.diagnose_deprecated_defines(define_parts)
if !(prefs.is_debug && define == 'debug') { if !(prefs.is_debug && define == 'debug') {
prefs.build_options << '-d $define' 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 `=`.') println('V error: Unknown define argument: ${define}. Expected at most one `=`.')
exit(1) 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`.')
}
}