Skip to content

Commit d4d7626

Browse files
committed
编译安装扩展
1 parent 62e5ace commit d4d7626

File tree

1 file changed

+82
-35
lines changed

1 file changed

+82
-35
lines changed

src/InstallCommand.php

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ protected function buildEnvDetection()
8383
*/
8484
protected function cloneOpenCV(string $directory)
8585
{
86+
87+
//检测OpenCV是否已经存在,如果已经存在则过滤
88+
if (file_exists($directory . '/opencv')) {
89+
//todo 切回到某个版本
90+
return;
91+
}
8692
$version = self::OPENCV_VERSION;
8793
$opencvUrl = 'https://github.com/opencv/opencv.git';
8894
$command = "git clone {$opencvUrl} --branch {$version} --depth 1";
@@ -104,6 +110,12 @@ protected function cloneOpenCV(string $directory)
104110
*/
105111
protected function cloneOpenCVContrib(string $directory)
106112
{
113+
114+
//检测OpenCV是否已经存在,如果已经存在则过滤
115+
if (file_exists($directory . '/opencv_contrib')) {
116+
//todo 切回到某个版本
117+
return;
118+
}
107119
$version = self::OPENCV_VERSION;
108120
$opencvContribUrl = 'https://github.com/opencv/opencv_contrib.git';
109121
$command = "git clone {$opencvContribUrl} --branch {$version} --depth 1";
@@ -117,6 +129,12 @@ protected function cloneOpenCVContrib(string $directory)
117129
}
118130

119131

132+
/**
133+
* 检测OpenCV是否已经安装
134+
* @author hihozhou
135+
*
136+
* @param OutputInterface $output
137+
*/
120138
protected function findExistOpenCV(OutputInterface $output)
121139
{
122140
$output->writeln('Try to find the installed OpenCV on the system via pkg-config...');
@@ -125,10 +143,12 @@ protected function findExistOpenCV(OutputInterface $output)
125143
$process->mustRun();
126144
$existOpencvVersion = $process->getOutput();
127145
$output->writeln('Found, opencv version is ' . $existOpencvVersion);
146+
return true;
128147

129148
} catch (\Exception $e) {
130149
//没有检测到opencv
131150
$output->writeln('Did not find opencv installed on the system.');
151+
return false;
132152
}
133153
}
134154

@@ -195,30 +215,38 @@ protected function createBaseDir($directory, OutputInterface $output)
195215
*/
196216
public function buildOpenCV($directory)
197217
{
218+
if (!file_exists($directory)) {
219+
$process = new Process(['mkdir', 'build'], $directory . '/opencv');
220+
try {
221+
$process->mustRun();
222+
} catch (\Exception $e) {
223+
throw new RuntimeException($process->getErrorOutput());
224+
}
225+
}
226+
227+
$cmakeCommand = 'cmake -D CMAKE_BUILD_TYPE=RELEASE';
228+
$cmakeCommand .= ' -D CMAKE_INSTALL_PREFIX=/usr/local';
229+
$cmakeCommand .= ' -D WITH_TBB=ON';
230+
$cmakeCommand .= ' -D WITH_V4L=ON';
231+
$cmakeCommand .= ' -D INSTALL_C_EXAMPLES=OFF';
232+
$cmakeCommand .= ' -D INSTALL_PYTHON_EXAMPLES=OFF';
233+
$cmakeCommand .= ' -D BUILD_EXAMPLES=OFF';
234+
$cmakeCommand .= ' -D BUILD_JAVA=OFF';
235+
$cmakeCommand .= ' -D BUILD_TESTS=OFF';
236+
$cmakeCommand .= ' -D WITH_QT=ON';
237+
$cmakeCommand .= ' -D WITH_OPENGL=ON';
238+
$cmakeCommand .= ' -D OPENCV_PYTHON_SKIP_DETECTION=ON';
239+
$cmakeCommand .= ' -D OPENCV_GENERATE_PKGCONFIG=ON';
240+
$cmakeCommand .= ' -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules';
241+
$cmakeCommand .= ' ..';
198242
//编译安装
199243
$commands = [
200-
'cd opencv',
201-
'mkdir build',
202-
'cd build',
203-
'cmake -D CMAKE_BUILD_TYPE=RELEASE \
204-
-D CMAKE_INSTALL_PREFIX=/usr/local \
205-
-D WITH_TBB=ON \
206-
-D WITH_V4L=ON \
207-
-D INSTALL_C_EXAMPLES=OFF \
208-
-D INSTALL_PYTHON_EXAMPLES=OFF \
209-
-D BUILD_EXAMPLES=OFF \
210-
-D BUILD_JAVA=OFF \
211-
-D BUILD_TESTS=OFF \
212-
-D WITH_QT=ON \
213-
-D WITH_OPENGL=ON \
214-
-D BUILD_opencv_world=ON \
215-
-D OPENCV_PYTHON_SKIP_DETECTION=ON \
216-
-D OPENCV_GENERATE_PKGCONFIG=ON \
217-
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..\
218-
&& make\
219-
&& sudo make install',
220-
'sh -c \'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf\''
221-
244+
'cd opencv/build',
245+
$cmakeCommand,
246+
'make',
247+
'sudo make install',
248+
'sudo sh -c \'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf\'',
249+
'sudo ldconfig'
222250
];
223251
$process = new Process(implode(' && ', $commands), $directory, null, null, null);
224252
$process->setTty(Process::isTtySupported());//检查TTY支持
@@ -231,15 +259,31 @@ public function buildOpenCV($directory)
231259

232260
protected function buildPHPOpenCV($directory)
233261
{
234-
$phpOpencvUrl = 'https://github.com/hihozhou/php-opencv.git';
235-
$command = "git clone {$phpOpencvUrl} --branch master --depth 1";
236-
$process = new Process($command, $directory, null, null, null);
237-
$process->setTty(Process::isTtySupported());//检查TTY支持
262+
263+
if (!file_exists($directory . '/php-opencv')) {
264+
$phpOpencvUrl = 'https://github.com/hihozhou/php-opencv.git';
265+
$command = "git clone {$phpOpencvUrl} --branch master --depth 1";
266+
$process = new Process($command, $directory, null, null, null);
267+
$process->setTty(Process::isTtySupported());//检查TTY支持
268+
try {
269+
$process->mustRun();
270+
} catch (\Exception $e) {
271+
throw new RuntimeException('Aborting.');
272+
}
273+
}
274+
238275
try {
276+
$commands = [
277+
'cd php-opencv',
278+
'phpize',//todo
279+
'./configure --with-php-config=/usr/bin/php-config',//todo
280+
'make',
281+
'sudo make install'
282+
];
283+
$process = new Process(implode(' && ', $commands), $directory, null, null, null);
284+
$process->setTty(Process::isTtySupported());//检查TTY支持
239285
$process->mustRun();
240286

241-
//
242-
243287
} catch (\Exception $e) {
244288
throw new RuntimeException('Aborting.');
245289
}
@@ -258,17 +302,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
258302
$this->checkIsRoot();
259303
$this->checkExtensionIsInstall($output);
260304
$this->buildEnvDetection();
261-
$this->findExistOpenCV($output);
262305
//创建目录
263306
$directory = $input->getOption('path');
264-
265307
$this->createBaseDir($directory, $output);
266-
//克隆项目
267-
$this->cloneOpenCV($directory);
268-
$this->cloneOpenCVContrib($directory);
269308

270-
//编译扩展
271-
$this->buildOpenCV($directory);
309+
if (!$this->findExistOpenCV($output)) {
310+
311+
//克隆项目
312+
$this->cloneOpenCV($directory);
313+
$this->cloneOpenCVContrib($directory);
314+
315+
//编译扩展
316+
$this->buildOpenCV($directory);
317+
}
318+
272319
//编译phpopencv扩展
273320
$this->buildPHPOpenCV($directory);
274321

0 commit comments

Comments
 (0)