Skip to content

Commit

Permalink
build: cmake OpenCV DLL (#498)
Browse files Browse the repository at this point in the history
* wip build opencv dll

* build: add bazel_ruls_dict's sha256

* build: include opencv_world by default

* add libopencv_world.so.meta

* wip: fix windows path

* wip: build opencv_world on macOS

* wip: OpenCV refactoring test

* wip: load libopencv_world.so at startup

* refactor: OpenCV build

* wip: build OpenCV world on Windows

* fix: uninstall command fails on Windows

* rename options
  • Loading branch information
homuler authored Mar 21, 2022
1 parent b1c7b61 commit 881642e
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 187 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,20 @@ http_archive(
urls = ["https://github.com/bazelbuild/rules_cc/archive/main.zip"],
)

http_archive(
name = "bazel_rules_dict",
strip_prefix = "bazel_rules_dict-0.1.1",
sha256 = "00adce0dc43d7ef39dcb7f59f8cc5644cde02766bb193f342ecff13d70f60b07",
urls = [
"https://github.com/homuler/bazel_rules_dict/archive/refs/tags/v0.1.1.tar.gz",
],
)

http_archive(
name = "rules_foreign_cc",
sha256 = "30c970bfaeda3485100c62b13093da2be2c70ed99ec8d30f4fac6dd37cb25f34",
strip_prefix = "rules_foreign_cc-0.6.0",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.6.0.zip",
sha256 = "47f94195f144952c5a47245363d4a27b0e7ef3037a58ecf13aca8b5dbe3c2609",
strip_prefix = "rules_foreign_cc-feat-cache_entries_target",
url = "https://github.com/homuler/rules_foreign_cc/archive/feat/cache_entries_target.zip",
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
Expand Down
31 changes: 7 additions & 24 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def _copytree(self, source, dest):

def _remove(self, path):
self.console.v(f"Removing '{path}'...")
os.remove(path)
try:
os.remove(path)
except PermissionError:
self._run_command(['rm', path])

def _rmtree(self, path):
if os.path.exists(path):
Expand All @@ -105,7 +108,6 @@ def __init__(self, command_args):
self.resources = command_args.args.resources
self.analyzers = command_args.args.analyzers
self.opencv = command_args.args.opencv
self.include_opencv_libs = command_args.args.include_opencv_libs

self.compilation_mode = command_args.args.compilation_mode
self.linkopt = command_args.args.linkopt
Expand Down Expand Up @@ -146,15 +148,6 @@ def run(self):
os.path.join(_BAZEL_BIN_PATH, 'mediapipe_api', 'mediapipe_desktop.zip'),
os.path.join(_BUILD_PATH, 'Plugins'))

if self.include_opencv_libs:
if self.opencv == 'cmake':
self.console.warn('OpenCV objects are included in libmediapipe_c, so skip copying OpenCV library files')
else:
self._run_command(self._build_opencv_libs())
self._unzip(
os.path.join(_BAZEL_BIN_PATH, 'mediapipe_api', 'opencv_libs.zip'),
os.path.join(_BUILD_PATH, 'Plugins'))

self.console.info('Built native libraries for Desktop')

if self.android:
Expand Down Expand Up @@ -245,7 +238,8 @@ def _build_linkopt(self):
return ['--linkopt={}'.format(l) for l in self.linkopt]

def _build_opencv_switch(self):
commands = [f'--@opencv//:switch={self.opencv}']
switch = 'cmake_static' if self.opencv == 'cmake' else self.opencv
commands = [f'--@opencv//:switch={switch}']

return commands

Expand All @@ -270,16 +264,6 @@ def _build_desktop_commands(self):
commands.append('//mediapipe_api:mediapipe_desktop')
return commands

def _build_opencv_libs(self):
if not self.include_opencv_libs:
return []

commands = self._build_common_commands()
commands += self._build_desktop_options()
commands.append('//mediapipe_api:opencv_libs')

return commands

def _build_android_commands(self):
if self.android is None:
return []
Expand Down Expand Up @@ -420,8 +404,7 @@ def __init__(self):
build_command_parser.add_argument('--resources', action=argparse.BooleanOptionalAction, default=True)
build_command_parser.add_argument('--analyzers', action=argparse.BooleanOptionalAction, default=False, help='Install Roslyn Analyzers')
build_command_parser.add_argument('--compilation_mode', '-c', choices=['fastbuild', 'opt', 'dbg'], default='opt')
build_command_parser.add_argument('--opencv', choices=['local', 'cmake'], default='local', help='Decide to which OpenCV to link for Desktop native libraries')
build_command_parser.add_argument('--include_opencv_libs', action='store_true', help='Include OpenCV\'s native libraries for Desktop')
build_command_parser.add_argument('--opencv', choices=['local', 'cmake', 'cmake_static', 'cmake_dynamic'], default='local', help='Decide to which OpenCV to link for Desktop native libraries')
build_command_parser.add_argument('--linkopt', '-l', action='append', help='Linker options')
build_command_parser.add_argument('--verbose', '-v', action='count', default=0)

Expand Down
17 changes: 4 additions & 13 deletions mediapipe_api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ cc_library(

pkg_zip(
name = "mediapipe_desktop",
srcs = [":libmediapipe_c"],
srcs = [
":libmediapipe_c",
"@opencv//:opencv_world_dll",
],
)

pkg_asset(
Expand Down Expand Up @@ -205,15 +208,3 @@ pkg_zip(
"//mediapipe_api/util:proto_srcs",
],
)

pkg_zip(
name = "opencv_libs",
srcs = select({
"@opencv//:source_build": ["@opencv//:opencv_libs"],
"@com_google_mediapipe//mediapipe:windows": ["@windows_opencv//:opencv_libs"],
"@com_google_mediapipe//mediapipe:macos": [],
"@com_google_mediapipe//mediapipe:android": [],
"@com_google_mediapipe//mediapipe:ios": [],
"//conditions:default": ["@linux_opencv//:opencv_libs"],
}),
)
21 changes: 0 additions & 21 deletions mediapipe_api/defs.bzl

This file was deleted.

Loading

0 comments on commit 881642e

Please sign in to comment.