blob: 8fda85708004bfebb1ce303ecc1e53c575397307 [file] [log] [blame]
Andrew Geisslerc5535c92023-01-27 16:10:19 -06001From: Victor Westerhuis <victor@westerhu.is>
2Date: Sat, 19 Nov 2022 21:45:39 +0100
3Subject: Do not embed build directory in binaries
4
5This makes the opencv core module build reproducibly.
6
7https://salsa.debian.org/science-team/opencv/-/raw/master/debian/patches/0008-Do-not-embed-build-directory-in-binaries.patch
8Upstream-Status: Backport
9Signed-off-by: Chee Yang Lee <chee.yang.lee@intel.com>
10---
11 modules/core/CMakeLists.txt | 10 ------
12 modules/core/include/opencv2/core/private.hpp | 4 +--
13 modules/core/include/opencv2/core/utility.hpp | 4 +--
14 modules/core/src/utils/datafile.cpp | 48 +--------------------------
15 4 files changed, 5 insertions(+), 61 deletions(-)
16
17diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt
18index b78bb98..d96b669 100644
19--- a/modules/core/CMakeLists.txt
20+++ b/modules/core/CMakeLists.txt
21@@ -188,16 +188,6 @@ if(OPENCV_OTHER_INSTALL_PATH)
22 ")
23 endif()
24
25-set(OPENCV_DATA_CONFIG_STR "${OPENCV_DATA_CONFIG_STR}
26-#define OPENCV_BUILD_DIR \"${CMAKE_BINARY_DIR}\"
27-")
28-
29-file(RELATIVE_PATH SOURCE_DIR_RELATIVE ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR})
30-set(OPENCV_DATA_CONFIG_STR "${OPENCV_DATA_CONFIG_STR}
31-#define OPENCV_DATA_BUILD_DIR_SEARCH_PATHS \\
32- \"${SOURCE_DIR_RELATIVE}/\"
33-")
34-
35 if(WIN32)
36 file(RELATIVE_PATH INSTALL_DATA_DIR_RELATIVE "${CMAKE_INSTALL_PREFIX}/${OPENCV_BIN_INSTALL_PATH}" "${CMAKE_INSTALL_PREFIX}/${OPENCV_OTHER_INSTALL_PATH}")
37 else()
38diff --git a/modules/core/include/opencv2/core/private.hpp b/modules/core/include/opencv2/core/private.hpp
39index 146d37f..dd675bc 100644
40--- a/modules/core/include/opencv2/core/private.hpp
41+++ b/modules/core/include/opencv2/core/private.hpp
42@@ -818,10 +818,10 @@ Search directories:
43 2. Check path specified by configuration parameter with "_HINT" suffix (name of environment variable).
44 3. Check path specified by configuration parameter (name of environment variable).
45 If parameter value is not empty and nothing is found then stop searching.
46-4. Detects build/install path based on:
47+4. Detects install path based on:
48 a. current working directory (CWD)
49 b. and/or binary module location (opencv_core/opencv_world, doesn't work with static linkage)
50-5. Scan `<source>/{,data}` directories if build directory is detected or the current directory is in source tree.
51+5. Scan `<source>/{,data}` directories if the current directory is in source tree.
52 6. Scan `<install>/share/OpenCV` directory if install directory is detected.
53
54 @param relative_path Relative path to data file
55diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp
56index 108c0d9..f15d9ea 100644
57--- a/modules/core/include/opencv2/core/utility.hpp
58+++ b/modules/core/include/opencv2/core/utility.hpp
59@@ -1163,10 +1163,10 @@ Search directories:
60 2. OPENCV_SAMPLES_DATA_PATH_HINT environment variable
61 3. OPENCV_SAMPLES_DATA_PATH environment variable
62 If parameter value is not empty and nothing is found then stop searching.
63-4. Detects build/install path based on:
64+4. Detects install path based on:
65 a. current working directory (CWD)
66 b. and/or binary module location (opencv_core/opencv_world, doesn't work with static linkage)
67-5. Scan `<source>/{,data,samples/data}` directories if build directory is detected or the current directory is in source tree.
68+5. Scan `<source>/{,data,samples/data}` directories if the current directory is in source tree.
69 6. Scan `<install>/share/OpenCV` directory if install directory is detected.
70
71 @see cv::utils::findDataFile
72diff --git a/modules/core/src/utils/datafile.cpp b/modules/core/src/utils/datafile.cpp
73index 3af83a5..6bda857 100644
74--- a/modules/core/src/utils/datafile.cpp
75+++ b/modules/core/src/utils/datafile.cpp
76@@ -280,32 +280,8 @@ cv::String findDataFile(const cv::String& relative_path,
77
78
79 // Steps: 4, 5, 6
80- cv::String cwd = utils::fs::getcwd();
81- cv::String build_dir(OPENCV_BUILD_DIR);
82- bool has_tested_build_directory = false;
83- if (isSubDirectory(build_dir, cwd) || isSubDirectory(utils::fs::canonical(build_dir), utils::fs::canonical(cwd)))
84- {
85- CV_LOG_DEBUG(NULL, "utils::findDataFile(): the current directory is build sub-directory: " << cwd);
86- const char* build_subdirs[] = { OPENCV_DATA_BUILD_DIR_SEARCH_PATHS };
87- for (size_t k = 0; k < sizeof(build_subdirs)/sizeof(build_subdirs[0]); k++)
88- {
89- CV_LOG_DEBUG(NULL, "utils::findDataFile(): <build>/" << build_subdirs[k]);
90- cv::String datapath = utils::fs::join(build_dir, build_subdirs[k]);
91- if (utils::fs::isDirectory(datapath))
92- {
93- for(size_t i = search_subdir.size(); i > 0; i--)
94- {
95- const cv::String& subdir = search_subdir[i - 1];
96- cv::String prefix = utils::fs::join(datapath, subdir);
97- TRY_FILE_WITH_PREFIX(prefix);
98- }
99- }
100- }
101- has_tested_build_directory = true;
102- }
103-
104 cv::String source_dir;
105- cv::String try_source_dir = cwd;
106+ cv::String try_source_dir = utils::fs::getcwd();
107 for (int levels = 0; levels < 3; ++levels)
108 {
109 if (utils::fs::exists(utils::fs::join(try_source_dir, "modules/core/include/opencv2/core/version.hpp")))
110@@ -341,28 +317,6 @@ cv::String findDataFile(const cv::String& relative_path,
111 CV_LOG_INFO(NULL, "Can't detect module binaries location");
112 }
113
114- if (!has_tested_build_directory &&
115- (isSubDirectory(build_dir, module_path) || isSubDirectory(utils::fs::canonical(build_dir), utils::fs::canonical(module_path)))
116- )
117- {
118- CV_LOG_DEBUG(NULL, "utils::findDataFile(): the binary module directory is build sub-directory: " << module_path);
119- const char* build_subdirs[] = { OPENCV_DATA_BUILD_DIR_SEARCH_PATHS };
120- for (size_t k = 0; k < sizeof(build_subdirs)/sizeof(build_subdirs[0]); k++)
121- {
122- CV_LOG_DEBUG(NULL, "utils::findDataFile(): <build>/" << build_subdirs[k]);
123- cv::String datapath = utils::fs::join(build_dir, build_subdirs[k]);
124- if (utils::fs::isDirectory(datapath))
125- {
126- for(size_t i = search_subdir.size(); i > 0; i--)
127- {
128- const cv::String& subdir = search_subdir[i - 1];
129- cv::String prefix = utils::fs::join(datapath, subdir);
130- TRY_FILE_WITH_PREFIX(prefix);
131- }
132- }
133- }
134- }
135-
136 #if defined OPENCV_INSTALL_DATA_DIR_RELATIVE
137 if (!module_path.empty()) // require module path
138 {