···11{ fetchurl, stdenv, lib
22, enableStatic ? stdenv.hostPlatform.isStatic
33, enableShared ? !stdenv.hostPlatform.isStatic
44+, enableDarwinABICompat ? false
45}:
5667# assert !stdenv.hostPlatform.isLinux || stdenv.hostPlatform != stdenv.buildPlatform; # TODO: improve on cross
···2829 ''
2930 + lib.optionalString (!enableShared) ''
3031 sed -i -e '/preload/d' Makefile.in
3232+ ''
3333+ # The system libiconv is based on libiconv 1.11 with some ABI differences. The following changes
3434+ # build a compatible libiconv on Darwin, allowing it to be sustituted in place of the system one
3535+ # using `install_name_tool`. This removes the need to for a separate, Darwin-specific libiconv
3636+ # derivation and allows Darwin to benefit from upstream updates and fixes.
3737+ + lib.optionalString enableDarwinABICompat ''
3838+ for iconv_h_in in iconv.h.in iconv.h.build.in; do
3939+ substituteInPlace "include/$iconv_h_in" \
4040+ --replace "#define iconv libiconv" "" \
4141+ --replace "#define iconv_close libiconv_close" "" \
4242+ --replace "#define iconv_open libiconv_open" "" \
4343+ --replace "#define iconv_open_into libiconv_open_into" "" \
4444+ --replace "#define iconvctl libiconvctl" "" \
4545+ --replace "#define iconvlist libiconvlist" ""
4646+ done
3147 '';
4848+4949+ # This is hacky, but `libiconv.dylib` needs to reexport `libcharset.dylib` to match the behavior
5050+ # of the system libiconv on Darwin. Trying to do this by modifying the `Makefile` results in an
5151+ # error linking `iconv` because `libcharset.dylib` is not at its final path yet. Avoid the error
5252+ # by building without the reexport then clean and rebuild `libiconv.dylib` with the reexport.
5353+ #
5454+ # For an explanation why `libcharset.dylib` is reexported, see:
5555+ # https://github.com/apple-oss-distributions/libiconv/blob/a167071feb7a83a01b27ec8d238590c14eb6faff/xcodeconfig/libiconv.xcconfig
5656+ postBuild = lib.optionalString enableDarwinABICompat ''
5757+ make clean -C lib
5858+ NIX_CFLAGS_COMPILE+=" -Wl,-reexport-lcharset -L. " make -C lib -j$NIX_BUILD_CORES SHELL=$SHELL
5959+ '';
32603361 configureFlags = [
3462 (lib.enableFeature enableStatic "static")