From 61bd1d3b0c4adfbdd15ec6fdcb2de3233643ba08 Mon Sep 17 00:00:00 2001 From: yejianwu Date: Thu, 9 Aug 2018 15:46:29 +0800 Subject: [PATCH] fix link for libmace.so and libmodels.a --- docs/user_guide/advanced_usage.rst | 3 +++ mace/codegen/BUILD | 8 +----- mace/examples/cli/BUILD | 2 +- mace/libmace/mace_version_script.lds | 14 +++++++++++ mace/tools/validation/BUILD | 2 +- tools/bazel.rc | 4 ++- tools/build-standalone-lib.sh | 6 ++--- tools/converter.py | 37 +++++++++++++++------------- tools/sh_commands.py | 3 +++ 9 files changed, 49 insertions(+), 30 deletions(-) diff --git a/docs/user_guide/advanced_usage.rst b/docs/user_guide/advanced_usage.rst index dcef4c6f..c0782ff4 100644 --- a/docs/user_guide/advanced_usage.rst +++ b/docs/user_guide/advanced_usage.rst @@ -378,6 +378,9 @@ Reduce Library Size - Futher more, if only ``cpu`` device needed, change ``--define opencl=true`` to ``false``. This way will reduce half of library size to about ``700KB`` for ``armeabi-v7a`` and ``1000KB`` for ``arm64-v8a`` + - About ``300KB`` can be reduced when add ``--config symbol_hidden`` building option. It will change + the visibility of inner apis of libmace.so and lead to linking error when load model(s) in ``code``. + * **static library** - The methods in dynamic library can be useful for static library too. In additional, the static diff --git a/mace/codegen/BUILD b/mace/codegen/BUILD index e42a1b13..1c76e8dd 100644 --- a/mace/codegen/BUILD +++ b/mace/codegen/BUILD @@ -44,13 +44,7 @@ cc_library( cc_library( name = "generated_libmace", - srcs = glob(["lib/*.so"]), - visibility = ["//visibility:public"], -) - -cc_library( - name = "generated_libmace_static", - srcs = glob(["lib/*.a"]), + srcs = glob(["lib/*"]), linkstatic = 1, visibility = ["//visibility:public"], ) diff --git a/mace/examples/cli/BUILD b/mace/examples/cli/BUILD index 7767980a..6599acae 100644 --- a/mace/examples/cli/BUILD +++ b/mace/examples/cli/BUILD @@ -29,7 +29,7 @@ cc_binary( deps = [ "//external:gflags_nothreads", "//mace/codegen:generated_mace_engine_factory", - "//mace/codegen:generated_libmace_static", + "//mace/codegen:generated_libmace", ] + if_hexagon_enabled([ "//third_party/nnlib:libhexagon", ]), diff --git a/mace/libmace/mace_version_script.lds b/mace/libmace/mace_version_script.lds index 27cc2caa..76d8f1c2 100644 --- a/mace/libmace/mace_version_script.lds +++ b/mace/libmace/mace_version_script.lds @@ -12,6 +12,20 @@ mace { *SetOpenMPThreadAffinity*; *GetBigLittleCoreIDs*; *MaceVersion*; + + # api for static library of models + *mace*logging*LogMessage*; + *mace*NetDef*; + *mace*MemoryArena*; + *mace*InputInfo*; + *mace*OutputInfo*; + *mace*OutputShape*; + *mace*OperatorDef*; + *mace*ConstTensor*; + *mace*Argument*; + *mace*MemoryBlock*; + *google*protobuf*; + local: *; }; diff --git a/mace/tools/validation/BUILD b/mace/tools/validation/BUILD index 1dad55bc..5a5a8da3 100644 --- a/mace/tools/validation/BUILD +++ b/mace/tools/validation/BUILD @@ -42,7 +42,7 @@ cc_binary( deps = [ "//external:gflags_nothreads", "//mace/codegen:generated_mace_engine_factory", + "//mace/codegen:generated_models", "//mace/libmace:libmace_dynamic", - "//mace/utils:utils", ], ) diff --git a/tools/bazel.rc b/tools/bazel.rc index 22357b79..f0907c52 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -11,6 +11,9 @@ build --copt=-D_GLIBCXX_USE_C99_MATH_TR1 build --copt=-DMACE_OBFUSCATE_LITERALS build --copt=-DGEMMLOWP_USE_OPENMP +# Usage example: bazel build --config symbol_hidden +build:symbol_hidden --copt=-fvisibility=hidden + # Usage example: bazel build --config android build:android --crosstool_top=//external:android/crosstool build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain @@ -23,7 +26,6 @@ build:optimization --linkopt=-Wl,--strip-all build:optimization --copt=-ffunction-sections build:optimization --copt=-fdata-sections build:optimization --linkopt=-Wl,--gc-sections -build:optimization --copt=-fvisibility=hidden # Address sanitizer build:asan --strip=never diff --git a/tools/build-standalone-lib.sh b/tools/build-standalone-lib.sh index 59f0b8d8..a08fb3b0 100755 --- a/tools/build-standalone-lib.sh +++ b/tools/build-standalone-lib.sh @@ -44,16 +44,16 @@ fi # build static libraries echo "build static lib for armeabi-v7a + cpu_gpu_dsp" -bazel build --config android --config optimization mace/libmace:libmace_static --define neon=true --define openmp=true --define opencl=true --define hexagon=true --cpu=armeabi-v7a +bazel build --config android --config optimization mace/libmace:libmace_static --config symbol_hidden --define neon=true --define openmp=true --define opencl=true --define hexagon=true --cpu=armeabi-v7a cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/armeabi-v7a/cpu_gpu_dsp/ cp third_party/nnlib/*so $LIB_DIR/armeabi-v7a/cpu_gpu_dsp/ echo "build static lib for armeabi-v7a + cpu_gpu" -bazel build --config android --config optimization mace/libmace:libmace_static --define neon=true --define openmp=true --define opencl=true --cpu=armeabi-v7a +bazel build --config android --config optimization mace/libmace:libmace_static --config symbol_hidden --define neon=true --define openmp=true --define opencl=true --cpu=armeabi-v7a cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/armeabi-v7a/cpu_gpu/ echo "build static lib for arm64-v8a + cpu_gpu" -bazel build --config android --config optimization mace/libmace:libmace_static --define neon=true --define openmp=true --define opencl=true --cpu=arm64-v8a +bazel build --config android --config optimization mace/libmace:libmace_static --config symbol_hidden --define neon=true --define openmp=true --define opencl=true --cpu=arm64-v8a cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/arm64-v8a/cpu_gpu/ if [[ "$OSTYPE" != "darwin"* ]];then diff --git a/tools/converter.py b/tools/converter.py index e78b54bc..86b72a9c 100644 --- a/tools/converter.py +++ b/tools/converter.py @@ -786,7 +786,8 @@ def build_model_lib(configs, address_sanitizer): abi=target_abi, hexagon_mode=hexagon_mode, enable_opencl=get_opencl_mode(configs), - address_sanitizer=address_sanitizer + address_sanitizer=address_sanitizer, + symbol_hidden=True ) sh.cp("-f", MODEL_LIB_PATH, model_lib_output_path) @@ -880,8 +881,10 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer, sh.rm("-rf", build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir) + symbol_hidden = True mace_run_target = MACE_RUN_STATIC_TARGET if mace_lib_type == MACELibType.dynamic: + symbol_hidden = False mace_run_target = MACE_RUN_DYNAMIC_TARGET build_arg = "" if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: @@ -897,6 +900,7 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer, enable_openmp=enable_openmp, enable_opencl=get_opencl_mode(configs), address_sanitizer=address_sanitizer, + symbol_hidden=symbol_hidden, extra_args=build_arg ) sh_commands.update_mace_run_binary(build_tmp_binary_dir, @@ -924,6 +928,7 @@ def build_quantize_stat(configs): quantize_stat_target, abi=ABIType.host, enable_openmp=True, + symbol_hidden=True, extra_args=build_arg ) @@ -943,36 +948,39 @@ def build_example(configs, target_abi, enable_openmp, mace_lib_type): sh.rm("-rf", build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir) + symbol_hidden = True libmace_target = LIBMACE_STATIC_TARGET if mace_lib_type == MACELibType.dynamic: + symbol_hidden = False libmace_target = LIBMACE_SO_TARGET sh_commands.bazel_build(libmace_target, abi=target_abi, enable_openmp=enable_openmp, enable_opencl=get_opencl_mode(configs), - hexagon_mode=hexagon_mode) + hexagon_mode=hexagon_mode, + symbol_hidden=symbol_hidden) if os.path.exists(LIB_CODEGEN_DIR): sh.rm("-rf", LIB_CODEGEN_DIR) sh.mkdir("-p", LIB_CODEGEN_DIR) build_arg = "" + if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: + mace_check(os.path.exists(ENGINE_CODEGEN_DIR), + ModuleName.RUN, + "You should convert model first.") + model_lib_path = get_model_lib_output_path(library_name, + target_abi) + sh.cp("-f", model_lib_path, LIB_CODEGEN_DIR) + build_arg = "--per_file_copt=mace/examples/cli/example.cc@-DMODEL_GRAPH_FORMAT_CODE" # noqa + if mace_lib_type == MACELibType.dynamic: example_target = EXAMPLE_DYNAMIC_TARGET sh.cp("-f", LIBMACE_DYNAMIC_PATH, LIB_CODEGEN_DIR) else: - sh.cp("-f", LIBMACE_STATIC_PATH, LIB_CODEGEN_DIR) - if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: - mace_check(os.path.exists(ENGINE_CODEGEN_DIR), - ModuleName.RUN, - "You should convert model first.") - model_lib_path = get_model_lib_output_path(library_name, - target_abi) - sh.cp("-f", model_lib_path, LIB_CODEGEN_DIR) - build_arg = "--per_file_copt=mace/examples/cli/example.cc@-DMODEL_GRAPH_FORMAT_CODE" # noqa - example_target = EXAMPLE_STATIC_TARGET + sh.cp("-f", LIBMACE_STATIC_PATH, LIB_CODEGEN_DIR) sh_commands.bazel_build(example_target, abi=target_abi, @@ -1325,11 +1333,6 @@ def print_package_summary(package_path): def run_mace(flags): configs = format_model_config(flags) - if flags.mace_lib_type == MACELibType.dynamic and \ - configs[YAMLKeyword.model_graph_format] == ModelFormat.code: - MaceLogger.error(ModuleName.YAML_CONFIG, - "If you want to link MACE dynamic library, " - "you must use file-type MACE model.") clear_build_dirs(configs[YAMLKeyword.library_name]) diff --git a/tools/sh_commands.py b/tools/sh_commands.py index 4969262d..f8e09e80 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -279,6 +279,7 @@ def bazel_build(target, enable_neon=True, enable_opencl=True, address_sanitizer=False, + symbol_hidden=False, extra_args=""): print("* Build %s with ABI %s" % (target, abi)) if abi == "host": @@ -307,6 +308,8 @@ def bazel_build(target, bazel_args += ("--config", "asan") else: bazel_args += ("--config", "optimization") + if symbol_hidden: + bazel_args += ("--config", "symbol_hidden") if extra_args: bazel_args += (extra_args, ) print bazel_args -- GitLab