6.22. Using iconv

After 2013-10-08 (254273), FreeBSD  10-CURRENT and newer versions have a native iconv in the operating system. On earlier versions, converters/libiconv was used as a dependency.

For software that needs iconv, define USES=iconv. FreeBSD versions before 10-CURRENT on 2013-08-13 (254273) do not have a native iconv. On these earlier versions, a dependency on converters/libiconv will be added automatically.

When a port defines USES=iconv, these variables will be available:

Variable namePurposeValue before FreeBSD 10-CURRENT 254273 (2013-08-13)Value after FreeBSD 10-CURRENT 254273 (2013-08-13)
ICONV_CMDDirectory where the iconv binary resides${LOCALBASE}/bin/iconv/usr/bin/iconv
ICONV_LIBld argument to link to libiconv (if needed)-liconv(empty)
ICONV_PREFIXDirectory where the iconv implementation resides (useful for configure scripts)${LOCALBASE}/usr
ICONV_CONFIGURE_ARGPreconstructed configure argument for configure scripts--with-libiconv-prefix=${LOCALBASE}(empty)
ICONV_CONFIGURE_BASEPreconstructed configure argument for configure scripts--with-libiconv=${LOCALBASE}(empty)

These two examples automatically populate the variables with the correct value for systems using converters/libiconv or the native iconv respectively:

Example 6.14. Simple iconv Usage
USES=		iconv
LDFLAGS+=	-L${LOCALBASE}/lib ${ICONV_LIB}

Example 6.15. iconv Usage with configure
USES=		iconv
CONFIGURE_ARGS+=${ICONV_CONFIGURE_ARG}

As shown above, ICONV_LIB is empty when a native iconv is present. This can be used to detect the native iconv and respond appropriately.

Sometimes a program has an ld argument or search path hardcoded in a Makefile or configure script. This approach can be used to solve that problem:

Example 6.16. Fixing Hardcoded -liconv
USES=		iconv

post-patch:
	@${REINPLACE_CMD} -e 's/-liconv/${ICONV_LIB}/' ${WRKSRC}/Makefile

In some cases it is necessary to set alternate values or perform operations depending on whether there is a native iconv. bsd.port.pre.mk must be included before testing the value of ICONV_LIB:

Example 6.17. Checking for Native iconv Availability
USES=		iconv

.include <bsd.port.pre.mk>

post-patch:
.if empty(ICONV_LIB)
	# native iconv detected
	@${REINPLACE_CMD} -e 's|iconv||' ${WRKSRC}/Config.sh
.endif

.include <bsd.port.post.mk>

All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/doc/

Questions that are not answered by the documentation may be sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.