Skip to content

Commit 6a4cc03

Browse files
authored
Merge pull request #11263 from Calinou/custom-modules-in-cpp-remove-dynamic-linking
Remove outdated/broken shared library section in Custom modules in C++
2 parents 8ad6d1c + 160aa37 commit 6a4cc03

File tree

1 file changed

+11
-114
lines changed

1 file changed

+11
-114
lines changed

engine_details/architecture/custom_modules_in_cpp.rst

Lines changed: 11 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ split for use and reuse in different modules.
1313

1414
Modules are located in the ``modules/`` subdirectory of the build system.
1515
By default, dozens of modules are enabled, such as GDScript (which, yes,
16-
is not part of the base engine), the Mono runtime, a regular expressions
16+
is not part of the base engine), GridMap support, a regular expressions
1717
module, and others. As many new modules as desired can be
1818
created and combined. The SCons build system will take care of it
1919
transparently.
@@ -31,6 +31,16 @@ instead. Adding C++ modules can be useful in the following scenarios:
3131
- Porting an existing game to Godot.
3232
- Write a whole, new game in C++ because you can't live without C++.
3333

34+
35+
.. note::
36+
37+
While it is possible to use modules for custom game logic,
38+
:ref:`GDExtension <doc_gdextension>` is generally more suited as it doesn't
39+
require recompiling the engine after every code change.
40+
41+
C++ modules are mainly needed when GDExtension doesn't suffice and deeper engine
42+
integration is required.
43+
3444
Creating a new module
3545
---------------------
3646

@@ -378,119 +388,6 @@ We now need to add this method to ``register_types`` header and source files:
378388
// Nothing to do here in this example.
379389
}
380390
381-
Improving the build system for development
382-
------------------------------------------
383-
384-
.. warning::
385-
386-
This shared library support is not designed to support distributing a module
387-
to other users without recompiling the engine. For that purpose, use
388-
a GDExtension instead.
389-
390-
So far, we defined a clean SCsub that allows us to add the sources
391-
of our new module as part of the Godot binary.
392-
393-
This static approach is fine when we want to build a release version of our
394-
game, given we want all the modules in a single binary.
395-
396-
However, the trade-off is that every single change requires a full recompilation of the
397-
game. Even though SCons is able to detect and recompile only the file that was
398-
changed, finding such files and eventually linking the final binary takes a long time.
399-
400-
The solution to avoid such a cost is to build our own module as a shared
401-
library that will be dynamically loaded when starting our game's binary.
402-
403-
.. code-block:: python
404-
:caption: godot/modules/summator/SCsub
405-
406-
Import('env')
407-
408-
sources = [
409-
"register_types.cpp",
410-
"summator.cpp"
411-
]
412-
413-
# First, create a custom env for the shared library.
414-
module_env = env.Clone()
415-
416-
# Position-independent code is required for a shared library.
417-
module_env.Append(CCFLAGS=['-fPIC'])
418-
419-
# Don't inject Godot's dependencies into our shared library.
420-
module_env['LIBS'] = []
421-
422-
# Define the shared library. By default, it would be built in the module's
423-
# folder, however it's better to output it into `bin` next to the
424-
# Godot binary.
425-
shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)
426-
427-
# Finally, notify the main build environment it now has our shared library
428-
# as a new dependency.
429-
430-
# LIBPATH and LIBS need to be set on the real "env" (not the clone)
431-
# to link the specified libraries to the Godot executable.
432-
433-
env.Append(LIBPATH=['#bin'])
434-
435-
# SCons wants the name of the library with it custom suffixes
436-
# (e.g. ".linuxbsd.tools.64") but without the final ".so".
437-
shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
438-
env.Append(LIBS=[shared_lib_shim])
439-
440-
Once compiled, we should end up with a ``bin`` directory containing both the
441-
``godot*`` binary and our ``libsummator*.so``. However given the .so is not in
442-
a standard directory (like ``/usr/lib``), we have to help our binary find it
443-
during runtime with the ``LD_LIBRARY_PATH`` environment variable:
444-
445-
.. code-block:: shell
446-
447-
export LD_LIBRARY_PATH="$PWD/bin/"
448-
./bin/godot*
449-
450-
.. note::
451-
You have to ``export`` the environment variable. Otherwise,
452-
you won't be able to run your project from the editor.
453-
454-
On top of that, it would be nice to be able to select whether to compile our
455-
module as shared library (for development) or as a part of the Godot binary
456-
(for release). To do that we can define a custom flag to be passed to SCons
457-
using the ``ARGUMENT`` command:
458-
459-
.. code-block:: python
460-
:caption: godot/modules/summator/SCsub
461-
462-
Import('env')
463-
464-
sources = [
465-
"register_types.cpp",
466-
"summator.cpp"
467-
]
468-
469-
module_env = env.Clone()
470-
module_env.Append(CCFLAGS=['-O2'])
471-
472-
if ARGUMENTS.get('summator_shared', 'no') == 'yes':
473-
# Shared lib compilation
474-
module_env.Append(CCFLAGS=['-fPIC'])
475-
module_env['LIBS'] = []
476-
shared_lib = module_env.SharedLibrary(target='#bin/summator', source=sources)
477-
shared_lib_shim = shared_lib[0].name.rsplit('.', 1)[0]
478-
env.Append(LIBS=[shared_lib_shim])
479-
env.Append(LIBPATH=['#bin'])
480-
else:
481-
# Static compilation
482-
module_env.add_source_files(env.modules_sources, sources)
483-
484-
Now by default ``scons`` command will build our module as part of Godot's binary
485-
and as a shared library when passing ``summator_shared=yes``.
486-
487-
Finally, you can even speed up the build further by explicitly specifying your
488-
shared module as target in the SCons command:
489-
490-
.. code-block:: shell
491-
492-
scons summator_shared=yes platform=linuxbsd bin/libsummator.linuxbsd.tools.64.so
493-
494391
Writing custom documentation
495392
----------------------------
496393

0 commit comments

Comments
 (0)