godotsharp_builds.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*************************************************************************/
  2. /* godotsharp_builds.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "godotsharp_builds.h"
  31. #include "core/vector.h"
  32. #include "main/main.h"
  33. #include "../glue/cs_glue_version.gen.h"
  34. #include "../godotsharp_dirs.h"
  35. #include "../mono_gd/gd_mono_class.h"
  36. #include "../mono_gd/gd_mono_marshal.h"
  37. #include "../utils/path_utils.h"
  38. #include "bindings_generator.h"
  39. #include "csharp_project.h"
  40. #include "godotsharp_editor.h"
  41. #define PROP_NAME_MSBUILD_MONO "MSBuild (Mono)"
  42. #define PROP_NAME_MSBUILD_VS "MSBuild (VS Build Tools)"
  43. #define PROP_NAME_XBUILD "xbuild (Deprecated)"
  44. void godot_icall_BuildInstance_ExitCallback(MonoString *p_solution, MonoString *p_config, int p_exit_code) {
  45. String solution = GDMonoMarshal::mono_string_to_godot(p_solution);
  46. String config = GDMonoMarshal::mono_string_to_godot(p_config);
  47. GodotSharpBuilds::get_singleton()->build_exit_callback(MonoBuildInfo(solution, config), p_exit_code);
  48. }
  49. static Vector<const char *> _get_msbuild_hint_dirs() {
  50. Vector<const char *> ret;
  51. #ifdef OSX_ENABLED
  52. ret.push_back("/Library/Frameworks/Mono.framework/Versions/Current/bin/");
  53. ret.push_back("/usr/local/var/homebrew/linked/mono/bin/");
  54. #endif
  55. ret.push_back("/opt/novell/mono/bin/");
  56. return ret;
  57. }
  58. #ifdef UNIX_ENABLED
  59. String _find_build_engine_on_unix(const String &p_name) {
  60. String ret = path_which(p_name);
  61. if (ret.length())
  62. return ret;
  63. String ret_fallback = path_which(p_name + ".exe");
  64. if (ret_fallback.length())
  65. return ret_fallback;
  66. static Vector<const char *> locations = _get_msbuild_hint_dirs();
  67. for (int i = 0; i < locations.size(); i++) {
  68. String hint_path = locations[i] + p_name;
  69. if (FileAccess::exists(hint_path)) {
  70. return hint_path;
  71. }
  72. }
  73. return String();
  74. }
  75. #endif
  76. MonoString *godot_icall_BuildInstance_get_MSBuildPath() {
  77. GodotSharpBuilds::BuildTool build_tool = GodotSharpBuilds::BuildTool(int(EditorSettings::get_singleton()->get("mono/builds/build_tool")));
  78. #if defined(WINDOWS_ENABLED)
  79. switch (build_tool) {
  80. case GodotSharpBuilds::MSBUILD_VS: {
  81. static String msbuild_tools_path;
  82. if (msbuild_tools_path.empty() || !FileAccess::exists(msbuild_tools_path)) {
  83. // Try to search it again if it wasn't found last time or if it was removed from its location
  84. msbuild_tools_path = MonoRegUtils::find_msbuild_tools_path();
  85. }
  86. if (msbuild_tools_path.length()) {
  87. if (!msbuild_tools_path.ends_with("\\"))
  88. msbuild_tools_path += "\\";
  89. return GDMonoMarshal::mono_string_from_godot(msbuild_tools_path + "MSBuild.exe");
  90. }
  91. print_verbose("Cannot find executable for '" PROP_NAME_MSBUILD_VS "'. Trying with '" PROP_NAME_MSBUILD_MONO "'...");
  92. } // FALL THROUGH
  93. case GodotSharpBuilds::MSBUILD_MONO: {
  94. String msbuild_path = GDMono::get_singleton()->get_mono_reg_info().bin_dir.plus_file("msbuild.bat");
  95. if (!FileAccess::exists(msbuild_path)) {
  96. WARN_PRINTS("Cannot find executable for '" PROP_NAME_MSBUILD_MONO "'. Tried with path: " + msbuild_path);
  97. }
  98. return GDMonoMarshal::mono_string_from_godot(msbuild_path);
  99. } break;
  100. case GodotSharpBuilds::XBUILD: {
  101. String xbuild_path = GDMono::get_singleton()->get_mono_reg_info().bin_dir.plus_file("xbuild.bat");
  102. if (!FileAccess::exists(xbuild_path)) {
  103. WARN_PRINTS("Cannot find executable for '" PROP_NAME_XBUILD "'. Tried with path: " + xbuild_path);
  104. }
  105. return GDMonoMarshal::mono_string_from_godot(xbuild_path);
  106. } break;
  107. default:
  108. ERR_EXPLAIN("You don't deserve to live");
  109. CRASH_NOW();
  110. }
  111. #elif defined(UNIX_ENABLED)
  112. static String msbuild_path;
  113. static String xbuild_path;
  114. if (build_tool == GodotSharpBuilds::XBUILD) {
  115. if (xbuild_path.empty() || !FileAccess::exists(xbuild_path)) {
  116. // Try to search it again if it wasn't found last time or if it was removed from its location
  117. xbuild_path = _find_build_engine_on_unix("msbuild");
  118. }
  119. if (xbuild_path.empty()) {
  120. WARN_PRINT("Cannot find binary for '" PROP_NAME_XBUILD "'");
  121. return NULL;
  122. }
  123. } else {
  124. if (msbuild_path.empty() || !FileAccess::exists(msbuild_path)) {
  125. // Try to search it again if it wasn't found last time or if it was removed from its location
  126. msbuild_path = _find_build_engine_on_unix("msbuild");
  127. }
  128. if (msbuild_path.empty()) {
  129. WARN_PRINT("Cannot find binary for '" PROP_NAME_MSBUILD_MONO "'");
  130. return NULL;
  131. }
  132. }
  133. return GDMonoMarshal::mono_string_from_godot(build_tool != GodotSharpBuilds::XBUILD ? msbuild_path : xbuild_path);
  134. #else
  135. (void)build_tool; // UNUSED
  136. ERR_EXPLAIN("Not implemented on this platform");
  137. ERR_FAIL_V(NULL);
  138. #endif
  139. }
  140. MonoString *godot_icall_BuildInstance_get_FrameworkPath() {
  141. #if defined(WINDOWS_ENABLED)
  142. const MonoRegInfo &mono_reg_info = GDMono::get_singleton()->get_mono_reg_info();
  143. if (mono_reg_info.assembly_dir.length()) {
  144. String framework_path = path_join(mono_reg_info.assembly_dir, "mono", "4.5");
  145. return GDMonoMarshal::mono_string_from_godot(framework_path);
  146. }
  147. ERR_EXPLAIN("Cannot find Mono's assemblies directory in the registry");
  148. ERR_FAIL_V(NULL);
  149. #else
  150. return NULL;
  151. #endif
  152. }
  153. MonoString *godot_icall_BuildInstance_get_MonoWindowsBinDir() {
  154. #if defined(WINDOWS_ENABLED)
  155. const MonoRegInfo &mono_reg_info = GDMono::get_singleton()->get_mono_reg_info();
  156. if (mono_reg_info.bin_dir.length()) {
  157. return GDMonoMarshal::mono_string_from_godot(mono_reg_info.bin_dir);
  158. }
  159. ERR_EXPLAIN("Cannot find Mono's binaries directory in the registry");
  160. ERR_FAIL_V(NULL);
  161. #else
  162. return NULL;
  163. #endif
  164. }
  165. MonoBoolean godot_icall_BuildInstance_get_UsingMonoMSBuildOnWindows() {
  166. #if defined(WINDOWS_ENABLED)
  167. return GodotSharpBuilds::BuildTool(int(EditorSettings::get_singleton()->get("mono/builds/build_tool"))) == GodotSharpBuilds::MSBUILD_MONO;
  168. #else
  169. return false;
  170. #endif
  171. }
  172. void GodotSharpBuilds::register_internal_calls() {
  173. static bool registered = false;
  174. ERR_FAIL_COND(registered);
  175. registered = true;
  176. mono_add_internal_call("GodotSharpTools.Build.BuildSystem::godot_icall_BuildInstance_ExitCallback", (void *)godot_icall_BuildInstance_ExitCallback);
  177. mono_add_internal_call("GodotSharpTools.Build.BuildInstance::godot_icall_BuildInstance_get_MSBuildPath", (void *)godot_icall_BuildInstance_get_MSBuildPath);
  178. mono_add_internal_call("GodotSharpTools.Build.BuildInstance::godot_icall_BuildInstance_get_FrameworkPath", (void *)godot_icall_BuildInstance_get_FrameworkPath);
  179. mono_add_internal_call("GodotSharpTools.Build.BuildInstance::godot_icall_BuildInstance_get_MonoWindowsBinDir", (void *)godot_icall_BuildInstance_get_MonoWindowsBinDir);
  180. mono_add_internal_call("GodotSharpTools.Build.BuildInstance::godot_icall_BuildInstance_get_UsingMonoMSBuildOnWindows", (void *)godot_icall_BuildInstance_get_UsingMonoMSBuildOnWindows);
  181. }
  182. void GodotSharpBuilds::show_build_error_dialog(const String &p_message) {
  183. GodotSharpEditor::get_singleton()->show_error_dialog(p_message, "Build error");
  184. MonoBottomPanel::get_singleton()->show_build_tab();
  185. }
  186. bool GodotSharpBuilds::build_api_sln(const String &p_api_sln_dir, const String &p_config) {
  187. String api_sln_file = p_api_sln_dir.plus_file(API_SOLUTION_NAME ".sln");
  188. String core_api_assembly_dir = p_api_sln_dir.plus_file(CORE_API_ASSEMBLY_NAME).plus_file("bin").plus_file(p_config);
  189. String core_api_assembly_file = core_api_assembly_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
  190. String editor_api_assembly_dir = p_api_sln_dir.plus_file(EDITOR_API_ASSEMBLY_NAME).plus_file("bin").plus_file(p_config);
  191. String editor_api_assembly_file = editor_api_assembly_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
  192. if (!FileAccess::exists(core_api_assembly_file) || !FileAccess::exists(editor_api_assembly_file)) {
  193. MonoBuildInfo api_build_info(api_sln_file, p_config);
  194. // TODO Replace this global NoWarn with '#pragma warning' directives on generated files,
  195. // once we start to actively document manually maintained C# classes
  196. api_build_info.custom_props.push_back("NoWarn=1591"); // Ignore missing documentation warnings
  197. if (!GodotSharpBuilds::get_singleton()->build(api_build_info)) {
  198. show_build_error_dialog("Failed to build " API_SOLUTION_NAME " solution.");
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. bool GodotSharpBuilds::copy_api_assembly(const String &p_src_dir, const String &p_dst_dir, const String &p_assembly_name, APIAssembly::Type p_api_type) {
  205. // Create destination directory if needed
  206. if (!DirAccess::exists(p_dst_dir)) {
  207. DirAccess *da = DirAccess::create_for_path(p_dst_dir);
  208. Error err = da->make_dir_recursive(p_dst_dir);
  209. memdelete(da);
  210. if (err != OK) {
  211. show_build_error_dialog("Failed to create destination directory for the API assemblies. Error: " + itos(err));
  212. return false;
  213. }
  214. }
  215. String assembly_file = p_assembly_name + ".dll";
  216. String assembly_src = p_src_dir.plus_file(assembly_file);
  217. String assembly_dst = p_dst_dir.plus_file(assembly_file);
  218. if (!FileAccess::exists(assembly_dst) ||
  219. FileAccess::get_modified_time(assembly_src) > FileAccess::get_modified_time(assembly_dst) ||
  220. GDMono::get_singleton()->metadata_is_api_assembly_invalidated(p_api_type)) {
  221. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  222. String xml_file = p_assembly_name + ".xml";
  223. if (da->copy(p_src_dir.plus_file(xml_file), p_dst_dir.plus_file(xml_file)) != OK)
  224. WARN_PRINTS("Failed to copy " + xml_file);
  225. String pdb_file = p_assembly_name + ".pdb";
  226. if (da->copy(p_src_dir.plus_file(pdb_file), p_dst_dir.plus_file(pdb_file)) != OK)
  227. WARN_PRINTS("Failed to copy " + pdb_file);
  228. Error err = da->copy(assembly_src, assembly_dst);
  229. if (err != OK) {
  230. show_build_error_dialog("Failed to copy " + assembly_file);
  231. return false;
  232. }
  233. GDMono::get_singleton()->metadata_set_api_assembly_invalidated(p_api_type, false);
  234. }
  235. return true;
  236. }
  237. String GodotSharpBuilds::_api_folder_name(APIAssembly::Type p_api_type) {
  238. uint64_t api_hash = p_api_type == APIAssembly::API_CORE ?
  239. GDMono::get_singleton()->get_api_core_hash() :
  240. GDMono::get_singleton()->get_api_editor_hash();
  241. return String::num_uint64(api_hash) +
  242. "_" + String::num_uint64(BindingsGenerator::get_version()) +
  243. "_" + String::num_uint64(CS_GLUE_VERSION);
  244. }
  245. bool GodotSharpBuilds::make_api_assembly(APIAssembly::Type p_api_type) {
  246. String api_name = p_api_type == APIAssembly::API_CORE ? CORE_API_ASSEMBLY_NAME : EDITOR_API_ASSEMBLY_NAME;
  247. String editor_prebuilt_api_dir = GodotSharpDirs::get_data_editor_prebuilt_api_dir();
  248. String res_assemblies_dir = GodotSharpDirs::get_res_assemblies_dir();
  249. if (FileAccess::exists(editor_prebuilt_api_dir.plus_file(api_name + ".dll"))) {
  250. EditorProgress pr("mono_copy_prebuilt_api_assembly", "Copying prebuilt " + api_name + " assembly...", 1);
  251. pr.step("Copying " + api_name + " assembly", 0);
  252. return GodotSharpBuilds::copy_api_assembly(editor_prebuilt_api_dir, res_assemblies_dir, api_name, p_api_type);
  253. }
  254. String api_build_config = "Release";
  255. EditorProgress pr("mono_build_release_" API_SOLUTION_NAME, "Building " API_SOLUTION_NAME " solution...", 3);
  256. pr.step("Generating " API_SOLUTION_NAME " solution", 0);
  257. String api_sln_dir = GodotSharpDirs::get_mono_solutions_dir()
  258. .plus_file(_api_folder_name(APIAssembly::API_CORE));
  259. String api_sln_file = api_sln_dir.plus_file(API_SOLUTION_NAME ".sln");
  260. if (!DirAccess::exists(api_sln_dir) || !FileAccess::exists(api_sln_file)) {
  261. BindingsGenerator *gen = BindingsGenerator::get_singleton();
  262. bool gen_verbose = OS::get_singleton()->is_stdout_verbose();
  263. Error err = gen->generate_cs_api(api_sln_dir, gen_verbose);
  264. if (err != OK) {
  265. show_build_error_dialog("Failed to generate " API_SOLUTION_NAME " solution. Error: " + itos(err));
  266. return false;
  267. }
  268. }
  269. pr.step("Building " API_SOLUTION_NAME " solution", 1);
  270. if (!GodotSharpBuilds::build_api_sln(api_sln_dir, api_build_config))
  271. return false;
  272. pr.step("Copying " + api_name + " assembly", 2);
  273. // Copy the built assembly to the assemblies directory
  274. String api_assembly_dir = api_sln_dir.plus_file(api_name).plus_file("bin").plus_file(api_build_config);
  275. if (!GodotSharpBuilds::copy_api_assembly(api_assembly_dir, res_assemblies_dir, api_name, p_api_type))
  276. return false;
  277. return true;
  278. }
  279. bool GodotSharpBuilds::build_project_blocking(const String &p_config) {
  280. if (!FileAccess::exists(GodotSharpDirs::get_project_sln_path()))
  281. return true; // No solution to build
  282. if (!GodotSharpBuilds::make_api_assembly(APIAssembly::API_CORE))
  283. return false;
  284. if (!GodotSharpBuilds::make_api_assembly(APIAssembly::API_EDITOR))
  285. return false;
  286. EditorProgress pr("mono_project_debug_build", "Building project solution...", 1);
  287. pr.step("Building project solution", 0);
  288. MonoBuildInfo build_info(GodotSharpDirs::get_project_sln_path(), p_config);
  289. if (!GodotSharpBuilds::get_singleton()->build(build_info)) {
  290. GodotSharpBuilds::show_build_error_dialog("Failed to build project solution");
  291. return false;
  292. }
  293. return true;
  294. }
  295. bool GodotSharpBuilds::editor_build_callback() {
  296. String scripts_metadata_path_editor = GodotSharpDirs::get_res_metadata_dir().plus_file("scripts_metadata.editor");
  297. String scripts_metadata_path_player = GodotSharpDirs::get_res_metadata_dir().plus_file("scripts_metadata.editor_player");
  298. Error metadata_err = CSharpProject::generate_scripts_metadata(GodotSharpDirs::get_project_csproj_path(), scripts_metadata_path_editor);
  299. ERR_FAIL_COND_V(metadata_err != OK, false);
  300. if (FileAccess::exists(scripts_metadata_path_editor)) {
  301. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  302. Error copy_err = da->copy(scripts_metadata_path_editor, scripts_metadata_path_player);
  303. ERR_EXPLAIN("Failed to copy scripts metadata file");
  304. ERR_FAIL_COND_V(copy_err != OK, false);
  305. }
  306. return build_project_blocking("Tools");
  307. }
  308. GodotSharpBuilds *GodotSharpBuilds::singleton = NULL;
  309. void GodotSharpBuilds::build_exit_callback(const MonoBuildInfo &p_build_info, int p_exit_code) {
  310. BuildProcess *match = builds.getptr(p_build_info);
  311. ERR_FAIL_NULL(match);
  312. BuildProcess &bp = *match;
  313. bp.on_exit(p_exit_code);
  314. }
  315. void GodotSharpBuilds::restart_build(MonoBuildTab *p_build_tab) {
  316. }
  317. void GodotSharpBuilds::stop_build(MonoBuildTab *p_build_tab) {
  318. }
  319. bool GodotSharpBuilds::build(const MonoBuildInfo &p_build_info) {
  320. BuildProcess *match = builds.getptr(p_build_info);
  321. if (match) {
  322. BuildProcess &bp = *match;
  323. bp.start(true);
  324. return bp.exit_code == 0;
  325. } else {
  326. BuildProcess bp = BuildProcess(p_build_info);
  327. bp.start(true);
  328. builds.set(p_build_info, bp);
  329. return bp.exit_code == 0;
  330. }
  331. }
  332. bool GodotSharpBuilds::build_async(const MonoBuildInfo &p_build_info, GodotSharpBuild_ExitCallback p_callback) {
  333. BuildProcess *match = builds.getptr(p_build_info);
  334. if (match) {
  335. BuildProcess &bp = *match;
  336. bp.start();
  337. return !bp.exited; // failed to start
  338. } else {
  339. BuildProcess bp = BuildProcess(p_build_info, p_callback);
  340. bp.start();
  341. builds.set(p_build_info, bp);
  342. return !bp.exited; // failed to start
  343. }
  344. }
  345. GodotSharpBuilds::GodotSharpBuilds() {
  346. singleton = this;
  347. EditorNode::get_singleton()->add_build_callback(&GodotSharpBuilds::editor_build_callback);
  348. // Build tool settings
  349. EditorSettings *ed_settings = EditorSettings::get_singleton();
  350. EDITOR_DEF("mono/builds/build_tool", MSBUILD_MONO);
  351. ed_settings->add_property_hint(PropertyInfo(Variant::INT, "mono/builds/build_tool", PROPERTY_HINT_ENUM,
  352. PROP_NAME_MSBUILD_MONO
  353. #ifdef WINDOWS_ENABLED
  354. "," PROP_NAME_MSBUILD_VS
  355. #endif
  356. "," PROP_NAME_XBUILD));
  357. }
  358. GodotSharpBuilds::~GodotSharpBuilds() {
  359. singleton = NULL;
  360. }
  361. void GodotSharpBuilds::BuildProcess::on_exit(int p_exit_code) {
  362. exited = true;
  363. exit_code = p_exit_code;
  364. build_tab->on_build_exit(p_exit_code == 0 ? MonoBuildTab::RESULT_SUCCESS : MonoBuildTab::RESULT_ERROR);
  365. build_instance.unref();
  366. if (exit_callback)
  367. exit_callback(exit_code);
  368. }
  369. void GodotSharpBuilds::BuildProcess::start(bool p_blocking) {
  370. _GDMONO_SCOPE_DOMAIN_(TOOLS_DOMAIN)
  371. exit_code = -1;
  372. String log_dirpath = build_info.get_log_dirpath();
  373. if (build_tab) {
  374. build_tab->on_build_start();
  375. } else {
  376. build_tab = memnew(MonoBuildTab(build_info, log_dirpath));
  377. MonoBottomPanel::get_singleton()->add_build_tab(build_tab);
  378. }
  379. if (p_blocking) {
  380. // Required in order to update the build tasks list
  381. Main::iteration();
  382. }
  383. if (!exited) {
  384. exited = true;
  385. String message = "Tried to start build process, but it is already running";
  386. build_tab->on_build_exec_failed(message);
  387. ERR_EXPLAIN(message);
  388. ERR_FAIL();
  389. }
  390. exited = false;
  391. // Remove old issues file
  392. String issues_file = "msbuild_issues.csv";
  393. DirAccessRef d = DirAccess::create_for_path(log_dirpath);
  394. if (d->file_exists(issues_file)) {
  395. Error err = d->remove(issues_file);
  396. if (err != OK) {
  397. exited = true;
  398. String file_path = ProjectSettings::get_singleton()->localize_path(log_dirpath).plus_file(issues_file);
  399. String message = "Cannot remove issues file: " + file_path;
  400. build_tab->on_build_exec_failed(message);
  401. ERR_EXPLAIN(message);
  402. ERR_FAIL();
  403. }
  404. }
  405. GDMonoClass *klass = GDMono::get_singleton()->get_editor_tools_assembly()->get_class("GodotSharpTools.Build", "BuildInstance");
  406. MonoObject *mono_object = mono_object_new(mono_domain_get(), klass->get_mono_ptr());
  407. // Construct
  408. Variant solution = build_info.solution;
  409. Variant config = build_info.configuration;
  410. const Variant *ctor_args[2] = { &solution, &config };
  411. MonoException *exc = NULL;
  412. GDMonoMethod *ctor = klass->get_method(".ctor", 2);
  413. ctor->invoke(mono_object, ctor_args, &exc);
  414. if (exc) {
  415. exited = true;
  416. GDMonoUtils::debug_unhandled_exception(exc);
  417. String message = "The build constructor threw an exception.\n" + GDMonoUtils::get_exception_name_and_message(exc);
  418. build_tab->on_build_exec_failed(message);
  419. ERR_EXPLAIN(message);
  420. ERR_FAIL();
  421. }
  422. // Call Build
  423. String logger_assembly_path = GDMono::get_singleton()->get_editor_tools_assembly()->get_path();
  424. Variant logger_assembly = ProjectSettings::get_singleton()->globalize_path(logger_assembly_path);
  425. Variant logger_output_dir = log_dirpath;
  426. Variant custom_props = build_info.custom_props;
  427. const Variant *args[3] = { &logger_assembly, &logger_output_dir, &custom_props };
  428. exc = NULL;
  429. GDMonoMethod *build_method = klass->get_method(p_blocking ? "Build" : "BuildAsync", 3);
  430. build_method->invoke(mono_object, args, &exc);
  431. if (exc) {
  432. exited = true;
  433. GDMonoUtils::debug_unhandled_exception(exc);
  434. String message = "The build method threw an exception.\n" + GDMonoUtils::get_exception_name_and_message(exc);
  435. build_tab->on_build_exec_failed(message);
  436. ERR_EXPLAIN(message);
  437. ERR_FAIL();
  438. }
  439. // Build returned
  440. if (p_blocking) {
  441. exited = true;
  442. exit_code = klass->get_field("exitCode")->get_int_value(mono_object);
  443. if (exit_code != 0) {
  444. print_verbose("MSBuild finished with exit code " + itos(exit_code));
  445. }
  446. build_tab->on_build_exit(exit_code == 0 ? MonoBuildTab::RESULT_SUCCESS : MonoBuildTab::RESULT_ERROR);
  447. } else {
  448. build_instance = MonoGCHandle::create_strong(mono_object);
  449. exited = false;
  450. }
  451. }
  452. GodotSharpBuilds::BuildProcess::BuildProcess(const MonoBuildInfo &p_build_info, GodotSharpBuild_ExitCallback p_callback) :
  453. build_info(p_build_info),
  454. build_tab(NULL),
  455. exit_callback(p_callback),
  456. exited(true),
  457. exit_code(-1) {
  458. }