From 04481d9ef487df5e8c80d38dbf1fdeb4a4f81cb0 Mon Sep 17 00:00:00 2001
From: Andrey Kamaev <andrey.kamaev@itseez.com>
Date: Mon, 19 Nov 2012 15:16:13 +0400
Subject: [PATCH] Fix Android build with clang compiler

---
 3rdparty/libjasper/CMakeLists.txt                  | 1 +
 3rdparty/libjpeg/CMakeLists.txt                    | 1 +
 3rdparty/libtiff/CMakeLists.txt                    | 1 +
 3rdparty/tbb/CMakeLists.txt                        | 5 +++++
 cmake/OpenCVDetectCXXCompiler.cmake                | 2 +-
 modules/contrib/src/detection_based_tracker.cpp    | 2 ++
 modules/core/include/opencv2/core/core.hpp         | 4 ++--
 modules/core/include/opencv2/core/operations.hpp   | 2 +-
 samples/android/tutorial-3-native/jni/jni_part.cpp | 2 ++
 samples/android/tutorial-4-mixed/jni/jni_part.cpp  | 2 ++
 10 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/3rdparty/libjasper/CMakeLists.txt b/3rdparty/libjasper/CMakeLists.txt
index a6fb71ec5..42855e2a6 100644
--- a/3rdparty/libjasper/CMakeLists.txt
+++ b/3rdparty/libjasper/CMakeLists.txt
@@ -24,6 +24,7 @@ if(WIN32 AND NOT MINGW)
 endif(WIN32 AND NOT MINGW)
 
 ocv_warnings_disable(CMAKE_C_FLAGS -Wno-implicit-function-declaration -Wno-uninitialized -Wmissing-prototypes -Wmissing-declarations -Wunused -Wshadow -Wsign-compare)
+ocv_warnings_disable(CMAKE_C_FLAGS -Wunused-parameter) # clang
 ocv_warnings_disable(CMAKE_C_FLAGS /wd4013 /wd4018 /wd4101 /wd4244 /wd4267 /wd4715) # vs2005
 
 if(UNIX)
diff --git a/3rdparty/libjpeg/CMakeLists.txt b/3rdparty/libjpeg/CMakeLists.txt
index 708e63e25..b4000a40a 100644
--- a/3rdparty/libjpeg/CMakeLists.txt
+++ b/3rdparty/libjpeg/CMakeLists.txt
@@ -26,6 +26,7 @@ if(CMAKE_COMPILER_IS_GNUCXX)
 endif()
 
 ocv_warnings_disable(CMAKE_C_FLAGS -Wcast-align -Wshadow -Wunused)
+ocv_warnings_disable(CMAKE_C_FLAGS -Wunused-parameter) # clang
 
 set_target_properties(${JPEG_LIBRARY}
   PROPERTIES OUTPUT_NAME ${JPEG_LIBRARY}
diff --git a/3rdparty/libtiff/CMakeLists.txt b/3rdparty/libtiff/CMakeLists.txt
index 25ff0d94e..46fef61c7 100644
--- a/3rdparty/libtiff/CMakeLists.txt
+++ b/3rdparty/libtiff/CMakeLists.txt
@@ -89,6 +89,7 @@ endif(WIN32)
 
 ocv_warnings_disable(CMAKE_C_FLAGS -Wno-unused-but-set-variable -Wmissing-prototypes -Wmissing-declarations -Wundef -Wunused -Wsign-compare
                                    -Wcast-align -Wshadow -Wno-maybe-uninitialized -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast)
+ocv_warnings_disable(CMAKE_C_FLAGS -Wunused-parameter) # clang
 ocv_warnings_disable(CMAKE_CXX_FLAGS -Wmissing-declarations -Wunused-parameter)
 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4018 /wd4100 /wd4127 /wd4311 /wd4701 /wd4706) # vs2005
 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4244) # vs2008
diff --git a/3rdparty/tbb/CMakeLists.txt b/3rdparty/tbb/CMakeLists.txt
index bf91f4d8b..2f1394999 100644
--- a/3rdparty/tbb/CMakeLists.txt
+++ b/3rdparty/tbb/CMakeLists.txt
@@ -123,6 +123,11 @@ add_definitions(-D__TBB_DYNAMIC_LOAD_ENABLED=0         #required
                 -DDO_ITT_NOTIFY=0                      #it seems that we don't need these notifications
                )
 
+if(ANDROID_COMPILER_IS_CLANG)
+  add_definitions(-D__TBB_GCC_BUILTIN_ATOMICS_PRESENT=1)
+  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wmissing-prototypes)
+endif()
+
 if(tbb_need_GENERIC_DWORD_LOAD_STORE)
   #needed by TBB 4.0 update 1,2; fixed in TBB 4.0 update 3 but it has 2 new problems
   add_definitions(-D__TBB_USE_GENERIC_DWORD_LOAD_STORE=1)
diff --git a/cmake/OpenCVDetectCXXCompiler.cmake b/cmake/OpenCVDetectCXXCompiler.cmake
index 65edf72ed..d8c00cfdd 100644
--- a/cmake/OpenCVDetectCXXCompiler.cmake
+++ b/cmake/OpenCVDetectCXXCompiler.cmake
@@ -54,7 +54,7 @@ endif()
 # Detect GNU version:
 # ----------------------------------------------------------------------------
 if(CMAKE_COMPILER_IS_GNUCXX)
-    execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} --version
+    execute_process(COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
                   OUTPUT_VARIABLE CMAKE_OPENCV_GCC_VERSION_FULL
                   OUTPUT_STRIP_TRAILING_WHITESPACE)
 
diff --git a/modules/contrib/src/detection_based_tracker.cpp b/modules/contrib/src/detection_based_tracker.cpp
index 5deb3b418..a1e5dea4d 100644
--- a/modules/contrib/src/detection_based_tracker.cpp
+++ b/modules/contrib/src/detection_based_tracker.cpp
@@ -1,6 +1,8 @@
 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
 #include "opencv2/contrib/detection_based_tracker.hpp"
 
+#include <pthread.h>
+
 #define DEBUGLOGS 1
 
 #ifdef ANDROID
diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp
index 561d26b3a..0454cf4e9 100644
--- a/modules/core/include/opencv2/core/core.hpp
+++ b/modules/core/include/opencv2/core/core.hpp
@@ -204,11 +204,11 @@ CV_EXPORTS ErrorCallback redirectError( ErrorCallback errCallback,
 #ifdef __GNUC__
 #define CV_Error( code, msg ) cv::error( cv::Exception(code, msg, __func__, __FILE__, __LINE__) )
 #define CV_Error_( code, args ) cv::error( cv::Exception(code, cv::format args, __func__, __FILE__, __LINE__) )
-#define CV_Assert( expr ) if((expr)) ; else cv::error( cv::Exception(CV_StsAssert, #expr, __func__, __FILE__, __LINE__) )
+#define CV_Assert( expr ) if(!!(expr)) ; else cv::error( cv::Exception(CV_StsAssert, #expr, __func__, __FILE__, __LINE__) )
 #else
 #define CV_Error( code, msg ) cv::error( cv::Exception(code, msg, "", __FILE__, __LINE__) )
 #define CV_Error_( code, args ) cv::error( cv::Exception(code, cv::format args, "", __FILE__, __LINE__) )
-#define CV_Assert( expr ) if((expr)) ; else cv::error( cv::Exception(CV_StsAssert, #expr, "", __FILE__, __LINE__) )
+#define CV_Assert( expr ) if(!!(expr)) ; else cv::error( cv::Exception(CV_StsAssert, #expr, "", __FILE__, __LINE__) )
 #endif
 
 #ifdef _DEBUG
diff --git a/modules/core/include/opencv2/core/operations.hpp b/modules/core/include/opencv2/core/operations.hpp
index 847daf8c2..e3ebe6e67 100644
--- a/modules/core/include/opencv2/core/operations.hpp
+++ b/modules/core/include/opencv2/core/operations.hpp
@@ -56,7 +56,7 @@
   #define CV_XADD(addr,delta) _InterlockedExchangeAdd(const_cast<void*>(reinterpret_cast<volatile void*>(addr)), delta)
 #elif defined __GNUC__
 
-  #if defined __clang__ && __clang_major__ >= 3
+  #if defined __clang__ && __clang_major__ >= 3 && !defined __ANDROID__
     #ifdef __ATOMIC_SEQ_CST
         #define CV_XADD(addr, delta) __c11_atomic_fetch_add((_Atomic(int)*)(addr), (delta), __ATOMIC_SEQ_CST)
     #else
diff --git a/samples/android/tutorial-3-native/jni/jni_part.cpp b/samples/android/tutorial-3-native/jni/jni_part.cpp
index 2448948d1..fa3c857ab 100644
--- a/samples/android/tutorial-3-native/jni/jni_part.cpp
+++ b/samples/android/tutorial-3-native/jni/jni_part.cpp
@@ -8,6 +8,8 @@ using namespace std;
 using namespace cv;
 
 extern "C" {
+JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3Native_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);
+
 JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3Native_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
 {
     Mat* pMatGr=(Mat*)addrGray;
diff --git a/samples/android/tutorial-4-mixed/jni/jni_part.cpp b/samples/android/tutorial-4-mixed/jni/jni_part.cpp
index 6b0b48ee8..0c395d629 100644
--- a/samples/android/tutorial-4-mixed/jni/jni_part.cpp
+++ b/samples/android/tutorial-4-mixed/jni/jni_part.cpp
@@ -8,6 +8,8 @@ using namespace std;
 using namespace cv;
 
 extern "C" {
+JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4Mixed_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);
+
 JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4Mixed_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
 {
     Mat* pMatGr=(Mat*)addrGray;