Compile Variables

Compile variables are accessible by importing the "builtin" package, which the compiler makes available to every Zig source file. It contains compile-time constants such as the current target, endianness, and release mode.

  1. const builtin = @import("builtin");
  2. const separator = if (builtin.os.tag == builtin.Os.windows) '\\' else '/';

Example of what is imported with @import("builtin"):

  1. const std = @import("std");
  2. /// Zig version. When writing code that supports multiple versions of Zig, prefer
  3. /// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
  4. pub const zig_version = std.SemanticVersion.parse("0.8.0") catch unreachable;
  5. /// Temporary until self-hosted is feature complete.
  6. pub const zig_is_stage2 = true;
  7. /// Temporary until self-hosted supports the `cpu.arch` value.
  8. pub const stage2_arch: std.Target.Cpu.Arch = .x86_64;
  9. pub const output_mode = std.builtin.OutputMode.Obj;
  10. pub const link_mode = std.builtin.LinkMode.Static;
  11. pub const is_test = false;
  12. pub const single_threaded = false;
  13. pub const abi = std.Target.Abi.gnu;
  14. pub const cpu: std.Target.Cpu = .{
  15. .arch = .x86_64,
  16. .model = &std.Target.x86.cpu.skylake,
  17. .features = std.Target.x86.featureSet(&[_]std.Target.x86.Feature{
  18. .@"64bit",
  19. .adx,
  20. .aes,
  21. .avx,
  22. .avx2,
  23. .bmi,
  24. .bmi2,
  25. .clflushopt,
  26. .cmov,
  27. .cx16,
  28. .cx8,
  29. .ermsb,
  30. .f16c,
  31. .false_deps_popcnt,
  32. .fast_15bytenop,
  33. .fast_gather,
  34. .fast_scalar_fsqrt,
  35. .fast_shld_rotate,
  36. .fast_variable_shuffle,
  37. .fast_vector_fsqrt,
  38. .fma,
  39. .fsgsbase,
  40. .fxsr,
  41. .idivq_to_divl,
  42. .invpcid,
  43. .lzcnt,
  44. .macrofusion,
  45. .mmx,
  46. .movbe,
  47. .nopl,
  48. .pclmul,
  49. .popcnt,
  50. .prfchw,
  51. .rdrnd,
  52. .rdseed,
  53. .sahf,
  54. .sgx,
  55. .slow_3ops_lea,
  56. .sse,
  57. .sse2,
  58. .sse3,
  59. .sse4_1,
  60. .sse4_2,
  61. .ssse3,
  62. .vzeroupper,
  63. .x87,
  64. .xsave,
  65. .xsavec,
  66. .xsaveopt,
  67. .xsaves,
  68. }),
  69. };
  70. pub const os = std.Target.Os{
  71. .tag = .linux,
  72. .version_range = .{ .linux = .{
  73. .range = .{
  74. .min = .{
  75. .major = 5,
  76. .minor = 4,
  77. .patch = 106,
  78. },
  79. .max = .{
  80. .major = 5,
  81. .minor = 4,
  82. .patch = 106,
  83. },
  84. },
  85. .glibc = .{
  86. .major = 2,
  87. .minor = 31,
  88. .patch = 0,
  89. },
  90. }},
  91. };
  92. pub const target = std.Target{
  93. .cpu = cpu,
  94. .os = os,
  95. .abi = abi,
  96. };
  97. pub const object_format = std.Target.ObjectFormat.elf;
  98. pub const mode = std.builtin.Mode.Debug;
  99. pub const link_libc = false;
  100. pub const link_libcpp = false;
  101. pub const have_error_return_tracing = true;
  102. pub const valgrind_support = true;
  103. pub const position_independent_code = false;
  104. pub const position_independent_executable = false;
  105. pub const strip_debug_info = false;
  106. pub const code_model = std.builtin.CodeModel.default;

See also: