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