···1{ fetchurl, stdenv, lib
2, enableStatic ? stdenv.hostPlatform.isStatic
3, enableShared ? !stdenv.hostPlatform.isStatic
4+, enableDarwinABICompat ? false
5}:
67# assert !stdenv.hostPlatform.isLinux || stdenv.hostPlatform != stdenv.buildPlatform; # TODO: improve on cross
···29 ''
30 + lib.optionalString (!enableShared) ''
31 sed -i -e '/preload/d' Makefile.in
32+ ''
33+ # The system libiconv is based on libiconv 1.11 with some ABI differences. The following changes
34+ # build a compatible libiconv on Darwin, allowing it to be sustituted in place of the system one
35+ # using `install_name_tool`. This removes the need to for a separate, Darwin-specific libiconv
36+ # derivation and allows Darwin to benefit from upstream updates and fixes.
37+ + lib.optionalString enableDarwinABICompat ''
38+ for iconv_h_in in iconv.h.in iconv.h.build.in; do
39+ substituteInPlace "include/$iconv_h_in" \
40+ --replace "#define iconv libiconv" "" \
41+ --replace "#define iconv_close libiconv_close" "" \
42+ --replace "#define iconv_open libiconv_open" "" \
43+ --replace "#define iconv_open_into libiconv_open_into" "" \
44+ --replace "#define iconvctl libiconvctl" "" \
45+ --replace "#define iconvlist libiconvlist" ""
46+ done
47 '';
48+49+ # This is hacky, but `libiconv.dylib` needs to reexport `libcharset.dylib` to match the behavior
50+ # of the system libiconv on Darwin. Trying to do this by modifying the `Makefile` results in an
51+ # error linking `iconv` because `libcharset.dylib` is not at its final path yet. Avoid the error
52+ # by building without the reexport then clean and rebuild `libiconv.dylib` with the reexport.
53+ #
54+ # For an explanation why `libcharset.dylib` is reexported, see:
55+ # https://github.com/apple-oss-distributions/libiconv/blob/a167071feb7a83a01b27ec8d238590c14eb6faff/xcodeconfig/libiconv.xcconfig
56+ postBuild = lib.optionalString enableDarwinABICompat ''
57+ make clean -C lib
58+ NIX_CFLAGS_COMPILE+=" -Wl,-reexport-lcharset -L. " make -C lib -j$NIX_BUILD_CORES SHELL=$SHELL
59+ '';
6061 configureFlags = [
62 (lib.enableFeature enableStatic "static")