BootApp.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. #pragma warning(disable:4996)
  2. //#define BFBUILD_MAIN_THREAD_COMPILE
  3. #include "BootApp.h"
  4. #include <iostream>
  5. #include "BeefySysLib/util/String.h"
  6. #include "BeefySysLib/util/FileEnumerator.h"
  7. #include "BeefySysLib/util/WorkThread.h"
  8. #include "BeefySysLib/platform/PlatformHelper.h"
  9. #include "Compiler/BfSystem.h"
  10. #ifdef BF_PLATFORM_WINDOWS
  11. #include <direct.h>
  12. #endif
  13. BF_IMPORT void BF_CALLTYPE Targets_Create();
  14. BF_IMPORT void BF_CALLTYPE Targets_Delete();
  15. BF_IMPORT void BF_CALLTYPE BfSystem_ReportMemory(void* bfSystem);
  16. BF_EXPORT void BF_CALLTYPE BfCompiler_ProgramDone();
  17. BF_IMPORT void BF_CALLTYPE Debugger_FullReportMemory();
  18. //////////////////////////////////////////////////////////////////////////
  19. BF_IMPORT void BF_CALLTYPE BfCompiler_Delete(void* bfCompiler);
  20. BF_EXPORT void BF_CALLTYPE BfCompiler_SetOptions(void* bfCompiler, void* hotProject, int hotIdx,
  21. int machineType, int toolsetType, int simdSetting, int allocStackCount, int maxWorkerThreads,
  22. Beefy::BfCompilerOptionFlags optionFlags, const char* mallocLinkName, const char* freeLinkName);
  23. BF_IMPORT void BF_CALLTYPE BfCompiler_ClearBuildCache(void* bfCompiler);
  24. BF_IMPORT bool BF_CALLTYPE BfCompiler_Compile(void* bfCompiler, void* bfPassInstance, const char* outputPath);
  25. BF_IMPORT float BF_CALLTYPE BfCompiler_GetCompletionPercentage(void* bfCompiler);
  26. BF_IMPORT const char* BF_CALLTYPE BfCompiler_GetOutputFileNames(void* bfCompiler, void* bfProject, bool* hadOutputChanges);
  27. BF_IMPORT const char* BF_CALLTYPE BfCompiler_GetUsedOutputFileNames(void* bfCompiler, void* bfProject, bool flushQueuedHotFiles, bool* hadOutputChanges);
  28. BF_IMPORT void* BF_CALLTYPE BfSystem_CreateParser(void* bfSystem, void* bfProject);
  29. BF_IMPORT void BF_CALLTYPE BfParser_SetSource(void* bfParser, const char* data, int length, const char* fileName);
  30. BF_IMPORT void BF_CALLTYPE BfParser_SetCharIdData(void* bfParser, uint8* data, int length);
  31. BF_IMPORT bool BF_CALLTYPE BfParser_Parse(void* bfParser, void* bfPassInstance, bool compatMode);
  32. BF_IMPORT bool BF_CALLTYPE BfParser_Reduce(void* bfParser, void* bfPassInstance);
  33. BF_IMPORT bool BF_CALLTYPE BfParser_BuildDefs(void* bfParser, void* bfPassInstance, void* resolvePassData, bool fullRefresh);
  34. //////////////////////////////////////////////////////////////////////////
  35. BF_IMPORT void* BF_CALLTYPE BfSystem_Create();
  36. BF_IMPORT void BF_CALLTYPE BfSystem_ReportMemory(void* bfSystem);
  37. BF_IMPORT void BF_CALLTYPE BfSystem_Delete(void* bfSystem);
  38. BF_IMPORT void* BF_CALLTYPE BfSystem_CreatePassInstance(void* bfSystem);
  39. BF_IMPORT void* BF_CALLTYPE BfSystem_CreateCompiler(void* bfSystem, bool isResolveOnly);
  40. BF_IMPORT void* BF_CALLTYPE BfSystem_CreateProject(void* bfSystem, const char* projectName);
  41. BF_IMPORT void BF_CALLTYPE BfParser_Delete(void* bfParser);
  42. BF_IMPORT void BF_CALLTYPE BfSystem_AddTypeOptions(void* bfSystem, const char* filter, int32 simdSetting, int32 optimizationLevel, int32 emitDebugInfo, int32 arrayBoundsCheck,
  43. int32 initLocalVariables, int32 emitDynamicCastCheck, int32 emitObjectAccessCheck, int32 allocStackTraceDepth);
  44. //////////////////////////////////////////////////////////////////////////
  45. BF_IMPORT void BF_CALLTYPE BfProject_SetDisabled(void* bfProject, bool disabled);
  46. BF_IMPORT void BF_CALLTYPE BfProject_SetOptions(void* bfProject, int targetType, const char* startupObject, const char* preprocessorMacros,
  47. int optLevel, int ltoType, int32 flags);
  48. BF_IMPORT void BF_CALLTYPE BfProject_ClearDependencies(void* bfProject);
  49. BF_IMPORT void BF_CALLTYPE BfProject_AddDependency(void* bfProject, void* depProject);
  50. //////////////////////////////////////////////////////////////////////////
  51. BF_IMPORT const char* BF_CALLTYPE BfPassInstance_PopOutString(void* bfPassInstance);
  52. BF_IMPORT void BF_CALLTYPE BfPassInstance_Delete(void* bfPassInstance);
  53. //////////////////////////////////////////////////////////////////////////
  54. BF_IMPORT const char* BF_CALLTYPE VSSupport_Find();
  55. //////////////////////////////////////////////////////////////////////////
  56. USING_NS_BF;
  57. BootApp* Beefy::gApp = NULL;
  58. uint32 gConsoleFGColor = 0;
  59. uint32 gConsoleBGColor = 0;
  60. static bool GetConsoleColor(uint32& fgColor, uint32& bgColor)
  61. {
  62. #ifdef _WIN32
  63. static uint32 consoleColors[16] = { 0xff000000, 0xff000080, 0xff008000, 0xff008080, 0xff800000, 0xff800080, 0xff808000, 0xffc0c0c0,
  64. 0xff808080, 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000, 0xffff00ff, 0xffffff00, 0xffffffff };
  65. CONSOLE_SCREEN_BUFFER_INFO screenBuffInfo = { 0 };
  66. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screenBuffInfo);
  67. fgColor = consoleColors[screenBuffInfo.wAttributes & 0xF];
  68. bgColor = consoleColors[(screenBuffInfo.wAttributes >> 4) & 0xF];
  69. return true;
  70. #else
  71. fgColor = 0xFF808080;
  72. bgColor = 0xFF000000;
  73. return false;
  74. #endif
  75. }
  76. static WORD GetColorCode(uint32 color)
  77. {
  78. WORD code = 0;
  79. #ifdef _WIN32
  80. if (((color >> 0) & 0xFF) > 0x40)
  81. code |= FOREGROUND_BLUE;
  82. if (((color >> 8) & 0xFF) > 0x40)
  83. code |= FOREGROUND_GREEN;
  84. if (((color >> 16) & 0xFF) > 0x40)
  85. code |= FOREGROUND_RED;
  86. if ((((color >> 0) & 0xFF) > 0xC0) ||
  87. (((color >> 8) & 0xFF) > 0xC0) ||
  88. (((color >> 16) & 0xFF) > 0xC0))
  89. code |= FOREGROUND_INTENSITY;
  90. #endif
  91. return code;
  92. }
  93. static bool SetConsoleColor(uint32 fgColor, uint32 bgColor)
  94. {
  95. #ifdef _WIN32
  96. WORD attr = GetColorCode(fgColor) | (GetColorCode(bgColor) << 4);
  97. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr);
  98. SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), attr);
  99. return true;
  100. #else
  101. return false;
  102. #endif
  103. }
  104. BootApp::BootApp()
  105. {
  106. Targets_Create();
  107. mVerbosity = Verbosity_Normal;
  108. mTargetType = BfTargetType_BeefConsoleApplication;
  109. //char str[MAX_PATH];
  110. //GetModuleFileNameA(NULL, str, MAX_PATH);
  111. //mInstallDir = GetFileDir(str) + "/";
  112. //getcwd(str, MAX_PATH);
  113. //mStartupDir = str;
  114. //mStartupDir += "/";
  115. //mDoClean = false;
  116. mHadCmdLine = false;
  117. mShowedHelp = false;
  118. mHadErrors = false;
  119. mSystem = NULL;
  120. mCompiler = NULL;
  121. mProject = NULL;
  122. mCELibProject = NULL;
  123. mIsCERun = false;
  124. mAsmKind = BfAsmKind_None;
  125. mStartupObject = "Program";
  126. #ifdef BF_PLATFORM_WINDOWS
  127. mOptLevel = BfOptLevel_OgPlus;
  128. mToolset = BfToolsetType_Microsoft;
  129. #else
  130. mOptLevel = BfOptLevel_O0;
  131. mToolset = BfToolsetType_GNU;
  132. #endif
  133. mEmitIR = false;
  134. GetConsoleColor(gConsoleFGColor, gConsoleBGColor);
  135. }
  136. BootApp::~BootApp()
  137. {
  138. Targets_Delete();
  139. }
  140. void BootApp::OutputLine(const String& text, OutputPri outputPri)
  141. {
  142. if (mLogFile.IsOpen())
  143. {
  144. mLogFile.WriteSNZ(text);
  145. mLogFile.WriteSNZ("\n");
  146. }
  147. if (outputPri == OutputPri_Error)
  148. mHadErrors = true;
  149. switch (outputPri)
  150. {
  151. case OutputPri_Low:
  152. if (mVerbosity < Verbosity_Detailed)
  153. return;
  154. break;
  155. case OutputPri_Normal:
  156. if (mVerbosity < Verbosity_Normal)
  157. return;
  158. break;
  159. case OutputPri_High:
  160. case OutputPri_Warning:
  161. case OutputPri_Error:
  162. if (mVerbosity < Verbosity_Minimal)
  163. return;
  164. break;
  165. default: break;
  166. }
  167. if (outputPri == OutputPri_Warning)
  168. {
  169. SetConsoleColor(0xFFFFFF00, gConsoleBGColor);
  170. std::cerr << text.c_str() << std::endl;
  171. SetConsoleColor(gConsoleFGColor, gConsoleBGColor);
  172. }
  173. else if (outputPri == OutputPri_Error)
  174. {
  175. SetConsoleColor(0xFFFF0000, gConsoleBGColor);
  176. std::cerr << text.c_str() << std::endl;
  177. SetConsoleColor(gConsoleFGColor, gConsoleBGColor);
  178. }
  179. else
  180. std::cout << text.c_str() << std::endl;
  181. }
  182. void BootApp::Fail(const String& error)
  183. {
  184. if (mLogFile.IsOpen())
  185. mLogFile.WriteSNZ("FAIL: " + error + "\n");
  186. std::cerr << "FAIL: " << error.c_str() << std::endl;
  187. mHadErrors = true;
  188. }
  189. bool BootApp::HandleCmdLine(const String &cmd, const String& param)
  190. {
  191. mHadCmdLine = true;
  192. bool wantedParam = false;
  193. if ((!cmd.StartsWith("-")) && (mIsCERun) && (mCESrc.IsEmpty()))
  194. {
  195. mCESrc = cmd;
  196. return true;
  197. }
  198. if ((cmd == "-help") || (cmd == "-h") || (cmd == "/?"))
  199. {
  200. mShowedHelp = true;
  201. std::cout << "BeefBoot - Beef bootstrapping tool" << std::endl;
  202. return false;
  203. }
  204. else if (cmd == "-src")
  205. {
  206. mRequestedSrc.Add(param);
  207. wantedParam = true;
  208. }
  209. else if (cmd == "-verbosity")
  210. {
  211. if (param == "quiet")
  212. mVerbosity = Verbosity_Quiet;
  213. else if (param == "minimal")
  214. mVerbosity = Verbosity_Minimal;
  215. else if (param == "normal")
  216. mVerbosity = Verbosity_Normal;
  217. else if (param == "detailed")
  218. mVerbosity = Verbosity_Detailed;
  219. else if (param == "diagnostic")
  220. mVerbosity = Verbosity_Diagnostic;
  221. else
  222. {
  223. Fail(StrFormat("Invalid verbosity level: '%s'", param.c_str()));
  224. return false;
  225. }
  226. wantedParam = true;
  227. }
  228. else if (cmd == "-version")
  229. {
  230. BfpSystemResult sysResult;
  231. String exePath;
  232. BFP_GETSTR_HELPER(exePath, sysResult, BfpSystem_GetExecutablePath(__STR, __STRLEN, &sysResult));
  233. std::cout << "0.0.0" << std::endl;
  234. mShowedHelp = true;
  235. return true;
  236. }
  237. else if (cmd == "-define")
  238. {
  239. if (!mDefines.IsEmpty())
  240. mDefines += "\n";
  241. mDefines += param;
  242. wantedParam = true;
  243. }
  244. else if (cmd == "-startup")
  245. {
  246. mStartupObject = param;
  247. wantedParam = true;
  248. }
  249. else if (cmd == "-out")
  250. {
  251. mTargetPath = param;
  252. wantedParam = true;
  253. }
  254. else if (cmd == "-linkparams")
  255. {
  256. mLinkParams = param;
  257. wantedParam = true;
  258. }
  259. else if (cmd == "-Og+")
  260. {
  261. mOptLevel = BfOptLevel_OgPlus;
  262. }
  263. else if (cmd == "-O0")
  264. {
  265. mOptLevel = BfOptLevel_O0;
  266. }
  267. else if (cmd == "-O1")
  268. {
  269. mOptLevel = BfOptLevel_O1;
  270. }
  271. else if (cmd == "-O2")
  272. {
  273. mOptLevel = BfOptLevel_O2;
  274. }
  275. else if (cmd == "-O3")
  276. {
  277. mOptLevel = BfOptLevel_O3;
  278. }
  279. else if (cmd == "-gnu")
  280. {
  281. mToolset = BfToolsetType_GNU;
  282. }
  283. else if (cmd == "-emitir")
  284. {
  285. mEmitIR = true;
  286. }
  287. else if (cmd == "-cedest")
  288. {
  289. mIsCERun = true;
  290. mCEDest = param;
  291. wantedParam = true;
  292. }
  293. else if (cmd == "-cesrc")
  294. {
  295. mIsCERun = true;
  296. }
  297. else if (cmd == "-emitasm")
  298. {
  299. if (param.IsEmpty())
  300. {
  301. mAsmKind = BfAsmKind_Intel;
  302. }
  303. else
  304. {
  305. if (param == "att")
  306. mAsmKind = BfAsmKind_ATT;
  307. else
  308. mAsmKind = BfAsmKind_Intel;
  309. wantedParam = true;
  310. }
  311. }
  312. else
  313. {
  314. Fail("Unknown option: " + cmd);
  315. return false;
  316. }
  317. if ((wantedParam) && (param.empty()))
  318. {
  319. Fail(StrFormat("Parameter expected for '%s'", cmd.c_str()));
  320. return false;
  321. }
  322. else if ((!wantedParam) && (!param.empty()))
  323. {
  324. Fail(StrFormat("No parameter expected for '%s'", cmd.c_str()));
  325. return false;
  326. }
  327. return true;
  328. }
  329. bool BootApp::Init()
  330. {
  331. char* cwdPtr = getcwd(NULL, 0);
  332. mWorkingDir = cwdPtr;
  333. free(cwdPtr);
  334. if ((mTargetPath.IsEmpty()) && (mCESrc.IsEmpty()))
  335. {
  336. Fail("'Out' path not specified");
  337. }
  338. if (mRequestedSrc.IsEmpty())
  339. {
  340. Fail("No source specified");
  341. }
  342. return !mHadErrors;
  343. }
  344. void BootApp::QueueFile(const StringImpl& path, void* project)
  345. {
  346. String ext;
  347. ext = GetFileExtension(path);
  348. if ((ext.Equals(".bf", StringImpl::CompareKind_OrdinalIgnoreCase)) ||
  349. (ext.Equals(".cs", StringImpl::CompareKind_OrdinalIgnoreCase)))
  350. {
  351. int len;
  352. const char* data = LoadTextData(path, &len);
  353. if (data == NULL)
  354. {
  355. Fail(StrFormat("Unable to load file '%s'", path.c_str()));
  356. return;
  357. }
  358. bool worked = true;
  359. void* bfParser = BfSystem_CreateParser(mSystem, project);
  360. BfParser_SetSource(bfParser, data, len, path.c_str());
  361. //bfParser.SetCharIdData(charIdData);
  362. worked &= BfParser_Parse(bfParser, mPassInstance, false);
  363. worked &= BfParser_Reduce(bfParser, mPassInstance);
  364. worked &= BfParser_BuildDefs(bfParser, mPassInstance, NULL, false);
  365. delete data;
  366. }
  367. }
  368. void BootApp::QueuePath(const StringImpl& path)
  369. {
  370. if (DirectoryExists(path))
  371. {
  372. for (auto& fileEntry : FileEnumerator(path, FileEnumerator::Flags_Files))
  373. {
  374. String filePath = fileEntry.GetFilePath();
  375. String fileName;
  376. fileName = GetFileName(filePath);
  377. QueueFile(filePath, (mCELibProject != NULL) ? mCELibProject : mProject);
  378. }
  379. for (auto& fileEntry : FileEnumerator(path, FileEnumerator::Flags_Directories))
  380. {
  381. String childPath = fileEntry.GetFilePath();
  382. String dirName;
  383. dirName = GetFileName(childPath);
  384. if (dirName == "build")
  385. continue;
  386. QueuePath(childPath);
  387. }
  388. }
  389. else
  390. {
  391. QueueFile(path, mProject);
  392. }
  393. }
  394. static void CompileThread(void* param)
  395. {
  396. BfpThread_SetName(NULL, "CompileThread", NULL);
  397. BootApp* app = (BootApp*)param;
  398. BfCompiler_ClearBuildCache(app->mCompiler);
  399. if (!BfCompiler_Compile(app->mCompiler, app->mPassInstance, app->mBuildDir.c_str()))
  400. app->mHadErrors = true;
  401. }
  402. void BootApp::DoCompile()
  403. {
  404. #ifdef BFBUILD_MAIN_THREAD_COMPILE
  405. mOutputDirectory = outputDirectory;
  406. CompileThread(this);
  407. #else
  408. WorkThreadFunc workThread;
  409. workThread.Start(CompileThread, this);
  410. int lastProgressTicks = 0;
  411. bool showProgress = mVerbosity >= Verbosity_Normal;
  412. int progressSize = 30;
  413. if (showProgress)
  414. {
  415. std::cout << "[";
  416. for (int i = 0; i < progressSize; i++)
  417. std::cout << " ";
  418. std::cout << "]";
  419. for (int i = 0; i < progressSize + 1; i++)
  420. std::cout << "\b";
  421. std::cout.flush();
  422. }
  423. while (true)
  424. {
  425. bool isDone = workThread.WaitForFinish(100);
  426. float pct = BfCompiler_GetCompletionPercentage(mCompiler);
  427. if (isDone)
  428. pct = 1.0;
  429. int progressTicks = (int)(pct * progressSize + 0.5f);
  430. while (progressTicks > lastProgressTicks)
  431. {
  432. if (showProgress)
  433. {
  434. std::cout << "*";
  435. std::cout.flush();
  436. }
  437. lastProgressTicks++;
  438. }
  439. if (isDone)
  440. break;
  441. }
  442. if (showProgress)
  443. std::cout << std::endl;
  444. #endif
  445. }
  446. struct OutputContext
  447. {
  448. bool mIsError;
  449. BfpFile* mFile;
  450. };
  451. static void OutputThread(void* param)
  452. {
  453. OutputContext* context = (OutputContext*)param;
  454. String queuedStr;
  455. while (true)
  456. {
  457. char data[1024];
  458. BfpFileResult result;
  459. int bytesRead = (int)BfpFile_Read(context->mFile, data, 1023, -1, &result);
  460. if ((result != BfpFileResult_Ok) && (result != BfpFileResult_PartialData))
  461. return;
  462. data[bytesRead] = 0;
  463. if (context->mIsError)
  464. {
  465. std::cerr << data;
  466. std::cerr.flush();
  467. }
  468. else
  469. {
  470. std::cout << data;
  471. std::cout.flush();
  472. }
  473. if (gApp->mLogFile.IsOpen())
  474. {
  475. // This is to ensure that error and output lines are not merged together, though they may interleave
  476. queuedStr.Append(data, bytesRead);
  477. while (true)
  478. {
  479. int crPos = (int)queuedStr.IndexOf('\n');
  480. if (crPos == -1)
  481. break;
  482. AutoCrit autoCrit(gApp->mLogCritSect);
  483. if (context->mIsError)
  484. gApp->mLogFile.WriteSNZ("err> ");
  485. else
  486. gApp->mLogFile.WriteSNZ("out> ");
  487. int endPos = crPos;
  488. if ((endPos > 0) && (queuedStr[endPos - 1] == '\r'))
  489. endPos--;
  490. gApp->mLogFile.Write((void*)queuedStr.c_str(), endPos);
  491. gApp->mLogFile.WriteSNZ("\n");
  492. queuedStr.Remove(0, crPos + 1);
  493. }
  494. }
  495. }
  496. }
  497. bool BootApp::QueueRun(const String& fileName, const String& args, const String& workingDir, BfpSpawnFlags extraFlags)
  498. {
  499. OutputLine(StrFormat("EXECUTING: %s %s", fileName.c_str(), args.c_str()), OutputPri_Low);
  500. BfpSpawnFlags spawnFlags = (BfpSpawnFlags)(BfpSpawnFlag_NoWindow | BfpSpawnFlag_RedirectStdOutput | BfpSpawnFlag_RedirectStdError | extraFlags);
  501. BfpSpawn* spawn = BfpSpawn_Create(fileName.c_str(), args.c_str(), workingDir.c_str(), NULL, spawnFlags, NULL);
  502. if (spawn == NULL)
  503. {
  504. Fail(StrFormat("Failed to execute '%s'", fileName.c_str()));
  505. return false;
  506. }
  507. int exitCode = 0;
  508. OutputContext outputContext;;
  509. outputContext.mIsError = false;
  510. OutputContext errorContext;
  511. errorContext.mIsError = false;
  512. BfpSpawn_GetStdHandles(spawn, NULL, &outputContext.mFile, &errorContext.mFile);
  513. BfpThread* outputThread = BfpThread_Create(OutputThread, (void*)&outputContext);
  514. BfpThread* errorThread = BfpThread_Create(OutputThread, (void*)&errorContext);
  515. BfpSpawn_WaitFor(spawn, -1, &exitCode, NULL);
  516. if (outputContext.mFile != NULL)
  517. BfpFile_Close(outputContext.mFile, NULL);
  518. if (errorContext.mFile != NULL)
  519. BfpFile_Close(errorContext.mFile, NULL);
  520. BfpThread_WaitFor(outputThread, -1);
  521. BfpThread_WaitFor(errorThread, -1);
  522. BfpThread_Release(outputThread);
  523. BfpThread_Release(errorThread);
  524. BfpSpawn_Release(spawn);
  525. if (exitCode != 0)
  526. {
  527. Fail(StrFormat("Exit code returned: %d", exitCode));
  528. return false;
  529. }
  530. return true;
  531. }
  532. bool BootApp::CopyFile(const StringImpl& srcPath, const StringImpl& destPath)
  533. {
  534. BfpFileResult result = BfpFileResult_Ok;
  535. for (int i = 0; i < 20; i++)
  536. {
  537. BfpFile_Copy(srcPath.c_str(), destPath.c_str(), BfpFileCopyKind_Always, &result);
  538. if (result == BfpFileResult_Ok)
  539. return true;
  540. BfpThread_Sleep(100);
  541. }
  542. Fail(StrFormat("Failed to copy '%s' to '%s'", srcPath.c_str(), destPath.c_str()));
  543. return false;
  544. }
  545. #ifdef BF_PLATFORM_WINDOWS
  546. void BootApp::DoLinkMS()
  547. {
  548. String vsStr = VSSupport_Find();
  549. int toolIdx = (int)vsStr.IndexOf("TOOL64\t");
  550. int toolCrIdx = (int)vsStr.IndexOf('\n', toolIdx + 1);
  551. if ((toolIdx == -1) || (toolCrIdx == -1))
  552. {
  553. Fail("Failed to detect Visual Studio configuration. Is Visual Studio 2015 or later installed?");
  554. return;
  555. }
  556. String linkerPath = vsStr.Substring(toolIdx + 7, toolCrIdx - toolIdx - 7);
  557. linkerPath.Append("\\link.exe");
  558. String linkLine;
  559. String targetPath = mTargetPath;
  560. bool hadOutputChanges;
  561. const char* result = BfCompiler_GetUsedOutputFileNames(mCompiler, mProject, true, &hadOutputChanges);
  562. if (result == NULL)
  563. return;
  564. std::string fileNamesStr;
  565. fileNamesStr += result;
  566. if (fileNamesStr.length() == 0)
  567. return;
  568. int curIdx = -1;
  569. while (curIdx < (int)fileNamesStr.length())
  570. {
  571. int nextBr = (int)fileNamesStr.find('\n', curIdx + 1);
  572. if (nextBr == -1)
  573. nextBr = (int)fileNamesStr.length();
  574. linkLine.Append(fileNamesStr.substr(curIdx + 1, nextBr - curIdx - 1));
  575. linkLine.Append(" ");
  576. curIdx = nextBr;
  577. }
  578. linkLine.Append("-out:");
  579. IDEUtils::AppendWithOptionalQuotes(linkLine, targetPath);
  580. linkLine.Append(" ");
  581. if (mTargetType == BfTargetType_BeefConsoleApplication)
  582. linkLine.Append("-subsystem:console ");
  583. else
  584. linkLine.Append("-subsystem:windows ");
  585. linkLine.Append("-defaultlib:libcmtd ");
  586. linkLine.Append("-nologo ");
  587. linkLine.Append("-pdb:");
  588. int lastDotPos = (int)targetPath.LastIndexOf('.');
  589. if (lastDotPos == -1)
  590. lastDotPos = (int)targetPath.length();
  591. auto pdbName = String(targetPath, 0, lastDotPos);
  592. pdbName.Append(".pdb");
  593. IDEUtils::AppendWithOptionalQuotes(linkLine, pdbName);
  594. linkLine.Append(" ");
  595. linkLine.Append("-debug ");
  596. int checkIdx = 0;
  597. while (true)
  598. {
  599. int libIdx = (int)vsStr.IndexOf("LIB64\t", checkIdx);
  600. if (libIdx == -1)
  601. break;
  602. int libCrIdx = (int)vsStr.IndexOf('\n', libIdx + 1);
  603. if (libCrIdx == -1)
  604. break;
  605. String libPath = vsStr.Substring(libIdx + 6, libCrIdx - libIdx - 6);
  606. linkLine.Append("-libpath:\"");
  607. linkLine.Append(libPath);
  608. linkLine.Append("\" ");
  609. checkIdx = libCrIdx + 1;
  610. }
  611. linkLine.Append(mLinkParams);
  612. BfpSpawnFlags flags = BfpSpawnFlag_None;
  613. flags = (BfpSpawnFlags)(BfpSpawnFlag_UseArgsFile | BfpSpawnFlag_UseArgsFile_Native | BfpSpawnFlag_UseArgsFile_BOM);
  614. auto runCmd = QueueRun(linkerPath, linkLine, mWorkingDir, flags);
  615. }
  616. #endif
  617. void BootApp::DoLinkGNU()
  618. {
  619. String linkerPath = "/usr/bin/c++";
  620. String linkLine;
  621. String targetPath = mTargetPath;
  622. bool hadOutputChanges;
  623. const char* result = BfCompiler_GetUsedOutputFileNames(mCompiler, mProject, true, &hadOutputChanges);
  624. if (result == NULL)
  625. return;
  626. std::string fileNamesStr;
  627. fileNamesStr += result;
  628. if (fileNamesStr.length() == 0)
  629. return;
  630. int curIdx = -1;
  631. while (curIdx < (int)fileNamesStr.length())
  632. {
  633. int nextBr = (int)fileNamesStr.find('\n', curIdx + 1);
  634. if (nextBr == -1)
  635. nextBr = (int)fileNamesStr.length();
  636. linkLine.Append(fileNamesStr.substr(curIdx + 1, nextBr - curIdx - 1));
  637. linkLine.Append(" ");
  638. curIdx = nextBr;
  639. }
  640. linkLine.Append("-o ");
  641. IDEUtils::AppendWithOptionalQuotes(linkLine, targetPath);
  642. linkLine.Append(" ");
  643. linkLine.Append("-g ");
  644. linkLine.Append("-debug -no-pie ");
  645. linkLine.Append(mLinkParams);
  646. auto runCmd = QueueRun(linkerPath, linkLine, mWorkingDir, BfpSpawnFlag_UseArgsFile);
  647. }
  648. bool BootApp::Compile()
  649. {
  650. DWORD startTick = BFTickCount();
  651. mSystem = BfSystem_Create();
  652. mCompiler = BfSystem_CreateCompiler(mSystem, false);
  653. String projectName = GetFileName(mTargetPath);
  654. int dotPos = (int)projectName.IndexOf('.');
  655. if (dotPos != -1)
  656. projectName.RemoveToEnd(dotPos);
  657. if (projectName.IsEmpty())
  658. projectName.Append("BeefProject");
  659. mProject = BfSystem_CreateProject(mSystem, projectName.c_str());
  660. if (mIsCERun)
  661. {
  662. mCELibProject = BfSystem_CreateProject(mSystem, "BeefLib");
  663. BfProjectFlags flags = BfProjectFlags_None;
  664. BfProject_SetOptions(mCELibProject, BfTargetType_BeefLib, "", mDefines.c_str(), mOptLevel, 0, flags);
  665. }
  666. if (!mDefines.IsEmpty())
  667. mDefines.Append("\n");
  668. mDefines.Append("BF_64_BIT");
  669. mDefines.Append("\nBF_LITTLE_ENDIAN");
  670. mDefines.Append("\n");
  671. mDefines.Append(BF_PLATFORM_NAME);
  672. int ltoType = 0;
  673. BfProjectFlags flags = BfProjectFlags_None;
  674. if (mIsCERun)
  675. {
  676. flags = (BfProjectFlags)(flags | BfProjectFlags_SingleModule | BfProjectFlags_AlwaysIncludeAll);
  677. if (mAsmKind == BfAsmKind_ATT)
  678. flags = (BfProjectFlags)(flags | BfProjectFlags_AsmOutput | BfProjectFlags_AsmOutput_ATT);
  679. else if (mAsmKind == BfAsmKind_Intel)
  680. flags = (BfProjectFlags)(flags | BfProjectFlags_AsmOutput);
  681. }
  682. BfProject_SetOptions(mProject, mTargetType, mStartupObject.c_str(), mDefines.c_str(), mOptLevel, ltoType, flags);
  683. if (mCELibProject != NULL)
  684. BfProject_AddDependency(mProject, mCELibProject);
  685. mPassInstance = BfSystem_CreatePassInstance(mSystem);
  686. Beefy::String exePath;
  687. BfpGetStrHelper(exePath, [](char* outStr, int* inOutStrSize, BfpResult* result)
  688. {
  689. BfpSystem_GetExecutablePath(outStr, inOutStrSize, (BfpSystemResult*)result);
  690. });
  691. mBuildDir = GetFileDir(exePath) + "/build";
  692. RecursiveCreateDirectory(mBuildDir + "/" + projectName);
  693. if (mIsCERun)
  694. RecursiveCreateDirectory(mBuildDir + "/BeefLib");
  695. BfCompilerOptionFlags optionFlags = (BfCompilerOptionFlags)(BfCompilerOptionFlag_EmitDebugInfo | BfCompilerOptionFlag_EmitLineInfo | BfCompilerOptionFlag_GenerateOBJ | BfCompilerOptionFlag_OmitDebugHelpers);
  696. if (mEmitIR)
  697. optionFlags = (BfCompilerOptionFlags)(optionFlags | BfCompilerOptionFlag_WriteIR);
  698. int maxWorkerThreads = BfpSystem_GetNumLogicalCPUs(NULL);
  699. if (maxWorkerThreads <= 1)
  700. maxWorkerThreads = 6;
  701. BfCompiler_SetOptions(mCompiler, NULL, 0, BfMachineType_x64, mToolset, BfSIMDSetting_SSE2, 1, maxWorkerThreads, optionFlags, "malloc", "free");
  702. if (mIsCERun)
  703. {
  704. QueueFile(mCESrc, mProject);
  705. }
  706. for (auto& srcName : mRequestedSrc)
  707. {
  708. String absPath = GetAbsPath(srcName, mWorkingDir);
  709. QueuePath(absPath);
  710. }
  711. if (!mHadErrors)
  712. {
  713. DoCompile();
  714. OutputLine(StrFormat("TIMING: Beef compiling: %0.1fs", (BFTickCount() - startTick) / 1000.0), OutputPri_Normal);
  715. if (!mCEDest.IsEmpty())
  716. {
  717. String ext;
  718. String srcResult = mBuildDir + "/BeefProject/BeefProject";
  719. if (mAsmKind == BfAsmKind_None)
  720. srcResult += BF_OBJ_EXT;
  721. else
  722. srcResult += ".s";
  723. CopyFile(srcResult, mCEDest);
  724. }
  725. if ((mIsCERun) && (mEmitIR))
  726. {
  727. String ext;
  728. String srcResult = mBuildDir + "/BeefProject/BeefProject";
  729. String irDestPath = mCEDest;
  730. int dotPos = (int)irDestPath.LastIndexOf('.');
  731. if (dotPos != -1)
  732. irDestPath.RemoveToEnd(dotPos);
  733. if (mOptLevel == BfOptLevel_OgPlus)
  734. {
  735. srcResult += ".beir";
  736. irDestPath += ".ll";
  737. }
  738. else
  739. {
  740. srcResult += ".ll";
  741. irDestPath += ".ll";
  742. }
  743. CopyFile(srcResult, irDestPath);
  744. }
  745. }
  746. while (true)
  747. {
  748. const char* msg = BfPassInstance_PopOutString(mPassInstance);
  749. if (msg == NULL)
  750. break;
  751. if ((strncmp(msg, ":warn ", 6) == 0))
  752. {
  753. OutputLine(msg + 6, OutputPri_Warning);
  754. }
  755. else if ((strncmp(msg, ":error ", 7) == 0))
  756. {
  757. OutputLine(msg + 7, OutputPri_Error);
  758. }
  759. else if ((strncmp(msg, ":med ", 5) == 0))
  760. {
  761. OutputLine(msg + 5, OutputPri_Normal);
  762. }
  763. else if ((strncmp(msg, ":low ", 5) == 0))
  764. {
  765. OutputLine(msg + 5, OutputPri_Low);
  766. }
  767. else if ((strncmp(msg, "ERROR(", 6) == 0) || (strncmp(msg, "ERROR:", 6) == 0))
  768. {
  769. OutputLine(msg, OutputPri_Error);
  770. }
  771. else if ((strncmp(msg, "WARNING(", 8) == 0) || (strncmp(msg, "WARNING:", 8) == 0))
  772. {
  773. OutputLine(msg, OutputPri_Warning);
  774. }
  775. else
  776. OutputLine(msg);
  777. }
  778. if ((!mHadErrors) && (!mTargetPath.IsEmpty()))
  779. {
  780. if (mVerbosity == Verbosity_Normal)
  781. {
  782. std::cout << "Linking " << mTargetPath.c_str() << "...";
  783. std::cout.flush();
  784. }
  785. #ifdef BF_PLATFORM_WINDOWS
  786. DoLinkMS();
  787. #else
  788. DoLinkGNU();
  789. #endif
  790. if (mVerbosity == Verbosity_Normal)
  791. std::cout << std::endl;
  792. }
  793. BfPassInstance_Delete(mPassInstance);
  794. BfCompiler_Delete(mCompiler);
  795. BfSystem_Delete(mSystem);
  796. return !mHadErrors;
  797. }