1
2# Copyright (C) 2012-2019 Daniel Scharrer
3#
4# This software is provided 'as-is', without any express or implied
5# warranty.  In no event will the author(s) be held liable for any damages
6# arising from the use of this software.
7#
8# Permission is granted to anyone to use this software for any purpose,
9# including commercial applications, and to alter it and redistribute it
10# freely, subject to the following restrictions:
11#
12# 1. The origin of this software must not be misrepresented; you must not
13#    claim that you wrote the original software. If you use this software
14#    in a product, an acknowledgment in the product documentation would be
15#    appreciated but is not required.
16# 2. Altered source versions must be plainly marked as such, and must not be
17#    misrepresented as being the original software.
18# 3. This notice may not be removed or altered from any source distribution.
19
20# Try to find the iconv library and include path for iconv.h.
21# Once done this will define
22#
23# ICONV_FOUND
24# iconv_INCLUDE_DIR   Where to find iconv.h
25# iconv_LIBRARIES     The libiconv library or empty if none was found
26# iconv_DEFINITIONS   Definitions to use when compiling code that uses iconv
27#
28# An empty iconv_LIBRARIES is not an error as iconv is often included in the system libc.
29#
30# Typical usage could be something like:
31#   find_package(iconv REQUIRED)
32#   include_directories(SYSTEM ${iconv_INCLUDE_DIR})
33#   add_definitions(${iconv_DEFINITIONS})
34#   ...
35#   target_link_libraries(myexe ${iconv_LIBRARIES})
36#
37# The following additional options can be defined before the find_package() call:
38# iconv_USE_STATIC_LIBS  Statically link against libiconv (default: OFF)
39
40include(UseStaticLibs)
41
42foreach(static IN ITEMS 1 0)
43
44	if(static)
45		use_static_libs(iconv)
46	endif()
47
48	if(APPLE)
49		# Prefer local iconv.h location over the system iconv.h location as /opt/local/include
50		# may be added to the include path by other libraries, resulting in the #include
51		# statements finding the local copy while we will link agains the system lib.
52		# This way we always find both include file and library in /opt/local/ if there is one.
53		find_path(iconv_INCLUDE_DIR iconv.h
54			PATHS /opt/local/include
55			DOC "The directory where iconv.h resides"
56			NO_CMAKE_SYSTEM_PATH
57		)
58	endif(APPLE)
59
60	find_path(iconv_INCLUDE_DIR iconv.h
61		PATHS /opt/local/include
62		DOC "The directory where iconv.h resides"
63	)
64	mark_as_advanced(iconv_INCLUDE_DIR)
65
66	# Prefer libraries in the same prefix as the include files
67	string(REGEX REPLACE "(.*)/include/?" "\\1" iconv_BASE_DIR ${iconv_INCLUDE_DIR})
68
69	find_library(iconv_LIBRARY iconv libiconv
70		HINTS "${iconv_BASE_DIR}/lib"
71		PATHS /opt/local/lib
72		DOC "The iconv library"
73	)
74	mark_as_advanced(iconv_LIBRARY)
75
76	if(static)
77		use_static_libs_restore()
78	endif()
79
80	if(iconv_LIBRARY OR STRICT_USE)
81		break()
82	endif()
83
84endforeach()
85
86set(iconv_DEFINITIONS)
87if(WIN32 AND iconv_USE_STATIC_LIBS)
88	set(iconv_DEFINITIONS -DLIBICONV_STATIC -DUSING_STATIC_LIBICONV)
89endif()
90
91# handle the QUIETLY and REQUIRED arguments and set iconv_FOUND to TRUE if
92# all listed variables are TRUE
93include(FindPackageHandleStandardArgs)
94find_package_handle_standard_args(iconv DEFAULT_MSG iconv_INCLUDE_DIR)
95
96# For some reason, find_package_... uppercases it's first argument. Nice!
97if(ICONV_FOUND)
98	set(iconv_LIBRARIES)
99	if(iconv_LIBRARY)
100		list(APPEND iconv_LIBRARIES ${iconv_LIBRARY})
101	endif()
102endif(ICONV_FOUND)
103