Skip to content

Commit f4eebd6

Browse files
committed
Added Findlibraries cmake files, to search for the respective libraries, instead of hardcoding in a sub depend folder.
Added pkg-config support for linux to find libraries externally.
1 parent 9720e6e commit f4eebd6

File tree

6 files changed

+249
-53
lines changed

6 files changed

+249
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ examples/protonect/include/libfreenect2/config.h
33

44
# generated resource file
55
examples/protonect/src/resources.inc
6+
examples/protonect/build
67

78
# Dependency folders
89
depends/*/

examples/protonect/CMakeLists.txt

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
22

3+
macro(auto_detect_lib LIBNAME)
4+
if(NOT DEFINED ${${LIBNAME}_FOUND})
5+
if ( PKG_CONFIG_FOUND )
6+
string(TOLOWER ${LIBNAME} PKGCONFIGNAME)
7+
pkg_check_modules(${LIBNAME} ${PKGCONFIGNAME} ${ARGN})
8+
if(${${LIBNAME}_FOUND})
9+
MESSAGE(STATUS "LIB ${LIBNAME} found and include is in ${${LIBNAME}_INCLUDE_DIRS}")
10+
endif()
11+
else(PKG_CONFIG_FOUND)
12+
#MESSAGE("Fallback to non-pkg-config")
13+
FIND_PACKAGE(${LIBNAME} ${ARGN})
14+
endif(PKG_CONFIG_FOUND)
15+
else()
16+
MESSAGE(STATUS "LIB was already known ${LIBNAME} becasue of ${${LIBNAME}_FOUND}")
17+
endif()
18+
endmacro()
19+
320
PROJECT(libfreenect2)
421
SET(CMAKE_BUILD_TYPE RelWithDebInfo)
522

@@ -27,7 +44,6 @@ LIST(APPEND CMAKE_MODULE_PATH ${MY_DIR}/cmake_modules)
2744

2845
# setup threading
2946
INCLUDE(SetupLibfreenect2Threading)
30-
INCLUDE_DIRECTORIES(${LIBFREENECT2_THREADING_INCLUDE_DIR})
3147

3248
INCLUDE(GenerateResources)
3349

@@ -37,30 +53,24 @@ SET(EXECUTABLE_OUTPUT_PATH ${MY_DIR}/bin)
3753
#set the default path for built libraries to the "lib" directory
3854
SET(LIBRARY_OUTPUT_PATH ${MY_DIR}/lib)
3955

40-
FIND_PACKAGE(OpenCL)
41-
4256
# dependencies
43-
FIND_PACKAGE(OpenCV REQUIRED)
44-
45-
# OpenCV
46-
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIR})
47-
48-
# LibUSB
49-
INCLUDE_DIRECTORIES("${MY_DIR}/../../depends/libusb/include/libusb-1.0/")
50-
LINK_DIRECTORIES("${MY_DIR}/../../depends/libusb/lib/")
57+
find_package(PkgConfig) # try find PKGConfig as it will be used if found
58+
auto_detect_lib(LibUSB REQUIRED)
59+
auto_detect_lib(OpenCV REQUIRED)
60+
FIND_PACKAGE(TurboJPEG REQUIRED) #does not provide a package-config file
61+
62+
#hack for buggy libusb pkgconfig file
63+
if(NOT LibUSB_INCLUDE_DIR)
64+
find_path(LibUSB_INCLUDE_DIR
65+
NAMES libusb.h
66+
PATHS /usr/include/libusb-1.0
67+
)
68+
endif()
5169

52-
IF(ENABLE_OPENGL)
53-
# GLFW
54-
SET(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries")
55-
SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
56-
SET(GLFW_BUILD_TESTS OFF CACHE BOOL "Build the GLFW test programs")
57-
SET(GLFW_BUILD_DOCS OFF CACHE BOOL "Build the GLFW documentation")
58-
59-
ADD_SUBDIRECTORY(${MY_DIR}/../../depends/glfw_src/ ${MY_DIR}/../../depends/glfw)
60-
INCLUDE_DIRECTORIES(${MY_DIR}/../../depends/glfw_src/include/)
61-
ENDIF(ENABLE_OPENGL)
70+
# Add includes
71+
INCLUDE_DIRECTORIES("${MY_DIR}/include" ${LIBFREENECT2_THREADING_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS} ${LibUSB_INCLUDE_DIR} ${TurboJPEG_INCLUDE_DIR})
6272

63-
if(APPLE)
73+
if(APPLE AND NOT ${TurboJPEG_FOUND})
6474
# libjpeg-turbo
6575
INCLUDE_DIRECTORIES("${MY_DIR}/../../depends/libjpeg_turbo/include/")
6676
LINK_DIRECTORIES("${MY_DIR}/../../depends/libjpeg_turbo/lib/")
@@ -115,9 +125,10 @@ SET(SOURCES
115125
)
116126

117127
SET(LIBRARIES
118-
usb-1.0
119128
${OpenCV_LIBS}
120-
turbojpeg
129+
${OpenCV_LIBRARIES}
130+
${LibUSB_LIBRARIES}
131+
${TurboJPEG_LIBRARY}
121132
${LIBFREENECT2_THREADING_LIBRARIES}
122133
)
123134

@@ -129,17 +140,40 @@ SET(RESOURCES
129140

130141

131142
IF(ENABLE_OPENGL)
143+
auto_detect_lib(GLFW3)
144+
145+
#Assuming local build witout global deps, ugly to to this automatic but to keep backward compatibiliy
146+
if(${GLFW3_FOUND} STREQUAL "FALSE")
147+
148+
MESSAGE(STATUS "Building local GLFW")
149+
# GLFW
150+
SET(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries")
151+
SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
152+
SET(GLFW_BUILD_TESTS OFF CACHE BOOL "Build the GLFW test programs")
153+
SET(GLFW_BUILD_DOCS OFF CACHE BOOL "Build the GLFW documentation")
154+
155+
ADD_SUBDIRECTORY(${MY_DIR}/../../depends/glfw_src/ ${MY_DIR}/../../depends/glfw)
156+
INCLUDE_DIRECTORIES(${MY_DIR}/../../depends/glfw_src/include/)
157+
158+
LIST(APPEND LIBRARIES
159+
glfw
160+
)
161+
162+
else()
163+
INCLUDE_DIRECTORIES(${GLFW3_INCLUDE_DIRS})
164+
165+
LINK_DIRECTORIES(${GLFW3_LIBRARY_DIRS})
166+
LIST(APPEND LIBRARIES
167+
${GLFW3_LIBRARIES}
168+
)
169+
endif()
170+
132171
SET(LIBFREENECT2_WITH_OPENGL_SUPPORT 1)
133172
LIST(APPEND SOURCES
134173
src/flextGL.c
135174
src/opengl_depth_packet_processor.cpp
136175
)
137-
138-
LIST(APPEND LIBRARIES
139-
glfw
140-
${GLFW_LIBRARIES}
141-
)
142-
176+
143177
LIST(APPEND RESOURCES
144178
src/shader/debug.fs
145179
src/shader/default.vs
@@ -150,31 +184,33 @@ IF(ENABLE_OPENGL)
150184
)
151185
ENDIF(ENABLE_OPENGL)
152186

153-
IF(ENABLE_OPENCL AND OPENCL_FOUND)
154-
SET(LIBFREENECT2_WITH_OPENCL_SUPPORT 1)
155-
INCLUDE_DIRECTORIES(${OPENCL_INCLUDE_DIRS})
156-
157-
LIST(APPEND SOURCES
158-
src/opencl_depth_packet_processor.cpp
159-
)
160-
161-
LIST(APPEND LIBRARIES
162-
${OPENCL_LIBRARIES}
163-
)
164-
165-
LIST(APPEND RESOURCES
166-
src/opencl_depth_packet_processor.cl
167-
)
168-
ENDIF(ENABLE_OPENCL AND OPENCL_FOUND)
187+
IF(ENABLE_OPENCL)
188+
FIND_PACKAGE(OpenCL)
189+
190+
IF(OPENCL_FOUND)
191+
SET(LIBFREENECT2_WITH_OPENCL_SUPPORT 1)
192+
INCLUDE_DIRECTORIES(${OPENCL_INCLUDE_DIRS})
193+
194+
LIST(APPEND SOURCES
195+
src/opencl_depth_packet_processor.cpp
196+
)
197+
198+
LIST(APPEND LIBRARIES
199+
${OPENCL_LIBRARIES}
200+
)
201+
202+
LIST(APPEND RESOURCES
203+
src/opencl_depth_packet_processor.cl
204+
)
205+
ENDIF(OPENCL_FOUND)
206+
ENDIF(ENABLE_OPENCL)
169207

170208
CONFIGURE_FILE("${MY_DIR}/include/libfreenect2/config.h.in" "${MY_DIR}/include/libfreenect2/config.h" @ONLY)
171-
172209
GENERATE_RESOURCES(${RESOURCES_INC_FILE} ${MY_DIR} ${RESOURCES})
173210

174-
INCLUDE_DIRECTORIES("${MY_DIR}/include")
175-
176211
ADD_DEFINITIONS(-DRESOURCES_INC)
177212
ADD_LIBRARY(freenect2 SHARED ${SOURCES})
213+
MESSAGE("Linking with these libraries: ${LIBRARIES}")
178214
TARGET_LINK_LIBRARIES(freenect2 ${LIBRARIES})
179215

180216
ADD_EXECUTABLE(Protonect
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# - Try to find GLFW3
2+
#
3+
# Will define the following:
4+
# GLFW3_FOUND
5+
# GLFW3_INCLUDE_DIRS
6+
# GLFW3_LIBRARIES
7+
8+
include(FindPackageHandleStandardArgs)
9+
10+
IF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
11+
find_path(GLFW3_INCLUDE_DIRS glfw/glfw3.h DOC "GLFW include directory " HINTS $ENV{GLFW_ROOT}/include)
12+
13+
find_library(GLFW3_LIBRARIES NAMES glfw3dll.lib HINTS $ENV{GLFW_ROOT}/lib/)
14+
15+
ENDIF()
16+
17+
find_package_handle_standard_args(GLFW3 "Could not find GLFW3 - try adding GLFW_ROOT in enviroment variables." GLFW3_INCLUDE_DIRS GLFW3_LIBRARIES)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# - Find libusb for portable USB support
2+
# This module will find libusb as published by
3+
# http://libusb.sf.net and
4+
# http://libusb-win32.sf.net
5+
#
6+
# It will use PkgConfig if present and supported, else search
7+
# it on its own. If the LibUSB_ROOT_DIR environment variable
8+
# is defined, it will be used as base path.
9+
# The following standard variables get defined:
10+
# LibUSB_FOUND: true if LibUSB was found
11+
# LibUSB_INCLUDE_DIR: the directory that contains the include file
12+
# LibUSB_LIBRARIES: the library
13+
14+
include ( CheckLibraryExists )
15+
include ( CheckIncludeFile )
16+
17+
find_path ( LibUSB_INCLUDE_DIR
18+
NAMES
19+
libusb.h
20+
PATHS
21+
$ENV{ProgramFiles}/LibUSB-Win32
22+
$ENV{LibUSB_ROOT_DIR}
23+
PATH_SUFFIXES
24+
libusb
25+
)
26+
27+
mark_as_advanced ( LibUSB_INCLUDE_DIR )
28+
29+
if ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
30+
# LibUSB-Win32 binary distribution contains several libs.
31+
# Use the lib that got compiled with the same compiler.
32+
if ( MSVC )
33+
if ( ${CMAKE_SIZEOF_VOID_P} EQUAL 8 )
34+
set ( LibUSB_LIBRARY_PATH_SUFFIX_RELEASE x64/Release/dll )
35+
set ( LibUSB_LIBRARY_PATH_SUFFIX_DEBUG x64/Debug/dll )
36+
else ()
37+
set ( LibUSB_LIBRARY_PATH_SUFFIX_RELEASE win32/Release/dll )
38+
set ( LibUSB_LIBRARY_PATH_SUFFIX_DEBUG win32/Debug/dll )
39+
endif ()
40+
endif ( MSVC )
41+
endif ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
42+
43+
find_library ( LibUSB_LIBRARY_RELEASE
44+
NAMES
45+
libusb libusb-1.0 usb
46+
PATHS
47+
$ENV{ProgramFiles}/LibUSB-Win32
48+
$ENV{LibUSB_ROOT_DIR}
49+
PATH_SUFFIXES
50+
${LibUSB_LIBRARY_PATH_SUFFIX_RELEASE}
51+
)
52+
53+
find_library ( LibUSB_LIBRARY_DEBUG
54+
NAMES
55+
libusb libusb-1.0 libusb-1.0d usb
56+
PATHS
57+
$ENV{ProgramFiles}/LibUSB-Win32
58+
$ENV{LibUSB_ROOT_DIR}
59+
PATH_SUFFIXES
60+
${LibUSB_LIBRARY_PATH_SUFFIX_DEBUG}
61+
)
62+
63+
set (LibUSB_LIBRARIES
64+
debug ${LibUSB_LIBRARY_DEBUG}
65+
optimized ${LibUSB_LIBRARY_RELEASE}
66+
)
67+
68+
if ( LibUSB_INCLUDE_DIR AND LibUSB_LIBRARIES )
69+
set ( LibUSB_FOUND 1 )
70+
endif ( LibUSB_INCLUDE_DIR AND LibUSB_LIBRARIES )
71+
72+
if ( LibUSB_FOUND )
73+
set ( CMAKE_REQUIRED_INCLUDES "${LibUSB_INCLUDE_DIR}" )
74+
check_include_file ( usb.h LibUSB_FOUND )
75+
endif ( LibUSB_FOUND )
76+
77+
if ( LibUSB_FOUND )
78+
check_library_exists ( "${LibUSB_LIBRARIES}" usb_open "" LibUSB_FOUND )
79+
endif ( LibUSB_FOUND )
80+
81+
if ( NOT LibUSB_FOUND )
82+
if ( NOT LibUSB_FIND_QUIETLY )
83+
message ( STATUS "LibUSB not found, try setting LibUSB_ROOT_DIR environment variable." )
84+
endif ( NOT LibUSB_FIND_QUIETLY )
85+
if ( LibUSB_FIND_REQUIRED )
86+
message ( FATAL_ERROR "LibUSB could not be found." )
87+
endif ( LibUSB_FIND_REQUIRED )
88+
endif ( NOT LibUSB_FOUND )

examples/protonect/cmake_modules/FindOpenCL.cmake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# OPENCL_INCLUDE_DIRS - the OpenCL include directory
1212
# OPENCL_LIBRARIES - link these to use OpenCL
1313
#
14-
# WIN32 should work, but is untested
1514

1615
FIND_PACKAGE(PackageHandleStandardArgs)
1716

@@ -31,11 +30,11 @@ ELSE (APPLE)
3130
IF (WIN32)
3231
# The AMD SDK currently installs both x86 and x86_64 libraries
3332
# This is only a hack to find out architecture
34-
IF( ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64" )
33+
IF( ${CMAKE_SIZEOF_VOID_P} EQUAL 8 )
3534
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL.lib PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" PATH_SUFFIXES "/lib/x86_64")
36-
ELSE (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
35+
ELSE (${CMAKE_SIZEOF_VOID_P} EQUAL 8)
3736
FIND_LIBRARY(OPENCL_LIBRARIES OpenCL.lib PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" PATH_SUFFIXES "/lib/x86")
38-
ENDIF( ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64" )
37+
ENDIF( ${CMAKE_SIZEOF_VOID_P} EQUAL 8 )
3938

4039
# On Win32 search relative to the library
4140
FIND_PATH(OPENCL_INCLUDE_DIRS CL/cl.h PATHS "$ENV{ATISTREAMSDKROOT}" "$ENV{AMDAPPSDKROOT}" PATH_SUFFIXES "/include")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
include(CheckCSourceCompiles)
2+
3+
if(NOT TurboJPEG_INCLUDE_DIR)
4+
if(WIN32)
5+
if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
6+
set(DEFAULT_TurboJPEG_INCLUDE_DIR $ENV{TurboJPEG64_ROOT}/include)
7+
else()
8+
set(DEFAULT_TurboJPEG_INCLUDE_DIR $ENV{TurboJPEG_ROOT}/include)
9+
endif()
10+
else()
11+
set(DEFAULT_TurboJPEG_INCLUDE_DIR /opt/libjpeg-turbo/include)
12+
endif()
13+
else()
14+
set(DEFAULT_TurboJPEG_INCLUDE_DIR ${TurboJPEG_INCLUDE_DIR})
15+
unset(TurboJPEG_INCLUDE_DIR)
16+
unset(TurboJPEG_INCLUDE_DIR CACHE)
17+
endif()
18+
19+
find_path(TurboJPEG_INCLUDE_DIR turbojpeg.h DOC "TurboJPEG include directory (default: ${DEFAULT_TurboJPEG_INCLUDE_DIR})" HINTS ${DEFAULT_TurboJPEG_INCLUDE_DIR})
20+
21+
if(TurboJPEG_INCLUDE_DIR STREQUAL "TurboJPEG_INCLUDE_DIR-NOTFOUND")
22+
message(FATAL_ERROR "Could not find turbojpeg.h - Try define TurboJPEG_ROOT as a system variable.")
23+
else()
24+
message(STATUS "TurboJPEG_INCLUDE_DIR = ${TurboJPEG_INCLUDE_DIR}")
25+
endif()
26+
27+
28+
if(WIN32)
29+
if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
30+
set(DEFAULT_TurboJPEG_LIBRARY $ENV{TurboJPEG64_ROOT}/lib/turbojpeg.lib)
31+
else()
32+
set(DEFAULT_TurboJPEG_LIBRARY $ENV{TurboJPEG_ROOT}/lib/turbojpeg.lib)
33+
endif()
34+
else()
35+
find_library(DEFAULT_TurboJPEG_LIBRARY NAMES libturbojpeg.so libturbojpeg.a
36+
HINTS /opt/libjpeg-turbo/lib64/ /opt/libjpeg-turbo/lib/)
37+
endif()
38+
39+
set(TurboJPEG_LIBRARY ${DEFAULT_TurboJPEG_LIBRARY} CACHE PATH
40+
"TurboJPEG library path (default: ${DEFAULT_TurboJPEG_LIBRARY})")
41+
42+
if(WIN32)
43+
set(CMAKE_REQUIRED_DEFINITIONS -MT)
44+
endif()
45+
set(CMAKE_REQUIRED_INCLUDES ${TurboJPEG_INCLUDE_DIR})
46+
set(CMAKE_REQUIRED_LIBRARIES ${TurboJPEG_LIBRARY})
47+
check_c_source_compiles("#include <turbojpeg.h>\nint main(void) { tjhandle h=tjInitCompress(); return 0; }" TURBOJPEG_WORKS)
48+
set(CMAKE_REQUIRED_DEFINITIONS)
49+
set(CMAKE_REQUIRED_INCLUDES)
50+
set(CMAKE_REQUIRED_LIBRARIES)
51+
if(NOT TURBOJPEG_WORKS)
52+
message(FATAL_ERROR "Could not link with TurboJPEG library ${TurboJPEG_LIBRARY}. If it is installed in a different place, then set TurboJPEG_LIBRARY accordingly.")
53+
endif()
54+
55+
message(STATUS "TurboJPEG_LIBRARY = ${TurboJPEG_LIBRARY}")

0 commit comments

Comments
 (0)