diff --git a/mace/BUILD b/mace/BUILD new file mode 100644 index 0000000000000000000000000000000000000000..17a4be41ce7a07fb8d8a69f6e9b021b6eb0fa45e --- /dev/null +++ b/mace/BUILD @@ -0,0 +1,25 @@ +config_setting( + name = "android", + values = { + "crosstool_top": "//external:android/crosstool", + }, + visibility = ["//visibility:public"], +) + +config_setting( + name = "android_armv7", + values = { + "crosstool_top": "//external:android/crosstool", + "android_cpu": "armeabi-v7a", + }, + visibility = ["//visibility:public"], +) + +config_setting( + name = "android_arm64", + values = { + "crosstool_top": "//external:android/crosstool", + "android_cpu": "arm64-v8a", + }, + visibility = ["//visibility:public"], +) diff --git a/mace/core/BUILD b/mace/core/BUILD index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b6b2cdbd285f452f4e76a4e0edb945979f7ea15b 100644 --- a/mace/core/BUILD +++ b/mace/core/BUILD @@ -0,0 +1,10 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "lib_core", + hdrs = [ + "logging.h" + ], + srcs = [ + ], + ) diff --git a/mace/core/logging.h b/mace/core/logging.h new file mode 100644 index 0000000000000000000000000000000000000000..ba5e4c2a753c3580dad8dc98438854ca53c36c6d --- /dev/null +++ b/mace/core/logging.h @@ -0,0 +1,60 @@ +#ifndef MACE_COMMON_LOGGING_H_ +#define MACE_COMMON_LOGGING_H_ + +#ifdef __ANDROID__ +#include +#else +#include +#endif + +namespace mace { + +const int FATAL = 0; +const int ERROR = 1; +const int WARN = 2; +const int INFO = 3; +const int DEBUG = 4; +const int VERBOSE = 5; + +namespace internal { + +const char *kTag = "MACE"; + + +#ifdef __ANDROID__ + +#define _MACE_LOG_FATAL \ + do { \ + __android_log_print(ANDROID_LOG_FATAL, mace::internal::kTag, __VA_ARGS__); \ + abort(); \ + } while (0) + +#define _MACE_LOG_ERROR(...) \ + __android_log_print(ANDROID_LOG_ERROR, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_WARN(...) \ + __android_log_print(ANDROID_LOG_WARN, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_INFO(...) \ + __android_log_print(ANDROID_LOG_INFO, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_DEBUG(...) \ + __android_log_print(ANDROID_LOG_DEBUG, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_VERBOSE(...) \ + __android_log_print(ANDROID_LOG_VERBOSE, mace::internal::kTag, __VA_ARGS__) + + +#define LOG(severity, ...) _MACE_LOG_##severity(__VA_ARGS__) + +#else // Non Android, just for tests + +// TODO(heliangliang): Fix newline +#define LOG(severity, ...) \ + do { \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) + +#endif // __ANDROID__ + +} // namespace internal +} // namespace mace + +#endif // MACE_COMMON_LOGGING_H_ diff --git a/mace/examples/BUILD b/mace/examples/BUILD index 887f401b1d191aef3bcbd67e5ee35b20d22fbc80..e932dd3804d0e139a3cadcee4ee7de4bbfef1086 100644 --- a/mace/examples/BUILD +++ b/mace/examples/BUILD @@ -1,11 +1,16 @@ # Examples +load( + "//mace:mace.bzl", + "if_android", + ) cc_binary( name = "helloworld", srcs = [ "helloworld.cc", ], - linkopts = [ - "-pie", - ] + deps = [ + "//mace/core:lib_core", + ], + linkopts = if_android(["-pie", "-llog"]) ) diff --git a/mace/examples/README.md b/mace/examples/README.md index 25d1f638e91a70e632040d091bce2f0f207192a7..4feb5c8d88ba45f7ff84b5e28a145843a3d0a1c3 100644 --- a/mace/examples/README.md +++ b/mace/examples/README.md @@ -1,7 +1,7 @@ Examples ======= -* Build the example +* Build the example (e.g., with armeabi-v7a target) ``` bazel build mace/examples:helloworld \ --crosstool_top=//external:android/crosstool \ @@ -9,7 +9,7 @@ bazel build mace/examples:helloworld \ --cpu=armeabi-v7a ``` -* To run adb inside docker, the network should use 'host': +* To run adb inside docker, the container network should use 'host' ``` docker run -it --net=host mace-dev /bin/bash ``` @@ -20,3 +20,8 @@ adb shell "mkdir /data/local/tmp/helloword" adb shell push bazel-bin/mace/examples/helloworld /data/local/tmp/helloword adb shell /data/local/tmp/helloword/helloworld ``` + +* Check the logs +``` +adb logcat | grep MACE +``` diff --git a/mace/examples/helloworld.cc b/mace/examples/helloworld.cc index 5b38b4bdce8898e79f44c63fb3c46d78a901b594..f4883a85c3ad696bb9cae1caccf0a2904ab9581c 100644 --- a/mace/examples/helloworld.cc +++ b/mace/examples/helloworld.cc @@ -1,6 +1,7 @@ -#include +#include "mace/core/logging.h" int main() { - printf("Hello world\n"); + LOG(INFO, "Hello World"); + return 0; } diff --git a/mace/mace.bzl b/mace/mace.bzl new file mode 100644 index 0000000000000000000000000000000000000000..3b57e72421beaecbdc19deb9a1cfb0174fd11831 --- /dev/null +++ b/mace/mace.bzl @@ -0,0 +1,25 @@ +# -*- Python -*- + +def if_android(a): + return select({ + "//mace:android": a, + "//conditions:default": [], + }) + +def if_not_android(a): + return select({ + "//mace:android": [], + "//conditions:default": a, + }) + +def if_android_armv7(a): + return select({ + "//mace:android_armv7": a, + "//conditions:default": [], + }) + +def if_android_arm64(a): + return select({ + "//mace:android_arm64": a, + "//conditions:default": [], + }) diff --git a/mace/utils/BUILD b/mace/utils/BUILD deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000