driver.h (10179B)
1 #ifndef KIT_DRIVER_H 2 #define KIT_DRIVER_H 3 4 #include <kit/compile.h> 5 #include <kit/core.h> 6 #include <kit/dbg.h> 7 #include <kit/jit.h> 8 9 #include "env.h" 10 11 /* The kit CLI driver. Multi-call binary: dispatches to a named tool 12 * front-ends by argv[0]'s basename, falling back to argv[1] (e.g. 13 * `kit cc ...`). The driver only depends on libkit's public API 14 * (<kit/...>); it has no access to libkit's internal headers. 15 * 16 * Preprocessor-only mode is `cc -E` — there is no separate `cpp` tool. */ 17 18 typedef enum DriverTool { 19 DRIVER_TOOL_CC, 20 DRIVER_TOOL_CHECK, 21 DRIVER_TOOL_CPP, 22 DRIVER_TOOL_AS, 23 DRIVER_TOOL_LD, 24 DRIVER_TOOL_AR, 25 DRIVER_TOOL_RANLIB, 26 DRIVER_TOOL_OBJDUMP, 27 DRIVER_TOOL_DBG, 28 DRIVER_TOOL_RUN, 29 DRIVER_TOOL_EMU, 30 DRIVER_TOOL_NM, 31 DRIVER_TOOL_SIZE, 32 DRIVER_TOOL_ADDR2LINE, 33 } DriverTool; 34 35 /* Multi-call entry: dispatches by argv[0] basename (or argv[1] fallback). */ 36 int driver_main(int argc, char** argv); 37 38 /* Direct entry per tool. Each lives in driver/<tool>.c. */ 39 int driver_cc(int argc, char** argv); 40 int driver_check(int argc, char** argv); 41 int driver_build_exe(int argc, char** argv); 42 int driver_build_lib(int argc, char** argv); 43 int driver_build_obj(int argc, char** argv); 44 int driver_install(int argc, char** argv); 45 int driver_cpp(int argc, char** argv); 46 int driver_as(int argc, char** argv); 47 int driver_ld(int argc, char** argv); 48 int driver_ar(int argc, char** argv); 49 int driver_ranlib(int argc, char** argv); 50 int driver_strip(int argc, char** argv); 51 int driver_objcopy(int argc, char** argv); 52 int driver_objdump(int argc, char** argv); 53 int driver_dbg(int argc, char** argv); 54 int driver_run(int argc, char** argv); 55 int driver_emu(int argc, char** argv); 56 int driver_nm(int argc, char** argv); 57 int driver_size(int argc, char** argv); 58 int driver_addr2line(int argc, char** argv); 59 int driver_symbolize(int argc, char** argv); 60 int driver_strings(int argc, char** argv); 61 int driver_cas(int argc, char** argv); 62 int driver_pkg(int argc, char** argv); 63 int driver_xxd(int argc, char** argv); 64 int driver_cmp(int argc, char** argv); 65 int driver_hash(int argc, char** argv); 66 int driver_sha256sum(int argc, char** argv); 67 int driver_b2sum(int argc, char** argv); 68 int driver_crc32(int argc, char** argv); 69 int driver_compress(int argc, char** argv); 70 int driver_gzip(int argc, char** argv); 71 int driver_gunzip(int argc, char** argv); 72 int driver_lz4(int argc, char** argv); 73 int driver_lz4c(int argc, char** argv); 74 int driver_disas(int argc, char** argv); 75 int driver_mc(int argc, char** argv); 76 77 /* Per-tool help printers. Write a multi-section help text to stdout and 78 * return. The tool entry-points call these when invoked with no args, -h, 79 * or --help (objdump excepts -h, since GNU objdump uses it for section 80 * headers — only --help triggers help there). */ 81 void driver_help_cc(void); 82 void driver_help_check(void); 83 void driver_help_build_exe(void); 84 void driver_help_build_lib(void); 85 void driver_help_build_obj(void); 86 void driver_help_install(void); 87 void driver_help_cpp(void); 88 void driver_help_as(void); 89 void driver_help_ld(void); 90 void driver_help_ar(void); 91 void driver_help_ranlib(void); 92 void driver_help_strip(void); 93 void driver_help_objcopy(void); 94 void driver_help_objdump(void); 95 void driver_help_dbg(void); 96 void driver_help_run(void); 97 void driver_help_emu(void); 98 void driver_help_nm(void); 99 void driver_help_size(void); 100 void driver_help_addr2line(void); 101 void driver_help_symbolize(void); 102 void driver_help_strings(void); 103 void driver_help_cas(void); 104 void driver_help_pkg(void); 105 void driver_help_xxd(void); 106 void driver_help_cmp(void); 107 void driver_help_hash(void); 108 void driver_help_compress(void); 109 void driver_help_disas(void); 110 void driver_help_mc(void); 111 112 /* Multi-call top-level help (`kit`, `kit -h`, `kit --help`, 113 * `kit help`). Lists each tool with a one-line summary and explains 114 * the multi-call dispatch. Writes to stdout. */ 115 void driver_help_top(void); 116 117 /* Tool grouping, used by `install` to pick a default set without 118 * duplicating the tool list. The centralized table in main.c tags every 119 * row; the groups with a non-zero bit make up the default install set 120 * (the binutils/compiler toolchain plus the byte utilities whose names 121 * match standard commands). */ 122 typedef enum DriverToolGroup { 123 DRIVER_GROUP_OTHER = 0, 124 DRIVER_GROUP_TOOLCHAIN = 1u << 0, /* binutils + compiler driver */ 125 DRIVER_GROUP_BYTEUTIL = 1u << 1, /* standard-named byte utilities */ 126 } DriverToolGroup; 127 128 /* Read-only views over the centralized tool table (main.c), so the 129 * `install` tool always reflects exactly the tools compiled into this 130 * binary. Indices are stable within a process and run 0..count-1. */ 131 unsigned driver_tool_count(void); 132 const char* driver_tool_name(unsigned index); /* NULL if out of range */ 133 unsigned driver_tool_groups(unsigned index); /* 0 if out of range */ 134 /* Index of the tool named `name`, or -1 if there is no such tool. */ 135 int driver_tool_find(const char* name); 136 137 /* Returns 1 if `arg` is "--help" or "-help". The short "-h" is treated 138 * as a help request by every tool except objdump (where it means 139 * "section headers"); callers that want to honour it should test it 140 * explicitly via driver_argv_wants_help(..., 1). */ 141 int driver_is_help_flag(const char* arg); 142 143 /* Scan argv[1..argc-1] for a help request, returning 1 on a hit. Triggers 144 * on "--help" / "-help" always, and on "-h" when accept_short_h is set. 145 * Tool entries also treat argc < 2 (no positional or option args) as a 146 * help request — the convention is "no args means show help". */ 147 int driver_argv_wants_help(int argc, char** argv, int accept_short_h); 148 149 /* Parse a target triple string (`<arch>[-<vendor>]-<os>[-<env>]`) into a 150 * KitTargetSpec. Recognized arches: x86_64/amd64, i386/i486/i586/i686, aarch64/ 151 * arm64, arm/armv7, riscv64, riscv32, wasm32, wasm64. Recognized OSes (scanned 152 * across the remaining components, so vendor tokens like `pc`/`apple`/ 153 * `unknown` are skipped): linux, darwin/macos, windows/win32, wasi, none/ 154 * freestanding. Sets arch/os/obj/ptr_size/ptr_align/big_endian; pic and 155 * code_model are left at their defaults. Returns 0 on success, nonzero on 156 * unrecognized arch or NULL inputs. */ 157 int driver_target_from_triple(const char* triple, KitTargetSpec* out); 158 159 /* Render a canonical driver target triple into `buf`. Returns 0 on success, 160 * nonzero when `buf` is too small. */ 161 int driver_target_to_triple(KitTargetSpec target, char* buf, size_t cap); 162 163 /* Map an architecture-name literal (the arch component of a triple: 164 * x86_64/amd64, i386/i486/i586/i686, aarch64/arm64, arm/armv7, riscv64, 165 * riscv32, wasm32, wasm64) to its KitArchKind and natural pointer size. The 166 * single authority for arch-name spellings; driver_target_from_triple uses 167 * the same table. Returns 0 on success, nonzero on an unrecognized name or a 168 * NULL argument. Out-pointers may be NULL. */ 169 int driver_arch_from_name(const char* name, KitArchKind* arch_out, 170 uint8_t* ptr_size_out); 171 172 /* Whether `path` is a compilable source file: a path some registered frontend 173 * claims (via the canonical extension registry, case-insensitively), excluding 174 * C headers. The C frontend registers ".h" so language-for-path can identify 175 * headers, but a header is not a translation unit to compile. The single 176 * source of truth for source classification shared by cc/build/run/dbg, so 177 * adding a frontend extension reaches every tool at once. */ 178 int driver_path_is_source(const char* path); 179 180 typedef struct DriverTargetFeatures { 181 DriverEnv* env; 182 KitTargetFeature* features; 183 uint32_t nfeatures; 184 uint32_t cap_features; 185 KitSlice isa; 186 KitSlice cpu; 187 KitSlice tune; 188 KitSlice abi; 189 } DriverTargetFeatures; 190 191 /* Shared target-feature parser. Canonical spelling is `-mattr=+foo,-bar`; 192 * GCC/clang-style `-mfoo` and `-mno-foo` are accepted as aliases and lowered 193 * into the same KitTargetFeature list before libkit sees the target. */ 194 int driver_target_features_init(DriverTargetFeatures*, DriverEnv*, 195 int argc_bound); 196 void driver_target_features_fini(DriverTargetFeatures*, DriverEnv*); 197 int driver_target_features_try_consume(DriverTargetFeatures*, DriverEnv*, 198 const char* tool, int argc, char** argv, 199 int* i); 200 int driver_target_options(const DriverTargetFeatures*, const char* tool, 201 KitTargetSpec, KitTargetOptions* out); 202 KitStatus driver_target_new(const KitContext*, KitTargetSpec, 203 const DriverTargetFeatures*, const char* tool, 204 KitTarget** out); 205 206 /* Default PIC/PIE model for a target. Hosted targets default to PIE, 207 * matching modern gcc/clang and platform norms (ELF -> ET_DYN, Mach-O -> 208 * MH_PIE, PE/COFF -> .reloc + DYNAMIC_BASE). Freestanding targets (no 209 * dynamic loader) and WASM stay non-PIE. */ 210 KitPic driver_default_pic(KitObjFmt obj, KitOSKind os); 211 212 /* Resolve whether the link step should emit a position-independent 213 * executable. An explicit -pie always wins; -shared and -r (relocatable) 214 * always suppress it; otherwise it follows the target's PIC model. */ 215 int driver_link_pie(KitTargetSpec target, int explicit_pie, int shared, 216 int relocatable); 217 218 /* Per-target driver defaults, centralizing the per-OS forks the cc/build 219 * drivers used to inline. */ 220 221 /* Default executable name when the user gives no -o: "a.exe" on Windows 222 * (PE/COFF), "a.out" elsewhere. */ 223 const char* driver_default_exe_name(KitTargetSpec target); 224 225 /* Default relocatable-object extension for the target: ".obj" (len 4) on 226 * Windows, ".o" (len 2) elsewhere. NULL out-pointers are ignored. */ 227 void driver_default_obj_ext(KitTargetSpec target, const char** ext_out, 228 size_t* ext_len_out); 229 230 /* Whether `<sysroot>/lib` should be folded into the library search path for 231 * this target (Windows mingw import-library tree). */ 232 int driver_target_needs_sysroot_libdir(KitTargetSpec target); 233 234 /* Whether the hosted libc profile is engaged by default for this target 235 * (Windows-COFF, given a sysroot and no -nostdlib). */ 236 int driver_target_default_hosted_profile(KitTargetSpec target); 237 238 #endif