diff options
Diffstat (limited to 'examples/core.zig')
| -rw-r--r-- | examples/core.zig | 340 |
1 files changed, 340 insertions, 0 deletions
diff --git a/examples/core.zig b/examples/core.zig index e69de29..f393e0f 100644 --- a/examples/core.zig +++ b/examples/core.zig | |||
| @@ -0,0 +1,340 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap").core; | ||
| 3 | |||
| 4 | const debug = std.debug; | ||
| 5 | const mem = std.mem; | ||
| 6 | |||
| 7 | const Names = clap.Names; | ||
| 8 | const Param = clap.Param; | ||
| 9 | |||
| 10 | const Command = enum { | ||
| 11 | Help, | ||
| 12 | Build, | ||
| 13 | BuildExe, | ||
| 14 | BuildLib, | ||
| 15 | BuildObj, | ||
| 16 | Fmt, | ||
| 17 | Run, | ||
| 18 | Targets, | ||
| 19 | Test, | ||
| 20 | Version, | ||
| 21 | Zen, | ||
| 22 | }; | ||
| 23 | |||
| 24 | const params = []Param(Command){ | ||
| 25 | Param(Command).init(Command.Help, false, Names.prefix("help")), | ||
| 26 | Param(Command).init(Command.Build, false, Names.bare("build")), | ||
| 27 | Param(Command).init(Command.BuildExe, false, Names.bare("build-exe")), | ||
| 28 | Param(Command).init(Command.BuildLib, false, Names.bare("build-lib")), | ||
| 29 | Param(Command).init(Command.BuildObj, false, Names.bare("build-obj")), | ||
| 30 | Param(Command).init(Command.Fmt, false, Names.bare("fmt")), | ||
| 31 | Param(Command).init(Command.Run, false, Names.bare("run")), | ||
| 32 | Param(Command).init(Command.Targets, false, Names.bare("targets")), | ||
| 33 | Param(Command).init(Command.Test, false, Names.bare("test")), | ||
| 34 | Param(Command).init(Command.Version, false, Names.bare("version")), | ||
| 35 | Param(Command).init(Command.Zen, false, Names.bare("zen")), | ||
| 36 | }; | ||
| 37 | |||
| 38 | const usage = | ||
| 39 | \\usage: zig [command] [options] | ||
| 40 | \\ | ||
| 41 | \\Commands: | ||
| 42 | \\ | ||
| 43 | \\ build Build project from build.zig | ||
| 44 | \\ build-exe [source] Create executable from source or object files | ||
| 45 | \\ build-lib [source] Create library from source or object files | ||
| 46 | \\ build-obj [source] Create object from source or assembly | ||
| 47 | \\ fmt [source] Parse file and render in canonical zig format | ||
| 48 | \\ run [source] Create executable and run immediately | ||
| 49 | \\ targets List available compilation targets | ||
| 50 | \\ test [source] Create and run a test build | ||
| 51 | \\ translate-c [source] Convert c code to zig code | ||
| 52 | \\ version Print version number and exit | ||
| 53 | \\ zen Print zen of zig and exit | ||
| 54 | \\ | ||
| 55 | \\ | ||
| 56 | ; | ||
| 57 | |||
| 58 | pub fn main() !void { | ||
| 59 | var direct_allocator = std.heap.DirectAllocator.init(); | ||
| 60 | defer direct_allocator.deinit(); | ||
| 61 | var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator); | ||
| 62 | defer arena.deinit(); | ||
| 63 | |||
| 64 | const allocator = &arena.allocator; | ||
| 65 | |||
| 66 | var args = clap.OsArgIterator.init(); | ||
| 67 | var parser = clap.Clap(Command).init(params, &args.iter, allocator); | ||
| 68 | defer parser.deinit(); | ||
| 69 | |||
| 70 | const exe = try parser.nextNoParse(); | ||
| 71 | const maybe_arg = parser.next() catch |err| b: { | ||
| 72 | debug.warn("{}.\n", @errorName(err)); | ||
| 73 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches | ||
| 74 | return err; | ||
| 75 | }; | ||
| 76 | const arg = maybe_arg ?? { | ||
| 77 | debug.warn("No command found.\n"); | ||
| 78 | // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches | ||
| 79 | return error.NoCommandFound; | ||
| 80 | }; | ||
| 81 | |||
| 82 | switch (arg.id) { | ||
| 83 | Command.Help => return, // debug.warn(usage), TODO: error: evaluation exceeded 1000 backwards branches | ||
| 84 | Command.Build => try cmdBuild(allocator, parser.iter), | ||
| 85 | Command.BuildExe, | ||
| 86 | Command.BuildLib, | ||
| 87 | Command.BuildObj, | ||
| 88 | Command.Fmt, | ||
| 89 | Command.Run, | ||
| 90 | Command.Targets, | ||
| 91 | Command.Test, | ||
| 92 | Command.Version, | ||
| 93 | Command.Zen => unreachable, | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | // cmd:build /////////////////////////////////////////////////////////////////////////////////////// | ||
| 98 | |||
| 99 | const BuildArg = enum { | ||
| 100 | Help, | ||
| 101 | Init, | ||
| 102 | BuildFile, | ||
| 103 | CacheDir, | ||
| 104 | Verbose, | ||
| 105 | Prefix, | ||
| 106 | VerboseTokenize, | ||
| 107 | VerboseAst, | ||
| 108 | VerboseLink, | ||
| 109 | VerboseIr, | ||
| 110 | VerboseLlvmIr, | ||
| 111 | VerboseCImport, | ||
| 112 | }; | ||
| 113 | |||
| 114 | const build_params = []Param(BuildArg){ | ||
| 115 | Param(BuildArg).init(BuildArg.Help, false, Names.prefix("help")), | ||
| 116 | Param(BuildArg).init(BuildArg.Init, false, Names.long("init")), | ||
| 117 | Param(BuildArg).init(BuildArg.BuildFile, true, Names.long("build-file")), | ||
| 118 | Param(BuildArg).init(BuildArg.CacheDir, true, Names.long("cache-dir")), | ||
| 119 | Param(BuildArg).init(BuildArg.Verbose, false, Names.prefix("verbose")), | ||
| 120 | Param(BuildArg).init(BuildArg.Prefix, true, Names.long("prefix")), | ||
| 121 | |||
| 122 | Param(BuildArg).init(BuildArg.VerboseTokenize, false, Names.prefix("verbose-tokenize")), | ||
| 123 | Param(BuildArg).init(BuildArg.VerboseAst, false, Names.prefix("verbose-ast")), | ||
| 124 | Param(BuildArg).init(BuildArg.VerboseLink, false, Names.prefix("verbose-link")), | ||
| 125 | Param(BuildArg).init(BuildArg.VerboseIr, false, Names.prefix("verbose-ir")), | ||
| 126 | Param(BuildArg).init(BuildArg.VerboseLlvmIr, false, Names.prefix("verbose-llvm-ir")), | ||
| 127 | Param(BuildArg).init(BuildArg.VerboseCImport, false, Names.prefix("verbose-cimport")), | ||
| 128 | }; | ||
| 129 | |||
| 130 | const build_usage = | ||
| 131 | \\usage: zig build <options> | ||
| 132 | \\ | ||
| 133 | \\General Options: | ||
| 134 | \\ -h, --help Print this help and exit | ||
| 135 | \\ --init Generate a build.zig template | ||
| 136 | \\ --build-file [file] Override path to build.zig | ||
| 137 | \\ --cache-dir [path] Override path to cache directory | ||
| 138 | \\ -v, --verbose Print commands before executing them | ||
| 139 | \\ --prefix [path] Override default install prefix | ||
| 140 | \\ | ||
| 141 | \\Project-Specific Options: | ||
| 142 | \\ | ||
| 143 | \\ Project-specific options become available when the build file is found. | ||
| 144 | \\ | ||
| 145 | \\Advanced Options: | ||
| 146 | \\ --verbose-tokenize Enable compiler debug output for tokenization | ||
| 147 | \\ --verbose-ast Enable compiler debug output for parsing into an AST | ||
| 148 | \\ --verbose-link Enable compiler debug output for linking | ||
| 149 | \\ --verbose-ir Enable compiler debug output for Zig IR | ||
| 150 | \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR | ||
| 151 | \\ --verbose-cimport Enable compiler debug output for C imports | ||
| 152 | \\ | ||
| 153 | \\ | ||
| 154 | ; | ||
| 155 | |||
| 156 | const missing_build_file = | ||
| 157 | \\No 'build.zig' file found. | ||
| 158 | \\ | ||
| 159 | \\Initialize a 'build.zig' template file with `zig build --init`, | ||
| 160 | \\or build an executable directly with `zig build-exe $FILENAME.zig`. | ||
| 161 | \\ | ||
| 162 | \\See: `zig build --help` or `zig help` for more options. | ||
| 163 | \\ | ||
| 164 | ; | ||
| 165 | |||
| 166 | fn cmdBuild(allocator: &mem.Allocator, args: &clap.ArgIterator) !void { | ||
| 167 | var init = false; | ||
| 168 | var build_file: []const u8 = "build.zig"; | ||
| 169 | var cache_dir: []const u8 = "zig-cache"; | ||
| 170 | var verbose = false; | ||
| 171 | var prefix: []const u8 = ""; | ||
| 172 | var verbose_tokenize = false; | ||
| 173 | var verbose_ast = false; | ||
| 174 | var verbose_link = false; | ||
| 175 | var verbose_ir = false; | ||
| 176 | var verbose_llvm_ir = false; | ||
| 177 | var verbose_cimport = false; | ||
| 178 | |||
| 179 | var parser = clap.Clap(BuildArg).init(build_params, args, allocator); | ||
| 180 | defer parser.deinit(); | ||
| 181 | |||
| 182 | while (parser.next() catch |err| { | ||
| 183 | debug.warn("{}.\n", @errorName(err)); | ||
| 184 | // debug.warn(build_usage); TODO: error: evaluation exceeded 1000 backwards branches | ||
| 185 | return err; | ||
| 186 | }) |arg| switch (arg.id) { | ||
| 187 | BuildArg.Help => return, // debug.warn(build_usage) TODO: error: evaluation exceeded 1000 backwards branches, | ||
| 188 | BuildArg.Init => init = true, | ||
| 189 | BuildArg.BuildFile => build_file = ??arg.value, | ||
| 190 | BuildArg.CacheDir => cache_dir = ??arg.value, | ||
| 191 | BuildArg.Verbose => verbose = true, | ||
| 192 | BuildArg.Prefix => prefix = ??arg.value, | ||
| 193 | BuildArg.VerboseTokenize => verbose_tokenize = true, | ||
| 194 | BuildArg.VerboseAst => verbose_ast = true, | ||
| 195 | BuildArg.VerboseLink => verbose_link = true, | ||
| 196 | BuildArg.VerboseIr => verbose_ir = true, | ||
| 197 | BuildArg.VerboseLlvmIr => verbose_llvm_ir = true, | ||
| 198 | BuildArg.VerboseCImport => verbose_cimport = true, | ||
| 199 | }; | ||
| 200 | |||
| 201 | debug.warn("command: build\n"); | ||
| 202 | debug.warn("init = {}\n", init); | ||
| 203 | debug.warn("build_file = {}\n", build_file); | ||
| 204 | debug.warn("cache_dir = {}\n", cache_dir); | ||
| 205 | debug.warn("verbose = {}\n", verbose); | ||
| 206 | debug.warn("prefix = {}\n", prefix); | ||
| 207 | debug.warn("verbose_tokenize = {}\n", verbose_tokenize); | ||
| 208 | debug.warn("verbose_ast = {}\n", verbose_ast); | ||
| 209 | debug.warn("verbose_link = {}\n", verbose_link); | ||
| 210 | debug.warn("verbose_ir = {}\n", verbose_ir); | ||
| 211 | debug.warn("verbose_llvm_ir = {}\n", verbose_llvm_ir); | ||
| 212 | debug.warn("verbose_cimport = {}\n", verbose_cimport); | ||
| 213 | } | ||
| 214 | |||
| 215 | // cmd:build-exe /////////////////////////////////////////////////////////////////////////////////// | ||
| 216 | |||
| 217 | const BuildGeneric = enum { | ||
| 218 | File, | ||
| 219 | Help, | ||
| 220 | Color, | ||
| 221 | |||
| 222 | Assembly, | ||
| 223 | CacheDir, | ||
| 224 | Emit, | ||
| 225 | EnableTimingInfo, | ||
| 226 | LibCDir, | ||
| 227 | Name, | ||
| 228 | Output, | ||
| 229 | OutputH, | ||
| 230 | PkgBegin, | ||
| 231 | PkgEnd, | ||
| 232 | ReleaseFast, | ||
| 233 | ReleaseSafe, | ||
| 234 | Static, | ||
| 235 | Strip, | ||
| 236 | TargetArch, | ||
| 237 | TargetEnviron, | ||
| 238 | TargetOs, | ||
| 239 | VerboseTokenize, | ||
| 240 | VerboseAst, | ||
| 241 | VerboseLink, | ||
| 242 | VerboseIr, | ||
| 243 | VerboseLlvmIr, | ||
| 244 | VerboseCImport, | ||
| 245 | DirAfter, | ||
| 246 | ISystem, | ||
| 247 | MLlvm, | ||
| 248 | |||
| 249 | ArPath, | ||
| 250 | DynamicLinker, | ||
| 251 | EachLibRPath, | ||
| 252 | LibcLibDir, | ||
| 253 | LibcStaticLibDir, | ||
| 254 | MsvcLibDir, | ||
| 255 | Kernel32LibDir, | ||
| 256 | Library, | ||
| 257 | ForbidLibrary, | ||
| 258 | LibraryPath, | ||
| 259 | LinkerScript, | ||
| 260 | Object, | ||
| 261 | RDynamic, | ||
| 262 | RPath, | ||
| 263 | MConsole, | ||
| 264 | MWindows, | ||
| 265 | Framework, | ||
| 266 | MiOsVersionMin, | ||
| 267 | MMacOsXVersonMin, | ||
| 268 | VerMajor, | ||
| 269 | VerMinor, | ||
| 270 | VerPatch, | ||
| 271 | }; | ||
| 272 | |||
| 273 | const build_generic_params = []Param(BuildArg){ | ||
| 274 | Param(BuildArg).init(BuildArg.Help, false, Names.prefix("help")), | ||
| 275 | }; | ||
| 276 | |||
| 277 | const build_generic_usage = | ||
| 278 | \\usage: zig build-exe <options> [file] | ||
| 279 | \\ zig build-lib <options> [file] | ||
| 280 | \\ zig build-obj <options> [file] | ||
| 281 | \\ | ||
| 282 | \\General Options: | ||
| 283 | \\ -h, --help Print this help and exit | ||
| 284 | \\ -c, --color [auto|off|on] Enable or disable colored error messages | ||
| 285 | \\ | ||
| 286 | \\Compile Options: | ||
| 287 | \\ --assembly [source] Add assembly file to build | ||
| 288 | \\ --cache-dir [path] Override the cache directory | ||
| 289 | \\ --emit [filetype] Emit a specific file format as compilation output | ||
| 290 | \\ --enable-timing-info Print timing diagnostics | ||
| 291 | \\ --libc-include-dir [path] Directory where libc stdlib.h resides | ||
| 292 | \\ --name [name] Override output name | ||
| 293 | \\ --output [file] Override destination path | ||
| 294 | \\ --output-h [file] Override generated header file path | ||
| 295 | \\ --pkg-begin [name] [path] Make package available to import and push current pkg | ||
| 296 | \\ --pkg-end Pop current pkg | ||
| 297 | \\ --release-fast Build with optimizations on and safety off | ||
| 298 | \\ --release-safe Build with optimizations on and safety on | ||
| 299 | \\ --static Output will be statically linked | ||
| 300 | \\ --strip Exclude debug symbols | ||
| 301 | \\ --target-arch [name] Specify target architecture | ||
| 302 | \\ --target-environ [name] Specify target environment | ||
| 303 | \\ --target-os [name] Specify target operating system | ||
| 304 | \\ --verbose-tokenize Turn on compiler debug output for tokenization | ||
| 305 | \\ --verbose-ast-tree Turn on compiler debug output for parsing into an AST (tree view) | ||
| 306 | \\ --verbose-ast-fmt Turn on compiler debug output for parsing into an AST (render source) | ||
| 307 | \\ --verbose-link Turn on compiler debug output for linking | ||
| 308 | \\ --verbose-ir Turn on compiler debug output for Zig IR | ||
| 309 | \\ --verbose-llvm-ir Turn on compiler debug output for LLVM IR | ||
| 310 | \\ --verbose-cimport Turn on compiler debug output for C imports | ||
| 311 | \\ --dirafter [dir] Same as --isystem but do it last | ||
| 312 | \\ --isystem [dir] Add additional search path for other .h files | ||
| 313 | \\ --mllvm [arg] Additional arguments to forward to LLVM's option processing | ||
| 314 | \\ | ||
| 315 | \\Link Options: | ||
| 316 | \\ --ar-path [path] Set the path to ar | ||
| 317 | \\ --dynamic-linker [path] Set the path to ld.so | ||
| 318 | \\ --each-lib-rpath Add rpath for each used dynamic library | ||
| 319 | \\ --libc-lib-dir [path] Directory where libc crt1.o resides | ||
| 320 | \\ --libc-static-lib-dir [path] Directory where libc crtbegin.o resides | ||
| 321 | \\ --msvc-lib-dir [path] (windows) directory where vcruntime.lib resides | ||
| 322 | \\ --kernel32-lib-dir [path] (windows) directory where kernel32.lib resides | ||
| 323 | \\ --library [lib] Link against lib | ||
| 324 | \\ --forbid-library [lib] Make it an error to link against lib | ||
| 325 | \\ --library-path [dir] Add a directory to the library search path | ||
| 326 | \\ --linker-script [path] Use a custom linker script | ||
| 327 | \\ --object [obj] Add object file to build | ||
| 328 | \\ --rdynamic Add all symbols to the dynamic symbol table | ||
| 329 | \\ --rpath [path] Add directory to the runtime library search path | ||
| 330 | \\ --mconsole (windows) --subsystem console to the linker | ||
| 331 | \\ --mwindows (windows) --subsystem windows to the linker | ||
| 332 | \\ --framework [name] (darwin) link against framework | ||
| 333 | \\ --mios-version-min [ver] (darwin) set iOS deployment target | ||
| 334 | \\ --mmacosx-version-min [ver] (darwin) set Mac OS X deployment target | ||
| 335 | \\ --ver-major [ver] Dynamic library semver major version | ||
| 336 | \\ --ver-minor [ver] Dynamic library semver minor version | ||
| 337 | \\ --ver-patch [ver] Dynamic library semver patch version | ||
| 338 | \\ | ||
| 339 | \\ | ||
| 340 | ; | ||