1 From eceada586bbf18fc267e437522ec4f1f23ddc656 Mon Sep 17 00:00:00 2001
2 From: Samuel Martin <s.martin49@gmail.com>
3 Date: Fri, 3 Oct 2014 00:32:40 +0200
4 Subject: [PATCH] cmake/OpenCVGenPkgconfig.cmake: rework opencv.pc generation
6 Using absolute path to locate the components in the "Libs:" field of the
7 *.pc can badly break cross-compilation, especially when building
8 statically linked objects.
10 Indeed, pkg-config automatically replaces the '-I...' and '-L...' paths
11 when the PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_LIBDIR environment
12 variables are set [1]. This feature is very helpful and common in
13 cross-compilation framework like Buildroot [2,3].
15 When there are absolute paths in the *.pc files, pkg-config won't be
16 able to do the path substitions for these paths when the afromentioned
17 environment variables are set.
18 In such case, since the prefix is the target one, not the sysroot one,
19 these libraries' abolute paths will point to:
20 - in the best case: a non-existing file (i.e. these files do not exists
22 - at worst: the host system's libraries. This will make the linking
23 failed because these host system's libraries will most likely not be
24 build for the target architecture [4].
26 So, this patch replace the components' absolute paths by the form:
27 -L<libdir> -l<libname>
29 This way, the linker will be able to resolve each dependency path,
30 whatever the kind of objects/build (shared object or static build) it
33 Note that for static link, the library order does matter [5]. The order
34 of the opencv components has been carefully chosen to comply with this
39 [1] http://linux.die.net/man/1/pkg-config
40 [2] http://buildroot.org/
41 [3] http://git.buildroot.net/buildroot/tree/package/pkgconf/pkg-config.in
42 [4] http://autobuild.buildroot.net/results/e8a/e8a859276db34aff87ef181b0cce98916b0afc90/build-end.log
43 [5] http://stackoverflow.com/questions/45135/linker-order-gcc
45 Signed-off-by: Samuel Martin <s.martin49@gmail.com>
48 Note: this patch properly applies on top of the master branch, though it
49 has been written on top of the 2.4 branch.
51 cmake/OpenCVGenPkgconfig.cmake | 64 +++++++++++++++++++++++++++---------------
52 1 file changed, 42 insertions(+), 22 deletions(-)
54 diff --git a/cmake/OpenCVGenPkgconfig.cmake b/cmake/OpenCVGenPkgconfig.cmake
55 index fa57db9..183c56d 100644
56 --- a/cmake/OpenCVGenPkgconfig.cmake
57 +++ b/cmake/OpenCVGenPkgconfig.cmake
60 # ${BIN_DIR}/unix-install/opencv.pc -> For use *with* "make install"
61 # -------------------------------------------------------------------------------------------
62 -set(prefix "${CMAKE_INSTALL_PREFIX}")
63 -set(exec_prefix "\${prefix}")
64 -set(libdir "") #TODO: need link paths for OpenCV_EXTRA_COMPONENTS
65 -set(includedir "\${prefix}/${OPENCV_INCLUDE_INSTALL_PATH}")
67 if(CMAKE_BUILD_TYPE MATCHES "Release")
69 @@ -35,42 +31,66 @@ ocv_list_reverse(OpenCV_LIB_COMPONENTS)
70 ocv_list_reverse(OpenCV_EXTRA_COMPONENTS)
72 #build the list of components
73 -set(OpenCV_LIB_COMPONENTS_ "")
74 -foreach(CVLib ${OpenCV_LIB_COMPONENTS})
75 - get_target_property(libpath ${CVLib} LOCATION_${CMAKE_BUILD_TYPE})
76 - get_filename_component(libname "${libpath}" NAME)
78 - if(INSTALL_TO_MANGLED_PATHS)
79 - set(libname "${libname}.${OPENCV_VERSION}")
82 +# when linking against static libraries, if libfoo depends on libbar, then
83 +# libfoo must come first in the linker flags.
85 +# world is a special target whose library should come first, especially for
87 +if(OpenCV_LIB_COMPONENTS MATCHES "opencv_world")
88 + list(REMOVE_ITEM OpenCV_LIB_COMPONENTS "opencv_world")
89 + list(INSERT OpenCV_LIB_COMPONENTS 0 "opencv_world")
92 +set(OpenCV_LIB_COMPONENTS_)
93 +foreach(CVLib ${OpenCV_LIB_COMPONENTS})
95 - #need better solution....
96 - if(libpath MATCHES "3rdparty")
97 - set(installDir "share/OpenCV/3rdparty/${OPENCV_LIB_INSTALL_PATH}")
98 + get_target_property(libloc ${CVLib} LOCATION_${CMAKE_BUILD_TYPE})
99 + if(libloc MATCHES "3rdparty")
100 + set(libpath "\${exec_prefix}/share/OpenCV/3rdparty/${OPENCV_LIB_INSTALL_PATH}")
102 - set(installDir "${OPENCV_LIB_INSTALL_PATH}")
103 + set(libpath "\${exec_prefix}/${OPENCV_LIB_INSTALL_PATH}")
105 + list(APPEND OpenCV_LIB_COMPONENTS_ "-L${libpath}")
107 + get_filename_component(libname ${CVLib} NAME_WE)
108 + string(REGEX REPLACE "^lib" "" libname "${libname}")
109 + list(APPEND OpenCV_LIB_COMPONENTS_ "-l${libname}")
111 - set(OpenCV_LIB_COMPONENTS_ "${OpenCV_LIB_COMPONENTS_} \${exec_prefix}/${installDir}/${libname}")
114 # add extra dependencies required for OpenCV
115 -set(OpenCV_LIB_COMPONENTS ${OpenCV_LIB_COMPONENTS_})
116 if(OpenCV_EXTRA_COMPONENTS)
117 foreach(extra_component ${OpenCV_EXTRA_COMPONENTS})
119 - if(extra_component MATCHES "^-[lL]" OR extra_component MATCHES "[\\/]")
120 - set(maybe_l_prefix "")
121 + if(extra_component MATCHES "^-[lL]")
123 + set(libname "${extra_component}")
124 + elseif(extra_component MATCHES "[\\/]")
125 + get_filename_component(libdir "${extra_component}" PATH)
126 + list(APPEND OpenCV_LIB_COMPONENTS_ "-L${libdir}")
127 + get_filename_component(libname "${extra_component}" NAME_WE)
128 + string(REGEX REPLACE "^lib" "" libname "${libname}")
129 + set(libprefix "-l")
131 - set(maybe_l_prefix "-l")
132 + set(libprefix "-l")
133 + set(libname "${extra_component}")
136 - set(OpenCV_LIB_COMPONENTS "${OpenCV_LIB_COMPONENTS} ${maybe_l_prefix}${extra_component}")
137 + list(APPEND OpenCV_LIB_COMPONENTS_ "${libprefix}${libname}")
142 +list(REMOVE_DUPLICATES OpenCV_LIB_COMPONENTS_)
143 +string(REPLACE ";" " " OpenCV_LIB_COMPONENTS "${OpenCV_LIB_COMPONENTS_}")
145 #generate the .pc file
146 +set(prefix "${CMAKE_INSTALL_PREFIX}")
147 +set(exec_prefix "\${prefix}")
148 +set(libdir "\${exec_prefix}/${OPENCV_LIB_INSTALL_PATH}")
149 +set(includedir "\${prefix}/${OPENCV_INCLUDE_INSTALL_PATH}")
151 if(INSTALL_TO_MANGLED_PATHS)
152 set(OPENCV_PC_FILE_NAME "opencv-${OPENCV_VERSION}.pc")