Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce modern way to link with pthread library #1227

Merged
merged 8 commits into from
Sep 7, 2023
48 changes: 11 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,40 +382,15 @@ if(WIN32)
endif()
endif()

# Add library ws2_32.a or ws2_32.lib for vpNetwork class
if(WIN32 AND NOT CYGWIN)
if(MINGW)
set(WS2_32_LIBRARY "ws2_32.a")
check_library_exists(${WS2_32_LIBRARY} getch "" HAVE_LIBWS2_32) # for inet_ntoa() and socket functionalities
if(HAVE_LIBWS2_32)
list(APPEND VISP_LINKER_LIBS ${WS2_32_LIBRARY})
else()
find_library(HAVE_LIBWS2_32 ${WS2_32_LIBRARY}
"$ENV{MINGW_DIR}/lib"
"$ENV{MINGW_DIR}/mingw/lib"
C:/mingw/mingw/lib)
if(HAVE_LIBWS2_32)
list(APPEND VISP_LINKER_LIBS "${WS2_32_LIBRARY}")
endif()
endif()
elseif(WINRT)
# Since check_library_exists() and find_library() does't work to detect ws2_32.lib we add the lib that is part of Windows SDK
set(WS2_32_LIBRARY "ws2_32.lib")
list(APPEND VISP_LINKER_LIBS ${WS2_32_LIBRARY})
else() # pure WIN32
set(WS2_32_LIBRARY "ws2_32.lib")
#check_library_exists("ws2_32.lib" getch "" HAVE_LIBWS2_32) # for inet_ntoa() and socket functionalities
check_library_exists(${WS2_32_LIBRARY} getch "" HAVE_LIBWS2_32) # for inet_ntoa() and socket functionalities
if(HAVE_LIBWS2_32)
list(APPEND VISP_LINKER_LIBS ${WS2_32_LIBRARY})
endif()
endif()
mark_as_advanced(HAVE_LIBWS2_32)
VP_CHECK_PACKAGE(WS2_32)
# Should be before include(cmake/VISPDetectCXXStandard.cmake)
VP_CHECK_FUNCTION_EXISTS(inet_ntop "${WS2_32_LIBRARY}")
else()
# Should be before include(cmake/VISPDetectCXXStandard.cmake)
VP_CHECK_FUNCTION_EXISTS(inet_ntop "")
endif()

# Should be before include(cmake/VISPDetectCXXStandard.cmake)
VP_CHECK_FUNCTION_EXISTS(inet_ntop "${VISP_LINKER_LIBS}")

#--------------------------------------------------------------------
# Option management
#--------------------------------------------------------------------
Expand Down Expand Up @@ -571,19 +546,18 @@ if(SOQT_FOUND) # SoQt < 1.6.0 that depends on Qt4 was found. We need an explicit
VP_OPTION(USE_QT Qt "" "Include Coin/SoQt/Qt support" "" ON IF USE_SOQT AND NOT WINRT AND NOT IOS)
endif()
VP_OPTION(USE_SOXT SOXT "" "Include Coin/SoXt support" "" OFF IF USE_COIN3D AND NOT WINRT AND NOT IOS)
if (ANDROID)
VP_OPTION(USE_PTHREAD Threads "" "Include pthread support" "" ON)
else()
VP_OPTION(USE_PTHREAD PTHREAD "" "Include pthread support" "" ON)
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)
VP_OPTION(USE_PTHREAD Threads "" "Include pthread support" "" ON IF NOT (WIN32 OR MINGW))

# We need threads with c++11
if((VISP_CXX_STANDARD GREATER VISP_CXX_STANDARD_98) AND NOT USE_PTHREAD)
if(Threads_FOUND OR PTHREAD_FOUND)
if(Threads_FOUND)
message(WARNING "We need threads. Turn USE_PTHREAD=ON.")
unset(USE_PTHREAD)
set(USE_PTHREAD ON CACHE BOOL "Include pthread support" FORCE)
endif()
endif()

VP_OPTION(USE_XML2 XML2 "" "Include xml support" "" ON IF NOT WINRT)
if(CMAKE_TOOLCHAIN_FILE)
# Find opencv2.framework for ios and naoqi
Expand Down
6 changes: 6 additions & 0 deletions cmake/AddExtraCompilationFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ if(USE_OPENMP)
add_extra_compiler_option("${OpenMP_CXX_FLAGS}")
endif()

if(USE_PTHREAD)
if(THREADS_HAVE_PTHREAD_ARG)
add_extra_compiler_option("-pthread")
endif()
endif()

if((VISP_CXX_STANDARD EQUAL VISP_CXX_STANDARD_11) AND CXX11_CXX_FLAGS)
add_extra_compiler_option("${CXX11_CXX_FLAGS}")
elseif((VISP_CXX_STANDARD EQUAL VISP_CXX_STANDARD_14) AND CXX14_CXX_FLAGS)
Expand Down
99 changes: 0 additions & 99 deletions cmake/FindPTHREAD.cmake

This file was deleted.

76 changes: 76 additions & 0 deletions cmake/FindWS2_32.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#############################################################################
#
# ViSP, open source Visual Servoing Platform software.
# Copyright (C) 2005 - 2023 by Inria. All rights reserved.
#
# This software is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# See the file LICENSE.txt at the root directory of this source
# distribution for additional information about the GNU GPL.
#
# For using ViSP with software that can not be combined with the GNU
# GPL, please contact Inria about acquiring a ViSP Professional
# Edition License.
#
# See https://visp.inria.fr for more information.
#
# This software was developed at:
# Inria Rennes - Bretagne Atlantique
# Campus Universitaire de Beaulieu
# 35042 Rennes Cedex
# France
#
# If you have questions regarding the use of this file, please contact
# Inria at [email protected]
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Description:
# Try to find ws2_32 library.
#
# WS2_32_FOUND - true if ws2_32 library is detected
# WS2_32_LIBRARY - Library name
#
#############################################################################

if(WIN32 AND NOT CYGWIN)
if(MINGW)
set(WS2_32_LIBNAME "ws2_32.a")
check_library_exists(${WS2_32_LIBNAME} getch "" HAVE_LIBWS2_32) # for inet_ntoa() and socket functionalities
if(HAVE_LIBWS2_32)
set(WS2_32_LIBRARY ${WS2_32_LIBNAME})
set(WS2_32_FOUND TRUE)
else()
find_library(HAVE_LIBWS2_32 ${WS2_32_LIBNAME}
"$ENV{MINGW_DIR}/lib"
"$ENV{MINGW_DIR}/mingw/lib"
C:/mingw/mingw/lib)
if(HAVE_LIBWS2_32)
set(WS2_32_LIBRARY ${WS2_32_LIBNAME})
set(WS2_32_FOUND TRUE)
else()
set(WS2_32_FOUND FALSE)
endif()
endif()
elseif(WINRT)
# Since check_library_exists() and find_library() does't work to detect ws2_32.lib we add the lib that is part of Windows SDK
set(WS2_32_LIBNAME "ws2_32.lib")
set(WS2_32_LIBRARY ${WS2_32_LIBNAME})
set(WS2_32_FOUND TRUE)
else() # pure WIN32
set(WS2_32_LIBNAME "ws2_32.lib")
check_library_exists(${WS2_32_LIBNAME} getch "" HAVE_LIBWS2_32) # for inet_ntoa() and socket functionalities
if(HAVE_LIBWS2_32)
message("-----------> ${WS2_32_LIBNAME} is found")
set(WS2_32_LIBRARY ${WS2_32_LIBNAME})
set(WS2_32_FOUND TRUE)
else()
set(WS2_32_FOUND FALSE)
endif()
endif()
mark_as_advanced(WS2_32_LIBNAME)
mark_as_advanced(HAVE_LIBWS2_32)
endif()
8 changes: 4 additions & 4 deletions cmake/VISPModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ endmacro()
# creates ViSP module in current folder
# creates new target, configures standard dependencies, compilers flags, install rules
# Usage:
# vp_create_module(<extra link dependencies> LINK_PRIVATE <private link dependencies>)
# vp_create_module(<extra link dependencies>)
# vp_create_module()
macro(vp_create_module)
vp_debug_message("vp_create_module(" ${ARGN} ")")
Expand All @@ -801,11 +801,11 @@ macro(_vp_create_module)
vp_add_library(${the_module} ${VISP_MODULE_TYPE} ${VISP_MODULE_${the_module}_HEADERS} ${VISP_MODULE_${the_module}_SOURCES})

vp_target_link_libraries(${the_module}
LINK_PUBLIC
PUBLIC
${VISP_MODULE_${the_module}_DEPS_TO_LINK}
${VISP_MODULE_${the_module}_DEPS_EXT}
${VISP_MODULE_${the_module}_LINK_DEPS}
LINK_PRIVATE
PRIVATE
${VISP_MODULE_${the_module}_PRIVATE_REQ_DEPS}
${VISP_MODULE_${the_module}_PRIVATE_OPT_DEPS}
${VISP_LINKER_LIBS})
Expand Down Expand Up @@ -1016,7 +1016,7 @@ macro(vp_add_tests)
# From source compile the binary and add link rules
vp_add_executable(${the_target} ${t})
vp_target_include_modules(${the_target} ${test_deps})
vp_target_link_libraries(${the_target} ${test_deps} ${VISP_MODULE_${the_module}_DEPS} ${VISP_LINKER_LIBS})
vp_target_link_libraries(${the_target} ${test_deps} ${VISP_MODULE_${the_module}_DEPS}) # should be removed ? ${VISP_LINKER_LIBS})

# ctest only:
# - if required dataset available
Expand Down
3 changes: 2 additions & 1 deletion cmake/VISPUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ macro(VP_CHECK_FUNCTION_EXISTS function libraries)
string(TOUPPER "${ALIAS}" ALIAS_UPPER)
set(ALIAS_HAVE HAVE_FUNC_${ALIAS_UPPER})
set(CMAKE_REQUIRED_LIBRARIES "${libraries}")
message(" -------> CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
check_function_exists(${ALIAS} ${ALIAS_HAVE})
endmacro()

Expand Down Expand Up @@ -1645,7 +1646,7 @@ macro(vp_get_all_libs _modules _extra_opt _extra_dbg _3rdparty)
endif()
endforeach()

foreach (dep ${deps} ${VISP_LINKER_LIBS})
foreach (dep ${deps}) # Should be remove ? ${VISP_LINKER_LIBS})
if (NOT DEFINED VISP_MODULE_${dep}_LOCATION)
if(dep MATCHES "^\\$<LINK_ONLY:([^>]+)>$")
set(dep_dbg "${CMAKE_MATCH_1}")
Expand Down
24 changes: 21 additions & 3 deletions modules/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
set(opt_incs "")
set(opt_libs "")

# Add library ws2_32.a or ws2_32.lib for vpNetwork class
if(WS2_32_FOUND)
list(APPEND opt_libs ${WS2_32_LIBRARY})
endif()

# OpenCV
if(USE_OPENCV)
# On win32 since OpenCV 2.4.7 and on OSX with OpenCV 2.4.10 we cannot use OpenCV_LIBS to set ViSP 3rd party libraries.
Expand Down Expand Up @@ -222,16 +227,29 @@ if(USE_XML2)
list(APPEND opt_libs ${XML2_LIBRARIES})
endif()
if(USE_PTHREAD)
list(APPEND opt_incs ${PTHREAD_INCLUDE_DIRS})
list(APPEND opt_libs ${PTHREAD_LIBRARIES})
if(CMAKE_THREAD_LIBS_INIT)
list(APPEND opt_libs "${CMAKE_THREAD_LIBS_INIT}")
endif()
endif()
if(USE_ZLIB)
list(APPEND opt_incs ${ZLIB_INCLUDE_DIRS})
list(APPEND opt_libs ${ZLIB_LIBRARIES})
endif()
if(USE_OPENMP)
list(APPEND opt_incs ${OpenMP_CXX_INCLUDE_DIRS})
list(APPEND opt_libs ${OpenMP_CXX_LIBRARIES})
# Because there is an explicit link to libpthread location that breaks visp conda package usage on linux
# we cannot use OpenMP_CXX_LIBRARIES that contains /usr/lib/gcc/x86_64-linux-gnu/11/libgomp.so;/usr/lib/x86_64-linux-gnu/libpthread.a
# by adding:
# list(APPEND opt_libs ${OpenMP_CXX_LIBRARIES})
foreach(lib_ ${OpenMP_CXX_LIB_NAMES})
if("x${lib_}" STREQUAL "xpthread")
if(CMAKE_THREAD_LIBS_INIT)
list(APPEND opt_libs "${CMAKE_THREAD_LIBS_INIT}")
endif()
else()
list(APPEND opt_libs ${OpenMP_${lib_}_LIBRARY})
endif()
endforeach()
endif()
if(USE_NLOHMANN_JSON)
get_target_property(_inc_dirs "nlohmann_json::nlohmann_json" INTERFACE_INCLUDE_DIRECTORIES)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/include/visp3/core/vpImageCircle.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include <visp3/core/vpRect.h>

#if defined(VISP_HAVE_OPENCV)
#include <opencv2/core.hpp>
#include <opencv2/core/core.hpp>
#endif

/**
Expand Down
Loading
Loading