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 == builtin.Os.windows) '\\' else '/';

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

  1. pub const StackTrace = struct {
  2. index: usize,
  3. instruction_addresses: []usize,
  4. };
  5. pub const PanicFn = fn([]const u8, ?*StackTrace) noreturn;
  6. pub const Os = enum {
  7. freestanding,
  8. ananas,
  9. cloudabi,
  10. dragonfly,
  11. freebsd,
  12. fuchsia,
  13. ios,
  14. kfreebsd,
  15. linux,
  16. lv2,
  17. macosx,
  18. netbsd,
  19. openbsd,
  20. solaris,
  21. windows,
  22. haiku,
  23. minix,
  24. rtems,
  25. nacl,
  26. cnk,
  27. aix,
  28. cuda,
  29. nvcl,
  30. amdhsa,
  31. ps4,
  32. elfiamcu,
  33. tvos,
  34. watchos,
  35. mesa3d,
  36. contiki,
  37. amdpal,
  38. hermit,
  39. hurd,
  40. wasi,
  41. zen,
  42. uefi,
  43. };
  44. pub const Arch = union(enum) {
  45. arm: Arm32,
  46. armeb: Arm32,
  47. aarch64: Arm64,
  48. aarch64_be: Arm64,
  49. arc,
  50. avr,
  51. bpfel,
  52. bpfeb,
  53. hexagon,
  54. mips,
  55. mipsel,
  56. mips64,
  57. mips64el,
  58. msp430,
  59. powerpc,
  60. powerpc64,
  61. powerpc64le,
  62. r600,
  63. amdgcn,
  64. riscv32,
  65. riscv64,
  66. sparc,
  67. sparcv9,
  68. sparcel,
  69. s390x,
  70. tce,
  71. tcele,
  72. thumb: Arm32,
  73. thumbeb: Arm32,
  74. i386,
  75. x86_64,
  76. xcore,
  77. nvptx,
  78. nvptx64,
  79. le32,
  80. le64,
  81. amdil,
  82. amdil64,
  83. hsail,
  84. hsail64,
  85. spir,
  86. spir64,
  87. kalimba: Kalimba,
  88. shave,
  89. lanai,
  90. wasm32,
  91. wasm64,
  92. renderscript32,
  93. renderscript64,
  94. pub const Arm32 = enum {
  95. v8_5a,
  96. v8_4a,
  97. v8_3a,
  98. v8_2a,
  99. v8_1a,
  100. v8,
  101. v8r,
  102. v8m_baseline,
  103. v8m_mainline,
  104. v7,
  105. v7em,
  106. v7m,
  107. v7s,
  108. v7k,
  109. v7ve,
  110. v6,
  111. v6m,
  112. v6k,
  113. v6t2,
  114. v5,
  115. v5te,
  116. v4t,
  117. };
  118. pub const Arm64 = enum {
  119. v8_5a,
  120. v8_4a,
  121. v8_3a,
  122. v8_2a,
  123. v8_1a,
  124. v8,
  125. v8r,
  126. v8m_baseline,
  127. v8m_mainline,
  128. };
  129. pub const Kalimba = enum {
  130. v5,
  131. v4,
  132. v3,
  133. };
  134. pub const Mips = enum {
  135. r6,
  136. };
  137. };
  138. pub const Abi = enum {
  139. none,
  140. gnu,
  141. gnuabin32,
  142. gnuabi64,
  143. gnueabi,
  144. gnueabihf,
  145. gnux32,
  146. code16,
  147. eabi,
  148. eabihf,
  149. android,
  150. musl,
  151. musleabi,
  152. musleabihf,
  153. msvc,
  154. itanium,
  155. cygnus,
  156. coreclr,
  157. simulator,
  158. };
  159. pub const ObjectFormat = enum {
  160. unknown,
  161. coff,
  162. elf,
  163. macho,
  164. wasm,
  165. };
  166. pub const GlobalLinkage = enum {
  167. Internal,
  168. Strong,
  169. Weak,
  170. LinkOnce,
  171. };
  172. pub const AtomicOrder = enum {
  173. Unordered,
  174. Monotonic,
  175. Acquire,
  176. Release,
  177. AcqRel,
  178. SeqCst,
  179. };
  180. pub const AtomicRmwOp = enum {
  181. Xchg,
  182. Add,
  183. Sub,
  184. And,
  185. Nand,
  186. Or,
  187. Xor,
  188. Max,
  189. Min,
  190. };
  191. pub const Mode = enum {
  192. Debug,
  193. ReleaseSafe,
  194. ReleaseFast,
  195. ReleaseSmall,
  196. };
  197. pub const TypeId = enum {
  198. Type,
  199. Void,
  200. Bool,
  201. NoReturn,
  202. Int,
  203. Float,
  204. Pointer,
  205. Array,
  206. Struct,
  207. ComptimeFloat,
  208. ComptimeInt,
  209. Undefined,
  210. Null,
  211. Optional,
  212. ErrorUnion,
  213. ErrorSet,
  214. Enum,
  215. Union,
  216. Fn,
  217. BoundFn,
  218. ArgTuple,
  219. Opaque,
  220. Promise,
  221. Vector,
  222. EnumLiteral,
  223. };
  224. pub const TypeInfo = union(TypeId) {
  225. Type: void,
  226. Void: void,
  227. Bool: void,
  228. NoReturn: void,
  229. Int: Int,
  230. Float: Float,
  231. Pointer: Pointer,
  232. Array: Array,
  233. Struct: Struct,
  234. ComptimeFloat: void,
  235. ComptimeInt: void,
  236. Undefined: void,
  237. Null: void,
  238. Optional: Optional,
  239. ErrorUnion: ErrorUnion,
  240. ErrorSet: ErrorSet,
  241. Enum: Enum,
  242. Union: Union,
  243. Fn: Fn,
  244. BoundFn: Fn,
  245. ArgTuple: void,
  246. Opaque: void,
  247. Promise: Promise,
  248. Vector: Vector,
  249. EnumLiteral: void,
  250. pub const Int = struct {
  251. is_signed: bool,
  252. bits: comptime_int,
  253. };
  254. pub const Float = struct {
  255. bits: comptime_int,
  256. };
  257. pub const Pointer = struct {
  258. size: Size,
  259. is_const: bool,
  260. is_volatile: bool,
  261. alignment: comptime_int,
  262. child: type,
  263. is_allowzero: bool,
  264. pub const Size = enum {
  265. One,
  266. Many,
  267. Slice,
  268. C,
  269. };
  270. };
  271. pub const Array = struct {
  272. len: comptime_int,
  273. child: type,
  274. };
  275. pub const ContainerLayout = enum {
  276. Auto,
  277. Extern,
  278. Packed,
  279. };
  280. pub const StructField = struct {
  281. name: []const u8,
  282. offset: ?comptime_int,
  283. field_type: type,
  284. };
  285. pub const Struct = struct {
  286. layout: ContainerLayout,
  287. fields: []StructField,
  288. defs: []Definition,
  289. };
  290. pub const Optional = struct {
  291. child: type,
  292. };
  293. pub const ErrorUnion = struct {
  294. error_set: type,
  295. payload: type,
  296. };
  297. pub const Error = struct {
  298. name: []const u8,
  299. value: comptime_int,
  300. };
  301. pub const ErrorSet = ?[]Error;
  302. pub const EnumField = struct {
  303. name: []const u8,
  304. value: comptime_int,
  305. };
  306. pub const Enum = struct {
  307. layout: ContainerLayout,
  308. tag_type: type,
  309. fields: []EnumField,
  310. defs: []Definition,
  311. };
  312. pub const UnionField = struct {
  313. name: []const u8,
  314. enum_field: ?EnumField,
  315. field_type: type,
  316. };
  317. pub const Union = struct {
  318. layout: ContainerLayout,
  319. tag_type: ?type,
  320. fields: []UnionField,
  321. defs: []Definition,
  322. };
  323. pub const CallingConvention = enum {
  324. Unspecified,
  325. C,
  326. Cold,
  327. Naked,
  328. Stdcall,
  329. Async,
  330. };
  331. pub const FnArg = struct {
  332. is_generic: bool,
  333. is_noalias: bool,
  334. arg_type: ?type,
  335. };
  336. pub const Fn = struct {
  337. calling_convention: CallingConvention,
  338. is_generic: bool,
  339. is_var_args: bool,
  340. return_type: ?type,
  341. async_allocator_type: ?type,
  342. args: []FnArg,
  343. };
  344. pub const Promise = struct {
  345. child: ?type,
  346. };
  347. pub const Vector = struct {
  348. len: comptime_int,
  349. child: type,
  350. };
  351. pub const Definition = struct {
  352. name: []const u8,
  353. is_pub: bool,
  354. data: Data,
  355. pub const Data = union(enum) {
  356. Type: type,
  357. Var: type,
  358. Fn: FnDef,
  359. pub const FnDef = struct {
  360. fn_type: type,
  361. inline_type: Inline,
  362. calling_convention: CallingConvention,
  363. is_var_args: bool,
  364. is_extern: bool,
  365. is_export: bool,
  366. lib_name: ?[]const u8,
  367. return_type: type,
  368. arg_names: [][] const u8,
  369. pub const Inline = enum {
  370. Auto,
  371. Always,
  372. Never,
  373. };
  374. };
  375. };
  376. };
  377. };
  378. pub const FloatMode = enum {
  379. Strict,
  380. Optimized,
  381. };
  382. pub const Endian = enum {
  383. Big,
  384. Little,
  385. };
  386. pub const endian = Endian.Little;
  387. pub const is_test = false;
  388. pub const single_threaded = false;
  389. pub const os = Os.linux;
  390. pub const arch = Arch.x86_64;
  391. pub const abi = Abi.gnu;
  392. pub const object_format = ObjectFormat.elf;
  393. pub const mode = Mode.Debug;
  394. pub const link_libc = false;
  395. pub const have_error_return_tracing = false;
  396. pub const valgrind_support = true;
  397. pub const position_independent_code = false;

See also: