BootApp.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. }
  166. if (outputPri == OutputPri_Warning)
  167. {
  168. SetConsoleColor(0xFFFFFF00, gConsoleBGColor);
  169. std::cerr << text.c_str() << std::endl;
  170. SetConsoleColor(gConsoleFGColor, gConsoleBGColor);
  171. }
  172. else if (outputPri == OutputPri_Error)
  173. {
  174. SetConsoleColor(0xFFFF0000, gConsoleBGColor);
  175. std::cerr << text.c_str() << std::endl;
  176. SetConsoleColor(gConsoleFGColor, gConsoleBGColor);
  177. }
  178. else
  179. std::cout << text.c_str() << std::endl;
  180. }
  181. void BootApp::Fail(const String& error)
  182. {
  183. if (mLogFile.IsOpen())
  184. mLogFile.WriteSNZ("FAIL: " + error + "\n");
  185. std::cerr << "FAIL: " << error.c_str() << std::endl;
  186. mHadErrors = true;
  187. }
  188. bool BootApp::HandleCmdLine(const String &cmd, const String& param)
  189. {
  190. mHadCmdLine = true;
  191. bool wantedParam = false;
  192. if ((!cmd.StartsWith("-")) && (mIsCERun) && (mCESrc.IsEmpty()))
  193. {
  194. mCESrc = cmd;
  195. return true;
  196. }
  197. if ((cmd == "-help") || (cmd == "-h") || (cmd == "/?"))
  198. {
  199. mShowedHelp = true;
  200. std::cout << "BeefBoot - Beef bootstrapping tool" << std::endl;
  201. return false;
  202. }
  203. else if (cmd == "-src")
  204. {
  205. mRequestedSrc.Add(param);
  206. wantedParam = true;
  207. }
  208. else if (cmd == "-verbosity")
  209. {
  210. if (param == "quiet")
  211. mVerbosity = Verbosity_Quiet;
  212. else if (param == "minimal")
  213. mVerbosity = Verbosity_Minimal;
  214. else if (param == "normal")
  215. mVerbosity = Verbosity_Normal;
  216. else if (param == "detailed")
  217. mVerbosity = Verbosity_Detailed;
  218. else if (param == "diagnostic")
  219. mVerbosity = Verbosity_Diagnostic;
  220. else
  221. {
  222. Fail(StrFormat("Invalid verbosity level: '%s'", param.c_str()));
  223. return false;
  224. }
  225. wantedParam = true;
  226. }
  227. else if (cmd == "-version")
  228. {
  229. BfpSystemResult sysResult;
  230. String exePath;
  231. BFP_GETSTR_HELPER(exePath, sysResult, BfpSystem_GetExecutablePath(__STR, __STRLEN, &sysResult));
  232. std::cout << "0.0.0" << std::endl;
  233. mShowedHelp = true;
  234. return true;
  235. }
  236. else if (cmd == "-define")
  237. {
  238. if (!mDefines.IsEmpty())
  239. mDefines += "\n";
  240. mDefines += param;
  241. wantedParam = true;
  242. }
  243. else if (cmd == "-startup")
  244. {
  245. mStartupObject = param;
  246. wantedParam = true;
  247. }
  248. else if (cmd == "-out")
  249. {
  250. mTargetPath = param;
  251. wantedParam = true;
  252. }
  253. else if (cmd == "-linkparams")
  254. {
  255. mLinkParams = param;
  256. wantedParam = true;
  257. }
  258. else if (cmd == "-Og+")
  259. {
  260. mOptLevel = BfOptLevel_OgPlus;
  261. }
  262. else if (cmd == "-O0")
  263. {
  264. mOptLevel = BfOptLevel_O0;
  265. }
  266. else if (cmd == "-O1")
  267. {
  268. mOptLevel = BfOptLevel_O1;
  269. }
  270. else if (cmd == "-O2")
  271. {
  272. mOptLevel = BfOptLevel_O2;
  273. }
  274. else if (cmd == "-O3")
  275. {
  276. mOptLevel = BfOptLevel_O3;
  277. }
  278. else if (cmd == "-gnu")
  279. {
  280. mToolset = BfToolsetType_GNU;
  281. }
  282. else if (cmd == "-emitir")
  283. {
  284. mEmitIR = true;
  285. }
  286. else if (cmd == "-cedest")
  287. {
  288. mIsCERun = true;
  289. mCEDest = param;
  290. wantedParam = true;
  291. }
  292. else if (cmd == "-cesrc")
  293. {
  294. mIsCERun = true;
  295. }
  296. else if (cmd == "-emitasm")
  297. {
  298. if (param.IsEmpty())
  299. {
  300. mAsmKind = BfAsmKind_Intel;
  301. }
  302. else
  303. {
  304. if (param == "att")
  305. mAsmKind = BfAsmKind_ATT;
  306. else
  307. mAsmKind = BfAsmKind_Intel;
  308. wantedParam = true;
  309. }
  310. }
  311. else
  312. {
  313. Fail("Unknown option: " + cmd);
  314. return false;
  315. }
  316. if ((wantedParam) && (param.empty()))
  317. {
  318. Fail(StrFormat("Parameter expected for '%s'", cmd.c_str()));
  319. return false;
  320. }
  321. else if ((!wantedParam) && (!param.empty()))
  322. {
  323. Fail(StrFormat("No parameter expected for '%s'", cmd.c_str()));
  324. return false;
  325. }
  326. return true;
  327. }
  328. bool BootApp::Init()
  329. {
  330. char* cwdPtr = getcwd(NULL, 0);
  331. mWorkingDir = cwdPtr;
  332. free(cwdPtr);
  333. if ((mTargetPath.IsEmpty()) && (mCESrc.IsEmpty()))
  334. {
  335. Fail("'Out' path not specified");
  336. }
  337. if (mRequestedSrc.IsEmpty())
  338. {
  339. Fail("No source specified");
  340. }
  341. return !mHadErrors;
  342. }
  343. void BootApp::QueueFile(const StringImpl& path, void* project)
  344. {
  345. String ext;
  346. ext = GetFileExtension(path);
  347. if ((ext.Equals(".bf", StringImpl::CompareKind_OrdinalIgnoreCase)) ||
  348. (ext.Equals(".cs", StringImpl::CompareKind_OrdinalIgnoreCase)))
  349. {
  350. int len;
  351. const char* data = LoadTextData(path, &len);
  352. if (data == NULL)
  353. {
  354. Fail(StrFormat("Unable to load file '%s'", path.c_str()));
  355. return;
  356. }
  357. bool worked = true;
  358. void* bfParser = BfSystem_CreateParser(mSystem, project);
  359. BfParser_SetSource(bfParser, data, len, path.c_str());
  360. //bfParser.SetCharIdData(charIdData);
  361. worked &= BfParser_Parse(bfParser, mPassInstance, false);
  362. worked &= BfParser_Reduce(bfParser, mPassInstance);
  363. worked &= BfParser_BuildDefs(bfParser, mPassInstance, NULL, false);
  364. delete data;
  365. }
  366. }
  367. void BootApp::QueuePath(const StringImpl& path)
  368. {
  369. if (DirectoryExists(path))
  370. {
  371. for (auto& fileEntry : FileEnumerator(path, FileEnumerator::Flags_Files))
  372. {
  373. String filePath = fileEntry.GetFilePath();
  374. String fileName;
  375. fileName = GetFileName(filePath);
  376. QueueFile(filePath, (mCELibProject != NULL) ? mCELibProject : mProject);
  377. }
  378. for (auto& fileEntry : FileEnumerator(path, FileEnumerator::Flags_Directories))
  379. {
  380. String childPath = fileEntry.GetFilePath();
  381. String dirName;
  382. dirName = GetFileName(childPath);
  383. if (dirName == "build")
  384. continue;
  385. QueuePath(childPath);
  386. }
  387. }
  388. else
  389. {
  390. QueueFile(path, mProject);
  391. }
  392. }
  393. static void CompileThread(void* param)
  394. {
  395. BfpThread_SetName(NULL, "CompileThread", NULL);
  396. BootApp* app = (BootApp*)param;
  397. BfCompiler_ClearBuildCache(app->mCompiler);
  398. if (!BfCompiler_Compile(app->mCompiler, app->mPassInstance, app->mBuildDir.c_str()))
  399. app->mHadErrors = true;
  400. }
  401. void BootApp::DoCompile()
  402. {
  403. #ifdef BFBUILD_MAIN_THREAD_COMPILE
  404. mOutputDirectory = outputDirectory;
  405. CompileThread(this);
  406. #else
  407. WorkThreadFunc workThread;
  408. workThread.Start(CompileThread, this);
  409. int lastProgressTicks = 0;
  410. bool showProgress = mVerbosity >= Verbosity_Normal;
  411. int progressSize = 30;
  412. if (showProgress)
  413. {
  414. std::cout << "[";
  415. for (int i = 0; i < progressSize; i++)
  416. std::cout << " ";
  417. std::cout << "]";
  418. for (int i = 0; i < progressSize + 1; i++)
  419. std::cout << "\b";
  420. std::cout.flush();
  421. }
  422. while (true)
  423. {
  424. bool isDone = workThread.WaitForFinish(100);
  425. float pct = BfCompiler_GetCompletionPercentage(mCompiler);
  426. if (isDone)
  427. pct = 1.0;
  428. int progressTicks = (int)(pct * progressSize + 0.5f);
  429. while (progressTicks > lastProgressTicks)
  430. {
  431. if (showProgress)
  432. {
  433. std::cout << "*";
  434. std::cout.flush();
  435. }
  436. lastProgressTicks++;
  437. }
  438. if (isDone)
  439. break;
  440. }
  441. if (showProgress)
  442. std::cout << std::endl;
  443. #endif
  444. }
  445. struct OutputContext
  446. {
  447. bool mIsError;
  448. BfpFile* mFile;
  449. };
  450. static void OutputThread(void* param)
  451. {
  452. OutputContext* context = (OutputContext*)param;
  453. String queuedStr;
  454. while (true)
  455. {
  456. char data[1024];
  457. BfpFileResult result;
  458. int bytesRead = (int)BfpFile_Read(context->mFile, data, 1023, -1, &result);
  459. if ((result != BfpFileResult_Ok) && (result != BfpFileResult_PartialData))
  460. return;
  461. data[bytesRead] = 0;
  462. if (context->mIsError)
  463. {
  464. std::cerr << data;
  465. std::cerr.flush();
  466. }
  467. else
  468. {
  469. std::cout << data;
  470. std::cout.flush();
  471. }
  472. if (gApp->mLogFile.IsOpen())
  473. {
  474. // This is to ensure that error and output lines are not merged together, though they may interleave
  475. queuedStr.Append(data, bytesRead);
  476. while (true)
  477. {
  478. int crPos = (int)queuedStr.IndexOf('\n');
  479. if (crPos == -1)
  480. break;
  481. AutoCrit autoCrit(gApp->mLogCritSect);
  482. if (context->mIsError)
  483. gApp->mLogFile.WriteSNZ("err> ");
  484. else
  485. gApp->mLogFile.WriteSNZ("out> ");
  486. int endPos = crPos;
  487. if ((endPos > 0) && (queuedStr[endPos - 1] == '\r'))
  488. endPos--;
  489. gApp->mLogFile.Write((void*)queuedStr.c_str(), endPos);
  490. gApp->mLogFile.WriteSNZ("\n");
  491. queuedStr.Remove(0, crPos + 1);
  492. }
  493. }
  494. }
  495. }
  496. bool BootApp::QueueRun(const String& fileName, const String& args, const String& workingDir, BfpSpawnFlags extraFlags)
  497. {
  498. OutputLine(StrFormat("EXECUTING: %s %s", fileName.c_str(), args.c_str()), OutputPri_Low);
  499. BfpSpawnFlags spawnFlags = (BfpSpawnFlags)(BfpSpawnFlag_NoWindow | BfpSpawnFlag_RedirectStdOutput | BfpSpawnFlag_RedirectStdError | extraFlags);
  500. BfpSpawn* spawn = BfpSpawn_Create(fileName.c_str(), args.c_str(), workingDir.c_str(), NULL, spawnFlags, NULL);
  501. if (spawn == NULL)
  502. {
  503. Fail(StrFormat("Failed to execute '%s'", fileName.c_str()));
  504. return false;
  505. }
  506. int exitCode = 0;
  507. OutputContext outputContext;;
  508. outputContext.mIsError = false;
  509. OutputContext errorContext;
  510. errorContext.mIsError = false;
  511. BfpSpawn_GetStdHandles(spawn, NULL, &outputContext.mFile, &errorContext.mFile);
  512. BfpThread* outputThread = BfpThread_Create(OutputThread, (void*)&outputContext);
  513. BfpThread* errorThread = BfpThread_Create(OutputThread, (void*)&errorContext);
  514. BfpSpawn_WaitFor(spawn, -1, &exitCode, NULL);
  515. if (outputContext.mFile != NULL)
  516. BfpFile_Close(outputContext.mFile, NULL);
  517. if (errorContext.mFile != NULL)
  518. BfpFile_Close(errorContext.mFile, NULL);
  519. BfpThread_WaitFor(outputThread, -1);
  520. BfpThread_WaitFor(errorThread, -1);
  521. BfpThread_Release(outputThread);
  522. BfpThread_Release(errorThread);
  523. BfpSpawn_Release(spawn);
  524. if (exitCode != 0)
  525. {
  526. Fail(StrFormat("Exit code returned: %d", exitCode));
  527. return false;
  528. }
  529. return true;
  530. }
  531. bool BootApp::CopyFile(const StringImpl& srcPath, const StringImpl& destPath)
  532. {
  533. BfpFileResult result = BfpFileResult_Ok;
  534. for (int i = 0; i < 20; i++)
  535. {
  536. BfpFile_Copy(srcPath.c_str(), destPath.c_str(), BfpFileCopyKind_Always, &result);
  537. if (result == BfpFileResult_Ok)
  538. return true;
  539. BfpThread_Sleep(100);
  540. }
  541. Fail(StrFormat("Failed to copy '%s' to '%s'", srcPath.c_str(), destPath.c_str()));
  542. return false;
  543. }
  544. #ifdef BF_PLATFORM_WINDOWS
  545. void BootApp::DoLinkMS()
  546. {
  547. String vsStr = VSSupport_Find();
  548. int toolIdx = (int)vsStr.IndexOf("TOOL64\t");
  549. int toolCrIdx = (int)vsStr.IndexOf('\n', toolIdx + 1);
  550. if ((toolIdx == -1) || (toolCrIdx == -1))
  551. {
  552. Fail("Failed to detect Visual Studio configuration. Is Visual Studio 2015 or later installed?");
  553. return;
  554. }
  555. String linkerPath = vsStr.Substring(toolIdx + 7, toolCrIdx - toolIdx - 7);
  556. linkerPath.Append("\\link.exe");
  557. String linkLine;
  558. String targetPath = mTargetPath;
  559. bool hadOutputChanges;
  560. const char* result = BfCompiler_GetUsedOutputFileNames(mCompiler, mProject, true, &hadOutputChanges);
  561. if (result == NULL)
  562. return;
  563. std::string fileNamesStr;
  564. fileNamesStr += result;
  565. if (fileNamesStr.length() == 0)
  566. return;
  567. int curIdx = -1;
  568. while (curIdx < (int)fileNamesStr.length())
  569. {
  570. int nextBr = (int)fileNamesStr.find('\n', curIdx + 1);
  571. if (nextBr == -1)
  572. nextBr = (int)fileNamesStr.length();
  573. linkLine.Append(fileNamesStr.substr(curIdx + 1, nextBr - curIdx - 1));
  574. linkLine.Append(" ");
  575. curIdx = nextBr;
  576. }
  577. linkLine.Append("-out:");
  578. IDEUtils::AppendWithOptionalQuotes(linkLine, targetPath);
  579. linkLine.Append(" ");
  580. if (mTargetType == BfTargetType_BeefConsoleApplication)
  581. linkLine.Append("-subsystem:console ");
  582. else
  583. linkLine.Append("-subsystem:windows ");
  584. linkLine.Append("-defaultlib:libcmtd ");
  585. linkLine.Append("-nologo ");
  586. linkLine.Append("-pdb:");
  587. int lastDotPos = (int)targetPath.LastIndexOf('.');
  588. if (lastDotPos == -1)
  589. lastDotPos = (int)targetPath.length();
  590. auto pdbName = String(targetPath, 0, lastDotPos);
  591. pdbName.Append(".pdb");
  592. IDEUtils::AppendWithOptionalQuotes(linkLine, pdbName);
  593. linkLine.Append(" ");
  594. linkLine.Append("-debug ");
  595. int checkIdx = 0;
  596. while (true)
  597. {
  598. int libIdx = (int)vsStr.IndexOf("LIB64\t", checkIdx);
  599. if (libIdx == -1)
  600. break;
  601. int libCrIdx = (int)vsStr.IndexOf('\n', libIdx + 1);
  602. if (libCrIdx == -1)
  603. break;
  604. String libPath = vsStr.Substring(libIdx + 6, libCrIdx - libIdx - 6);
  605. linkLine.Append("-libpath:\"");
  606. linkLine.Append(libPath);
  607. linkLine.Append("\" ");
  608. checkIdx = libCrIdx + 1;
  609. }
  610. linkLine.Append(mLinkParams);
  611. BfpSpawnFlags flags = BfpSpawnFlag_None;
  612. flags = (BfpSpawnFlags)(BfpSpawnFlag_UseArgsFile | BfpSpawnFlag_UseArgsFile_Native | BfpSpawnFlag_UseArgsFile_BOM);
  613. auto runCmd = QueueRun(linkerPath, linkLine, mWorkingDir, flags);
  614. }
  615. #endif
  616. void BootApp::DoLinkGNU()
  617. {
  618. String linkerPath = "/usr/bin/c++";
  619. String linkLine;
  620. String targetPath = mTargetPath;
  621. bool hadOutputChanges;
  622. const char* result = BfCompiler_GetUsedOutputFileNames(mCompiler, mProject, true, &hadOutputChanges);
  623. if (result == NULL)
  624. return;
  625. std::string fileNamesStr;
  626. fileNamesStr += result;
  627. if (fileNamesStr.length() == 0)
  628. return;
  629. int curIdx = -1;
  630. while (curIdx < (int)fileNamesStr.length())
  631. {
  632. int nextBr = (int)fileNamesStr.find('\n', curIdx + 1);
  633. if (nextBr == -1)
  634. nextBr = (int)fileNamesStr.length();
  635. linkLine.Append(fileNamesStr.substr(curIdx + 1, nextBr - curIdx - 1));
  636. linkLine.Append(" ");
  637. curIdx = nextBr;
  638. }
  639. linkLine.Append("-o ");
  640. IDEUtils::AppendWithOptionalQuotes(linkLine, targetPath);
  641. linkLine.Append(" ");
  642. linkLine.Append("-g ");
  643. linkLine.Append("-debug -no-pie ");
  644. linkLine.Append(mLinkParams);
  645. auto runCmd = QueueRun(linkerPath, linkLine, mWorkingDir, BfpSpawnFlag_UseArgsFile);
  646. }
  647. bool BootApp::Compile()
  648. {
  649. DWORD startTick = BFTickCount();
  650. mSystem = BfSystem_Create();
  651. mCompiler = BfSystem_CreateCompiler(mSystem, false);
  652. String projectName = GetFileName(mTargetPath);
  653. int dotPos = (int)projectName.IndexOf('.');
  654. if (dotPos != -1)
  655. projectName.RemoveToEnd(dotPos);
  656. if (projectName.IsEmpty())
  657. projectName.Append("BeefProject");
  658. mProject = BfSystem_CreateProject(mSystem, projectName.c_str());
  659. if (mIsCERun)
  660. {
  661. mCELibProject = BfSystem_CreateProject(mSystem, "BeefLib");
  662. BfProjectFlags flags = BfProjectFlags_None;
  663. BfProject_SetOptions(mCELibProject, BfTargetType_BeefLib, "", mDefines.c_str(), mOptLevel, 0, flags);
  664. }
  665. if (!mDefines.IsEmpty())
  666. mDefines.Append("\n");
  667. mDefines.Append("BF_64_BIT");
  668. mDefines.Append("\nBF_LITTLE_ENDIAN");
  669. mDefines.Append("\n");
  670. mDefines.Append(BF_PLATFORM_NAME);
  671. int ltoType = 0;
  672. BfProjectFlags flags = BfProjectFlags_None;
  673. if (mIsCERun)
  674. {
  675. flags = (BfProjectFlags)(flags | BfProjectFlags_SingleModule | BfProjectFlags_AlwaysIncludeAll);
  676. if (mAsmKind == BfAsmKind_ATT)
  677. flags = (BfProjectFlags)(flags | BfProjectFlags_AsmOutput | BfProjectFlags_AsmOutput_ATT);
  678. else if (mAsmKind == BfAsmKind_Intel)
  679. flags = (BfProjectFlags)(flags | BfProjectFlags_AsmOutput);
  680. }
  681. BfProject_SetOptions(mProject, mTargetType, mStartupObject.c_str(), mDefines.c_str(), mOptLevel, ltoType, flags);
  682. if (mCELibProject != NULL)
  683. BfProject_AddDependency(mProject, mCELibProject);
  684. mPassInstance = BfSystem_CreatePassInstance(mSystem);
  685. Beefy::String exePath;
  686. BfpGetStrHelper(exePath, [](char* outStr, int* inOutStrSize, BfpResult* result)
  687. {
  688. BfpSystem_GetExecutablePath(outStr, inOutStrSize, (BfpSystemResult*)result);
  689. });
  690. mBuildDir = GetFileDir(exePath) + "/build";
  691. RecursiveCreateDirectory(mBuildDir + "/" + projectName);
  692. if (mIsCERun)
  693. RecursiveCreateDirectory(mBuildDir + "/BeefLib");
  694. BfCompilerOptionFlags optionFlags = (BfCompilerOptionFlags)(BfCompilerOptionFlag_EmitDebugInfo | BfCompilerOptionFlag_EmitLineInfo | BfCompilerOptionFlag_GenerateOBJ | BfCompilerOptionFlag_OmitDebugHelpers);
  695. if (mEmitIR)
  696. optionFlags = (BfCompilerOptionFlags)(optionFlags | BfCompilerOptionFlag_WriteIR);
  697. int maxWorkerThreads = BfpSystem_GetNumLogicalCPUs(NULL);
  698. if (maxWorkerThreads <= 1)
  699. maxWorkerThreads = 6;
  700. BfCompiler_SetOptions(mCompiler, NULL, 0, BfMachineType_x64, mToolset, BfSIMDSetting_SSE2, 1, maxWorkerThreads, optionFlags, "malloc", "free");
  701. if (mIsCERun)
  702. {
  703. QueueFile(mCESrc, mProject);
  704. }
  705. for (auto& srcName : mRequestedSrc)
  706. {
  707. String absPath = GetAbsPath(srcName, mWorkingDir);
  708. QueuePath(absPath);
  709. }
  710. if (!mHadErrors)
  711. {
  712. DoCompile();
  713. OutputLine(StrFormat("TIMING: Beef compiling: %0.1fs", (BFTickCount() - startTick) / 1000.0), OutputPri_Normal);
  714. if (!mCEDest.IsEmpty())
  715. {
  716. String ext;
  717. String srcResult = mBuildDir + "/BeefProject/BeefProject";
  718. if (mAsmKind == BfAsmKind_None)
  719. srcResult += BF_OBJ_EXT;
  720. else
  721. srcResult += ".s";
  722. CopyFile(srcResult, mCEDest);
  723. }
  724. if ((mIsCERun) && (mEmitIR))
  725. {
  726. String ext;
  727. String srcResult = mBuildDir + "/BeefProject/BeefProject";
  728. String irDestPath = mCEDest;
  729. int dotPos = (int)irDestPath.LastIndexOf('.');
  730. if (dotPos != -1)
  731. irDestPath.RemoveToEnd(dotPos);
  732. if (mOptLevel == BfOptLevel_OgPlus)
  733. {
  734. srcResult += ".beir";
  735. irDestPath += ".ll";
  736. }
  737. else
  738. {
  739. srcResult += ".ll";
  740. irDestPath += ".ll";
  741. }
  742. CopyFile(srcResult, irDestPath);
  743. }
  744. }
  745. while (true)
  746. {
  747. const char* msg = BfPassInstance_PopOutString(mPassInstance);
  748. if (msg == NULL)
  749. break;
  750. if ((strncmp(msg, ":warn ", 6) == 0))
  751. {
  752. OutputLine(msg + 6, OutputPri_Warning);
  753. }
  754. else if ((strncmp(msg, ":error ", 7) == 0))
  755. {
  756. OutputLine(msg + 7, OutputPri_Error);
  757. }
  758. else if ((strncmp(msg, ":med ", 5) == 0))
  759. {
  760. OutputLine(msg + 5, OutputPri_Normal);
  761. }
  762. else if ((strncmp(msg, ":low ", 5) == 0))
  763. {
  764. OutputLine(msg + 5, OutputPri_Low);
  765. }
  766. else if ((strncmp(msg, "ERROR(", 6) == 0) || (strncmp(msg, "ERROR:", 6) == 0))
  767. {
  768. OutputLine(msg, OutputPri_Error);
  769. }
  770. else if ((strncmp(msg, "WARNING(", 8) == 0) || (strncmp(msg, "WARNING:", 8) == 0))
  771. {
  772. OutputLine(msg, OutputPri_Warning);
  773. }
  774. else
  775. OutputLine(msg);
  776. }
  777. if ((!mHadErrors) && (!mTargetPath.IsEmpty()))
  778. {
  779. if (mVerbosity == Verbosity_Normal)
  780. {
  781. std::cout << "Linking " << mTargetPath.c_str() << "...";
  782. std::cout.flush();
  783. }
  784. #ifdef BF_PLATFORM_WINDOWS
  785. DoLinkMS();
  786. #else
  787. DoLinkGNU();
  788. #endif
  789. if (mVerbosity == Verbosity_Normal)
  790. std::cout << std::endl;
  791. }
  792. BfPassInstance_Delete(mPassInstance);
  793. BfCompiler_Delete(mCompiler);
  794. BfSystem_Delete(mSystem);
  795. return !mHadErrors;
  796. }