| 1 | /**************************************************************************/ |
| 2 | /* godot_linuxbsd.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "os_linuxbsd.h" |
| 32 | |
| 33 | #include "core/profiling/profiling.h" |
| 34 | #include "main/main.h" |
| 35 | |
| 36 | #include <unistd.h> |
| 37 | #include <climits> |
| 38 | #include <clocale> |
| 39 | #include <cstdlib> |
| 40 | |
| 41 | #if defined(SANITIZERS_ENABLED) |
| 42 | #include <sys/resource.h> |
| 43 | #endif |
| 44 | |
| 45 | #if defined(__x86_64) || defined(__x86_64__) |
| 46 | void __cpuid(int *r_cpuinfo, int p_info) { |
| 47 | // Note: Some compilers have a buggy `__cpuid` intrinsic, using inline assembly (based on LLVM-20 implementation) instead. |
| 48 | __asm__ __volatile__( |
| 49 | "xchgq %%rbx, %q1;" |
| 50 | "cpuid;" |
| 51 | "xchgq %%rbx, %q1;" |
| 52 | : "=a"(r_cpuinfo[0]), "=r"(r_cpuinfo[1]), "=c"(r_cpuinfo[2]), "=d"(r_cpuinfo[3]) |
| 53 | : "0"(p_info)); |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | // For export templates, add a section; the exporter will patch it to enclose |
| 58 | // the data appended to the executable (bundled PCK). |
| 59 | #if !defined(TOOLS_ENABLED) && defined(__GNUC__) |
| 60 | static const char dummy[8] __attribute__((section("pck"), used)) = { 0 }; |
| 61 | |
| 62 | // Dummy function to prevent LTO from discarding "pck" section. |
| 63 | extern "C" const char *pck_section_dummy_call() __attribute__((used)); |
| 64 | extern "C" const char *pck_section_dummy_call() { |
| 65 | return &dummy[0]; |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | int main(int argc, char *argv[]) { |
| 70 | #if defined(__x86_64) || defined(__x86_64__) |
| 71 | int cpuinfo[4]; |
| 72 | __cpuid(cpuinfo, 0x01); |
| 73 | |
| 74 | if (!(cpuinfo[2] & (1 << 20))) { |
| 75 | printf("A CPU with SSE4.2 instruction set support is required.\n"); |
| 76 | |
| 77 | int ret = system("zenity --warning --title \"Godot Engine\" --text \"A CPU with SSE4.2 instruction set support is required.\" 2> /dev/null"); |
| 78 | if (ret != 0) { |
| 79 | ret = system("kdialog --title \"Godot Engine\" --sorry \"A CPU with SSE4.2 instruction set support is required.\" 2> /dev/null"); |
| 80 | } |
| 81 | if (ret != 0) { |
| 82 | ret = system("Xdialog --title \"Godot Engine\" --msgbox \"A CPU with SSE4.2 instruction set support is required.\" 0 0 2> /dev/null"); |
| 83 | } |
| 84 | if (ret != 0) { |
| 85 | ret = system("xmessage -center -title \"Godot Engine\" \"A CPU with SSE4.2 instruction set support is required.\" 2> /dev/null"); |
| 86 | } |
| 87 | abort(); |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | #if defined(SANITIZERS_ENABLED) |
| 92 | // Note: Set stack size to be at least 30 MB (vs 8 MB default) to avoid overflow, address sanitizer can increase stack usage up to 3 times. |
| 93 | struct rlimit stack_lim = { 0x1E00000, 0x1E00000 }; |
| 94 | setrlimit(RLIMIT_STACK, &stack_lim); |
| 95 | #endif |
| 96 | |
| 97 | godot_init_profiler(); |
| 98 | |
| 99 | OS_LinuxBSD os; |
| 100 | |
| 101 | setlocale(LC_CTYPE, ""); |
| 102 | |
| 103 | // We must override main when testing is enabled |
| 104 | TEST_MAIN_OVERRIDE |
| 105 | |
| 106 | char *cwd = (char *)malloc(PATH_MAX); |
| 107 | ERR_FAIL_NULL_V(cwd, ERR_OUT_OF_MEMORY); |
| 108 | char *ret = getcwd(cwd, PATH_MAX); |
| 109 | |
| 110 | Error err = Main::setup(argv[0], argc - 1, &argv[1]); |
| 111 | |
| 112 | if (err != OK) { |
| 113 | free(cwd); |
| 114 | if (err == ERR_HELP) { // Returned by --help and --version, so success. |
| 115 | return EXIT_SUCCESS; |
| 116 | } |
| 117 | return EXIT_FAILURE; |
| 118 | } |
| 119 | |
| 120 | if (Main::start() == EXIT_SUCCESS) { |
| 121 | os.run(); |
| 122 | } else { |
| 123 | os.set_exit_code(EXIT_FAILURE); |
| 124 | } |
| 125 | Main::cleanup(); |
| 126 | |
| 127 | if (ret) { // Previous getcwd was successful |
| 128 | if (chdir(cwd) != 0) { |
| 129 | ERR_PRINT("Couldn't return to previous working directory."); |
| 130 | } |
| 131 | } |
| 132 | free(cwd); |
| 133 | |
| 134 | godot_cleanup_profiler(); |
| 135 | return os.get_exit_code(); |
| 136 | } |