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

See also: