diff --git a/doc/docs.md b/doc/docs.md index 24e98f1fab..b3c6aa1307 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -6138,6 +6138,9 @@ that are substituted at compile time: - `@BUILD_DATE` => replaced with the build date, for example '2024-09-13' . - `@BUILD_TIME` => replaced with the build time, for example '12:32:07' . - `@BUILD_TIMESTAMP` => replaced with the build timestamp, for example '1726219885' . +- `@OS` => replaced with the OS type, for example 'linux' . +- `@COMPILER` => replaced with the C compiler type, for example 'gcc' . +- `@PLATFORM` => replaced with the platform type, for example 'amd64' . Note: `@BUILD_DATE`, `@BUILD_TIME`, `@BUILD_TIMESTAMP` represent times in the UTC timezone. By default, they are based on the current time of the compilation/build. They can be overridden by setting the environment variable `SOURCE_DATE_EPOCH`. That is also useful while making diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0233816cef..060b5a9d13 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4030,6 +4030,15 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type { .build_timestamp { node.val = util.stable_build_time.unix().str() } + .os { + node.val = pref.get_host_os().lower() + } + .compiler { + node.val = c.pref.ccompiler_type.str() + } + .platform { + node.val = c.pref.arch.str() + } .unknown { c.error('unknown @ identifier: ${node.name}. Available identifiers: ${token.valid_at_tokens}', node.pos) diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 33e2bc2207..4806f2d4c0 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -511,6 +511,9 @@ fn (mut p Parser) at() ast.AtExpr { '@BUILD_DATE' { token.AtKind.build_date } '@BUILD_TIME' { token.AtKind.build_time } '@BUILD_TIMESTAMP' { token.AtKind.build_timestamp } + '@OS' { token.AtKind.os } + '@COMPILER' { token.AtKind.compiler } + '@PLATFORM' { token.AtKind.platform } else { token.AtKind.unknown } } expr := ast.AtExpr{ diff --git a/vlib/v/tests/comptime/comptime_at_test.v b/vlib/v/tests/comptime/comptime_at_test.v index a6a761bc39..18de32a171 100644 --- a/vlib/v/tests/comptime/comptime_at_test.v +++ b/vlib/v/tests/comptime/comptime_at_test.v @@ -198,3 +198,21 @@ fn test_at_build_date_time_timestamp() { now_utc := dump(time.utc().unix()) assert now_utc >= bts.i64() } + +fn test_at_os() { + println('Current OS is ${@OS}') + assert @OS in ['termux', 'android', 'wasm32_emscripten', 'linux', 'ios', 'macos', 'windows', + 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'serenity', 'plan9', 'vinix', 'solaris', 'haiku', + 'js_node', 'js_freestanding', 'js_browser'] +} + +fn test_at_compiler() { + println('Current Compiler is ${@COMPILER}') + assert @COMPILER in ['gcc', 'tinyc', 'clang', 'emcc', 'mingw', 'msvc', 'cplusplus'] +} + +fn test_at_platform() { + println('Current Platform is ${@PLATFORM}') + assert @PLATFORM in ['amd64', 'arm64', 'arm32', 'rv64', 'rv32', 'i386', 's390x', 'ppc64le', + 'loongarch64', 'js_node', 'js_browser', 'js_freestanding', 'wasm32'] +} diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 386d874cd6..b18abd7e12 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -189,6 +189,9 @@ pub enum AtKind { build_date build_time build_timestamp + os + compiler + platform } pub const assign_tokens = [Kind.assign, .decl_assign, .plus_assign, .minus_assign, .mult_assign, @@ -197,7 +200,8 @@ pub const assign_tokens = [Kind.assign, .decl_assign, .plus_assign, .minus_assig pub const valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT', '@VEXE', '@FILE', '@DIR', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', - '@VMODHASH', '@FILE_LINE', '@LOCATION', '@BUILD_DATE', '@BUILD_TIME', '@BUILD_TIMESTAMP'] + '@VMODHASH', '@FILE_LINE', '@LOCATION', '@BUILD_DATE', '@BUILD_TIME', '@BUILD_TIMESTAMP', '@OS', + '@COMPILER', '@PLATFORM'] pub const token_str = build_token_str()