Commit 726b75d1b839649ee118e5521f52c669f4664d45

Russell Belfer 2013-12-02T11:32:37

Improve iconv finding for cmake * add FindIconv helper for CMake iconv detection * only default using iconv to ON for MacOS * update pkg-config generation to include iconv dependency better

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ac1032a..9c19a5a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,7 +34,7 @@ OPTION( ANDROID				"Build for android NDK"	 				OFF )
 OPTION( USE_ICONV			"Link with and use iconv library" 		OFF )
 OPTION( USE_SSH				"Link with libssh to enable SSH support" ON )
 
-IF(APPLE)
+IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
 	SET( USE_ICONV ON )
 ENDIF()
 
@@ -82,12 +82,6 @@ FUNCTION(TARGET_OS_LIBRARIES target)
 		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lrt" PARENT_SCOPE)
 	ENDIF()
 
-	IF(USE_ICONV)
-		TARGET_LINK_LIBRARIES(${target} iconv)
-		ADD_DEFINITIONS(-DGIT_USE_ICONV)
-		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -liconv" PARENT_SCOPE)
-	ENDIF()
-
 	IF(THREADSAFE)
 		TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
 	ENDIF()
@@ -190,6 +184,7 @@ ELSE()
 	FILE(GLOB SRC_ZLIB deps/zlib/*.c deps/zlib/*.h)
 ENDIF()
 
+# Optional external dependency: libssh2
 IF (USE_SSH AND NOT MINGW)
 	FIND_PACKAGE(LIBSSH2 QUIET)
 ENDIF()
@@ -200,6 +195,18 @@ IF (LIBSSH2_FOUND)
 	SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
 ENDIF()
 
+# Optional external dependency: iconv
+IF (USE_ICONV)
+	FIND_PACKAGE(ICONV QUIET)
+ENDIF()
+IF (ICONV_FOUND)
+	ADD_DEFINITIONS(-DGIT_USE_ICONV)
+	IF(ICONV_LIBRARIES MATCHES "libiconv")
+		SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -liconv")
+	ELSE()
+		SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} iconv")
+	ENDIF()
+ENDIF()
 
 # Platform specific compilation flags
 IF (MSVC)
@@ -353,6 +360,7 @@ ENDIF()
 ADD_LIBRARY(git2 ${SRC_H} ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
 TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
 TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES})
+TARGET_LINK_LIBRARIES(git2 ${ICONV_LIBRARIES})
 TARGET_OS_LIBRARIES(git2)
 
 # Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
@@ -418,6 +426,7 @@ IF (BUILD_CLAR)
 
 	TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
 	TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES})
+	TARGET_LINK_LIBRARIES(libgit2_clar ${ICONV_LIBRARIES})
 	TARGET_OS_LIBRARIES(libgit2_clar)
 	MSVC_SPLIT_SOURCES(libgit2_clar)
 
diff --git a/cmake/Modules/FindIconv.cmake b/cmake/Modules/FindIconv.cmake
new file mode 100644
index 0000000..fb6d1e2
--- /dev/null
+++ b/cmake/Modules/FindIconv.cmake
@@ -0,0 +1,42 @@
+# - Try to find Iconv
+# Once done this will define
+#
+# ICONV_FOUND - system has Iconv
+# ICONV_INCLUDE_DIR - the Iconv include directory
+# ICONV_LIBRARIES - Link these to use Iconv
+# ICONV_SECOND_ARGUMENT_IS_CONST - the second argument for iconv() is const
+#
+
+IF (ICONV_INCLUDE_DIR AND ICONV_LIBRARIES)
+	# Already in cache, be silent
+	SET(ICONV_FIND_QUIETLY TRUE)
+ENDIF (ICONV_INCLUDE_DIR AND ICONV_LIBRARIES)
+
+FIND_PATH(ICONV_INCLUDE_DIR iconv.h)
+
+FIND_LIBRARY(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c PATH)
+
+IF(ICONV_INCLUDE_DIR AND ICONV_LIBRARIES)
+	 SET(ICONV_FOUND TRUE)
+ENDIF(ICONV_INCLUDE_DIR AND ICONV_LIBRARIES)
+
+set(CMAKE_REQUIRED_INCLUDES ${ICONV_INCLUDE_DIR})
+set(CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARIES})
+set(CMAKE_REQUIRED_INCLUDES)
+set(CMAKE_REQUIRED_LIBRARIES)
+
+IF(ICONV_FOUND)
+	IF(NOT ICONV_FIND_QUIETLY)
+		MESSAGE(STATUS "Found Iconv: ${ICONV_LIBRARIES}")
+	ENDIF(NOT ICONV_FIND_QUIETLY)
+ELSE(ICONV_FOUND)
+	IF(Iconv_FIND_REQUIRED)
+		MESSAGE(FATAL_ERROR "Could not find Iconv")
+	ENDIF(Iconv_FIND_REQUIRED)
+ENDIF(ICONV_FOUND)
+
+MARK_AS_ADVANCED(
+	ICONV_INCLUDE_DIR
+	ICONV_LIBRARIES
+	ICONV_SECOND_ARGUMENT_IS_CONST
+)