11cb0ef41Sopenharmony_ci# Maintaining shared library support 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciNode.js unofficially supports a build option where Node.js is built as 41cb0ef41Sopenharmony_cia shared library. The shared library is called libnode with additional postfixes 51cb0ef41Sopenharmony_cias appropriate for the platform (for example libnode.dll on windows). 61cb0ef41Sopenharmony_ciThe shared library provides a way to embed Node.js into other 71cb0ef41Sopenharmony_ciapplications and to have multiple applications use a single copy of 81cb0ef41Sopenharmony_ciNode.js instead of having to bundle in the full Node.js footprint 91cb0ef41Sopenharmony_ciinto each application. For workloads that require multiple Node.js 101cb0ef41Sopenharmony_ciinstances this can result in a significant footprint savings. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciThis document provides an outline of the approach and things to look 131cb0ef41Sopenharmony_ciout for when maintaining the shared library support. 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciCurrently, shared library support has only been tested on: 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci* Linux 181cb0ef41Sopenharmony_ci* macOS 191cb0ef41Sopenharmony_ci* Windows 201cb0ef41Sopenharmony_ci* AIX 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci## Building with shared library option 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciOn non-Windows platforms, Node.js is built with the shared library 251cb0ef41Sopenharmony_cioption by adding `--shared` to the configure step. On Windows 261cb0ef41Sopenharmony_ciplatforms Node.js is built with the shared library option by 271cb0ef41Sopenharmony_ciadding `dll` to the vcbuild command line. 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciOnce built there are two key components: 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci* executable - node 321cb0ef41Sopenharmony_ci* library - libnode 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciThe node executable is a thin wrapper around libnode which is 351cb0ef41Sopenharmony_cigenerated so that we can run the standard Node.js test suite 361cb0ef41Sopenharmony_ciagainst the shared library. 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciThe executable and library will have extensions as appropriate 391cb0ef41Sopenharmony_cifor the platform on which they are built. For 401cb0ef41Sopenharmony_ciexample, node.exe on windows and node on other platforms for 411cb0ef41Sopenharmony_cithe executable. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_cilibnode may have additional naming components, as an example 441cb0ef41Sopenharmony_ciin a build on macOS `libnode.105.dylib`. For non-windows platforms 451cb0ef41Sopenharmony_cithe additional naming components include the `NODE_MODULE_VERSION` and 461cb0ef41Sopenharmony_cithe appropriate postfix used for shared libraries on the platform. 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ciIn cases where an application links against the shared 491cb0ef41Sopenharmony_cilibrary it is up to the application developer to add options 501cb0ef41Sopenharmony_ciso that the shared library can be found by the application or 511cb0ef41Sopenharmony_cito set the LIBPATH (AIX), LD\_LIBRARY\_PATH (Linux/Unix), etc. 521cb0ef41Sopenharmony_ciso that it is found at runtime. 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciFor the node wrapper, on linux and macOS it is built 551cb0ef41Sopenharmony_ciso that it can find the shared library in one of 561cb0ef41Sopenharmony_cithe following: 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci* the same directory as the node executable 591cb0ef41Sopenharmony_ci* ../lib with the expectation that the executable is 601cb0ef41Sopenharmony_ci installed in a `bin` directory at the same level 611cb0ef41Sopenharmony_ci as a `lib` directory in which the shared library is 621cb0ef41Sopenharmony_ci installed. This is where the default package that 631cb0ef41Sopenharmony_ci is build with the shared library option will 641cb0ef41Sopenharmony_ci place the executable and library. 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciFor the node wrapper on windows it is built expecting 671cb0ef41Sopenharmony_cithat both the executable and shared library will 681cb0ef41Sopenharmony_cibe in the same directory as it common practice on 691cb0ef41Sopenharmony_cithat platform. 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciFor the node wrapper on AIX, it is built with 721cb0ef41Sopenharmony_cithe path to the shared library hardcoded as that 731cb0ef41Sopenharmony_ciis the only option. 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci## Exports 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciOn windows, functions that may be linked from native 781cb0ef41Sopenharmony_ciaddons or additional Node.js executables need to have 791cb0ef41Sopenharmony_ciNODE\_EXTERN\_PRIVATE or NODE\_EXTERN otherwise they will 801cb0ef41Sopenharmony_cinot be exported by the shared library. In the case of 811cb0ef41Sopenharmony_cifunctions used by additional Node.js executables 821cb0ef41Sopenharmony_ci(ex: `mksnapshot`) a missing NODE\_EXTERN or 831cb0ef41Sopenharmony_ciNODE\_EXTERN\_PRIVATE will cause the build to fail. 841cb0ef41Sopenharmony_ciNODE\_EXTERN\_PRIVATE should be used in these cases 851cb0ef41Sopenharmony_ciunless the intent is to add the function to the 861cb0ef41Sopenharmony_cipublic embedder API. 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci## Native addons 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ciFor regular Node.js builds, running native addons relies on symbols 911cb0ef41Sopenharmony_ciexported by the node executable. As a result any 921cb0ef41Sopenharmony_cipre-built binaries expect symbols to be exported from the executable 931cb0ef41Sopenharmony_ciinstead of the shared library itself. 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ciThe node executable and shared library are built and linked 961cb0ef41Sopenharmony_ciso that the required symbols are exported from the node 971cb0ef41Sopenharmony_ciexecutable. This requires some extra work on some platforms 981cb0ef41Sopenharmony_ciand the process to build the node executable is a good example 991cb0ef41Sopenharmony_ciof how this can be achieved. Applications that use the shared 1001cb0ef41Sopenharmony_cilibrary and want to support native addons should employ 1011cb0ef41Sopenharmony_cia similar technique. 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci## Testing 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ciThere is currently no testing in the regular CI run for PRs. There 1061cb0ef41Sopenharmony_ciare some CI jobs that can be used to test the shared library support and 1071cb0ef41Sopenharmony_cisome are run as part of daily testing. These include: 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci* [node-test-commit-linux-as-shared-lib](https://ci.nodejs.org/view/Node.js%20Daily/job/node-test-commit-linux-as-shared-lib/) 1101cb0ef41Sopenharmony_ci* <https://ci.nodejs.org/view/All/job/node-test-commit-osx-as-shared-lib/> 1111cb0ef41Sopenharmony_ci* [node-test-commit-aix-shared-lib](https://ci.nodejs.org/view/Node.js%20Daily/job/node-test-commit-aix-shared-lib/) 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ciTODO: add a Job for windows 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ciFor code that modifies/affects shared library support these CI jobs should 1161cb0ef41Sopenharmony_cirun and it should be validated that there are no regressions in 1171cb0ef41Sopenharmony_cithe test suite. 118