1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup creator
*/
#include <cstdlib>
#include <cstring>
#ifdef WIN32
# include "utfconv.hh"
# include <windows.h>
# ifdef WITH_CPU_CHECK
# pragma comment(linker, "/include:cpu_check_win32")
# endif
#endif
#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG)
# pragma comment(lib, "tbbmalloc_proxy.lib")
# pragma comment(linker, "/include:__TBB_malloc_proxy")
#endif
#include "MEM_guardedalloc.h"
#include "CLG_log.h"
#include "DNA_genfile.h"
#include "BLI_string.h"
#include "BLI_system.h"
#include "BLI_task.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
/* Mostly initialization functions. */
#include "BKE_appdir.hh"
#include "BKE_blender.hh"
#include "BKE_brush.hh"
#include "BKE_cachefile.hh"
#include "BKE_callbacks.hh"
#include "BKE_context.hh"
#include "BKE_cpp_types.hh"
#include "BKE_global.hh"
#include "BKE_idtype.hh"
#include "BKE_material.h"
#include "BKE_modifier.hh"
#include "BKE_node.hh"
#include "BKE_particle.h"
#include "BKE_shader_fx.h"
#include "BKE_sound.h"
#include "BKE_vfont.hh"
#include "BKE_volume.hh"
#ifndef WITH_PYTHON_MODULE
# include "BLI_args.h"
#endif
#include "DEG_depsgraph.hh"
#include "IMB_imbuf.hh" /* For #IMB_init. */
#include "RE_engine.h"
#include "RE_texture.h"
#include "ED_datafiles.h"
#include "WM_api.hh"
#include "RNA_define.hh"
#include "GPU_compilation_subprocess.hh"
#ifdef WITH_FREESTYLE
# include "FRS_freestyle.h"
#endif
#include <csignal>
#ifdef __FreeBSD__
# include <floatingpoint.h>
#endif
#ifdef WITH_BINRELOC
# include "binreloc.h"
#endif
#ifdef WITH_LIBMV
# include "libmv-capi.h"
#endif
#ifdef WITH_CYCLES_LOGGING
# include "CCL_api.h"
#endif
#include "creator_intern.h" /* Own include. */
/* -------------------------------------------------------------------- */
/** \name Local Defines
* \{ */
/* When building as a Python module, don't use special argument handling
* so the module loading logic can control the `argv` & `argc`. */
#if defined(WIN32) && !defined(WITH_PYTHON_MODULE)
# define USE_WIN32_UNICODE_ARGS
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Local Application State
* \{ */
/* Written to by `creator_args.cc`. */
ApplicationState app_state = []() {
ApplicationState app_state{};
app_state.signal.use_crash_handler = true;
app_state.signal.use_abort_handler = true;
app_state.exit_code_on_error.python = 0;
app_state.main_arg_deferred = nullptr;
return app_state;
}();
/** \} */
/* -------------------------------------------------------------------- */
/** \name Application Level Callbacks
*
* Initialize callbacks for the modules that need them.
* \{ */
static void callback_mem_error(const char *errorStr)
{
fputs(errorStr, stderr);
fflush(stderr);
}
static void main_callback_setup()
{
/* Error output from the guarded allocation routines. */
MEM_set_error_callback(callback_mem_error);
}
/** Free data on early exit (if Python calls `sys.exit()` while parsing args for eg). */
struct CreatorAtExitData {
#ifndef WITH_PYTHON_MODULE
bArgs *ba;
#endif
#ifdef USE_WIN32_UNICODE_ARGS
char **argv;
int argv_num;
#endif
#if defined(WITH_PYTHON_MODULE) && !defined(USE_WIN32_UNICODE_ARGS)
void *_empty; /* Prevent empty struct error with MSVC. */
#endif
};
static void callback_main_atexit(void *user_data)
{
CreatorAtExitData *app_init_data = static_cast<CreatorAtExitData *>(user_data);
#ifndef WITH_PYTHON_MODULE
if (app_init_data->ba) {
BLI_args_destroy(app_init_data->ba);
app_init_data->ba = nullptr;
}
#else
UNUSED_VARS(app_init_data); /* May be unused. */
#endif
#ifdef USE_WIN32_UNICODE_ARGS
if (app_init_data->argv) {
while (app_init_data->argv_num) {
free((void *)app_init_data->argv[--app_init_data->argv_num]);
}
free((void *)app_init_data->argv);
app_init_data->argv = nullptr;
}
#else
UNUSED_VARS(app_init_data); /* May be unused. */
#endif
}
static void callback_clg_fatal(void *fp)
{
BLI_system_backtrace(static_cast<FILE *>(fp));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Blender as a Stand-Alone Python Module (bpy)
*
* While not officially supported, this can be useful for Python developers.
* See: https://developer.blender.org/docs/handbook/building_blender/python_module/
* \{ */
#ifdef WITH_PYTHON_MODULE
/* Called in `bpy_interface.cc` when building as a Python module. */
int main_python_enter(int argc, const char **argv);
void main_python_exit();
/* Rename the `main(..)` function, allowing Python initialization to call it. */
# define main main_python_enter
static void *evil_C = nullptr;
# ifdef __APPLE__
/* Environment is not available in macOS shared libraries. */
# include <crt_externs.h>
char **environ = nullptr;
# endif /* __APPLE__ */
#endif /* WITH_PYTHON_MODULE */
/** \} */
/* -------------------------------------------------------------------- */
/** \name GMP Allocator Workaround
* \{ */
#if (defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_GMP)) || \
defined(DOXYGEN)
# include "gmp.h"
# include "tbb/scalable_allocator.h"
void *gmp_alloc(size_t size)
{
return scalable_malloc(size);
}
void *gmp_realloc(void *ptr, size_t /*old_size*/, size_t new_size)
{
return scalable_realloc(ptr, new_size);
}
void gmp_free(void *ptr, size_t /*size*/)
{
scalable_free(ptr);
}
/**
* Use TBB's scalable_allocator on Windows.
* `TBBmalloc` correctly captures all allocations already,
* however, GMP is built with MINGW since it doesn't build with MSVC,
* which TBB has issues hooking into automatically.
*/
void gmp_blender_init_allocator()
{
mp_set_memory_functions(gmp_alloc, gmp_realloc, gmp_free);
}
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Main Function
* \{ */
#if defined(__APPLE__)
extern "C" int GHOST_HACK_getFirstFile(char buf[]);
#endif
/**
* Blender's main function responsibilities are:
* - setup subsystems.
* - handle arguments.
* - run #WM_main() event loop,
* or exit immediately when running in background-mode.
*/
int main(int argc,
#ifdef USE_WIN32_UNICODE_ARGS
const char ** /*argv_c*/
#else
const char **argv
#endif
)
{
bContext *C;
#ifndef WITH_PYTHON_MODULE
bArgs *ba;
#endif
#ifdef USE_WIN32_UNICODE_ARGS
char **argv;
int argv_num;
#endif
/* Ensure we free data on early-exit. */
CreatorAtExitData app_init_data = {nullptr};
BKE_blender_atexit_register(callback_main_atexit, &app_init_data);
/* Un-buffered `stdout` makes `stdout` and `stderr` better synchronized, and helps
* when stepping through code in a debugger (prints are immediately
* visible). However disabling buffering causes lock contention on windows
* see #76767 for details, since this is a debugging aid, we do not enable
* the un-buffered behavior for release builds. */
#ifndef NDEBUG
setvbuf(stdout, nullptr, _IONBF, 0);
#endif
#ifdef WIN32
/* We delay loading of OPENMP so we can set the policy here. */
# if defined(_MSC_VER)
_putenv_s("OMP_WAIT_POLICY", "PASSIVE");
# endif
# ifdef USE_WIN32_UNICODE_ARGS
/* Win32 Unicode Arguments. */
{
/* NOTE: Can't use `guardedalloc` allocation here, as it's not yet initialized
* (it depends on the arguments passed in, which is what we're getting here!). */
wchar_t **argv_16 = CommandLineToArgvW(GetCommandLineW(), &argc);
argv = static_cast<char **>(malloc(argc * sizeof(char *)));
for (argv_num = 0; argv_num < argc; argv_num++) {
argv[argv_num] = alloc_utf_8_from_16(argv_16[argv_num], 0);
}
LocalFree(argv_16);
/* Free on early-exit. */
app_init_data.argv = argv;
app_init_data.argv_num = argv_num;
}
# endif /* USE_WIN32_UNICODE_ARGS */
#endif /* WIN32 */
#if defined(WITH_OPENGL_BACKEND) && BLI_SUBPROCESS_SUPPORT
if (STREQ(argv[0], "--compilation-subprocess")) {
BLI_assert(argc == 2);
GPU_compilation_subprocess_run(argv[1]);
return 0;
}
#endif
/* NOTE: Special exception for guarded allocator type switch:
* we need to perform switch from lock-free to fully
* guarded allocator before any allocation happened.
*/
{
int i;
for (i = 0; i < argc; i++) {
if (STR_ELEM(argv[i], "-d", "--debug", "--debug-memory", "--debug-all")) {
printf("Switching to fully guarded memory allocator.\n");
MEM_use_guarded_allocator();
break;
}
if (STR_ELEM(argv[i], "--", "-c", "--command")) {
break;
}
}
MEM_init_memleak_detection();
}
#ifdef BUILD_DATE
{
const time_t temp_time = build_commit_timestamp;
const tm *tm = gmtime(&temp_time);
if (LIKELY(tm)) {
strftime(build_commit_date, sizeof(build_commit_date), "%Y-%m-%d", tm);
strftime(build_commit_time, sizeof(build_commit_time), "%H:%M", tm);
}
else {
const char *unknown = "date-unknown";
STRNCPY(build_commit_date, unknown);
STRNCPY(build_commit_time, unknown);
}
}
#endif
/* Initialize logging. */
CLG_init();
CLG_fatal_fn_set(callback_clg_fatal);
C = CTX_create();
#ifdef WITH_PYTHON_MODULE
# ifdef __APPLE__
environ = *_NSGetEnviron();
# endif
# undef main
evil_C = C;
#endif
#ifdef WITH_BINRELOC
br_init(nullptr);
#endif
#ifdef WITH_LIBMV
libmv_initLogging(argv[0]);
#elif defined(WITH_CYCLES_LOGGING)
CCL_init_logging(argv[0]);
#endif
#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_GMP)
gmp_blender_init_allocator();
#endif
main_callback_setup();
#if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE) && !defined(WITH_HEADLESS)
/* Patch to ignore argument finder gives us (PID?). */
if (argc == 2 && STRPREFIX(argv[1], "-psn_")) {
static char firstfilebuf[512];
argc = 1;
if (GHOST_HACK_getFirstFile(firstfilebuf)) {
argc = 2;
argv[1] = firstfilebuf;
}
}
#endif
#ifdef __FreeBSD__
fpsetmask(0);
#endif
/* Initialize path to executable. */
BKE_appdir_program_path_init(argv[0]);
BLI_threadapi_init();
DNA_sdna_current_init();
BKE_blender_globals_init(); /* `blender.cc` */
BKE_cpp_types_init();
BKE_idtype_init();
BKE_cachefiles_init();
BKE_modifier_init();
BKE_shaderfx_init();
BKE_volumes_init();
DEG_register_node_types();
BKE_brush_system_init();
RE_texture_rng_init();
BKE_callback_global_init();
/* First test for background-mode (#Global.background). */
#ifndef WITH_PYTHON_MODULE
ba = BLI_args_create(argc, (const char **)argv); /* Skip binary path. */
/* Ensure we free on early exit. */
app_init_data.ba = ba;
main_args_setup(C, ba, false);
/* Begin argument parsing, ignore leaks so arguments that call #exit
* (such as `--version` & `--help`) don't report leaks. */
MEM_use_memleak_detection(false);
/* Parse environment handling arguments. */
BLI_args_parse(ba, ARG_PASS_ENVIRONMENT, nullptr, nullptr);
#else
/* Using preferences or user startup makes no sense for #WITH_PYTHON_MODULE. */
G.factory_startup = true;
#endif
/* After parsing #ARG_PASS_ENVIRONMENT such as `--env-*`,
* since they impact `BKE_appdir` behavior. */
BKE_appdir_init();
/* After parsing number of threads argument. */
BLI_task_scheduler_init();
#ifndef WITH_PYTHON_MODULE
/* The settings pass includes:
* - Background-mode assignment (#Global.background), checked by other subsystems
* which may be skipped in background mode.
* - The animation player may be launched which takes over argument passing,
* initializes the sub-systems it needs which have not yet been started.
* The animation player will call `exit(..)` too, so code after this call
* never runs when it's invoked.
* - All the `--debug-*` flags.
*/
BLI_args_parse(ba, ARG_PASS_SETTINGS, nullptr, nullptr);
main_signal_setup();
#endif
/* Must be initialized after #BKE_appdir_init to account for color-management paths. */
IMB_init();
#ifdef WITH_FFMPEG
/* Keep after #ARG_PASS_SETTINGS since debug flags are checked. */
IMB_ffmpeg_init();
#endif
/* After #ARG_PASS_SETTINGS arguments, this is so #WM_main_playanim skips #RNA_init. */
RNA_init();
RE_engines_init();
blender::bke::node_system_init();
BKE_particle_init_rng();
/* End second initialization. */
#if defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS)
/* Python module mode ALWAYS runs in background-mode (for now). */
G.background = true;
/* Manually using `--background` also forces the audio device. */
BKE_sound_force_device("None");
#else
if (G.background) {
main_signal_setup_background();
}
#endif
/* Background render uses this font too. */
BKE_vfont_builtin_register(datatoc_bfont_pfb, datatoc_bfont_pfb_size);
/* Initialize FFMPEG if built in, also needed for background-mode if videos are
* rendered via FFMPEG. */
BKE_sound_init_once();
BKE_materials_init();
#ifndef WITH_PYTHON_MODULE
if (G.background == 0) {
BLI_args_parse(ba, ARG_PASS_SETTINGS_GUI, nullptr, nullptr);
}
BLI_args_parse(ba, ARG_PASS_SETTINGS_FORCE, nullptr, nullptr);
#endif
WM_init(C, argc, (const char **)argv);
#ifndef WITH_PYTHON
printf(
"\n* WARNING * - Blender compiled without Python!\n"
"this is not intended for typical usage\n\n");
#endif
#ifdef WITH_FREESTYLE
/* Initialize Freestyle. */
FRS_init();
FRS_set_context(C);
#endif
/* OK we are ready for it. */
#ifndef WITH_PYTHON_MODULE
/* Handles #ARG_PASS_FINAL. */
BLI_args_parse(ba, ARG_PASS_FINAL, main_args_handle_load_file, C);
#endif
/* Explicitly free data allocated for argument parsing:
* - `ba`
* - `argv` on WIN32.
*/
callback_main_atexit(&app_init_data);
BKE_blender_atexit_unregister(callback_main_atexit, &app_init_data);
/* End argument parsing, allow memory leaks to be printed. */
MEM_use_memleak_detection(true);
/* Paranoid, avoid accidental re-use. */
#ifndef WITH_PYTHON_MODULE
ba = nullptr;
(void)ba;
#endif
#ifdef USE_WIN32_UNICODE_ARGS
argv = nullptr;
(void)argv;
#endif
#ifndef WITH_PYTHON_MODULE
if (G.background) {
int exit_code;
if (app_state.main_arg_deferred != nullptr) {
exit_code = main_arg_deferred_handle();
main_arg_deferred_free();
}
else {
exit_code = G.is_break ? EXIT_FAILURE : EXIT_SUCCESS;
}
/* Using window-manager API in background-mode is a bit odd, but works fine. */
WM_exit(C, exit_code);
}
else {
/* Not supported, although it could be made to work if needed. */
BLI_assert(app_state.main_arg_deferred == nullptr);
/* Shows the splash as needed. */
WM_init_splash_on_startup(C);
WM_main(C);
}
/* Neither #WM_exit, #WM_main return, this quiets CLANG's `unreachable-code-return` warning. */
BLI_assert_unreachable();
#endif /* !WITH_PYTHON_MODULE */
return 0;
} /* End of `int main(...)` function. */
#ifdef WITH_PYTHON_MODULE
void main_python_exit()
{
WM_exit_ex((bContext *)evil_C, true, false);
evil_C = nullptr;
}
#endif
/** \} */
|