CMake: Fix compatibility with CMake 3
[metze/wireshark/wip.git] / CMakeLists.txt
index ff9f307f2ec7639b06b13a5a5a28ac12bdf76ec7..6d06c10c230568af5ef68970f01e1d76a7a3b7aa 100644 (file)
@@ -65,7 +65,7 @@ endif()
 if(WIN32)
        find_package(PowerShell REQUIRED)
 
-       if("${CMAKE_GENERATOR}" MATCHES "Win64")
+       if(${CMAKE_CL_64} OR "${CMAKE_GENERATOR}" MATCHES "Win64")
                set(WIRESHARK_TARGET_PLATFORM win64)
        elseif("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
                set(WIRESHARK_TARGET_PLATFORM win32)
@@ -134,6 +134,9 @@ if(WIN32)
        endif()
 
        # XXX Add a dependency on ${_ws_lib_dir}/current_tag.txt?
+
+       # Head off any attempts to use Cygwin's Python.
+       include(LocatePythonExecutable)
 endif(WIN32)
 
 include(UseCustomIncludes)
@@ -226,6 +229,13 @@ if( NOT CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
        set( DUMPCAP_INSTALL_OPTION )
 endif()
 
+# Always enable -fPIE (or -fPIC). -pie is added below for non-MSVC.
+# Needed when either:
+# - Qt5_POSITION_INDEPENDENT_CODE is set and CMake < 2.8.11
+# - PIE is wanted (-pie) and you want to add -fPIC/-fPIE automatically.
+# This option only has an effect on CMake >= 2.8.9
+set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+
 if( CMAKE_C_COMPILER_ID MATCHES "MSVC")
        if (MSVC10)
                set(MSC_VER_REQUIRED 1600)
@@ -346,7 +356,6 @@ else()
                -fwrapv
                -fno-strict-overflow
                -fno-delete-null-pointer-checks
-               -fPIE
                -Wvla
                -Waddress
                -Wattributes
@@ -440,13 +449,13 @@ else()
 
        if(CMAKE_C_COMPILER_ID MATCHES "Clang")
                set(WIRESHARK_COMMON_FLAGS ${WIRESHARK_COMMON_FLAGS}
-                       #-fcolor-diagnostics
+                       # avoid "argument unused during compilation" warnings
+                       # (for example, when getting the -gsplit-dwarf option or
+                       # when combining -fwrapv with -fno-strict-overflow)
+                       -Qunused-arguments
                )
 
-               # ccache + clang++ can result in "argument unused during
-               # compilation" warnings.
                set(WIRESHARK_CPP_ONLY_FLAGS ${WIRESHARK_CPP_ONLY_FLAGS}
-                       -Qunused-arguments
                )
        else()
                set(WIRESHARK_COMMON_FLAGS ${WIRESHARK_COMMON_FLAGS}
@@ -491,10 +500,14 @@ set( CPP_FLAG_TESTS ${WIRESHARK_COMMON_FLAGS} ${WIRESHARK_CPP_ONLY_FLAGS} )
 include(CheckCCompilerFlag)
 include(CheckCXXCompilerFlag)
 
-if(NOT DISABLE_WERROR AND NOT ENABLE_EXTRA_COMPILER_WARNINGS)
-       check_c_compiler_flag(-Werror WERROR)
+if(ENABLE_STATIC)
+       set(BUILD_SHARED_LIBS 0)
+       set(LINK_MODE_LIB STATIC)
+       set(LINK_MODE_MODULE STATIC)
 else()
-       set(WERROR FALSE)
+       set(BUILD_SHARED_LIBS 1)
+       set(LINK_MODE_LIB SHARED)
+       set(LINK_MODE_MODULE MODULE)
 endif()
 
 # Sigh: Have to use THIS_FLAG instead of ${F} for some reason
@@ -522,6 +535,21 @@ foreach(THIS_FLAG ${CPP_FLAG_TESTS})
 endforeach()
 set(CMAKE_CXX_FLAGS "${ADDED_CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
 
+if(NOT DISABLE_WERROR AND NOT ENABLE_EXTRA_COMPILER_WARNINGS)
+       if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
+               set(WERROR_COMMON_FLAGS "/WX")
+               set(NO_ERROR_DEPRECATED_DECLARATIONS_COMPILE_FLAGS)
+       else()
+               check_c_compiler_flag(-Werror WERROR)
+               if (WERROR)
+                       set(WERROR_COMMON_FLAGS "-Werror")
+                       set(NO_ERROR_DEPRECATED_DECLARATIONS_COMPILE_FLAGS "-Wno-error=deprecated-declarations")
+               endif()
+       endif()
+else()
+       set(WERROR_COMMON_FLAGS FALSE)
+endif()
+
 #
 # Try to have the compiler default to hiding symbols, so that only
 # symbols explicitly exported with WS_DLL_PUBLIC will be visible
@@ -568,11 +596,17 @@ endif()
 if(NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
        set(WIRESHARK_LD_FLAGS
                -Wl,--as-needed
-               -fPIE -pie
                # -flto
                # -fwhopr
                # -fwhole-program
        )
+       # CMAKE_POSITION_INDEPENDENT_CODE is only supported starting with CMake
+       # 2.8.9. Do not add -pie automatically for older versions.
+       if(NOT CMAKE_VERSION VERSION_LESS "2.8.9")
+               set(WIRESHARK_LD_FLAGS ${WIRESHARK_LD_FLAGS}
+                       -pie
+               )
+       endif()
 endif()
 
 include(CheckCLinkerFlag)
@@ -589,16 +623,6 @@ foreach(THIS_FLAG ${WIRESHARK_LD_FLAGS})
        math(EXPR _C "${_C} + 1")
 endforeach()
 
-if(ENABLE_STATIC)
-       set(BUILD_SHARED_LIBS 0)
-       set(LINK_MODE_LIB STATIC)
-       set(LINK_MODE_MODULE STATIC)
-else()
-       set(BUILD_SHARED_LIBS 1)
-       set(LINK_MODE_LIB SHARED)
-       set(LINK_MODE_MODULE MODULE)
-endif()
-
 if(APPLE AND EXISTS /usr/local/opt/gettext)
        # GLib on OS X requires libintl. Homebrew installs gettext (and
        # libintl) in /usr/local/opt/gettext
@@ -688,11 +712,15 @@ endif()
 # GNU crypto
 if(ENABLE_GCRYPT)
        set(PACKAGELIST ${PACKAGELIST} GCRYPT)
+       # Minimum version needed.
+       set(GCRYPT_OPTIONS "1.4.2")
 endif()
 
 # GNU SSL/TLS support
 if(ENABLE_GNUTLS)
        set(PACKAGELIST ${PACKAGELIST} GNUTLS)
+       # Minimum version needed.
+       set(GNUTLS_OPTIONS "2.12.0")
 endif()
 
 # Kerberos
@@ -767,10 +795,10 @@ endif()
 
 set(PROGLIST)
 
-#Sort the package list
+# Sort the package list
 list(SORT PACKAGELIST)
 message(STATUS "Packagelist: ${PACKAGELIST}")
-#Let's loop the package list
+# Let's loop the package list
 foreach(PACKAGE ${PACKAGELIST})
        if(${PACKAGE} STREQUAL "Qt4")
                set(PACKAGE_VAR "QT")
@@ -870,10 +898,56 @@ if(HAVE_LIBZLIB)
        include_directories(BEFORE ${ZLIB_INCLUDE_DIRS})
 endif()
 if (Qt5Widgets_FOUND)
-       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
-       if (Qt5_POSITION_INDEPENDENT_CODE)
-               set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+       #
+       # Qt5CoreConfigExtras.cmake in Qt 5.5.0 sets -fPIC unconditionally.
+       # https://bugreports.qt.io/browse/QTBUG-47942
+       #
+       # If it was added, we remove it, and then check whether it's
+       # necessary the same way we do for autotools, by checking
+       # whether we can compile and link a simple file with just
+       #
+       #       #include <QtCore>
+       #       int main() {}
+       #
+       # (Yes, check_XXX_source_compiles() should be renamed
+       # check_XXX_source_compiles_and_links().)
+       #
+       if ("${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}" MATCHES ".*-fPIC.*")
+               list(REMOVE_ITEM Qt5Widgets_EXECUTABLE_COMPILE_FLAGS "-fPIC")
+               set(CMAKE_REQUIRED_FLAGS ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS})
+               set(CMAKE_REQUIRED_INCLUDES ${Qt5Core_INCLUDE_DIRS})
+               set(CMAKE_REQUIRED_LIBS ${Qt5Core_LIBRARIES})
+               check_cxx_source_compiles(
+                     "#include <QtCore>
+                     int main() {}"
+                   WORKS_WITHOUT_FPIC)
+               if (NOT WORKS_WITHOUT_FPIC)
+                       #
+                       # OK, it won't compile without -fPIC.  Try adding it.
+                       #
+                       set(CMAKE_REQUIRED_FLAGS "-fPIC")
+                       check_cxx_source_compiles(
+                             "#include <QtCore>
+                             int main() {}"
+                           WORKS_WITH_FPIC)
+                       if (NOT WORKS_WITH_FPIC)
+                               #
+                               # It won't build with -fPIC or without -fPIC,
+                               # so we're hosed.
+                               #
+                               message(FATAL_ERROR "Couldn't compile Qt without -fPIC nor with -fPIC")
+                       endif()
+
+                       #
+                       # It compiles with -fPIC, so add it back.
+                       #
+                       list(APPEND Qt5Widgets_EXECUTABLE_COMPILE_FLAGS "-fPIC")
+               endif()
+               set(CMAKE_REQUIRED_FLAGS "")
+               set(CMAKE_REQUIRED_INCLUDES "")
+               set(CMAKE_REQUIRED_LIBS "")
        endif()
+       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
        set (QT_FOUND ON)
        set (QT_LIBRARIES ${Qt5Widgets_LIBRARIES} ${Qt5PrintSupport_LIBRARIES})
        if(Qt5Multimedia_FOUND)
@@ -897,6 +971,10 @@ elseif(QT_FOUND)
        message(STATUS "Qt includes: ${QT_INCLUDE_DIR}")
        message(STATUS "Qt libs: ${QT_LIBRARIES}")
        if(QT_QTMULTIMEDIA_FOUND)
+               include_directories(${QT_QTMULTIMEDIA_INCLUDE_DIR})
+               message(STATUS "QtMultimedia includes: ${QT_INCLUDE_DIR}")
+               set (QT_LIBRARIES ${QT_LIBRARIES} ${QT_QTMULTIMEDIA_LIBRARY})
+               message(STATUS "QtMultimedia libs: ${QT_QTMULTIMEDIA_LIBRARY}")
                # That's the name autofoo uses
                set(QT_MULTIMEDIA_LIB 1)
        endif()
@@ -904,6 +982,7 @@ endif()
 
 message(STATUS "C-Flags: ${CMAKE_C_FLAGS}")
 message(STATUS "CXX-Flags: ${CMAKE_CXX_FLAGS}")
+message(STATUS "Warnings as errors: ${WERROR_COMMON_FLAGS}")
 
 if(APPLE)
        #
@@ -1764,6 +1843,7 @@ if(BUILD_wireshark AND QT_FOUND)
                                        $<$<CONFIG:Debug>:--debug>
                                        $<$<NOT:$<CONFIG:Debug>>:--release>
                                        --no-compiler-runtime
+                                       --verbose 10
                                        "$<TARGET_FILE:wireshark>"
                        )
                        add_dependencies(copy_qt_dlls wireshark)
@@ -2407,11 +2487,11 @@ set(CLEAN_FILES
        ${androiddump_FILES}
 )
 
-if (WERROR)
+if (WERROR_COMMON_FLAGS)
        set_source_files_properties(
                ${CLEAN_FILES}
                PROPERTIES
-               COMPILE_FLAGS -Werror
+               COMPILE_FLAGS ${WERROR_COMMON_FLAGS}
        )
 endif()
 
@@ -2540,7 +2620,7 @@ else ()
        if (NOT "${HARDENING_CHECK_EXECUTABLE}" STREQUAL "HARDENING_CHECK_EXECUTABLE-NOTFOUND")
                foreach(_prog ${PROGLIST})
                        get_target_property(_prog_dir ${_prog} RUNTIME_OUTPUT_DIRECTORY)
-                       if (${_prog_dir} STREQUAL "_prog_dir-NOTFOUND")
+                       if ("${_prog_dir}" STREQUAL "_prog_dir-NOTFOUND")
                                set(_prog_dir "${CMAKE_BINARY_DIR}/run")
                        endif()
                        set(_prog_paths ${_prog_paths} "${_prog_dir}/${_prog}")