main.cpp 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. // #define NO_ARRAY_BOUNDS_CHECK
  2. #include "common.cpp"
  3. #include "timings.cpp"
  4. #include "tokenizer.cpp"
  5. #include "big_int.cpp"
  6. #include "exact_value.cpp"
  7. #include "build_settings.cpp"
  8. gb_global ThreadPool global_thread_pool;
  9. void init_global_thread_pool(void) {
  10. isize thread_count = gb_max(build_context.thread_count, 1);
  11. isize worker_count = thread_count-1; // NOTE(bill): The main thread will also be used for work
  12. thread_pool_init(&global_thread_pool, permanent_allocator(), worker_count, "ThreadPoolWorker");
  13. }
  14. bool global_thread_pool_add_task(WorkerTaskProc *proc, void *data) {
  15. return thread_pool_add_task(&global_thread_pool, proc, data);
  16. }
  17. void global_thread_pool_wait(void) {
  18. thread_pool_wait(&global_thread_pool);
  19. }
  20. void debugf(char const *fmt, ...) {
  21. if (build_context.show_debug_messages) {
  22. gb_printf_err("[DEBUG] ");
  23. va_list va;
  24. va_start(va, fmt);
  25. (void)gb_printf_err_va(fmt, va);
  26. va_end(va);
  27. }
  28. }
  29. gb_global Timings global_timings = {0};
  30. #if defined(GB_SYSTEM_WINDOWS)
  31. #include "llvm-c/Types.h"
  32. #else
  33. #include <llvm-c/Types.h>
  34. #endif
  35. #include "parser.hpp"
  36. #include "checker.hpp"
  37. #include "parser.cpp"
  38. #include "checker.cpp"
  39. #include "docs.cpp"
  40. #include "llvm_backend.cpp"
  41. #if defined(GB_SYSTEM_OSX)
  42. #include <llvm/Config/llvm-config.h>
  43. #if LLVM_VERSION_MAJOR < 11
  44. #error LLVM Version 11+ is required => "brew install llvm@11"
  45. #endif
  46. #endif
  47. #include "query_data.cpp"
  48. #if defined(GB_SYSTEM_WINDOWS)
  49. // NOTE(IC): In order to find Visual C++ paths without relying on environment variables.
  50. #include "microsoft_craziness.h"
  51. #endif
  52. // NOTE(bill): 'name' is used in debugging and profiling modes
  53. i32 system_exec_command_line_app(char const *name, char const *fmt, ...) {
  54. #if defined(GB_SYSTEM_WINDOWS)
  55. STARTUPINFOW start_info = {gb_size_of(STARTUPINFOW)};
  56. PROCESS_INFORMATION pi = {0};
  57. isize cmd_len = 0;
  58. isize const cmd_cap = 4096;
  59. char cmd_line[cmd_cap] = {};
  60. va_list va;
  61. String16 cmd;
  62. i32 exit_code = 0;
  63. start_info.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  64. start_info.wShowWindow = SW_SHOW;
  65. start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  66. start_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  67. start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  68. va_start(va, fmt);
  69. cmd_len = gb_snprintf_va(cmd_line, cmd_cap-1, fmt, va);
  70. va_end(va);
  71. if (build_context.show_system_calls) {
  72. gb_printf_err("[SYSTEM CALL] %s\n", name);
  73. gb_printf_err("%.*s\n\n", cast(int)(cmd_len-1), cmd_line);
  74. }
  75. cmd = string_to_string16(permanent_allocator(), make_string(cast(u8 *)cmd_line, cmd_len-1));
  76. if (CreateProcessW(nullptr, cmd.text,
  77. nullptr, nullptr, true, 0, nullptr, nullptr,
  78. &start_info, &pi)) {
  79. WaitForSingleObject(pi.hProcess, INFINITE);
  80. GetExitCodeProcess(pi.hProcess, cast(DWORD *)&exit_code);
  81. CloseHandle(pi.hProcess);
  82. CloseHandle(pi.hThread);
  83. } else {
  84. // NOTE(bill): failed to create process
  85. gb_printf_err("Failed to execute command:\n\t%s\n", cmd_line);
  86. exit_code = -1;
  87. }
  88. if (exit_code) {
  89. exit(exit_code);
  90. }
  91. return exit_code;
  92. #elif defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_UNIX)
  93. char cmd_line[4096] = {0};
  94. isize cmd_len;
  95. va_list va;
  96. String cmd;
  97. i32 exit_code = 0;
  98. va_start(va, fmt);
  99. cmd_len = gb_snprintf_va(cmd_line, gb_size_of(cmd_line), fmt, va);
  100. va_end(va);
  101. cmd = make_string(cast(u8 *)&cmd_line, cmd_len-1);
  102. if (build_context.show_system_calls) {
  103. gb_printf_err("[SYSTEM CALL] %s\n", name);
  104. gb_printf_err("%s\n\n", cmd_line);
  105. }
  106. exit_code = system(cmd_line);
  107. // pid_t pid = fork();
  108. // int status = 0;
  109. // if(pid == 0) {
  110. // // in child, pid == 0.
  111. // int ret = execvp(cmd.text, (char* const*) cmd.text);
  112. // if(ret == -1) {
  113. // gb_printf_err("Failed to execute command:\n\t%s\n", cmd_line);
  114. // // we're in the child, so returning won't do us any good -- just quit.
  115. // exit(-1);
  116. // }
  117. // // unreachable
  118. // abort();
  119. // } else {
  120. // // wait for child to finish, then we can continue cleanup
  121. // int s = 0;
  122. // waitpid(pid, &s, 0);
  123. // status = WEXITSTATUS(s);
  124. // }
  125. // exit_code = status
  126. return exit_code;
  127. #endif
  128. }
  129. i32 linker_stage(lbGenerator *gen) {
  130. i32 result = 0;
  131. Timings *timings = &global_timings;
  132. String output_base = gen->output_base;
  133. if (is_arch_wasm()) {
  134. timings_start_section(timings, str_lit("wasm-ld"));
  135. system_exec_command_line_app("wasm-ld",
  136. "\"%.*s\\bin\\wasm-ld\" \"%.*s.wasm-obj\" -o \"%.*s.wasm\" %.*s %.*s",
  137. LIT(build_context.ODIN_ROOT),
  138. LIT(output_base), LIT(output_base), LIT(build_context.link_flags), LIT(build_context.extra_linker_flags));
  139. }
  140. if (build_context.cross_compiling && selected_target_metrics->metrics == &target_essence_amd64) {
  141. #ifdef GB_SYSTEM_UNIX
  142. result = system_exec_command_line_app("linker", "x86_64-essence-gcc \"%.*s.o\" -o \"%.*s\" %.*s %.*s",
  143. LIT(output_base), LIT(output_base), LIT(build_context.link_flags), LIT(build_context.extra_linker_flags));
  144. #else
  145. gb_printf_err("Linking for cross compilation for this platform is not yet supported (%.*s %.*s)\n",
  146. LIT(target_os_names[build_context.metrics.os]),
  147. LIT(target_arch_names[build_context.metrics.arch])
  148. );
  149. #endif
  150. } else if (build_context.cross_compiling && build_context.different_os) {
  151. gb_printf_err("Linking for cross compilation for this platform is not yet supported (%.*s %.*s)\n",
  152. LIT(target_os_names[build_context.metrics.os]),
  153. LIT(target_arch_names[build_context.metrics.arch])
  154. );
  155. build_context.keep_object_files = true;
  156. } else {
  157. #if defined(GB_SYSTEM_WINDOWS)
  158. String section_name = str_lit("msvc-link");
  159. if (build_context.use_lld) {
  160. section_name = str_lit("lld-link");
  161. }
  162. timings_start_section(timings, section_name);
  163. gbString lib_str = gb_string_make(heap_allocator(), "");
  164. defer (gb_string_free(lib_str));
  165. char lib_str_buf[1024] = {0};
  166. char const *output_ext = "exe";
  167. gbString link_settings = gb_string_make_reserve(heap_allocator(), 256);
  168. defer (gb_string_free(link_settings));
  169. // NOTE(ic): It would be nice to extend this so that we could specify the Visual Studio version that we want instead of defaulting to the latest.
  170. Find_Result_Utf8 find_result = find_visual_studio_and_windows_sdk_utf8();
  171. if (find_result.windows_sdk_version == 0) {
  172. gb_printf_err("Windows SDK not found.\n");
  173. exit(1);
  174. }
  175. if (build_context.ignore_microsoft_magic) {
  176. find_result = {};
  177. }
  178. // Add library search paths.
  179. if (find_result.vs_library_path.len > 0) {
  180. GB_ASSERT(find_result.windows_sdk_um_library_path.len > 0);
  181. GB_ASSERT(find_result.windows_sdk_ucrt_library_path.len > 0);
  182. String path = {};
  183. auto add_path = [&](String path) {
  184. if (path[path.len-1] == '\\') {
  185. path.len -= 1;
  186. }
  187. link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%.*s\"", LIT(path));
  188. };
  189. add_path(find_result.windows_sdk_um_library_path);
  190. add_path(find_result.windows_sdk_ucrt_library_path);
  191. add_path(find_result.vs_library_path);
  192. }
  193. for_array(j, gen->modules.entries) {
  194. lbModule *m = gen->modules.entries[j].value;
  195. for_array(i, m->foreign_library_paths) {
  196. String lib = m->foreign_library_paths[i];
  197. GB_ASSERT(lib.len < gb_count_of(lib_str_buf)-1);
  198. gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf),
  199. " \"%.*s\"", LIT(lib));
  200. lib_str = gb_string_appendc(lib_str, lib_str_buf);
  201. }
  202. }
  203. for_array(i, gen->default_module.foreign_library_paths) {
  204. String lib = gen->default_module.foreign_library_paths[i];
  205. GB_ASSERT(lib.len < gb_count_of(lib_str_buf)-1);
  206. gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf),
  207. " \"%.*s\"", LIT(lib));
  208. lib_str = gb_string_appendc(lib_str, lib_str_buf);
  209. }
  210. if (build_context.build_mode == BuildMode_DynamicLibrary) {
  211. output_ext = "dll";
  212. link_settings = gb_string_append_fmt(link_settings, " /DLL");
  213. } else {
  214. link_settings = gb_string_append_fmt(link_settings, " /ENTRY:mainCRTStartup");
  215. }
  216. if (build_context.pdb_filepath != "") {
  217. link_settings = gb_string_append_fmt(link_settings, " /PDB:%.*s", LIT(build_context.pdb_filepath));
  218. }
  219. if (build_context.no_crt) {
  220. link_settings = gb_string_append_fmt(link_settings, " /nodefaultlib");
  221. } else {
  222. link_settings = gb_string_append_fmt(link_settings, " /defaultlib:libcmt");
  223. }
  224. if (build_context.ODIN_DEBUG) {
  225. link_settings = gb_string_append_fmt(link_settings, " /DEBUG");
  226. }
  227. gbString object_files = gb_string_make(heap_allocator(), "");
  228. defer (gb_string_free(object_files));
  229. for_array(i, gen->output_object_paths) {
  230. String object_path = gen->output_object_paths[i];
  231. object_files = gb_string_append_fmt(object_files, "\"%.*s\" ", LIT(object_path));
  232. }
  233. char const *subsystem_str = build_context.use_subsystem_windows ? "WINDOWS" : "CONSOLE";
  234. if (!build_context.use_lld) { // msvc
  235. if (build_context.has_resource) {
  236. result = system_exec_command_line_app("msvc-link",
  237. "\"rc.exe\" /nologo /fo \"%.*s.res\" \"%.*s.rc\"",
  238. LIT(output_base),
  239. LIT(build_context.resource_filepath)
  240. );
  241. if (result == 0) {
  242. result = system_exec_command_line_app("msvc-link",
  243. "\"%.*slink.exe\" %s \"%.*s.res\" -OUT:\"%.*s.%s\" %s "
  244. "/nologo /incremental:no /opt:ref /subsystem:%s "
  245. " %.*s "
  246. " %.*s "
  247. " %s "
  248. "",
  249. LIT(find_result.vs_exe_path), object_files, LIT(output_base), LIT(output_base), output_ext,
  250. link_settings,
  251. subsystem_str,
  252. LIT(build_context.link_flags),
  253. LIT(build_context.extra_linker_flags),
  254. lib_str
  255. );
  256. }
  257. } else {
  258. result = system_exec_command_line_app("msvc-link",
  259. "\"%.*slink.exe\" %s -OUT:\"%.*s.%s\" %s "
  260. "/nologo /incremental:no /opt:ref /subsystem:%s "
  261. " %.*s "
  262. " %.*s "
  263. " %s "
  264. "",
  265. LIT(find_result.vs_exe_path), object_files, LIT(output_base), output_ext,
  266. link_settings,
  267. subsystem_str,
  268. LIT(build_context.link_flags),
  269. LIT(build_context.extra_linker_flags),
  270. lib_str
  271. );
  272. }
  273. } else { // lld
  274. result = system_exec_command_line_app("msvc-lld-link",
  275. "\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s.%s\" %s "
  276. "/nologo /incremental:no /opt:ref /subsystem:%s "
  277. " %.*s "
  278. " %.*s "
  279. " %s "
  280. "",
  281. LIT(build_context.ODIN_ROOT), object_files, LIT(output_base),output_ext,
  282. link_settings,
  283. subsystem_str,
  284. LIT(build_context.link_flags),
  285. LIT(build_context.extra_linker_flags),
  286. lib_str
  287. );
  288. }
  289. #else
  290. timings_start_section(timings, str_lit("ld-link"));
  291. // NOTE(vassvik): get cwd, for used for local shared libs linking, since those have to be relative to the exe
  292. char cwd[256];
  293. getcwd(&cwd[0], 256);
  294. //printf("%s\n", cwd);
  295. // NOTE(vassvik): needs to add the root to the library search paths, so that the full filenames of the library
  296. // files can be passed with -l:
  297. gbString lib_str = gb_string_make(heap_allocator(), "-L/");
  298. defer (gb_string_free(lib_str));
  299. for_array(i, gen->default_module.foreign_library_paths) {
  300. String lib = gen->default_module.foreign_library_paths[i];
  301. // NOTE(zangent): Sometimes, you have to use -framework on MacOS.
  302. // This allows you to specify '-f' in a #foreign_system_library,
  303. // without having to implement any new syntax specifically for MacOS.
  304. #if defined(GB_SYSTEM_OSX)
  305. if (string_ends_with(lib, str_lit(".framework"))) {
  306. // framework thingie
  307. String lib_name = lib;
  308. lib_name = remove_extension_from_path(lib_name);
  309. lib_str = gb_string_append_fmt(lib_str, " -framework %.*s ", LIT(lib_name));
  310. } else if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o")) || string_ends_with(lib, str_lit(".dylib"))) {
  311. // For:
  312. // object
  313. // dynamic lib
  314. // static libs, absolute full path relative to the file in which the lib was imported from
  315. lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
  316. } else {
  317. // dynamic or static system lib, just link regularly searching system library paths
  318. lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
  319. }
  320. #else
  321. // NOTE(vassvik): static libraries (.a files) in linux can be linked to directly using the full path,
  322. // since those are statically linked to at link time. shared libraries (.so) has to be
  323. // available at runtime wherever the executable is run, so we make require those to be
  324. // local to the executable (unless the system collection is used, in which case we search
  325. // the system library paths for the library file).
  326. if (string_ends_with(lib, str_lit(".a"))) {
  327. // static libs, absolute full path relative to the file in which the lib was imported from
  328. lib_str = gb_string_append_fmt(lib_str, " -l:\"%.*s\" ", LIT(lib));
  329. } else if (string_ends_with(lib, str_lit(".so"))) {
  330. // dynamic lib, relative path to executable
  331. // NOTE(vassvik): it is the user's responsibility to make sure the shared library files are visible
  332. // at runtimeto the executable
  333. lib_str = gb_string_append_fmt(lib_str, " -l:\"%s/%.*s\" ", cwd, LIT(lib));
  334. } else {
  335. // dynamic or static system lib, just link regularly searching system library paths
  336. lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
  337. }
  338. #endif
  339. }
  340. gbString object_files = gb_string_make(heap_allocator(), "");
  341. defer (gb_string_free(object_files));
  342. for_array(i, gen->output_object_paths) {
  343. String object_path = gen->output_object_paths[i];
  344. object_files = gb_string_append_fmt(object_files, "\"%.*s\" ", LIT(object_path));
  345. }
  346. // Unlike the Win32 linker code, the output_ext includes the dot, because
  347. // typically executable files on *NIX systems don't have extensions.
  348. String output_ext = {};
  349. gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
  350. char const *linker;
  351. if (build_context.build_mode == BuildMode_DynamicLibrary) {
  352. // NOTE(tetra, 2020-11-06): __$startup_runtime must be called at DLL load time.
  353. // Clang, for some reason, won't let us pass the '-init' flag that lets us do this,
  354. // so use ld instead.
  355. // :UseLDForShared
  356. linker = "ld";
  357. link_settings = gb_string_appendc(link_settings, "-init '__$startup_runtime' ");
  358. // Shared libraries are .dylib on MacOS and .so on Linux.
  359. #if defined(GB_SYSTEM_OSX)
  360. output_ext = STR_LIT(".dylib");
  361. link_settings = gb_string_appendc(link_settings, "-dylib -dynamic ");
  362. #else
  363. output_ext = STR_LIT(".so");
  364. link_settings = gb_string_appendc(link_settings, "-shared ");
  365. #endif
  366. } else {
  367. #if defined(GB_SYSTEM_OSX)
  368. linker = "ld";
  369. #else
  370. // TODO(zangent): Figure out how to make ld work on Linux.
  371. // It probably has to do with including the entire CRT, but
  372. // that's quite a complicated issue to solve while remaining distro-agnostic.
  373. // Clang can figure out linker flags for us, and that's good enough _for now_.
  374. linker = "clang -Wno-unused-command-line-argument";
  375. #endif
  376. }
  377. if (build_context.metrics.os == TargetOs_linux) {
  378. link_settings = gb_string_appendc(link_settings, "-no-pie ");
  379. }
  380. if (build_context.out_filepath.len > 0) {
  381. //NOTE(thebirk): We have a custom -out arguments, so we should use the extension from that
  382. isize pos = string_extension_position(build_context.out_filepath);
  383. if (pos > 0) {
  384. output_ext = substring(build_context.out_filepath, pos, build_context.out_filepath.len);
  385. }
  386. }
  387. result = system_exec_command_line_app("ld-link",
  388. "%s %s -o \"%.*s%.*s\" %s "
  389. " %s "
  390. " %.*s "
  391. " %.*s "
  392. " %s "
  393. #if defined(GB_SYSTEM_OSX)
  394. // This sets a requirement of Mountain Lion and up, but the compiler doesn't work without this limit.
  395. // NOTE: If you change this (although this minimum is as low as you can go with Odin working)
  396. // make sure to also change the 'mtriple' param passed to 'opt'
  397. #if defined(GB_CPU_ARM)
  398. " -macosx_version_min 11.0.0 "
  399. #else
  400. " -macosx_version_min 10.8.0 "
  401. #endif
  402. // This points the linker to where the entry point is
  403. " -e _main "
  404. #endif
  405. , linker, object_files, LIT(output_base), LIT(output_ext),
  406. #if defined(GB_SYSTEM_OSX)
  407. "-lSystem -lm -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib",
  408. #else
  409. "-lc -lm",
  410. #endif
  411. lib_str,
  412. LIT(build_context.link_flags),
  413. LIT(build_context.extra_linker_flags),
  414. link_settings);
  415. #if defined(GB_SYSTEM_OSX)
  416. if (build_context.ODIN_DEBUG) {
  417. // NOTE: macOS links DWARF symbols dynamically. Dsymutil will map the stubs in the exe
  418. // to the symbols in the object file
  419. system_exec_command_line_app("dsymutil",
  420. "dsymutil %.*s%.*s", LIT(output_base), LIT(output_ext)
  421. );
  422. }
  423. #endif
  424. #endif
  425. }
  426. return result;
  427. }
  428. Array<String> setup_args(int argc, char const **argv) {
  429. gbAllocator a = heap_allocator();
  430. #if defined(GB_SYSTEM_WINDOWS)
  431. int wargc = 0;
  432. wchar_t **wargv = command_line_to_wargv(GetCommandLineW(), &wargc);
  433. auto args = array_make<String>(a, 0, wargc);
  434. for (isize i = 0; i < wargc; i++) {
  435. wchar_t *warg = wargv[i];
  436. isize wlen = string16_len(warg);
  437. String16 wstr = make_string16(warg, wlen);
  438. String arg = string16_to_string(a, wstr);
  439. if (arg.len > 0) {
  440. array_add(&args, arg);
  441. }
  442. }
  443. return args;
  444. #else
  445. auto args = array_make<String>(a, 0, argc);
  446. for (isize i = 0; i < argc; i++) {
  447. String arg = make_string_c(argv[i]);
  448. if (arg.len > 0) {
  449. array_add(&args, arg);
  450. }
  451. }
  452. return args;
  453. #endif
  454. }
  455. void print_usage_line(i32 indent, char const *fmt, ...) {
  456. while (indent --> 0) {
  457. gb_printf_err("\t");
  458. }
  459. va_list va;
  460. va_start(va, fmt);
  461. gb_printf_err_va(fmt, va);
  462. va_end(va);
  463. gb_printf_err("\n");
  464. }
  465. void usage(String argv0) {
  466. print_usage_line(0, "%.*s is a tool for managing Odin source code", LIT(argv0));
  467. print_usage_line(0, "Usage:");
  468. print_usage_line(1, "%.*s command [arguments]", LIT(argv0));
  469. print_usage_line(0, "Commands:");
  470. print_usage_line(1, "build compile .odin file, or directory of .odin files, as an executable.");
  471. print_usage_line(1, " one must contain the program's entry point, all must be in the same package.");
  472. print_usage_line(1, "run same as 'build', but also then runs the newly compiled executable.");
  473. print_usage_line(1, "check parse and type check .odin file");
  474. print_usage_line(1, "query parse, type check, and output a .json file containing information about the program");
  475. print_usage_line(1, "doc generate documentation .odin file, or directory of .odin files");
  476. print_usage_line(1, "version print version");
  477. print_usage_line(0, "");
  478. print_usage_line(0, "For more information of flags, apply the flag to see what is possible");
  479. print_usage_line(1, "-help");
  480. }
  481. bool string_is_valid_identifier(String str) {
  482. if (str.len <= 0) return false;
  483. isize rune_count = 0;
  484. isize w = 0;
  485. isize offset = 0;
  486. while (offset < str.len) {
  487. Rune r = 0;
  488. w = utf8_decode(str.text, str.len, &r);
  489. if (r == GB_RUNE_INVALID) {
  490. return false;
  491. }
  492. if (rune_count == 0) {
  493. if (!rune_is_letter(r)) {
  494. return false;
  495. }
  496. } else {
  497. if (!rune_is_letter(r) && !rune_is_digit(r)) {
  498. return false;
  499. }
  500. }
  501. rune_count += 1;
  502. offset += w;
  503. }
  504. return true;
  505. }
  506. enum BuildFlagKind {
  507. BuildFlag_Invalid,
  508. BuildFlag_Help,
  509. BuildFlag_OutFile,
  510. BuildFlag_OptimizationLevel,
  511. BuildFlag_OptimizationMode,
  512. BuildFlag_ShowTimings,
  513. BuildFlag_ShowUnused,
  514. BuildFlag_ShowUnusedWithLocation,
  515. BuildFlag_ShowMoreTimings,
  516. BuildFlag_ShowSystemCalls,
  517. BuildFlag_ThreadCount,
  518. BuildFlag_KeepTempFiles,
  519. BuildFlag_Collection,
  520. BuildFlag_Define,
  521. BuildFlag_BuildMode,
  522. BuildFlag_Target,
  523. BuildFlag_Debug,
  524. BuildFlag_DisableAssert,
  525. BuildFlag_NoBoundsCheck,
  526. BuildFlag_NoDynamicLiterals,
  527. BuildFlag_NoCRT,
  528. BuildFlag_NoEntryPoint,
  529. BuildFlag_UseLLD,
  530. BuildFlag_UseSeparateModules,
  531. BuildFlag_ThreadedChecker,
  532. BuildFlag_NoThreadedChecker,
  533. BuildFlag_ShowDebugMessages,
  534. BuildFlag_Vet,
  535. BuildFlag_VetExtra,
  536. BuildFlag_UseLLVMApi,
  537. BuildFlag_IgnoreUnknownAttributes,
  538. BuildFlag_ExtraLinkerFlags,
  539. BuildFlag_Microarch,
  540. BuildFlag_TestName,
  541. BuildFlag_DisallowDo,
  542. BuildFlag_DefaultToNilAllocator,
  543. BuildFlag_InsertSemicolon,
  544. BuildFlag_StrictStyle,
  545. BuildFlag_StrictStyleInitOnly,
  546. BuildFlag_Compact,
  547. BuildFlag_GlobalDefinitions,
  548. BuildFlag_GoToDefinitions,
  549. BuildFlag_Short,
  550. BuildFlag_AllPackages,
  551. BuildFlag_DocFormat,
  552. BuildFlag_IgnoreWarnings,
  553. BuildFlag_WarningsAsErrors,
  554. BuildFlag_VerboseErrors,
  555. BuildFlag_IgnoreLazy, // internal use only
  556. #if defined(GB_SYSTEM_WINDOWS)
  557. BuildFlag_IgnoreVsSearch,
  558. BuildFlag_ResourceFile,
  559. BuildFlag_WindowsPdbName,
  560. BuildFlag_Subsystem,
  561. #endif
  562. BuildFlag_COUNT,
  563. };
  564. enum BuildFlagParamKind {
  565. BuildFlagParam_None,
  566. BuildFlagParam_Boolean,
  567. BuildFlagParam_Integer,
  568. BuildFlagParam_Float,
  569. BuildFlagParam_String,
  570. BuildFlagParam_COUNT,
  571. };
  572. struct BuildFlag {
  573. BuildFlagKind kind;
  574. String name;
  575. BuildFlagParamKind param_kind;
  576. u32 command_support;
  577. bool allow_mulitple;
  578. };
  579. void add_flag(Array<BuildFlag> *build_flags, BuildFlagKind kind, String name, BuildFlagParamKind param_kind, u32 command_support, bool allow_mulitple=false) {
  580. BuildFlag flag = {kind, name, param_kind, command_support, allow_mulitple};
  581. array_add(build_flags, flag);
  582. }
  583. ExactValue build_param_to_exact_value(String name, String param) {
  584. ExactValue value = {};
  585. /*
  586. Bail out on an empty param string
  587. */
  588. if (param.len == 0) {
  589. gb_printf_err("Invalid flag parameter for '%.*s' = '%.*s'\n", LIT(name), LIT(param));
  590. return value;
  591. }
  592. /*
  593. Attempt to parse as bool first.
  594. */
  595. if (str_eq_ignore_case(param, str_lit("t")) || str_eq_ignore_case(param, str_lit("true"))) {
  596. return exact_value_bool(true);
  597. }
  598. if (str_eq_ignore_case(param, str_lit("f")) || str_eq_ignore_case(param, str_lit("false"))) {
  599. return exact_value_bool(false);
  600. }
  601. /*
  602. Try to parse as an integer or float
  603. */
  604. if (param[0] == '-' || param[0] == '+' || gb_is_between(param[0], '0', '9')) {
  605. if (string_contains_char(param, '.')) {
  606. value = exact_value_float_from_string(param);
  607. } else {
  608. value = exact_value_integer_from_string(param);
  609. }
  610. if (value.kind != ExactValue_Invalid) {
  611. return value;
  612. }
  613. }
  614. /*
  615. Treat the param as a string literal,
  616. optionally be quoted in '' to avoid being parsed as a bool, integer or float.
  617. */
  618. value = exact_value_string(param);
  619. if (param[0] == '\'' && value.kind == ExactValue_String) {
  620. String s = value.value_string;
  621. if (s.len > 1 && s[0] == '\'' && s[s.len-1] == '\'') {
  622. value.value_string = substring(s, 1, s.len-1);
  623. }
  624. }
  625. if (value.kind != ExactValue_String) {
  626. gb_printf_err("Invalid flag parameter for '%.*s' = '%.*s'\n", LIT(name), LIT(param));
  627. }
  628. return value;
  629. }
  630. bool parse_build_flags(Array<String> args) {
  631. auto build_flags = array_make<BuildFlag>(heap_allocator(), 0, BuildFlag_COUNT);
  632. add_flag(&build_flags, BuildFlag_Help, str_lit("help"), BuildFlagParam_None, Command_all);
  633. add_flag(&build_flags, BuildFlag_OutFile, str_lit("out"), BuildFlagParam_String, Command__does_build &~ Command_test);
  634. add_flag(&build_flags, BuildFlag_OptimizationLevel, str_lit("opt"), BuildFlagParam_Integer, Command__does_build);
  635. add_flag(&build_flags, BuildFlag_OptimizationMode, str_lit("o"), BuildFlagParam_String, Command__does_build);
  636. add_flag(&build_flags, BuildFlag_OptimizationMode, str_lit("O"), BuildFlagParam_String, Command__does_build);
  637. add_flag(&build_flags, BuildFlag_ShowTimings, str_lit("show-timings"), BuildFlagParam_None, Command__does_check);
  638. add_flag(&build_flags, BuildFlag_ShowMoreTimings, str_lit("show-more-timings"), BuildFlagParam_None, Command__does_check);
  639. add_flag(&build_flags, BuildFlag_ShowUnused, str_lit("show-unused"), BuildFlagParam_None, Command_check);
  640. add_flag(&build_flags, BuildFlag_ShowUnusedWithLocation, str_lit("show-unused-with-location"), BuildFlagParam_None, Command_check);
  641. add_flag(&build_flags, BuildFlag_ShowSystemCalls, str_lit("show-system-calls"), BuildFlagParam_None, Command_all);
  642. add_flag(&build_flags, BuildFlag_ThreadCount, str_lit("thread-count"), BuildFlagParam_Integer, Command_all);
  643. add_flag(&build_flags, BuildFlag_KeepTempFiles, str_lit("keep-temp-files"), BuildFlagParam_None, Command__does_build|Command_strip_semicolon);
  644. add_flag(&build_flags, BuildFlag_Collection, str_lit("collection"), BuildFlagParam_String, Command__does_check);
  645. add_flag(&build_flags, BuildFlag_Define, str_lit("define"), BuildFlagParam_String, Command__does_check, true);
  646. add_flag(&build_flags, BuildFlag_BuildMode, str_lit("build-mode"), BuildFlagParam_String, Command__does_build); // Commands_build is not used to allow for a better error message
  647. add_flag(&build_flags, BuildFlag_Target, str_lit("target"), BuildFlagParam_String, Command__does_check);
  648. add_flag(&build_flags, BuildFlag_Debug, str_lit("debug"), BuildFlagParam_None, Command__does_check);
  649. add_flag(&build_flags, BuildFlag_DisableAssert, str_lit("disable-assert"), BuildFlagParam_None, Command__does_check);
  650. add_flag(&build_flags, BuildFlag_NoBoundsCheck, str_lit("no-bounds-check"), BuildFlagParam_None, Command__does_check);
  651. add_flag(&build_flags, BuildFlag_NoDynamicLiterals, str_lit("no-dynamic-literals"), BuildFlagParam_None, Command__does_check);
  652. add_flag(&build_flags, BuildFlag_NoCRT, str_lit("no-crt"), BuildFlagParam_None, Command__does_build);
  653. add_flag(&build_flags, BuildFlag_NoEntryPoint, str_lit("no-entry-point"), BuildFlagParam_None, Command__does_check &~ Command_test);
  654. add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None, Command__does_build);
  655. add_flag(&build_flags, BuildFlag_UseSeparateModules,str_lit("use-separate-modules"),BuildFlagParam_None, Command__does_build);
  656. add_flag(&build_flags, BuildFlag_ThreadedChecker, str_lit("threaded-checker"), BuildFlagParam_None, Command__does_check);
  657. add_flag(&build_flags, BuildFlag_NoThreadedChecker, str_lit("no-threaded-checker"), BuildFlagParam_None, Command__does_check);
  658. add_flag(&build_flags, BuildFlag_ShowDebugMessages, str_lit("show-debug-messages"), BuildFlagParam_None, Command_all);
  659. add_flag(&build_flags, BuildFlag_Vet, str_lit("vet"), BuildFlagParam_None, Command__does_check);
  660. add_flag(&build_flags, BuildFlag_VetExtra, str_lit("vet-extra"), BuildFlagParam_None, Command__does_check);
  661. add_flag(&build_flags, BuildFlag_UseLLVMApi, str_lit("llvm-api"), BuildFlagParam_None, Command__does_build);
  662. add_flag(&build_flags, BuildFlag_IgnoreUnknownAttributes, str_lit("ignore-unknown-attributes"), BuildFlagParam_None, Command__does_check);
  663. add_flag(&build_flags, BuildFlag_ExtraLinkerFlags, str_lit("extra-linker-flags"), BuildFlagParam_String, Command__does_build);
  664. add_flag(&build_flags, BuildFlag_Microarch, str_lit("microarch"), BuildFlagParam_String, Command__does_build);
  665. add_flag(&build_flags, BuildFlag_TestName, str_lit("test-name"), BuildFlagParam_String, Command_test);
  666. add_flag(&build_flags, BuildFlag_DisallowDo, str_lit("disallow-do"), BuildFlagParam_None, Command__does_check);
  667. add_flag(&build_flags, BuildFlag_DefaultToNilAllocator, str_lit("default-to-nil-allocator"), BuildFlagParam_None, Command__does_check);
  668. add_flag(&build_flags, BuildFlag_InsertSemicolon, str_lit("insert-semicolon"), BuildFlagParam_None, Command__does_check);
  669. add_flag(&build_flags, BuildFlag_StrictStyle, str_lit("strict-style"), BuildFlagParam_None, Command__does_check);
  670. add_flag(&build_flags, BuildFlag_StrictStyleInitOnly, str_lit("strict-style-init-only"), BuildFlagParam_None, Command__does_check);
  671. add_flag(&build_flags, BuildFlag_Compact, str_lit("compact"), BuildFlagParam_None, Command_query);
  672. add_flag(&build_flags, BuildFlag_GlobalDefinitions, str_lit("global-definitions"), BuildFlagParam_None, Command_query);
  673. add_flag(&build_flags, BuildFlag_GoToDefinitions, str_lit("go-to-definitions"), BuildFlagParam_None, Command_query);
  674. add_flag(&build_flags, BuildFlag_Short, str_lit("short"), BuildFlagParam_None, Command_doc);
  675. add_flag(&build_flags, BuildFlag_AllPackages, str_lit("all-packages"), BuildFlagParam_None, Command_doc);
  676. add_flag(&build_flags, BuildFlag_DocFormat, str_lit("doc-format"), BuildFlagParam_None, Command_doc);
  677. add_flag(&build_flags, BuildFlag_IgnoreWarnings, str_lit("ignore-warnings"), BuildFlagParam_None, Command_all);
  678. add_flag(&build_flags, BuildFlag_WarningsAsErrors, str_lit("warnings-as-errors"), BuildFlagParam_None, Command_all);
  679. add_flag(&build_flags, BuildFlag_VerboseErrors, str_lit("verbose-errors"), BuildFlagParam_None, Command_all);
  680. add_flag(&build_flags, BuildFlag_IgnoreLazy, str_lit("ignore-lazy"), BuildFlagParam_None, Command_all);
  681. #if defined(GB_SYSTEM_WINDOWS)
  682. add_flag(&build_flags, BuildFlag_IgnoreVsSearch, str_lit("ignore-vs-search"), BuildFlagParam_None, Command__does_build);
  683. add_flag(&build_flags, BuildFlag_ResourceFile, str_lit("resource"), BuildFlagParam_String, Command__does_build);
  684. add_flag(&build_flags, BuildFlag_WindowsPdbName, str_lit("pdb-name"), BuildFlagParam_String, Command__does_build);
  685. add_flag(&build_flags, BuildFlag_Subsystem, str_lit("subsystem"), BuildFlagParam_String, Command__does_build);
  686. #endif
  687. GB_ASSERT(args.count >= 3);
  688. Array<String> flag_args = array_slice(args, 3, args.count);
  689. bool set_flags[BuildFlag_COUNT] = {};
  690. bool bad_flags = false;
  691. for_array(i, flag_args) {
  692. String flag = flag_args[i];
  693. if (flag[0] != '-') {
  694. gb_printf_err("Invalid flag: %.*s\n", LIT(flag));
  695. continue;
  696. }
  697. if (string_starts_with(flag, str_lit("--"))) {
  698. flag = substring(flag, 1, flag.len);
  699. }
  700. String name = substring(flag, 1, flag.len);
  701. isize end = 0;
  702. for (; end < name.len; end++) {
  703. if (name[end] == ':') break;
  704. if (name[end] == '=') break; // IMPORTANT TODO(bill): DEPRECATE THIS!!!!
  705. }
  706. name = substring(name, 0, end);
  707. String param = {};
  708. if (end < flag.len-1) param = substring(flag, 2+end, flag.len);
  709. bool is_supported = true;
  710. bool found = false;
  711. BuildFlag found_bf = {};
  712. for_array(build_flag_index, build_flags) {
  713. BuildFlag bf = build_flags[build_flag_index];
  714. if (bf.name == name) {
  715. found = true;
  716. found_bf = bf;
  717. if ((bf.command_support & build_context.command_kind) == 0) {
  718. is_supported = false;
  719. break;
  720. }
  721. if (set_flags[bf.kind]) {
  722. gb_printf_err("Previous flag set: '%.*s'\n", LIT(name));
  723. bad_flags = true;
  724. } else {
  725. ExactValue value = {};
  726. bool ok = false;
  727. if (bf.param_kind == BuildFlagParam_None) {
  728. if (param.len == 0) {
  729. ok = true;
  730. } else {
  731. gb_printf_err("Flag '%.*s' was not expecting a parameter '%.*s'\n", LIT(name), LIT(param));
  732. bad_flags = true;
  733. }
  734. } else if (param.len == 0) {
  735. gb_printf_err("Flag missing for '%.*s'\n", LIT(name));
  736. bad_flags = true;
  737. } else {
  738. ok = true;
  739. switch (bf.param_kind) {
  740. default:
  741. ok = false;
  742. break;
  743. case BuildFlagParam_Boolean: {
  744. if (str_eq_ignore_case(param, str_lit("t")) ||
  745. str_eq_ignore_case(param, str_lit("true")) ||
  746. param == "1") {
  747. value = exact_value_bool(true);
  748. } else if (str_eq_ignore_case(param, str_lit("f")) ||
  749. str_eq_ignore_case(param, str_lit("false")) ||
  750. param == "0") {
  751. value = exact_value_bool(false);
  752. } else {
  753. gb_printf_err("Invalid flag parameter for '%.*s' : '%.*s'\n", LIT(name), LIT(param));
  754. }
  755. } break;
  756. case BuildFlagParam_Integer:
  757. value = exact_value_integer_from_string(param);
  758. break;
  759. case BuildFlagParam_Float:
  760. value = exact_value_float_from_string(param);
  761. break;
  762. case BuildFlagParam_String: {
  763. value = exact_value_string(param);
  764. if (value.kind == ExactValue_String) {
  765. String s = value.value_string;
  766. if (s.len > 1 && s[0] == '"' && s[s.len-1] == '"') {
  767. value.value_string = substring(s, 1, s.len-1);
  768. }
  769. }
  770. break;
  771. }
  772. }
  773. }
  774. if (ok) {
  775. switch (bf.param_kind) {
  776. case BuildFlagParam_None:
  777. if (value.kind != ExactValue_Invalid) {
  778. gb_printf_err("%.*s expected no value, got %.*s", LIT(name), LIT(param));
  779. bad_flags = true;
  780. ok = false;
  781. }
  782. break;
  783. case BuildFlagParam_Boolean:
  784. if (value.kind != ExactValue_Bool) {
  785. gb_printf_err("%.*s expected a boolean, got %.*s", LIT(name), LIT(param));
  786. bad_flags = true;
  787. ok = false;
  788. }
  789. break;
  790. case BuildFlagParam_Integer:
  791. if (value.kind != ExactValue_Integer) {
  792. gb_printf_err("%.*s expected an integer, got %.*s", LIT(name), LIT(param));
  793. bad_flags = true;
  794. ok = false;
  795. }
  796. break;
  797. case BuildFlagParam_Float:
  798. if (value.kind != ExactValue_Float) {
  799. gb_printf_err("%.*s expected a floating pointer number, got %.*s", LIT(name), LIT(param));
  800. bad_flags = true;
  801. ok = false;
  802. }
  803. break;
  804. case BuildFlagParam_String:
  805. if (value.kind != ExactValue_String) {
  806. gb_printf_err("%.*s expected a string, got %.*s", LIT(name), LIT(param));
  807. bad_flags = true;
  808. ok = false;
  809. }
  810. break;
  811. }
  812. if (ok) switch (bf.kind) {
  813. case BuildFlag_Help:
  814. build_context.show_help = true;
  815. break;
  816. case BuildFlag_OutFile: {
  817. GB_ASSERT(value.kind == ExactValue_String);
  818. String path = value.value_string;
  819. path = string_trim_whitespace(path);
  820. if (is_build_flag_path_valid(path)) {
  821. build_context.out_filepath = path_to_full_path(heap_allocator(), path);
  822. } else {
  823. gb_printf_err("Invalid -out path, got %.*s\n", LIT(path));
  824. bad_flags = true;
  825. }
  826. break;
  827. }
  828. case BuildFlag_OptimizationLevel:
  829. GB_ASSERT(value.kind == ExactValue_Integer);
  830. if (set_flags[BuildFlag_OptimizationMode]) {
  831. gb_printf_err("Mixture of -opt and -o is not allowed\n");
  832. bad_flags = true;
  833. break;
  834. }
  835. build_context.optimization_level = cast(i32)big_int_to_i64(&value.value_integer);
  836. break;
  837. case BuildFlag_OptimizationMode:
  838. GB_ASSERT(value.kind == ExactValue_String);
  839. if (set_flags[BuildFlag_OptimizationLevel]) {
  840. gb_printf_err("Mixture of -opt and -o is not allowed\n");
  841. bad_flags = true;
  842. break;
  843. }
  844. if (value.value_string == "minimal") {
  845. build_context.optimization_level = 0;
  846. } else if (value.value_string == "size") {
  847. build_context.optimization_level = 1;
  848. } else if (value.value_string == "speed") {
  849. build_context.optimization_level = 2;
  850. } else {
  851. gb_printf_err("Invalid optimization mode for -o:<string>, got %.*s\n", LIT(value.value_string));
  852. gb_printf_err("Valid optimization modes:\n");
  853. gb_printf_err("\tminimal\n");
  854. gb_printf_err("\tsize\n");
  855. gb_printf_err("\tspeed\n");
  856. bad_flags = true;
  857. }
  858. break;
  859. case BuildFlag_ShowTimings:
  860. GB_ASSERT(value.kind == ExactValue_Invalid);
  861. build_context.show_timings = true;
  862. break;
  863. case BuildFlag_ShowUnused:
  864. GB_ASSERT(value.kind == ExactValue_Invalid);
  865. build_context.show_unused = true;
  866. break;
  867. case BuildFlag_ShowUnusedWithLocation:
  868. GB_ASSERT(value.kind == ExactValue_Invalid);
  869. build_context.show_unused = true;
  870. build_context.show_unused_with_location = true;
  871. break;
  872. case BuildFlag_ShowMoreTimings:
  873. GB_ASSERT(value.kind == ExactValue_Invalid);
  874. build_context.show_timings = true;
  875. build_context.show_more_timings = true;
  876. break;
  877. case BuildFlag_ShowSystemCalls:
  878. GB_ASSERT(value.kind == ExactValue_Invalid);
  879. build_context.show_system_calls = true;
  880. break;
  881. case BuildFlag_ThreadCount: {
  882. GB_ASSERT(value.kind == ExactValue_Integer);
  883. isize count = cast(isize)big_int_to_i64(&value.value_integer);
  884. if (count <= 0) {
  885. gb_printf_err("%.*s expected a positive non-zero number, got %.*s\n", LIT(name), LIT(param));
  886. build_context.thread_count = 1;
  887. } else {
  888. build_context.thread_count = count;
  889. }
  890. break;
  891. }
  892. case BuildFlag_KeepTempFiles:
  893. GB_ASSERT(value.kind == ExactValue_Invalid);
  894. build_context.keep_temp_files = true;
  895. break;
  896. case BuildFlag_Collection: {
  897. GB_ASSERT(value.kind == ExactValue_String);
  898. String str = value.value_string;
  899. isize eq_pos = -1;
  900. for (isize i = 0; i < str.len; i++) {
  901. if (str[i] == '=') {
  902. eq_pos = i;
  903. break;
  904. }
  905. }
  906. if (eq_pos < 0) {
  907. gb_printf_err("Expected 'name=path', got '%.*s'\n", LIT(param));
  908. bad_flags = true;
  909. break;
  910. }
  911. String name = substring(str, 0, eq_pos);
  912. String path = substring(str, eq_pos+1, str.len);
  913. if (name.len == 0 || path.len == 0) {
  914. gb_printf_err("Expected 'name=path', got '%.*s'\n", LIT(param));
  915. bad_flags = true;
  916. break;
  917. }
  918. if (!string_is_valid_identifier(name)) {
  919. gb_printf_err("Library collection name '%.*s' must be a valid identifier\n", LIT(name));
  920. bad_flags = true;
  921. break;
  922. }
  923. if (name == "_") {
  924. gb_printf_err("Library collection name cannot be an underscore\n");
  925. bad_flags = true;
  926. break;
  927. }
  928. if (name == "system") {
  929. gb_printf_err("Library collection name 'system' is reserved\n");
  930. bad_flags = true;
  931. break;
  932. }
  933. String prev_path = {};
  934. bool found = find_library_collection_path(name, &prev_path);
  935. if (found) {
  936. gb_printf_err("Library collection '%.*s' already exists with path '%.*s'\n", LIT(name), LIT(prev_path));
  937. bad_flags = true;
  938. break;
  939. }
  940. gbAllocator a = heap_allocator();
  941. String fullpath = path_to_fullpath(a, path);
  942. if (!path_is_directory(fullpath)) {
  943. gb_printf_err("Library collection '%.*s' path must be a directory, got '%.*s'\n", LIT(name), LIT(fullpath));
  944. gb_free(a, fullpath.text);
  945. bad_flags = true;
  946. break;
  947. }
  948. add_library_collection(name, path);
  949. // NOTE(bill): Allow for multiple library collections
  950. continue;
  951. }
  952. case BuildFlag_Define: {
  953. GB_ASSERT(value.kind == ExactValue_String);
  954. String str = value.value_string;
  955. isize eq_pos = -1;
  956. for (isize i = 0; i < str.len; i++) {
  957. if (str[i] == '=') {
  958. eq_pos = i;
  959. break;
  960. }
  961. }
  962. if (eq_pos < 0) {
  963. gb_printf_err("Expected 'name=value', got '%.*s'\n", LIT(param));
  964. bad_flags = true;
  965. break;
  966. }
  967. String name = substring(str, 0, eq_pos);
  968. String value = substring(str, eq_pos+1, str.len);
  969. if (name.len == 0 || value.len == 0) {
  970. gb_printf_err("Expected 'name=value', got '%.*s'\n", LIT(param));
  971. bad_flags = true;
  972. break;
  973. }
  974. if (!string_is_valid_identifier(name)) {
  975. gb_printf_err("Defined constant name '%.*s' must be a valid identifier\n", LIT(name));
  976. bad_flags = true;
  977. break;
  978. }
  979. if (name == "_") {
  980. gb_printf_err("Defined constant name cannot be an underscore\n");
  981. bad_flags = true;
  982. break;
  983. }
  984. HashKey key = hash_pointer(string_intern(name));
  985. if (map_get(&build_context.defined_values, key) != nullptr) {
  986. gb_printf_err("Defined constant '%.*s' already exists\n", LIT(name));
  987. bad_flags = true;
  988. break;
  989. }
  990. ExactValue v = build_param_to_exact_value(name, value);
  991. if (v.kind != ExactValue_Invalid) {
  992. map_set(&build_context.defined_values, key, v);
  993. } else {
  994. gb_printf_err("Invalid define constant value: '%.*s'. Define constants must be a valid Odin literal.\n", LIT(value));
  995. bad_flags = true;
  996. }
  997. break;
  998. }
  999. case BuildFlag_Target: {
  1000. GB_ASSERT(value.kind == ExactValue_String);
  1001. String str = value.value_string;
  1002. bool found = false;
  1003. for (isize i = 0; i < gb_count_of(named_targets); i++) {
  1004. if (str_eq_ignore_case(str, named_targets[i].name)) {
  1005. found = true;
  1006. selected_target_metrics = named_targets + i;
  1007. break;
  1008. }
  1009. }
  1010. if (!found) {
  1011. struct DistanceAndTargetIndex {
  1012. isize distance;
  1013. isize target_index;
  1014. };
  1015. DistanceAndTargetIndex distances[gb_count_of(named_targets)] = {};
  1016. for (isize i = 0; i < gb_count_of(named_targets); i++) {
  1017. distances[i].target_index = i;
  1018. distances[i].distance = levenstein_distance_case_insensitive(str, named_targets[i].name);
  1019. }
  1020. gb_sort_array(distances, gb_count_of(distances), gb_isize_cmp(gb_offset_of(DistanceAndTargetIndex, distance)));
  1021. gb_printf_err("Unknown target '%.*s'\n", LIT(str));
  1022. if (distances[0].distance <= MAX_SMALLEST_DID_YOU_MEAN_DISTANCE) {
  1023. gb_printf_err("Did you mean:\n");
  1024. for (isize i = 0; i < gb_count_of(named_targets); i++) {
  1025. if (distances[i].distance > MAX_SMALLEST_DID_YOU_MEAN_DISTANCE) {
  1026. break;
  1027. }
  1028. gb_printf_err("\t%.*s\n", LIT(named_targets[distances[i].target_index].name));
  1029. }
  1030. }
  1031. gb_printf_err("All supported targets:\n");
  1032. for (isize i = 0; i < gb_count_of(named_targets); i++) {
  1033. gb_printf_err("\t%.*s\n", LIT(named_targets[i].name));
  1034. }
  1035. bad_flags = true;
  1036. }
  1037. break;
  1038. }
  1039. case BuildFlag_BuildMode: {
  1040. GB_ASSERT(value.kind == ExactValue_String);
  1041. String str = value.value_string;
  1042. if (build_context.command != "build") {
  1043. gb_printf_err("'build-mode' can only be used with the 'build' command\n");
  1044. bad_flags = true;
  1045. break;
  1046. }
  1047. if (str == "dll" || str == "shared") {
  1048. build_context.build_mode = BuildMode_DynamicLibrary;
  1049. } else if (str == "obj" || str == "object") {
  1050. build_context.build_mode = BuildMode_Object;
  1051. } else if (str == "exe") {
  1052. build_context.build_mode = BuildMode_Executable;
  1053. } else if (str == "asm" || str == "assembly" || str == "assembler") {
  1054. build_context.build_mode = BuildMode_Assembly;
  1055. } else if (str == "llvm" || str == "llvm-ir") {
  1056. build_context.build_mode = BuildMode_LLVM_IR;
  1057. } else {
  1058. gb_printf_err("Unknown build mode '%.*s'\n", LIT(str));
  1059. gb_printf_err("Valid build modes:\n");
  1060. gb_printf_err("\tdll, shared\n");
  1061. gb_printf_err("\tobj, object\n");
  1062. gb_printf_err("\texe\n");
  1063. gb_printf_err("\tasm, assembly, assembler\n");
  1064. gb_printf_err("\tllvm, llvm-ir\n");
  1065. bad_flags = true;
  1066. break;
  1067. }
  1068. break;
  1069. }
  1070. case BuildFlag_Debug:
  1071. build_context.ODIN_DEBUG = true;
  1072. break;
  1073. case BuildFlag_DisableAssert:
  1074. build_context.ODIN_DISABLE_ASSERT = true;
  1075. break;
  1076. case BuildFlag_NoBoundsCheck:
  1077. build_context.no_bounds_check = true;
  1078. break;
  1079. case BuildFlag_NoDynamicLiterals:
  1080. build_context.no_dynamic_literals = true;
  1081. break;
  1082. case BuildFlag_NoCRT:
  1083. build_context.no_crt = true;
  1084. break;
  1085. case BuildFlag_NoEntryPoint:
  1086. build_context.no_entry_point = true;
  1087. break;
  1088. case BuildFlag_UseLLD:
  1089. build_context.use_lld = true;
  1090. break;
  1091. case BuildFlag_UseSeparateModules:
  1092. build_context.use_separate_modules = true;
  1093. break;
  1094. case BuildFlag_ThreadedChecker:
  1095. #if defined(DEFAULT_TO_THREADED_CHECKER)
  1096. gb_printf_err("-threaded-checker is the default on this platform\n");
  1097. bad_flags = true;
  1098. #endif
  1099. build_context.threaded_checker = true;
  1100. break;
  1101. case BuildFlag_NoThreadedChecker:
  1102. #if !defined(DEFAULT_TO_THREADED_CHECKER)
  1103. gb_printf_err("-no-threaded-checker is the default on this platform\n");
  1104. bad_flags = true;
  1105. #endif
  1106. build_context.threaded_checker = false;
  1107. break;
  1108. case BuildFlag_ShowDebugMessages:
  1109. build_context.show_debug_messages = true;
  1110. break;
  1111. case BuildFlag_Vet:
  1112. build_context.vet = true;
  1113. break;
  1114. case BuildFlag_VetExtra:
  1115. build_context.vet = true;
  1116. build_context.vet_extra = true;
  1117. break;
  1118. case BuildFlag_UseLLVMApi:
  1119. gb_printf_err("-llvm-api flag is not required any more\n");
  1120. bad_flags = true;
  1121. break;
  1122. case BuildFlag_IgnoreUnknownAttributes:
  1123. build_context.ignore_unknown_attributes = true;
  1124. break;
  1125. case BuildFlag_ExtraLinkerFlags:
  1126. GB_ASSERT(value.kind == ExactValue_String);
  1127. build_context.extra_linker_flags = value.value_string;
  1128. break;
  1129. case BuildFlag_Microarch:
  1130. GB_ASSERT(value.kind == ExactValue_String);
  1131. build_context.microarch = value.value_string;
  1132. string_to_lower(&build_context.microarch);
  1133. break;
  1134. case BuildFlag_TestName:
  1135. GB_ASSERT(value.kind == ExactValue_String);
  1136. {
  1137. String name = value.value_string;
  1138. if (!string_is_valid_identifier(name)) {
  1139. gb_printf_err("Test name '%.*s' must be a valid identifier\n", LIT(name));
  1140. bad_flags = true;
  1141. break;
  1142. }
  1143. string_set_add(&build_context.test_names, name);
  1144. // NOTE(bill): Allow for multiple -test-name
  1145. continue;
  1146. }
  1147. case BuildFlag_DisallowDo:
  1148. build_context.disallow_do = true;
  1149. break;
  1150. case BuildFlag_DefaultToNilAllocator:
  1151. build_context.ODIN_DEFAULT_TO_NIL_ALLOCATOR = true;
  1152. break;
  1153. case BuildFlag_InsertSemicolon:
  1154. gb_printf_err("-insert-semicolon flag is not required any more\n");
  1155. bad_flags = true;
  1156. break;
  1157. case BuildFlag_StrictStyle:
  1158. if (build_context.strict_style_init_only) {
  1159. gb_printf_err("-strict-style and -strict-style-init-only cannot be used together\n");
  1160. }
  1161. build_context.strict_style = true;
  1162. break;
  1163. case BuildFlag_StrictStyleInitOnly:
  1164. if (build_context.strict_style) {
  1165. gb_printf_err("-strict-style and -strict-style-init-only cannot be used together\n");
  1166. }
  1167. build_context.strict_style_init_only = true;
  1168. break;
  1169. case BuildFlag_Compact:
  1170. if (!build_context.query_data_set_settings.ok) {
  1171. gb_printf_err("Invalid use of -compact flag, only allowed with 'odin query'\n");
  1172. bad_flags = true;
  1173. } else {
  1174. build_context.query_data_set_settings.compact = true;
  1175. }
  1176. break;
  1177. case BuildFlag_GlobalDefinitions:
  1178. if (!build_context.query_data_set_settings.ok) {
  1179. gb_printf_err("Invalid use of -global-definitions flag, only allowed with 'odin query'\n");
  1180. bad_flags = true;
  1181. } else if (build_context.query_data_set_settings.kind != QueryDataSet_Invalid) {
  1182. gb_printf_err("Invalid use of -global-definitions flag, a previous flag for 'odin query' was set\n");
  1183. bad_flags = true;
  1184. } else {
  1185. build_context.query_data_set_settings.kind = QueryDataSet_GlobalDefinitions;
  1186. }
  1187. break;
  1188. case BuildFlag_GoToDefinitions:
  1189. if (!build_context.query_data_set_settings.ok) {
  1190. gb_printf_err("Invalid use of -go-to-definitions flag, only allowed with 'odin query'\n");
  1191. bad_flags = true;
  1192. } else if (build_context.query_data_set_settings.kind != QueryDataSet_Invalid) {
  1193. gb_printf_err("Invalid use of -global-definitions flag, a previous flag for 'odin query' was set\n");
  1194. bad_flags = true;
  1195. } else {
  1196. build_context.query_data_set_settings.kind = QueryDataSet_GoToDefinitions;
  1197. }
  1198. break;
  1199. case BuildFlag_Short:
  1200. build_context.cmd_doc_flags |= CmdDocFlag_Short;
  1201. break;
  1202. case BuildFlag_AllPackages:
  1203. build_context.cmd_doc_flags |= CmdDocFlag_AllPackages;
  1204. break;
  1205. case BuildFlag_DocFormat:
  1206. build_context.cmd_doc_flags |= CmdDocFlag_DocFormat;
  1207. break;
  1208. case BuildFlag_IgnoreWarnings:
  1209. if (build_context.warnings_as_errors) {
  1210. gb_printf_err("-ignore-warnings cannot be used with -warnings-as-errors\n");
  1211. bad_flags = true;
  1212. } else {
  1213. build_context.ignore_warnings = true;
  1214. }
  1215. break;
  1216. case BuildFlag_WarningsAsErrors:
  1217. if (build_context.ignore_warnings) {
  1218. gb_printf_err("-warnings-as-errors cannot be used with -ignore-warnings\n");
  1219. bad_flags = true;
  1220. } else {
  1221. build_context.warnings_as_errors = true;
  1222. }
  1223. break;
  1224. case BuildFlag_VerboseErrors:
  1225. build_context.show_error_line = true;
  1226. break;
  1227. case BuildFlag_IgnoreLazy:
  1228. build_context.ignore_lazy = true;
  1229. break;
  1230. #if defined(GB_SYSTEM_WINDOWS)
  1231. case BuildFlag_IgnoreVsSearch:
  1232. GB_ASSERT(value.kind == ExactValue_Invalid);
  1233. build_context.ignore_microsoft_magic = true;
  1234. break;
  1235. case BuildFlag_ResourceFile: {
  1236. GB_ASSERT(value.kind == ExactValue_String);
  1237. String path = value.value_string;
  1238. path = string_trim_whitespace(path);
  1239. if (is_build_flag_path_valid(path)) {
  1240. if(!string_ends_with(path, str_lit(".rc"))) {
  1241. gb_printf_err("Invalid -resource path %.*s, missing .rc\n", LIT(path));
  1242. bad_flags = true;
  1243. break;
  1244. }
  1245. build_context.resource_filepath = substring(path, 0, string_extension_position(path));
  1246. build_context.has_resource = true;
  1247. } else {
  1248. gb_printf_err("Invalid -resource path, got %.*s\n", LIT(path));
  1249. bad_flags = true;
  1250. }
  1251. break;
  1252. }
  1253. case BuildFlag_WindowsPdbName: {
  1254. GB_ASSERT(value.kind == ExactValue_String);
  1255. String path = value.value_string;
  1256. path = string_trim_whitespace(path);
  1257. if (is_build_flag_path_valid(path)) {
  1258. // #if defined(GB_SYSTEM_WINDOWS)
  1259. // String ext = path_extension(path);
  1260. // if (ext != ".pdb") {
  1261. // path = substring(path, 0, string_extension_position(path));
  1262. // }
  1263. // #endif
  1264. build_context.pdb_filepath = path;
  1265. } else {
  1266. gb_printf_err("Invalid -pdb-name path, got %.*s\n", LIT(path));
  1267. bad_flags = true;
  1268. }
  1269. break;
  1270. }
  1271. case BuildFlag_Subsystem: {
  1272. GB_ASSERT(value.kind == ExactValue_String);
  1273. String subsystem = value.value_string;
  1274. if (str_eq_ignore_case(subsystem, str_lit("console"))) {
  1275. build_context.use_subsystem_windows = false;
  1276. } else if (str_eq_ignore_case(subsystem, str_lit("window"))) {
  1277. build_context.use_subsystem_windows = true;
  1278. } else if (str_eq_ignore_case(subsystem, str_lit("windows"))) {
  1279. build_context.use_subsystem_windows = true;
  1280. } else {
  1281. gb_printf_err("Invalid -subsystem string, got %.*s, expected either 'console' or 'windows'\n", LIT(subsystem));
  1282. bad_flags = true;
  1283. }
  1284. break;
  1285. }
  1286. #endif
  1287. }
  1288. }
  1289. if (!bf.allow_mulitple) {
  1290. set_flags[bf.kind] = ok;
  1291. }
  1292. }
  1293. break;
  1294. }
  1295. }
  1296. if (found && !is_supported) {
  1297. gb_printf_err("Unknown flag for 'odin %.*s': '%.*s'\n", LIT(build_context.command), LIT(name));
  1298. gb_printf_err("'%.*s' is supported with the following commands:\n", LIT(name));
  1299. gb_printf_err("\t");
  1300. i32 count = 0;
  1301. for (u32 i = 0; i < 32; i++) {
  1302. if (found_bf.command_support & (1<<i)) {
  1303. if (count > 0) {
  1304. gb_printf_err(", ");
  1305. }
  1306. gb_printf_err("%s", odin_command_strings[i]);
  1307. count += 1;
  1308. }
  1309. }
  1310. gb_printf_err("\n");
  1311. bad_flags = true;
  1312. } else if (!found) {
  1313. gb_printf_err("Unknown flag: '%.*s'\n", LIT(name));
  1314. bad_flags = true;
  1315. }
  1316. }
  1317. if (build_context.query_data_set_settings.ok) {
  1318. if (build_context.query_data_set_settings.kind == QueryDataSet_Invalid) {
  1319. gb_printf_err("'odin query' requires a flag determining the kind of query data set to be returned\n");
  1320. gb_printf_err("\t-global-definitions : outputs a JSON file of global definitions\n");
  1321. gb_printf_err("\t-go-to-definitions : outputs a OGTD binary file of go to definitions for identifiers within an Odin project\n");
  1322. bad_flags = true;
  1323. }
  1324. }
  1325. return !bad_flags;
  1326. }
  1327. void show_timings(Checker *c, Timings *t) {
  1328. Parser *p = c->parser;
  1329. isize lines = p->total_line_count;
  1330. isize tokens = p->total_token_count;
  1331. isize files = 0;
  1332. isize packages = p->packages.count;
  1333. isize total_file_size = 0;
  1334. f64 total_tokenizing_time = 0;
  1335. f64 total_parsing_time = 0;
  1336. for_array(i, p->packages) {
  1337. files += p->packages[i]->files.count;
  1338. for_array(j, p->packages[i]->files) {
  1339. AstFile *file = p->packages[i]->files[j];
  1340. total_tokenizing_time += file->time_to_tokenize;
  1341. total_parsing_time += file->time_to_parse;
  1342. total_file_size += file->tokenizer.end - file->tokenizer.start;
  1343. }
  1344. }
  1345. timings_print_all(t);
  1346. if (build_context.show_debug_messages && build_context.show_more_timings) {
  1347. {
  1348. gb_printf("\n");
  1349. gb_printf("Total Lines - %td\n", lines);
  1350. gb_printf("Total Tokens - %td\n", tokens);
  1351. gb_printf("Total Files - %td\n", files);
  1352. gb_printf("Total Packages - %td\n", packages);
  1353. gb_printf("Total File Size - %td\n", total_file_size);
  1354. gb_printf("\n");
  1355. }
  1356. {
  1357. f64 time = total_tokenizing_time;
  1358. gb_printf("Tokenization Only\n");
  1359. gb_printf("LOC/s - %.3f\n", cast(f64)lines/time);
  1360. gb_printf("us/LOC - %.3f\n", 1.0e6*time/cast(f64)lines);
  1361. gb_printf("Tokens/s - %.3f\n", cast(f64)tokens/time);
  1362. gb_printf("us/Token - %.3f\n", 1.0e6*time/cast(f64)tokens);
  1363. gb_printf("bytes/s - %.3f\n", cast(f64)total_file_size/time);
  1364. gb_printf("MiB/s - %.3f\n", cast(f64)(total_file_size/time)/(1024*1024));
  1365. gb_printf("us/bytes - %.3f\n", 1.0e6*time/cast(f64)total_file_size);
  1366. gb_printf("\n");
  1367. }
  1368. {
  1369. f64 time = total_parsing_time;
  1370. gb_printf("Parsing Only\n");
  1371. gb_printf("LOC/s - %.3f\n", cast(f64)lines/time);
  1372. gb_printf("us/LOC - %.3f\n", 1.0e6*time/cast(f64)lines);
  1373. gb_printf("Tokens/s - %.3f\n", cast(f64)tokens/time);
  1374. gb_printf("us/Token - %.3f\n", 1.0e6*time/cast(f64)tokens);
  1375. gb_printf("bytes/s - %.3f\n", cast(f64)total_file_size/time);
  1376. gb_printf("MiB/s - %.3f\n", cast(f64)(total_file_size/time)/(1024*1024));
  1377. gb_printf("us/bytes - %.3f\n", 1.0e6*time/cast(f64)total_file_size);
  1378. gb_printf("\n");
  1379. }
  1380. {
  1381. TimeStamp ts = {};
  1382. for_array(i, t->sections) {
  1383. TimeStamp s = t->sections[i];
  1384. if (s.label == "parse files") {
  1385. ts = s;
  1386. break;
  1387. }
  1388. }
  1389. GB_ASSERT(ts.label == "parse files");
  1390. f64 parse_time = time_stamp_as_s(ts, t->freq);
  1391. gb_printf("Parse pass\n");
  1392. gb_printf("LOC/s - %.3f\n", cast(f64)lines/parse_time);
  1393. gb_printf("us/LOC - %.3f\n", 1.0e6*parse_time/cast(f64)lines);
  1394. gb_printf("Tokens/s - %.3f\n", cast(f64)tokens/parse_time);
  1395. gb_printf("us/Token - %.3f\n", 1.0e6*parse_time/cast(f64)tokens);
  1396. gb_printf("bytes/s - %.3f\n", cast(f64)total_file_size/parse_time);
  1397. gb_printf("MiB/s - %.3f\n", cast(f64)(total_file_size/parse_time)/(1024*1024));
  1398. gb_printf("us/bytes - %.3f\n", 1.0e6*parse_time/cast(f64)total_file_size);
  1399. gb_printf("\n");
  1400. }
  1401. {
  1402. TimeStamp ts = {};
  1403. TimeStamp ts_end = {};
  1404. for_array(i, t->sections) {
  1405. TimeStamp s = t->sections[i];
  1406. if (s.label == "type check") {
  1407. ts = s;
  1408. }
  1409. if (s.label == "type check finish") {
  1410. GB_ASSERT(ts.label != "");
  1411. ts_end = s;
  1412. break;
  1413. }
  1414. }
  1415. GB_ASSERT(ts.label != "");
  1416. GB_ASSERT(ts_end.label != "");
  1417. ts.finish = ts_end.finish;
  1418. f64 parse_time = time_stamp_as_s(ts, t->freq);
  1419. gb_printf("Checker pass\n");
  1420. gb_printf("LOC/s - %.3f\n", cast(f64)lines/parse_time);
  1421. gb_printf("us/LOC - %.3f\n", 1.0e6*parse_time/cast(f64)lines);
  1422. gb_printf("Tokens/s - %.3f\n", cast(f64)tokens/parse_time);
  1423. gb_printf("us/Token - %.3f\n", 1.0e6*parse_time/cast(f64)tokens);
  1424. gb_printf("bytes/s - %.3f\n", cast(f64)total_file_size/parse_time);
  1425. gb_printf("MiB/s - %.3f\n", (cast(f64)total_file_size/parse_time)/(1024*1024));
  1426. gb_printf("us/bytes - %.3f\n", 1.0e6*parse_time/cast(f64)total_file_size);
  1427. gb_printf("\n");
  1428. }
  1429. {
  1430. f64 total_time = t->total_time_seconds;
  1431. gb_printf("Total pass\n");
  1432. gb_printf("LOC/s - %.3f\n", cast(f64)lines/total_time);
  1433. gb_printf("us/LOC - %.3f\n", 1.0e6*total_time/cast(f64)lines);
  1434. gb_printf("Tokens/s - %.3f\n", cast(f64)tokens/total_time);
  1435. gb_printf("us/Token - %.3f\n", 1.0e6*total_time/cast(f64)tokens);
  1436. gb_printf("bytes/s - %.3f\n", cast(f64)total_file_size/total_time);
  1437. gb_printf("MiB/s - %.3f\n", cast(f64)(total_file_size/total_time)/(1024*1024));
  1438. gb_printf("us/bytes - %.3f\n", 1.0e6*total_time/cast(f64)total_file_size);
  1439. gb_printf("\n");
  1440. }
  1441. }
  1442. }
  1443. void remove_temp_files(lbGenerator *gen) {
  1444. if (build_context.keep_temp_files) return;
  1445. for_array(i, gen->output_temp_paths) {
  1446. String path = gen->output_temp_paths[i];
  1447. gb_file_remove(cast(char const *)path.text);
  1448. }
  1449. if (!build_context.keep_object_files) {
  1450. switch (build_context.build_mode) {
  1451. case BuildMode_Executable:
  1452. case BuildMode_DynamicLibrary:
  1453. for_array(i, gen->output_object_paths) {
  1454. String path = gen->output_object_paths[i];
  1455. gb_file_remove(cast(char const *)path.text);
  1456. }
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. void print_show_help(String const arg0, String const &command) {
  1462. print_usage_line(0, "%.*s is a tool for managing Odin source code", LIT(arg0));
  1463. print_usage_line(0, "Usage");
  1464. print_usage_line(1, "%.*s %.*s [arguments]", LIT(arg0), LIT(command));
  1465. print_usage_line(0, "");
  1466. if (command == "build") {
  1467. print_usage_line(1, "build compile .odin file, or directory of .odin files, as an executable.");
  1468. print_usage_line(1, " one must contain the program's entry point, all must be in the same package.");
  1469. } else if (command == "run") {
  1470. print_usage_line(1, "run same as 'build', but also then runs the newly compiled executable.");
  1471. } else if (command == "check") {
  1472. print_usage_line(1, "check parse and type check .odin file(s)");
  1473. } else if (command == "test") {
  1474. print_usage_line(1, "test build ands runs procedures with the attribute @(test) in the initial package");
  1475. } else if (command == "query") {
  1476. print_usage_line(1, "query [experimental] parse, type check, and output a .json file containing information about the program");
  1477. } else if (command == "doc") {
  1478. print_usage_line(1, "doc generate documentation from a .odin file, or directory of .odin files");
  1479. print_usage_line(2, "Examples:");
  1480. print_usage_line(3, "odin doc core/path");
  1481. print_usage_line(3, "odin doc core/path core/path/filepath");
  1482. } else if (command == "version") {
  1483. print_usage_line(1, "version print version");
  1484. } else if (command == "strip-semicolon") {
  1485. print_usage_line(1, "strip-semicolon");
  1486. print_usage_line(2, "parse and type check .odin file(s) and then remove unneeded semicolons from the entire project");
  1487. }
  1488. bool doc = command == "doc";
  1489. bool build = command == "build";
  1490. bool run_or_build = command == "run" || command == "build" || command == "test";
  1491. bool test_only = command == "test";
  1492. bool strip_semicolon = command == "strip-semicolon";
  1493. bool check_only = command == "check" || strip_semicolon;
  1494. bool check = run_or_build || check_only;
  1495. print_usage_line(0, "");
  1496. print_usage_line(1, "Flags");
  1497. print_usage_line(0, "");
  1498. if (doc) {
  1499. print_usage_line(1, "-short");
  1500. print_usage_line(2, "Show shortened documentation for the packages");
  1501. print_usage_line(0, "");
  1502. print_usage_line(1, "-all-packages");
  1503. print_usage_line(2, "Generates documentation for all packages used in the current project");
  1504. print_usage_line(0, "");
  1505. print_usage_line(1, "-doc-format");
  1506. print_usage_line(2, "Generates documentation as the .odin-doc format (useful for external tooling)");
  1507. print_usage_line(0, "");
  1508. }
  1509. if (run_or_build) {
  1510. print_usage_line(1, "-out:<filepath>");
  1511. print_usage_line(2, "Set the file name of the outputted executable");
  1512. print_usage_line(2, "Example: -out:foo.exe");
  1513. print_usage_line(0, "");
  1514. print_usage_line(1, "-opt:<integer>");
  1515. print_usage_line(2, "Set the optimization level for compilation");
  1516. print_usage_line(2, "Accepted values: 0, 1, 2, 3");
  1517. print_usage_line(2, "Example: -opt:2");
  1518. print_usage_line(0, "");
  1519. print_usage_line(1, "-o:<string>");
  1520. print_usage_line(2, "Set the optimization mode for compilation");
  1521. print_usage_line(2, "Accepted values: minimal, size, speed");
  1522. print_usage_line(2, "Example: -o:speed");
  1523. print_usage_line(0, "");
  1524. }
  1525. if (check) {
  1526. print_usage_line(1, "-show-timings");
  1527. print_usage_line(2, "Shows basic overview of the timings of different stages within the compiler in milliseconds");
  1528. print_usage_line(0, "");
  1529. print_usage_line(1, "-show-more-timings");
  1530. print_usage_line(2, "Shows an advanced overview of the timings of different stages within the compiler in milliseconds");
  1531. print_usage_line(0, "");
  1532. print_usage_line(1, "-thread-count:<integer>");
  1533. print_usage_line(2, "Override the number of threads the compiler will use to compile with");
  1534. print_usage_line(2, "Example: -thread-count:2");
  1535. print_usage_line(0, "");
  1536. }
  1537. if (check_only) {
  1538. print_usage_line(1, "-show-unused");
  1539. print_usage_line(2, "Shows unused package declarations within the current project");
  1540. print_usage_line(0, "");
  1541. print_usage_line(1, "-show-unused-with-location");
  1542. print_usage_line(2, "Shows unused package declarations within the current project with the declarations source location");
  1543. print_usage_line(0, "");
  1544. }
  1545. if (run_or_build) {
  1546. print_usage_line(1, "-keep-temp-files");
  1547. print_usage_line(2, "Keeps the temporary files generated during compilation");
  1548. print_usage_line(0, "");
  1549. } else if (strip_semicolon) {
  1550. print_usage_line(1, "-keep-temp-files");
  1551. print_usage_line(2, "Keeps the temporary files generated during stripping the unneeded semicolons from files");
  1552. print_usage_line(0, "");
  1553. }
  1554. if (check) {
  1555. print_usage_line(1, "-collection:<name>=<filepath>");
  1556. print_usage_line(2, "Defines a library collection used for imports");
  1557. print_usage_line(2, "Example: -collection:shared=dir/to/shared");
  1558. print_usage_line(2, "Usage in Code:");
  1559. print_usage_line(3, "import \"shared:foo\"");
  1560. print_usage_line(0, "");
  1561. print_usage_line(1, "-define:<name>=<expression>");
  1562. print_usage_line(2, "Defines a global constant with a value");
  1563. print_usage_line(2, "Example: -define:SPAM=123");
  1564. print_usage_line(0, "");
  1565. }
  1566. if (build) {
  1567. print_usage_line(1, "-build-mode:<mode>");
  1568. print_usage_line(2, "Sets the build mode");
  1569. print_usage_line(2, "Available options:");
  1570. print_usage_line(3, "-build-mode:exe Build as an executable");
  1571. print_usage_line(3, "-build-mode:dll Build as a dynamically linked library");
  1572. print_usage_line(3, "-build-mode:shared Build as a dynamically linked library");
  1573. print_usage_line(3, "-build-mode:obj Build as an object file");
  1574. print_usage_line(3, "-build-mode:object Build as an object file");
  1575. print_usage_line(3, "-build-mode:assembly Build as an object file");
  1576. print_usage_line(3, "-build-mode:assembler Build as an assembly file");
  1577. print_usage_line(3, "-build-mode:asm Build as an assembly file");
  1578. print_usage_line(3, "-build-mode:llvm-ir Build as an LLVM IR file");
  1579. print_usage_line(3, "-build-mode:llvm Build as an LLVM IR file");
  1580. print_usage_line(0, "");
  1581. }
  1582. if (check) {
  1583. print_usage_line(1, "-target:<string>");
  1584. print_usage_line(2, "Sets the target for the executable to be built in");
  1585. print_usage_line(0, "");
  1586. }
  1587. if (run_or_build) {
  1588. print_usage_line(1, "-debug");
  1589. print_usage_line(2, "Enabled debug information, and defines the global constant ODIN_DEBUG to be 'true'");
  1590. print_usage_line(0, "");
  1591. print_usage_line(1, "-disable-assert");
  1592. print_usage_line(2, "Disable the code generation of the built-in run-time 'assert' procedure, and defines the global constant ODIN_DISABLE_ASSERT to be 'true'");
  1593. print_usage_line(0, "");
  1594. print_usage_line(1, "-no-bounds-check");
  1595. print_usage_line(2, "Disables bounds checking program wide");
  1596. print_usage_line(0, "");
  1597. print_usage_line(1, "-no-crt");
  1598. print_usage_line(2, "Disables automatic linking with the C Run Time");
  1599. print_usage_line(0, "");
  1600. print_usage_line(1, "-lld");
  1601. print_usage_line(2, "Use the LLD linker rather than the default");
  1602. print_usage_line(0, "");
  1603. print_usage_line(1, "-use-separate-modules");
  1604. print_usage_line(1, "[EXPERIMENTAL]");
  1605. print_usage_line(2, "The backend generates multiple build units which are then linked together");
  1606. print_usage_line(2, "Normally, a single build unit is generated for a standard project");
  1607. print_usage_line(0, "");
  1608. }
  1609. if (check) {
  1610. #if defined(GB_SYSTEM_WINDOWS)
  1611. print_usage_line(1, "-no-threaded-checker");
  1612. print_usage_line(2, "Disabled multithreading in the semantic checker stage");
  1613. print_usage_line(0, "");
  1614. #else
  1615. print_usage_line(1, "-threaded-checker");
  1616. print_usage_line(1, "[EXPERIMENTAL]");
  1617. print_usage_line(2, "Multithread the semantic checker stage");
  1618. print_usage_line(0, "");
  1619. #endif
  1620. print_usage_line(1, "-vet");
  1621. print_usage_line(2, "Do extra checks on the code");
  1622. print_usage_line(2, "Extra checks include:");
  1623. print_usage_line(3, "Variable shadowing within procedures");
  1624. print_usage_line(3, "Unused declarations");
  1625. print_usage_line(0, "");
  1626. print_usage_line(1, "-vet-extra");
  1627. print_usage_line(2, "Do even more checks than standard vet on the code");
  1628. print_usage_line(2, "To treat the extra warnings as errors, use -warnings-as-errors");
  1629. print_usage_line(0, "");
  1630. print_usage_line(1, "-ignore-unknown-attributes");
  1631. print_usage_line(2, "Ignores unknown attributes");
  1632. print_usage_line(2, "This can be used with metaprogramming tools");
  1633. print_usage_line(0, "");
  1634. if (command != "test") {
  1635. print_usage_line(1, "-no-entry-point");
  1636. print_usage_line(2, "Removes default requirement of an entry point (e.g. main procedure)");
  1637. print_usage_line(0, "");
  1638. }
  1639. }
  1640. if (test_only) {
  1641. print_usage_line(1, "-test-name:<string>");
  1642. print_usage_line(2, "Run specific test only by name");
  1643. print_usage_line(0, "");
  1644. }
  1645. if (run_or_build) {
  1646. print_usage_line(1, "-extra-linker-flags:<string>");
  1647. print_usage_line(2, "Adds extra linker specific flags in a string");
  1648. print_usage_line(0, "");
  1649. print_usage_line(1, "-microarch:<string>");
  1650. print_usage_line(2, "Specifies the specific micro-architecture for the build in a string");
  1651. print_usage_line(2, "Examples:");
  1652. print_usage_line(3, "-microarch:sandybridge");
  1653. print_usage_line(3, "-microarch:native");
  1654. print_usage_line(0, "");
  1655. }
  1656. if (check) {
  1657. print_usage_line(1, "-disallow-do");
  1658. print_usage_line(2, "Disallows the 'do' keyword in the project");
  1659. print_usage_line(0, "");
  1660. print_usage_line(1, "-default-to-nil-allocator");
  1661. print_usage_line(2, "Sets the default allocator to be the nil_allocator, an allocator which does nothing");
  1662. print_usage_line(0, "");
  1663. print_usage_line(1, "-strict-style");
  1664. print_usage_line(2, "Errs on unneeded tokens, such as unneeded semicolons");
  1665. print_usage_line(0, "");
  1666. print_usage_line(1, "-strict-style-init-only");
  1667. print_usage_line(2, "Errs on unneeded tokens, such as unneeded semicolons, only on the initial project");
  1668. print_usage_line(0, "");
  1669. print_usage_line(1, "-ignore-warnings");
  1670. print_usage_line(2, "Ignores warning messages");
  1671. print_usage_line(0, "");
  1672. print_usage_line(1, "-warnings-as-errors");
  1673. print_usage_line(2, "Treats warning messages as error messages");
  1674. print_usage_line(0, "");
  1675. print_usage_line(1, "-verbose-errors");
  1676. print_usage_line(2, "Prints verbose error messages showing the code on that line and the location in that line");
  1677. print_usage_line(0, "");
  1678. }
  1679. if (run_or_build) {
  1680. #if defined(GB_SYSTEM_WINDOWS)
  1681. print_usage_line(1, "-ignore-vs-search");
  1682. print_usage_line(2, "[Windows only]");
  1683. print_usage_line(2, "Ignores the Visual Studio search for library paths");
  1684. print_usage_line(0, "");
  1685. print_usage_line(1, "-resource:<filepath>");
  1686. print_usage_line(2, "[Windows only]");
  1687. print_usage_line(2, "Defines the resource file for the executable");
  1688. print_usage_line(2, "Example: -resource:path/to/file.rc");
  1689. print_usage_line(0, "");
  1690. print_usage_line(1, "-pdb-name:<filepath>");
  1691. print_usage_line(2, "[Windows only]");
  1692. print_usage_line(2, "Defines the generated PDB name when -debug is enabled");
  1693. print_usage_line(2, "Example: -pdb-name:different.pdb");
  1694. print_usage_line(0, "");
  1695. print_usage_line(1, "-subsystem:<option>");
  1696. print_usage_line(2, "[Windows only]");
  1697. print_usage_line(2, "Defines the subsystem for the application");
  1698. print_usage_line(2, "Available options:");
  1699. print_usage_line(3, "console");
  1700. print_usage_line(3, "windows");
  1701. print_usage_line(0, "");
  1702. #endif
  1703. }
  1704. }
  1705. void print_show_unused(Checker *c) {
  1706. CheckerInfo *info = &c->info;
  1707. auto unused = array_make<Entity *>(permanent_allocator(), 0, info->entities.count);
  1708. for_array(i, info->entities) {
  1709. Entity *e = info->entities[i];
  1710. if (e == nullptr) {
  1711. continue;
  1712. }
  1713. if (e->pkg == nullptr || e->pkg->scope == nullptr) {
  1714. continue;
  1715. }
  1716. if (e->pkg->scope->flags & ScopeFlag_Builtin) {
  1717. continue;
  1718. }
  1719. switch (e->kind) {
  1720. case Entity_Invalid:
  1721. case Entity_Builtin:
  1722. case Entity_Nil:
  1723. case Entity_Label:
  1724. continue;
  1725. case Entity_Constant:
  1726. case Entity_Variable:
  1727. case Entity_TypeName:
  1728. case Entity_Procedure:
  1729. case Entity_ProcGroup:
  1730. case Entity_ImportName:
  1731. case Entity_LibraryName:
  1732. // Fine
  1733. break;
  1734. }
  1735. if ((e->scope->flags & (ScopeFlag_Pkg|ScopeFlag_File)) == 0) {
  1736. continue;
  1737. }
  1738. if (e->token.string.len == 0) {
  1739. continue;
  1740. }
  1741. if (e->token.string == "_") {
  1742. continue;
  1743. }
  1744. if (ptr_set_exists(&info->minimum_dependency_set, e)) {
  1745. continue;
  1746. }
  1747. array_add(&unused, e);
  1748. }
  1749. gb_sort_array(unused.data, unused.count, cmp_entities_for_printing);
  1750. print_usage_line(0, "Unused Package Declarations");
  1751. AstPackage *curr_pkg = nullptr;
  1752. EntityKind curr_entity_kind = Entity_Invalid;
  1753. for_array(i, unused) {
  1754. Entity *e = unused[i];
  1755. if (curr_pkg != e->pkg) {
  1756. curr_pkg = e->pkg;
  1757. curr_entity_kind = Entity_Invalid;
  1758. print_usage_line(0, "");
  1759. print_usage_line(0, "package %.*s", LIT(curr_pkg->name));
  1760. }
  1761. if (curr_entity_kind != e->kind) {
  1762. curr_entity_kind = e->kind;
  1763. print_usage_line(1, "%s", print_entity_names[e->kind]);
  1764. }
  1765. if (build_context.show_unused_with_location) {
  1766. TokenPos pos = e->token.pos;
  1767. print_usage_line(2, "%s %.*s", token_pos_to_string(pos), LIT(e->token.string));
  1768. } else {
  1769. print_usage_line(2, "%.*s", LIT(e->token.string));
  1770. }
  1771. }
  1772. print_usage_line(0, "");
  1773. }
  1774. bool check_env(void) {
  1775. gbAllocator a = heap_allocator();
  1776. char const *odin_root = gb_get_env("ODIN_ROOT", a);
  1777. defer (gb_free(a, cast(void *)odin_root));
  1778. if (odin_root) {
  1779. if (!gb_file_exists(odin_root)) {
  1780. gb_printf_err("Invalid ODIN_ROOT, directory does not exist, got %s\n", odin_root);
  1781. return false;
  1782. }
  1783. String path = make_string_c(odin_root);
  1784. if (!path_is_directory(path)) {
  1785. gb_printf_err("Invalid ODIN_ROOT, expected a directory, got %s\n", odin_root);
  1786. return false;
  1787. }
  1788. }
  1789. return true;
  1790. }
  1791. struct StripSemicolonFile {
  1792. String old_fullpath;
  1793. String old_fullpath_backup;
  1794. String new_fullpath;
  1795. AstFile *file;
  1796. i64 written;
  1797. };
  1798. gbFileError write_file_with_stripped_tokens(gbFile *f, AstFile *file, i64 *written_) {
  1799. i64 written = 0;
  1800. gbFileError err = gbFileError_None;
  1801. u8 const *file_data = file->tokenizer.start;
  1802. i32 prev_offset = 0;
  1803. i32 const end_offset = cast(i32)(file->tokenizer.end - file->tokenizer.start);
  1804. for_array(i, file->tokens) {
  1805. Token *token = &file->tokens[i];
  1806. if (token->flags & (TokenFlag_Remove|TokenFlag_Replace)) {
  1807. i32 offset = token->pos.offset;
  1808. i32 to_write = offset-prev_offset;
  1809. if (!gb_file_write(f, file_data+prev_offset, to_write)) {
  1810. return gbFileError_Invalid;
  1811. }
  1812. written += to_write;
  1813. prev_offset = token_pos_end(*token).offset;
  1814. }
  1815. if (token->flags & TokenFlag_Replace) {
  1816. if (token->kind == Token_Ellipsis) {
  1817. if (!gb_file_write(f, "..=", 3)) {
  1818. return gbFileError_Invalid;
  1819. }
  1820. written += 3;
  1821. } else {
  1822. return gbFileError_Invalid;
  1823. }
  1824. }
  1825. }
  1826. if (end_offset > prev_offset) {
  1827. i32 to_write = end_offset-prev_offset;
  1828. if (!gb_file_write(f, file_data+prev_offset, end_offset-prev_offset)) {
  1829. return gbFileError_Invalid;
  1830. }
  1831. written += to_write;
  1832. }
  1833. if (written_) *written_ = written;
  1834. return err;
  1835. }
  1836. int strip_semicolons(Parser *parser) {
  1837. isize file_count = 0;
  1838. for_array(i, parser->packages) {
  1839. AstPackage *pkg = parser->packages[i];
  1840. file_count += pkg->files.count;
  1841. }
  1842. auto generated_files = array_make<StripSemicolonFile>(permanent_allocator(), 0, file_count);
  1843. for_array(i, parser->packages) {
  1844. AstPackage *pkg = parser->packages[i];
  1845. for_array(j, pkg->files) {
  1846. AstFile *file = pkg->files[j];
  1847. bool nothing_to_change = true;
  1848. for_array(i, file->tokens) {
  1849. Token *token = &file->tokens[i];
  1850. if (token->flags) {
  1851. nothing_to_change = false;
  1852. break;
  1853. }
  1854. }
  1855. if (nothing_to_change) {
  1856. continue;
  1857. }
  1858. String old_fullpath = copy_string(permanent_allocator(), file->fullpath);
  1859. // assumes .odin extension
  1860. String fullpath_base = substring(old_fullpath, 0, old_fullpath.len-5);
  1861. String old_fullpath_backup = concatenate_strings(permanent_allocator(), fullpath_base, str_lit("~backup.odin-temp"));
  1862. String new_fullpath = concatenate_strings(permanent_allocator(), fullpath_base, str_lit("~temp.odin-temp"));
  1863. array_add(&generated_files, StripSemicolonFile{old_fullpath, old_fullpath_backup, new_fullpath, file});
  1864. }
  1865. }
  1866. gb_printf_err("File count to be stripped of unneeded tokens: %td\n", generated_files.count);
  1867. isize generated_count = 0;
  1868. bool failed = false;
  1869. for_array(i, generated_files) {
  1870. auto *file = &generated_files[i];
  1871. char const *filename = cast(char const *)file->new_fullpath.text;
  1872. gbFileError err = gbFileError_None;
  1873. defer (if (err != gbFileError_None) {
  1874. failed = true;
  1875. });
  1876. gbFile f = {};
  1877. err = gb_file_create(&f, filename);
  1878. if (err) {
  1879. break;
  1880. }
  1881. defer (err = gb_file_close(&f));
  1882. generated_count += 1;
  1883. i64 written = 0;
  1884. defer (gb_file_truncate(&f, written));
  1885. debugf("Write file with stripped tokens: %s\n", filename);
  1886. err = write_file_with_stripped_tokens(&f, file->file, &written);
  1887. if (err) {
  1888. break;
  1889. }
  1890. file->written = written;
  1891. }
  1892. if (failed) {
  1893. for (isize i = 0; i < generated_count; i++) {
  1894. auto *file = &generated_files[i];
  1895. char const *filename = nullptr;
  1896. filename = cast(char const *)file->new_fullpath.text;
  1897. GB_ASSERT_MSG(gb_file_remove(filename), "unable to delete file %s", filename);
  1898. }
  1899. return 1;
  1900. }
  1901. isize overwritten_files = 0;
  1902. for_array(i, generated_files) {
  1903. auto *file = &generated_files[i];
  1904. char const *old_fullpath = cast(char const *)file->old_fullpath.text;
  1905. char const *old_fullpath_backup = cast(char const *)file->old_fullpath_backup.text;
  1906. char const *new_fullpath = cast(char const *)file->new_fullpath.text;
  1907. debugf("Copy '%s' to '%s'\n", old_fullpath, old_fullpath_backup);
  1908. if (!gb_file_copy(old_fullpath, old_fullpath_backup, false)) {
  1909. gb_printf_err("failed to copy '%s' to '%s'\n", old_fullpath, old_fullpath_backup);
  1910. failed = true;
  1911. break;
  1912. }
  1913. debugf("Copy '%s' to '%s'\n", new_fullpath, old_fullpath);
  1914. if (!gb_file_copy(new_fullpath, old_fullpath, false)) {
  1915. gb_printf_err("failed to copy '%s' to '%s'\n", old_fullpath, new_fullpath);
  1916. debugf("Copy '%s' to '%s'\n", old_fullpath_backup, old_fullpath);
  1917. if (!gb_file_copy(old_fullpath_backup, old_fullpath, false)) {
  1918. gb_printf_err("failed to restore '%s' from '%s'\n", old_fullpath, old_fullpath_backup);
  1919. }
  1920. failed = true;
  1921. break;
  1922. }
  1923. debugf("Remove '%s'\n", old_fullpath_backup);
  1924. if (!gb_file_remove(old_fullpath_backup)) {
  1925. gb_printf_err("failed to remove '%s'\n", old_fullpath_backup);
  1926. }
  1927. overwritten_files++;
  1928. }
  1929. if (!build_context.keep_temp_files) {
  1930. for_array(i, generated_files) {
  1931. auto *file = &generated_files[i];
  1932. char const *filename = nullptr;
  1933. filename = cast(char const *)file->new_fullpath.text;
  1934. debugf("Remove '%s'\n", filename);
  1935. GB_ASSERT_MSG(gb_file_remove(filename), "unable to delete file %s", filename);
  1936. filename = cast(char const *)file->old_fullpath_backup.text;
  1937. debugf("Remove '%s'\n", filename);
  1938. if (gb_file_exists(filename) && !gb_file_remove(filename)) {
  1939. if (i < overwritten_files) {
  1940. gb_printf_err("unable to delete file %s", filename);
  1941. failed = true;
  1942. }
  1943. }
  1944. }
  1945. }
  1946. gb_printf_err("Files stripped of unneeded token: %td\n", generated_files.count);
  1947. return cast(int)failed;
  1948. }
  1949. int main(int arg_count, char const **arg_ptr) {
  1950. #define TIME_SECTION(str) do { debugf("[Section] %s\n", str); timings_start_section(&global_timings, str_lit(str)); } while (0)
  1951. if (arg_count < 2) {
  1952. usage(make_string_c(arg_ptr[0]));
  1953. return 1;
  1954. }
  1955. timings_init(&global_timings, str_lit("Total Time"), 2048);
  1956. defer (timings_destroy(&global_timings));
  1957. TIME_SECTION("initialization");
  1958. virtual_memory_init();
  1959. mutex_init(&fullpath_mutex);
  1960. mutex_init(&hash_exact_value_mutex);
  1961. init_string_buffer_memory();
  1962. init_string_interner();
  1963. init_global_error_collector();
  1964. init_keyword_hash_table();
  1965. init_type_mutex();
  1966. if (!check_env()) {
  1967. return 1;
  1968. }
  1969. array_init(&library_collections, heap_allocator());
  1970. // NOTE(bill): 'core' cannot be (re)defined by the user
  1971. add_library_collection(str_lit("core"), get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("core")));
  1972. add_library_collection(str_lit("vendor"), get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("vendor")));
  1973. map_init(&build_context.defined_values, heap_allocator());
  1974. build_context.extra_packages.allocator = heap_allocator();
  1975. string_set_init(&build_context.test_names, heap_allocator());
  1976. Array<String> args = setup_args(arg_count, arg_ptr);
  1977. String command = args[1];
  1978. String init_filename = {};
  1979. String run_args_string = {};
  1980. bool run_output = false;
  1981. if (command == "run" || command == "test") {
  1982. if (args.count < 3) {
  1983. usage(args[0]);
  1984. return 1;
  1985. }
  1986. build_context.command_kind = Command_run;
  1987. if (command == "test") {
  1988. build_context.command_kind = Command_test;
  1989. }
  1990. Array<String> run_args = array_make<String>(heap_allocator(), 0, arg_count);
  1991. defer (array_free(&run_args));
  1992. isize last_non_run_arg = args.count;
  1993. for_array(i, args) {
  1994. if (args[i] == "--") {
  1995. last_non_run_arg = i;
  1996. }
  1997. if (i <= last_non_run_arg) {
  1998. continue;
  1999. }
  2000. array_add(&run_args, args[i]);
  2001. }
  2002. args = array_slice(args, 0, last_non_run_arg);
  2003. run_args_string = string_join_and_quote(heap_allocator(), run_args);
  2004. init_filename = args[2];
  2005. run_output = true;
  2006. } else if (command == "build") {
  2007. if (args.count < 3) {
  2008. usage(args[0]);
  2009. return 1;
  2010. }
  2011. build_context.command_kind = Command_build;
  2012. init_filename = args[2];
  2013. } else if (command == "check") {
  2014. if (args.count < 3) {
  2015. usage(args[0]);
  2016. return 1;
  2017. }
  2018. build_context.command_kind = Command_check;
  2019. build_context.no_output_files = true;
  2020. init_filename = args[2];
  2021. } else if (command == "strip-semicolon") {
  2022. if (args.count < 3) {
  2023. usage(args[0]);
  2024. return 1;
  2025. }
  2026. build_context.command_kind = Command_strip_semicolon;
  2027. build_context.no_output_files = true;
  2028. init_filename = args[2];
  2029. } else if (command == "query") {
  2030. if (args.count < 3) {
  2031. usage(args[0]);
  2032. return 1;
  2033. }
  2034. build_context.command_kind = Command_query;
  2035. build_context.no_output_files = true;
  2036. build_context.query_data_set_settings.ok = true;
  2037. init_filename = args[2];
  2038. } else if (command == "doc") {
  2039. if (args.count < 3) {
  2040. usage(args[0]);
  2041. return 1;
  2042. }
  2043. build_context.command_kind = Command_doc;
  2044. init_filename = args[2];
  2045. for (isize i = 3; i < args.count; i++) {
  2046. auto arg = args[i];
  2047. if (string_starts_with(arg, str_lit("-"))) {
  2048. break;
  2049. }
  2050. array_add(&build_context.extra_packages, arg);
  2051. }
  2052. isize extra_count = build_context.extra_packages.count;
  2053. if (extra_count > 0) {
  2054. gb_memmove(args.data + 3, args.data + 3 + extra_count, extra_count * gb_size_of(*args.data));
  2055. args.count -= extra_count;
  2056. }
  2057. build_context.no_output_files = true;
  2058. build_context.generate_docs = true;
  2059. build_context.no_entry_point = true; // ignore entry point
  2060. #if 0
  2061. print_usage_line(0, "Documentation generation is not yet supported");
  2062. return 1;
  2063. #endif
  2064. } else if (command == "version") {
  2065. build_context.command_kind = Command_version;
  2066. gb_printf("%.*s version %.*s", LIT(args[0]), LIT(ODIN_VERSION));
  2067. #ifdef NIGHTLY
  2068. gb_printf("-nightly");
  2069. #endif
  2070. #ifdef GIT_SHA
  2071. gb_printf(":%s", GIT_SHA);
  2072. #endif
  2073. gb_printf("\n");
  2074. return 0;
  2075. } else {
  2076. usage(args[0]);
  2077. return 1;
  2078. }
  2079. if (init_filename == "-help") {
  2080. build_context.show_help = true;
  2081. }
  2082. build_context.command = command;
  2083. if (!parse_build_flags(args)) {
  2084. return 1;
  2085. }
  2086. if (build_context.show_help) {
  2087. print_show_help(args[0], command);
  2088. return 0;
  2089. }
  2090. // NOTE(bill): add 'shared' directory if it is not already set
  2091. if (!find_library_collection_path(str_lit("shared"), nullptr)) {
  2092. add_library_collection(str_lit("shared"),
  2093. get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("shared")));
  2094. }
  2095. init_build_context(selected_target_metrics ? selected_target_metrics->metrics : nullptr);
  2096. // if (build_context.word_size == 4 && build_context.metrics.os != TargetOs_js) {
  2097. // print_usage_line(0, "%.*s 32-bit is not yet supported for this platform", LIT(args[0]));
  2098. // return 1;
  2099. // }
  2100. init_global_thread_pool();
  2101. defer (thread_pool_destroy(&global_thread_pool));
  2102. init_universal();
  2103. // TODO(bill): prevent compiling without a linker
  2104. Parser *parser = gb_alloc_item(permanent_allocator(), Parser);
  2105. Checker *checker = gb_alloc_item(permanent_allocator(), Checker);
  2106. TIME_SECTION("parse files");
  2107. if (!init_parser(parser)) {
  2108. return 1;
  2109. }
  2110. defer (destroy_parser(parser));
  2111. if (parse_packages(parser, init_filename) != ParseFile_None) {
  2112. return 1;
  2113. }
  2114. if (any_errors()) {
  2115. return 1;
  2116. }
  2117. TIME_SECTION("type check");
  2118. checker->parser = parser;
  2119. init_checker(checker);
  2120. defer (destroy_checker(checker));
  2121. check_parsed_files(checker);
  2122. if (any_errors()) {
  2123. return 1;
  2124. }
  2125. if (build_context.command_kind == Command_strip_semicolon) {
  2126. return strip_semicolons(parser);
  2127. }
  2128. if (build_context.generate_docs) {
  2129. if (global_error_collector.count != 0) {
  2130. return 1;
  2131. }
  2132. generate_documentation(checker);
  2133. return 0;
  2134. }
  2135. if (build_context.no_output_files) {
  2136. if (build_context.show_unused) {
  2137. print_show_unused(checker);
  2138. }
  2139. if (build_context.query_data_set_settings.ok) {
  2140. generate_and_print_query_data(checker, &global_timings);
  2141. } else {
  2142. if (build_context.show_timings) {
  2143. show_timings(checker, &global_timings);
  2144. }
  2145. }
  2146. if (global_error_collector.count != 0) {
  2147. return 1;
  2148. }
  2149. return 0;
  2150. }
  2151. TIME_SECTION("LLVM API Code Gen");
  2152. lbGenerator *gen = gb_alloc_item(permanent_allocator(), lbGenerator);
  2153. if (!lb_init_generator(gen, checker)) {
  2154. return 1;
  2155. }
  2156. lb_generate_code(gen);
  2157. switch (build_context.build_mode) {
  2158. case BuildMode_Executable:
  2159. case BuildMode_DynamicLibrary:
  2160. i32 result = linker_stage(gen);
  2161. if (result != 0) {
  2162. if (build_context.show_timings) {
  2163. show_timings(checker, &global_timings);
  2164. }
  2165. return 1;
  2166. }
  2167. break;
  2168. }
  2169. if (build_context.show_timings) {
  2170. show_timings(checker, &global_timings);
  2171. }
  2172. remove_temp_files(gen);
  2173. if (run_output) {
  2174. #if defined(GB_SYSTEM_WINDOWS)
  2175. return system_exec_command_line_app("odin run", "%.*s.exe %.*s", LIT(gen->output_base), LIT(run_args_string));
  2176. #else
  2177. //NOTE(thebirk): This whole thing is a little leaky
  2178. String output_ext = {};
  2179. String complete_path = concatenate_strings(permanent_allocator(), gen->output_base, output_ext);
  2180. complete_path = path_to_full_path(permanent_allocator(), complete_path);
  2181. return system_exec_command_line_app("odin run", "\"%.*s\" %.*s", LIT(complete_path), LIT(run_args_string));
  2182. #endif
  2183. }
  2184. return 0;
  2185. }