diff --git a/doc/docs.md b/doc/docs.md index 6e12c909e8..d4df490b21 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3516,6 +3516,10 @@ $if test { $if debug { println('debugging') } +// v -prod ... +$if prod { + println('production build') +} // v -d option ... $if option ? { println('custom option') @@ -3527,11 +3531,11 @@ Right now it can be used to detect an OS, compiler, platform or compilation opti `$if debug` is a special option like `$if windows` or `$if x32`. If you're using a custom ifdef, then you do need `$if option ? {}` and compile with`v -d option`. Full list of builtin options: -| OS | Compilers | Platforms | Other | -| --- | --- | --- | --- | -| `windows`, `linux`, `macos` | `gcc`, `tinyc` | `amd64`, `aarch64` | `debug`, `test`, `js` | -| `mac`, `darwin`, `ios`, | `clang`, `mingw` | `x64`, `x32` | `glibc`, `prealloc` | -| `android`,`mach`, `dragonfly` | `msvc` | `little_endian` | `no_bounds_checking` | +| OS | Compilers | Platforms | Other | +| --- | --- | --- | --- | +| `windows`, `linux`, `macos` | `gcc`, `tinyc` | `amd64`, `aarch64` | `debug`, `prod`, `test` | +| `mac`, `darwin`, `ios`, | `clang`, `mingw` | `x64`, `x32` | `js`, `glibc`, `prealloc` | +| `android`,`mach`, `dragonfly` | `msvc` | `little_endian` | `no_bounds_checking` | | `gnu`, `hpux`, `haiku`, `qnx` | `cplusplus` | `big_endian` | | | `solaris`, `linux_or_macos` | | | | diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 295e46b3ee..9d47dadcb4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -27,7 +27,7 @@ const ( ] valid_comp_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus'] valid_comp_if_platforms = ['amd64', 'aarch64', 'x64', 'x32', 'little_endian', 'big_endian'] - valid_comp_if_other = ['js', 'debug', 'test', 'glibc', 'prealloc', 'no_bounds_checking'] + valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking'] array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort', 'contains', 'index', 'wait'] ) @@ -5150,6 +5150,7 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool { match cond.name { 'js' { return c.pref.backend != .js } 'debug' { return !c.pref.is_debug } + 'prod' { return !c.pref.is_prod } 'test' { return !c.pref.is_test } 'glibc' { return false } // TODO 'prealloc' { return !c.pref.prealloc } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index abb7425dff..879f993322 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -423,6 +423,9 @@ pub fn (mut g Gen) init() { if g.pref.is_debug || 'debug' in g.pref.compile_defines { g.comptime_defines.writeln('#define _VDEBUG (1)') } + if g.pref.is_prod || 'prod' in g.pref.compile_defines { + g.comptime_defines.writeln('#define _VPROD (1)') + } if g.pref.is_test || 'test' in g.pref.compile_defines { g.comptime_defines.writeln('#define _VTEST (1)') } diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index a7713a557a..2d33e646db 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -498,6 +498,9 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string 'debug' { return '_VDEBUG' } + 'prod' { + return '_VPROD' + } 'test' { return '_VTEST' } diff --git a/vlib/v/tests/prod/assoc.prod.v b/vlib/v/tests/prod/assoc.prod.v index 683342f2a5..1673355521 100644 --- a/vlib/v/tests/prod/assoc.prod.v +++ b/vlib/v/tests/prod/assoc.prod.v @@ -17,4 +17,11 @@ fn get_st() MyStruct { fn main() { s := get_st() println(s) + $if prod { + println('prod mode is on') + assert true + } $else { + println('prod mode is off') + assert false + } } diff --git a/vlib/v/tests/prod/assoc.prod.v.expected.txt b/vlib/v/tests/prod/assoc.prod.v.expected.txt index b9022ec7bc..bf189055c2 100644 --- a/vlib/v/tests/prod/assoc.prod.v.expected.txt +++ b/vlib/v/tests/prod/assoc.prod.v.expected.txt @@ -1,3 +1,4 @@ MyStruct{ s: '6' } +prod mode is on