Skip to content

Commit

Permalink
feat(sdk): build for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Jan 21, 2021
1 parent 515c2b9 commit aab99a8
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 20 deletions.
8 changes: 8 additions & 0 deletions C/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ build:linux --cxxopt=-std=c++17
build:linux --host_cxxopt=-std=c++17
build:linux --copt=-w

# windows
build:windows --cxxopt=/std:c++17
build:windows --host_cxxopt=/std:c++17
build:windows --copt=/w
# For using M_* math constants on Windows with MSVC.
build:windows --copt=/D_USE_MATH_DEFINES
build:windows --host_copt=/D_USE_MATH_DEFINES

# macOS
build:macos --cxxopt=-std=c++17
build:macos --host_cxxopt=-std=c++17
Expand Down
1 change: 1 addition & 0 deletions C/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ http_archive(
sha256 = "7ae90bdab3b602fc4782c1f8f00d7c05834ec2b9b3d638fe7fa80623ccc0aa9e",
strip_prefix = "mediapipe-0.8.2",
patches = [
"@//third_party:mediapipe_windows.patch",
"@//third_party:mediapipe_visibility.diff",
"@//third_party:mediapipe_model_path.diff",
"@//third_party:mediapipe_extension.diff",
Expand Down
9 changes: 9 additions & 0 deletions C/mediapipe_api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ cc_binary(
visibility = ["//visibility:public"],
)

cc_binary(
name = "libmediapipe_c.dll",
deps = [":mediapipe_c"],
linkshared = True,
linkstatic = True,
visibility = ["//visibility:public"],
)

alias(
name = "libmediapipe_c",
actual = select({
"@com_google_mediapipe//mediapipe:macos": ":libmediapipe_c.dylib",
"@com_google_mediapipe//mediapipe:windows": ":libmediapipe_c.dll",
"//conditions:default": ":libmediapipe_c.so",
}),
)
Expand Down
2 changes: 2 additions & 0 deletions C/mediapipe_api/common.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "mediapipe_api/common.h"

#ifndef _WIN32
thread_local struct sigaction mp_api::orig_act;
thread_local sigjmp_buf mp_api::abrt_jbuf;

void mp_api::sigabrt_handler(int sig) {
siglongjmp(abrt_jbuf, 1);
}
#endif
26 changes: 20 additions & 6 deletions C/mediapipe_api/common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef C_MEDIAPIPE_API_COMMON_H_
#define C_MEDIAPIPE_API_COMMON_H_

#ifdef DLL_EXPORTS
#ifdef _WIN32
#define MP_CAPI_EXPORT __declspec(dllexport)
#else
#define MP_CAPI_EXPORT
Expand Down Expand Up @@ -30,26 +30,32 @@ enum class MpReturnCode : int {
};

namespace mp_api {
#ifndef _WIN32
extern thread_local struct sigaction orig_act;
extern thread_local sigjmp_buf abrt_jbuf;

extern void sigabrt_handler(int sig);
#endif
}

// TODO(homuler): make code more readable
// TODO: make code more readable
#define TRY auto volatile _mp_return_code = MpReturnCode::Unset;\
try
#define CATCH_EXCEPTION catch (std::exception& e) {\
LOG(ERROR) << e.what();\
google::FlushLogFiles(google::ERROR);\
google::FlushLogFiles(google::GLOG_ERROR);\
_mp_return_code = MpReturnCode::StandardError;\
} catch (...) {\
LOG(ERROR) << "Unknown exception occured";\
google::FlushLogFiles(google::ERROR);\
google::FlushLogFiles(google::GLOG_ERROR);\
_mp_return_code = MpReturnCode::UnknownError;\
}\
return _mp_return_code;

#ifdef _WIN32
#define TRY_ALL TRY
#define CATCH_ALL CATCH_EXCEPTION
#else
#define TRY_ALL TRY {\
struct sigaction act;\
sigemptyset(&act.sa_mask);\
Expand All @@ -59,12 +65,20 @@ namespace mp_api {
if (sigsetjmp(mp_api::abrt_jbuf, 1) == 0)
#define CATCH_ALL else {\
LOG(ERROR) << "Aborted";\
google::FlushLogFiles(google::ERROR);\
google::FlushLogFiles(google::GLOG_ERROR);\
_mp_return_code = MpReturnCode::Aborted;\
}\
sigaction(SIGABRT, &mp_api::orig_act, nullptr);\
} CATCH_EXCEPTION
#endif // _WIN32

#define RETURN_CODE(code) _mp_return_code = code

#define MP_CAPI(rettype) MP_CAPI_EXPORT extern rettype
#ifdef _WIN32
#define CDECL __cdecl
#else
#define CDECL
#endif // _WIN32

#define MP_CAPI(rettype) MP_CAPI_EXPORT extern rettype CDECL
#endif // C_MEDIAPIPE_API_COMMON_H_
39 changes: 39 additions & 0 deletions C/third_party/mediapipe_windows.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
diff --git a/mediapipe/calculators/tflite/tflite_inference_calculator.cc b/mediapipe/calculators/tflite/tflite_inference_calculator.cc
index 2c956d6..df8b3c7 100644
--- a/mediapipe/calculators/tflite/tflite_inference_calculator.cc
+++ b/mediapipe/calculators/tflite/tflite_inference_calculator.cc
@@ -431,12 +431,14 @@ mediapipe::Status TfLiteInferenceCalculator::Process(CalculatorContext* cc) {

// 1. Receive pre-processed tensor inputs.
if (gpu_input_) {
- MP_RETURN_IF_ERROR(ProcessInputsGpu(cc,
#if MEDIAPIPE_TFLITE_METAL_INFERENCE
+ MP_RETURN_IF_ERROR(ProcessInputsGpu(cc,
compute_encoder,
-#endif // MEDIAPIPE_TFLITE_METAL_INFERENCE
-
output_tensors_gpu.get()));
+#else
+ MP_RETURN_IF_ERROR(ProcessInputsGpu(cc,
+ output_tensors_gpu.get()));
+#endif // MEDIAPIPE_TFLITE_METAL_INFERENCE
} else {
MP_RETURN_IF_ERROR(ProcessInputsCpu(cc, output_tensors_cpu.get()));
}
@@ -462,11 +464,13 @@ mediapipe::Status TfLiteInferenceCalculator::Process(CalculatorContext* cc) {

// 3. Output processed tensors.
if (gpu_output_ || use_advanced_gpu_api_) {
- MP_RETURN_IF_ERROR(ProcessOutputsGpu(cc, std::move(output_tensors_cpu),
#if MEDIAPIPE_TFLITE_METAL_INFERENCE
- compute_encoder,
-#endif // MEDIAPIPE_TFLITE_METAL_INFERENCE
+ MP_RETURN_IF_ERROR(ProcessOutputsGpu(cc, std::move(output_tensors_cpu),
+ compute_encoder, std::move(output_tensors_gpu)));
+#else
+ MP_RETURN_IF_ERROR(ProcessOutputsGpu(cc, std::move(output_tensors_cpu),
std::move(output_tensors_gpu)));
+#endif // MEDIAPIPE_TFLITE_METAL_INFERENCE
} else {
MP_RETURN_IF_ERROR(ProcessOutputsCpu(cc, std::move(output_tensors_cpu)));
}
29 changes: 15 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
BUILD := default
MODE := gpu


builddir := .build
sdkdir := Assets/MediaPipe/SDK
Expand All @@ -8,7 +7,7 @@ modeldir := $(sdkdir)/Models
scriptdir := $(sdkdir)/Scripts

bazelflags.gpu := --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11
bazelflags.cpu := --define MEDIAPIPE_DISABLE_GPU=1
bazelflags.cpu := --define MEDIAPIPE_DISABLE_GPU=1 --action_env PYTHON_BIN_PATH="$(PYTHON_BIN_PATH)"
bazelflags.android_arm := --config=android_arm
bazelflags.android_arm64 := --config=android_arm64
bazelflags.ios_arm64 := --config=ios_arm64 --copt=-fembed-bitcode --apple_bitcode=embedded
Expand All @@ -23,11 +22,11 @@ protobuf_bindir := $(protobuf_csharpdir)/src/Google.Protobuf/bin/Release/net45
protobuf_dll := $(protobuf_bindir)/Google.Protobuf.dll

bazel_root := C/bazel-bin/mediapipe_api
bazel_desktop_target := //mediapipe_api:libmediapipe_c
bazel_android_target := //mediapipe_api/java/org/homuler/mediapipe/unity:mediapipe_android
bazel_ios_target := //mediapipe_api/objc:MediaPipeUnity
bazel_models_target := //mediapipe_api:mediapipe_models
bazel_protos_target := //mediapipe_api:mediapipe_proto_srcs
bazel_desktop_target := mediapipe_api:libmediapipe_c
bazel_android_target := mediapipe_api/java/org/homuler/mediapipe/unity:mediapipe_android
bazel_ios_target := mediapipe_api/objc:MediaPipeUnity
bazel_models_target := mediapipe_api:mediapipe_models
bazel_protos_target := mediapipe_api:mediapipe_proto_srcs
bazel_common_target := $(bazel_models_target) $(bazel_protos_target)

.PHONY: all gpu cpu android_arm android_arm64 ios_arm64 clean \
Expand All @@ -36,19 +35,19 @@ bazel_common_target := $(bazel_models_target) $(bazel_protos_target)

# build
gpu: | $(protobuf_dll)
cd C && bazel build -c opt ${bazelflags.gpu} $(bazel_desktop_target) $(bazel_common_target)
cd C && bazel build -c opt $(bazelflags.gpu) $(bazel_desktop_target) $(bazel_common_target)

cpu: | $(protobuf_dll)
cd C && bazel build -c opt ${bazelflags.cpu} $(bazel_desktop_target) $(bazel_common_target)
cd C && bazel build -c opt $(bazelflags.cpu) $(bazel_desktop_target) $(bazel_common_target)

android_arm: | $(protobuf_dll)
cd C && bazel build -c opt ${bazelflags.android_arm} $(bazel_android_target) $(bazel_common_target)
cd C && bazel build -c opt $(bazelflags.android_arm) $(bazel_android_target) $(bazel_common_target)

android_arm64: | $(protobuf_dll)
cd C && bazel build -c opt ${bazelflags.android_arm64} $(bazel_android_target) $(bazel_common_target)
cd C && bazel build -c opt $(bazelflags.android_arm64) $(bazel_android_target) $(bazel_common_target)

ios_arm64: | $(protobuf_dll)
cd C && bazel build -c opt ${bazelflags.ios_arm64} $(bazel_ios_target) $(bazel_common_target)
cd C && bazel build -c opt $(bazelflags.ios_arm64_ $(bazel_ios_target) $(bazel_common_target)

$(plugindir)/Google.Protobuf.dll: Temp/$(protobuf_tarball)
cd Temp/protobuf-$(protobuf_version)/csharp && ./buildall.sh && mv src/Google.Protobuf/bin/Release/net45/* ../../../$(plugindir)
Expand All @@ -72,6 +71,8 @@ ifneq ("$(wildcard $(bazel_root)/libmediapipe_c.so)", "")
cp -f $(bazel_root)/libmediapipe_c.so $(plugindir)
else ifneq ("$(wildcard $(bazel_root)/libmediapipe_c.dylib)", "")
cp -f $(bazel_root)/libmediapipe_c.dylib $(plugindir)
else ifneq ("$(wildcard $(bazel_root)/libmediapipe_c.dll)", "")
cp -f $(bazel_root)/libmediapipe_c.dll $(plugindir)
else
# skip installing libmediapipe_c
endif
Expand Down Expand Up @@ -111,7 +112,7 @@ uninstall-protobuf:
rm -r $(plugindir)/Protobuf

uninstall-mediapipe_c:
rm -f $(plugindir)/libmediapipe_c.so && rm -f $(plugindir)/libmediapipe_c.dylib
rm -f $(plugindir)/libmediapipe_c.so && rm -f $(plugindir)/libmediapipe_c.dylib && rm -f $(plugindir)/libmediapipe_c.dll

uninstall-mediapipe_android:
rm -f $(plugindir)/Android/mediapipe_android.aar
Expand Down

0 comments on commit aab99a8

Please sign in to comment.