Some packages are not yet ready for this. There are two demands: the install function of a library must be called install_<libname> and the Prolog side must use use_foreign_library/1 as
:- use_foreign_library(foreign(libname)).
Some packages (still) name the install function simply install(), but this obviously cannot work as you get a name conflict.
There may be other problems Only working on the Emscripten version with this option. PRs welcome
After fixing some install_<name> naming problems and replacing 'autoload(…)withuse_foreign_library(foreign(libname))` I am getting some errors:
[..]
ERROR: /home/u/tmp/swipl-devel/build.static/home/xpce/prolog/boot/pce_principal.pl:71:
ERROR: '$activate_static_extension'/1: foreign_extension `shlib' does not exist
After commenting out autoload(library(shlib),[load_foreign_library/1]) I am left with these errors:
5/167] Generating lib/shlib.tex
FAILED: man/lib/shlib.tex man/lib/summaries.d/shlib.tex /tmp/swipl-devel/build.static/man/lib/shlib.tex /tmp/swipl-devel/build.static/man/lib/summaries.d/shlib.tex
cd /tmp/swipl-devel/build.static/man && /tmp/swipl-devel/build.static/src/swipl -f none --no-packs -x /tmp/swipl-devel/build.static/man/pldoc2tex -- --source=/tmp/swipl-devel/man --out=lib/shlib.tex --subsection --summaries "library(shlib)"
ERROR: /tmp/swipl-devel/man/pldoc2tex.pl:48: pltotex:main source_sink `library(shlib)' does not exist
[14/167] Generating lib/readutil.tex
FAILED: man/lib/readutil.tex man/lib/summaries.d/readutil.tex /tmp/swipl-devel/build.static/man/lib/readutil.tex /tmp/swipl-devel/build.static/man/lib/summaries.d/readutil.tex
cd /tmp/swipl-devel/build.static/man && /tmp/swipl-devel/build.static/src/swipl -f none --no-packs -x /tmp/swipl-devel/build.static/man/pldoc2tex -- --source=/tmp/swipl-devel/man --out=lib/readutil.tex --summaries "library(readutil)"
Warning: /tmp/swipl-devel/build.static/home/library/readutil.pl:89:
Warning: /tmp/swipl-devel/build.static/home/library/prolog_xref.pl:86:
Warning: library(shlib): No such file
ERROR: /tmp/swipl-devel/build.static/home/library/readutil.pl:89:
ERROR: prolog_xref:process_foreign/2: Unknown procedure: prolog_xref:current_foreign_library/2
Warning: Halting with status 1 due to 1 errors and 1 warnings
ninja: build stopped: subcommand failed.
obviously library(shlib) won’t be used for static binary, so we need some kind of conditional compile time flag that we can use like this:
:- if (...dynamic_binary...).
:- use_foreign_library(foreign(shlib)).
:- endif
we can use this flag for prolog_xref when it calls `current_foreign_library/2? or better, current_foreign_library/2 should work with static extensions linked in the executable?
Good question. Some of this can probably be resolved by avoiding the use of old stuff from library(shlib). I’m also in doubt whether using this should imply complete static linking on all platforms ort it should still allow for dynamic linking and only statically link the extensions? That would give two flags, one to decide whether or not to include the extensions statically and one to decide whether or not to support dynamic linking (if the platform allows for it).
I’ll push a few commits to ltx2htm and bdb packages to fix some of this.
I think the most useful use case is complete static linking (even allowing the user to statically link in some packs that use foreign modules) for unix like systems. This, by the way, may make it easier to embed a static swi-prolog in an android apk, making it much more easy to move forward with android apps.
Windows has no need of static linking since it is very good at binary compatibility already, so no need for static linking on windows.
Good points. For now I’ve pushed some changes that make the system compile out of the box on MacOS (I’m traveling) using -DSTATIC_EXTENSIONS. JPL is disabled (needs dynamic linkng). All tests pass and fthe result is a single executable that only depends on the system shared libs
xpce works for me. Whether or not to link the executable dynamically or statically against the system libraries is another decision. Fully static linking should not be hard. Not sure how useful this is, but it is good to know that dynamic linking is no longer really needed and for WASM this seems the preferred route.
Attaching user packs is another matter. For the whole thing to work the extension needs to use the cmake infrastructure that is used for the bundled extensions. That requires following much more strict conventions.
I did a ninja clean && ninja, now xpce works fine.
The only strange thing I noticed is that libpcre is linked twice, both the new libpcre2 and the old libpcre are linked. Wonder why?
There are two use cases where I see static linking as useful:
Android. Full static linking would be specially useful to run swi-prolog in Android. You could compile swi-prolog in some ARM compatible board and then include the static binary in your java Android app to run it on a phone. They could communicate via sockets, with the Java android app providing the gui (maybe just a webview and swiprolog acting as a web server) and the prolog code doing all the rest.
The static linking would shield swi-prolog from most of the non-standard changes they have made in Android, because the binary would be simply making direct system calls to the linux kernel without dependency on anything else. As long as they have not blocked/changed kernel system calls the binary should work. I have done this with a statically linked Ada executable and it works well.
Stable deployment of prolog executables. A fully statically linked binary would be very useful for deploying single binary prolog applications, without worrying about breaking things because we installed a new version of SWI prolog. I have single file prolog saved states that I run and some times I have to recompile them because of a WAM/VM change in libswipl.so or some such change. This would not be needed at all if I just used the static binary, and I could install the cutting-edge version of SWI-Prolog while all my saved states and prolog applications would continue to work without trouble.
What is needed to convert a user made pack into an extension? Probably use cmake, but what else would be needed?
No clue. Not on my Mac … Also the CMakeCache.txt only shows 2-8,
Not much. The dependencies and api to the Prolog system are the same. If you dump the Prolog and C/C++ files in a subdir of packages and add a cmake config it should work. Actually, you could simply maintain the layout of the pack and add a CMakeLists.txt at the top. The only issue is that pack_install/1 also triggers on a cmake config, but in a quite different way. That could be changed using some naming convention, I guess.