g-ir-scanner must set DYLD_LIBRARY_PATH not DYLD_FALLBACK_LIBRARY_PATH
g-ir-scanner (in the function get_internal_link_flags in the file giscanner/ccompiler.py) sets LD_LIBRARY_PATH and DYLD_FALLBACK_LIBRARY_PATH to the list of libpaths passed in to the function. This is wrong in two ways:
First, the variable must be DYLD_LIBRARY_PATH not DYLD_FALLBACK_LIBRARY_PATH. When macOS and other Darwin-based systems look for a library referenced by a program or other library, they do so in this order: 1. in paths mentioned in DYLD_LIBRARY_PATH; 2. in the absolute path recorded in the program or other library; 3. in paths mentioned in DYLD_FALLBACK_LIBRARY_PATH. Imagine that an existing copy of a library, let's say libpoppler, is installed, and now you want to build a new version of libpoppler. By using DYLD_FALLBACK_LIBRARY_PATH when you run g-ir-scanner, you guarantee that the program that g-ir-scanner runs will inadvertently use the older libpoppler that was already installed which is likely missing some new symbols that were added in the new libpoppler and so the program that g-ir-scanner runs will fail due to those missing symbols. See https://trac.macports.org/ticket/58574. You need to use DYLD_LIBRARY_PATH instead so that the newly-built libpoppler will be used by that program even if another copy of libpoppler is already installed in its final destination.
In fact g-ir-scanner used to use DYLD_LIBRARY_PATH. It was changed to DYLD_FALLBACK_LIBRARY_PATH in f3909530 to fix #205 (closed). This fix is incorrect and must be reverted.
Second, there is no point in setting both LD_LIBRARY_PATH and DYLD_LIBRARY_PATH. DYLD_LIBRARY_PATH is only used on Darwin and LD_LIBRARY_PATH is used on non-Darwin UNIX systems. So it would make more sense to set one or the other depending on whether sys.platform is 'darwin'.
After fixing the above, we must then return our attention to #205 (closed) and figure out how to fix it the right way. The problem reported there is that webkit2-gtk 2.14.2 failed to build with this error:
dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /opt/local/lib/libjpeg.9.dylib
webkit isn't a good package to test with since it is so huge and takes so long to build, but I was able to reproduce the problem with a much smaller package, libdazzle 3.36.0, which failed with this error:
dyld: Symbol not found: _sqlite3_intarray_bind
Referenced from: /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData
Expected in: /opt/local/lib/libsqlite3.dylib
I imagine this libsqlite3 error is the same as @tschoonj mentioned in #205 (closed).
Both the webkit and the libdazzle errors are the result of g-ir-scanner putting /opt/local/lib into DYLD_LIBRARY_PATH, which it must not do. It must only put into DYLD_LIBRARY_PATH paths of libraries that were just built and have not yet been installed.
In the case of libdazzle, it is invoking g-ir-scanner like this:
/opt/local/bin/g-ir-scanner -D_REENTRANT -I/opt/local/include/gobject-introspection-1.0 -I/opt/local/lib/libffi-3.2.1/include -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include --no-libtool --namespace=Dazzle --nsversion=1.0 --warn-all --output src/Dazzle-1.0.gir --c-include=dazzle.h --quiet -DDAZZLE_COMPILATION -I/path/to/libdazzle-3.36.0/src -I/path/to/build/src -I./. -I../libdazzle-3.36.0/. -I./src -I../libdazzle-3.36.0/src --filelist=/path/to/build/src/25a6634@@dazzle-1.0@sha/Dazzle_1.0_gir_filelist -L/path/to/build/src --extra-library=dazzle-1.0 --include=Gio-2.0 --include=Gtk-3.0 --symbol-prefix=dzl --identifier-prefix=Dzl --pkg-export=libdazzle-1.0 --cflags-begin -DHAVE_CONFIG_H -DDAZZLE_COMPILATION -Wcast-align -Wdeclaration-after-statement -Werror=address -Werror=array-bounds -Werror=empty-body -Werror=implicit -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=init-self -Werror=int-conversion -Werror=int-to-pointer-cast -Werror=main -Werror=missing-braces -Werror=missing-include-dirs -Werror=nonnull -Werror=overflow -Werror=pointer-arith -Werror=pointer-to-int-cast -Werror=redundant-decls -Werror=return-type -Werror=sequence-point -Werror=shadow -Werror=strict-prototypes -Werror=trigraphs -Werror=undef -Werror=write-strings -Wformat-nonliteral -Werror=format-security -Werror=format=2 -Wignored-qualifiers -Wimplicit-function-declaration -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wnested-externs -Wno-missing-field-initializers -Wno-sign-compare -Wno-unused-parameter -Wold-style-definition -Wpointer-arith -Wredundant-decls -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wuninitialized -Wunused -fno-strict-aliasing -fstack-protector-strong -I./. -I../libdazzle-3.36.0/. -I./src -I../libdazzle-3.36.0/src -D_REENTRANT -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I/opt/local/include/gtk-3.0 -I/opt/local/include/at-spi2-atk/2.0 -I/opt/local/include/at-spi-2.0 -I/opt/local/include/dbus-1.0 -I/opt/local/lib/dbus-1.0/include -I/opt/local/include/gio-unix-2.0/ -I/opt/local/include/cairo -I/opt/local/include/pango-1.0 -I/opt/local/include/harfbuzz -I/opt/local/include/fribidi -I/opt/local/include/atk-1.0 -I/opt/local/include/pixman-1 -I/opt/local/include/ossp -I/opt/local/include/freetype2 -I/opt/local/include/libpng16 -I/opt/local/include/gdk-pixbuf-2.0 -I/opt/local/include --cflags-end --library dazzle-1.0 -L/path/to/build/src -L/opt/local/lib -L/opt/local/lib --extra-library=gio-2.0 --extra-library=gobject-2.0 --extra-library=glib-2.0 --extra-library=intl --extra-library=gmodule-2.0 --extra-library=gtk-3 --extra-library=gdk-3 --extra-library=pangocairo-1.0 --extra-library=pango-1.0 --extra-library=atk-1.0 --extra-library=cairo-gobject --extra-library=cairo --extra-library=gdk_pixbuf-2.0 --extra-library=m --sources-top-dirs /path/to/libdazzle-3.36.0/subprojects/ --sources-top-dirs /path/to/build/subprojects/
So, I see -L/path/to/build/src (twice, which doesn't matter) and -L/opt/local/lib (twice). It appears that g-ir-scanner is adding all of the paths specified with -L and --library-path to LD_LIBRARY_PATH and (now) DYLD_LIBRARY_PATH. I cannot speak for LD_LIBRARY_PATH since I don't use any other UNIX. But on Darwin, DYLD_LIBRARY_PATH must contain (in this case) /path/to/build/src where the just-built library resides so that the just-built library can be found and it must not contain /opt/local/lib since that contains any number of libraries that might collide with those provided by the operating system (and as we can see the copies of libjpeg and libsqlite3 that Apple provides in macOS are sufficiently different from the standard open source versions so as not to be binary compatible). I'm afraid I'm not familiar with the code of gobject-introspection and don't know where or how this distinction between paths needs to be made.