Skip to content

Commit 59dc6e3

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. Fined some paths in libturbojpeg find-script to get it working on linux.
1 parent 3adda14 commit 59dc6e3

File tree

6 files changed

+252
-50
lines changed

6 files changed

+252
-50
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: 81 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(TJPEG 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} ${TJPEG_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/")
@@ -69,6 +79,7 @@ endif()
6979
SET(RESOURCES_INC_FILE "${MY_DIR}/src/resources.inc")
7080

7181
SET(SOURCES
82+
7283
src/transfer_pool.cpp
7384
src/event_loop.cpp
7485

@@ -93,9 +104,10 @@ SET(SOURCES
93104
)
94105

95106
SET(LIBRARIES
96-
usb-1.0
97107
${OpenCV_LIBS}
98-
turbojpeg
108+
${OpenCV_LIBRARIES}
109+
${LibUSB_LIBRARIES}
110+
${TJPEG_LIBRARY}
99111
${LIBFREENECT2_THREADING_LIBRARIES}
100112
)
101113

@@ -107,17 +119,35 @@ SET(RESOURCES
107119

108120

109121
IF(ENABLE_OPENGL)
122+
auto_detect_lib(GLFW3)
123+
124+
#Assuming local build witout global deps, ugly to to this automatic but to keep backward compatibiliy
125+
if(${GLFW3_FOUND} STREQUAL "FALSE")
126+
127+
MESSAGE(STATUS "Building local GLFW")
128+
# GLFW
129+
SET(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries")
130+
SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
131+
SET(GLFW_BUILD_TESTS OFF CACHE BOOL "Build the GLFW test programs")
132+
SET(GLFW_BUILD_DOCS OFF CACHE BOOL "Build the GLFW documentation")
133+
134+
ADD_SUBDIRECTORY(${MY_DIR}/../../depends/glfw_src/ ${MY_DIR}/../../depends/glfw)
135+
INCLUDE_DIRECTORIES(${MY_DIR}/../../depends/glfw_src/include/)
136+
else()
137+
INCLUDE_DIRECTORIES(${GLFW3_INCLUDE_DIRS})
138+
139+
LINK_DIRECTORIES(${GLFW3_LIBRARY_DIRS})
140+
LIST(APPEND LIBRARIES
141+
${GLFW3_LIBRARIES}
142+
)
143+
endif()
144+
110145
SET(LIBFREENECT2_WITH_OPENGL_SUPPORT 1)
111146
LIST(APPEND SOURCES
112147
src/flextGL.c
113148
src/opengl_depth_packet_processor.cpp
114149
)
115-
116-
LIST(APPEND LIBRARIES
117-
glfw
118-
${GLFW_LIBRARIES}
119-
)
120-
150+
121151
LIST(APPEND RESOURCES
122152
src/shader/debug.fs
123153
src/shader/default.vs
@@ -128,31 +158,33 @@ IF(ENABLE_OPENGL)
128158
)
129159
ENDIF(ENABLE_OPENGL)
130160

131-
IF(ENABLE_OPENCL AND OPENCL_FOUND)
132-
SET(LIBFREENECT2_WITH_OPENCL_SUPPORT 1)
133-
INCLUDE_DIRECTORIES(${OPENCL_INCLUDE_DIRS})
134-
135-
LIST(APPEND SOURCES
136-
src/opencl_depth_packet_processor.cpp
137-
)
138-
139-
LIST(APPEND LIBRARIES
140-
${OPENCL_LIBRARIES}
141-
)
142-
143-
LIST(APPEND RESOURCES
144-
src/opencl_depth_packet_processor.cl
145-
)
146-
ENDIF(ENABLE_OPENCL AND OPENCL_FOUND)
161+
IF(ENABLE_OPENCL)
162+
FIND_PACKAGE(OpenCL)
163+
164+
IF(OPENCL_FOUND)
165+
SET(LIBFREENECT2_WITH_OPENCL_SUPPORT 1)
166+
INCLUDE_DIRECTORIES(${OPENCL_INCLUDE_DIRS})
167+
168+
LIST(APPEND SOURCES
169+
src/opencl_depth_packet_processor.cpp
170+
)
171+
172+
LIST(APPEND LIBRARIES
173+
${OPENCL_LIBRARIES}
174+
)
175+
176+
LIST(APPEND RESOURCES
177+
src/opencl_depth_packet_processor.cl
178+
)
179+
ENDIF(OPENCL_FOUND)
180+
ENDIF(ENABLE_OPENCL)
147181

148182
CONFIGURE_FILE("${MY_DIR}/include/libfreenect2/config.h.in" "${MY_DIR}/include/libfreenect2/config.h" @ONLY)
149-
150183
GENERATE_RESOURCES(${RESOURCES_INC_FILE} ${MY_DIR} ${RESOURCES})
151184

152-
INCLUDE_DIRECTORIES("${MY_DIR}/include")
153-
154185
ADD_DEFINITIONS(-DRESOURCES_INC)
155186
ADD_LIBRARY(freenect2 SHARED ${SOURCES})
187+
MESSAGE("Linking with these libraries: ${LIBRARIES}")
156188
TARGET_LINK_LIBRARIES(freenect2 ${LIBRARIES})
157189

158190
ADD_EXECUTABLE(Protonect
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
17+
#TODO linux
18+
#find_path(GLFW_INCLUDE_DIRS glfw3.h DOC "GLFW include directory " HINTS /usr/include)
19+
20+
#find_library(GLFW_LIBRARIES NAMES glfw.so glfw.a )
21+
endif()
22+
23+
find_package_handle_standard_args(GLFW3 "Could not find GLFW3 - try adding GLFW_ROOT in enviroment variables." GLFW3_INCLUDE_DIRS GLFW3_LIBRARIES)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_SYSTEM_PROCESSOR} STREQUAL "x32" )
34+
set ( LibUSB_LIBRARY_PATH_SUFFIX_RELEASE win32/Release/dll )
35+
set ( LibUSB_LIBRARY_PATH_SUFFIX_DEBUG win32/Debug/dll )
36+
else ()
37+
set ( LibUSB_LIBRARY_PATH_SUFFIX_RELEASE x64/Release/dll )
38+
set ( LibUSB_LIBRARY_PATH_SUFFIX_DEBUG x64/Debug/dll )
39+
endif ()
40+
elseif ( BORLAND )
41+
set ( LibUSB_LIBRARY_PATH_SUFFIX lib/bcc )
42+
elseif ( CMAKE_COMPILER_IS_GNUCC )
43+
set ( LibUSB_LIBRARY_PATH_SUFFIX lib/gcc )
44+
endif ( MSVC )
45+
endif ( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
46+
47+
find_library ( LibUSB_LIBRARY_RELEASE
48+
NAMES
49+
libusb libusb-1.0 usb
50+
PATHS
51+
$ENV{ProgramFiles}/LibUSB-Win32
52+
$ENV{LibUSB_ROOT_DIR}
53+
PATH_SUFFIXES
54+
${LibUSB_LIBRARY_PATH_SUFFIX_RELEASE}
55+
)
56+
57+
find_library ( LibUSB_LIBRARY_DEBUG
58+
NAMES
59+
libusb libusb-1.0 libusb-1.0d usb
60+
PATHS
61+
$ENV{ProgramFiles}/LibUSB-Win32
62+
$ENV{LibUSB_ROOT_DIR}
63+
PATH_SUFFIXES
64+
${LibUSB_LIBRARY_PATH_SUFFIX_DEBUG}
65+
)
66+
67+
set (LibUSB_LIBRARIES
68+
debug ${LibUSB_LIBRARY_DEBUG}
69+
optimized ${LibUSB_LIBRARY_RELEASE}
70+
)
71+
72+
if ( LibUSB_INCLUDE_DIR AND LibUSB_LIBRARIES )
73+
set ( LibUSB_FOUND 1 )
74+
endif ( LibUSB_INCLUDE_DIR AND LibUSB_LIBRARIES )
75+
76+
if ( LibUSB_FOUND )
77+
set ( CMAKE_REQUIRED_INCLUDES "${LibUSB_INCLUDE_DIR}" )
78+
check_include_file ( usb.h LibUSB_FOUND )
79+
endif ( LibUSB_FOUND )
80+
81+
if ( LibUSB_FOUND )
82+
check_library_exists ( "${LibUSB_LIBRARIES}" usb_open "" LibUSB_FOUND )
83+
endif ( LibUSB_FOUND )
84+
85+
if ( NOT LibUSB_FOUND )
86+
if ( NOT LibUSB_FIND_QUIETLY )
87+
message ( STATUS "LibUSB not found, try setting LibUSB_ROOT_DIR environment variable." )
88+
endif ( NOT LibUSB_FIND_QUIETLY )
89+
if ( LibUSB_FIND_REQUIRED )
90+
message ( FATAL_ERROR "" )
91+
endif ( LibUSB_FIND_REQUIRED )
92+
endif ( NOT LibUSB_FOUND )

examples/protonect/cmake_modules/FindOpenCL.cmake

Lines changed: 0 additions & 1 deletion
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

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 TJPEG_INCLUDE_DIR)
4+
if(WIN32)
5+
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x32")
6+
set(DEFAULT_TJPEG_INCLUDE_DIR $ENV{TJPEG_ROOT}}/include)
7+
else()
8+
set(DEFAULT_TJPEG_INCLUDE_DIR $ENV{TJPEG_ROOT}/include)
9+
endif()
10+
else()
11+
set(DEFAULT_TJPEG_INCLUDE_DIR /opt/libjpeg-turbo/include)
12+
endif()
13+
else()
14+
set(DEFAULT_TJPEG_INCLUDE_DIR ${TJPEG_INCLUDE_DIR})
15+
unset(TJPEG_INCLUDE_DIR)
16+
unset(TJPEG_INCLUDE_DIR CACHE)
17+
endif()
18+
19+
find_path(TJPEG_INCLUDE_DIR turbojpeg.h DOC "TurboJPEG include directory (default: ${DEFAULT_TJPEG_INCLUDE_DIR})" HINTS ${DEFAULT_TJPEG_INCLUDE_DIR})
20+
21+
if(TJPEG_INCLUDE_DIR STREQUAL "TJPEG_INCLUDE_DIR-NOTFOUND")
22+
message(FATAL_ERROR "Could not find turbojpeg.h - Try define TJPEG_ROOT as a system variable.")
23+
else()
24+
message(STATUS "TJPEG_INCLUDE_DIR = ${TJPEG_INCLUDE_DIR}")
25+
endif()
26+
27+
28+
if(WIN32)
29+
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x32")
30+
set(DEFAULT_TJPEG_LIBRARY $ENV{TJPEG_ROOT}/lib/turbojpeg.lib)
31+
else()
32+
set(DEFAULT_TJPEG_LIBRARY $ENV{TJPEG_ROOT}/lib/turbojpeg.lib)
33+
endif()
34+
else()
35+
find_library(DEFAULT_TJPEG_LIBRARY NAMES libturbojpeg.so libturbojpeg.a
36+
HINTS /opt/libjpeg-turbo/lib64/ /opt/libjpeg-turbo/lib/)
37+
endif()
38+
39+
set(TJPEG_LIBRARY ${DEFAULT_TJPEG_LIBRARY} CACHE PATH
40+
"TurboJPEG library path (default: ${DEFAULT_TJPEG_LIBRARY})")
41+
42+
if(WIN32)
43+
set(CMAKE_REQUIRED_DEFINITIONS -MT)
44+
endif()
45+
set(CMAKE_REQUIRED_INCLUDES ${TJPEG_INCLUDE_DIR})
46+
set(CMAKE_REQUIRED_LIBRARIES ${TJPEG_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 ${TJPEG_LIBRARY}. If it is installed in a different place, then set TJPEG_LIBRARY accordingly.")
53+
endif()
54+
55+
message(STATUS "TJPEG_LIBRARY = ${TJPEG_LIBRARY}")

0 commit comments

Comments
 (0)