提交 61bd1d3b 编写于 作者: Y yejianwu

fix link for libmace.so and libmodels.a

上级 f291a048
...@@ -378,6 +378,9 @@ Reduce Library Size ...@@ -378,6 +378,9 @@ Reduce Library Size
- Futher more, if only ``cpu`` device needed, change ``--define opencl=true`` to ``false``. This way - 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`` 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** * **static library**
- The methods in dynamic library can be useful for static library too. In additional, the static - The methods in dynamic library can be useful for static library too. In additional, the static
......
...@@ -44,13 +44,7 @@ cc_library( ...@@ -44,13 +44,7 @@ cc_library(
cc_library( cc_library(
name = "generated_libmace", name = "generated_libmace",
srcs = glob(["lib/*.so"]), srcs = glob(["lib/*"]),
visibility = ["//visibility:public"],
)
cc_library(
name = "generated_libmace_static",
srcs = glob(["lib/*.a"]),
linkstatic = 1, linkstatic = 1,
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
...@@ -29,7 +29,7 @@ cc_binary( ...@@ -29,7 +29,7 @@ cc_binary(
deps = [ deps = [
"//external:gflags_nothreads", "//external:gflags_nothreads",
"//mace/codegen:generated_mace_engine_factory", "//mace/codegen:generated_mace_engine_factory",
"//mace/codegen:generated_libmace_static", "//mace/codegen:generated_libmace",
] + if_hexagon_enabled([ ] + if_hexagon_enabled([
"//third_party/nnlib:libhexagon", "//third_party/nnlib:libhexagon",
]), ]),
......
...@@ -12,6 +12,20 @@ mace { ...@@ -12,6 +12,20 @@ mace {
*SetOpenMPThreadAffinity*; *SetOpenMPThreadAffinity*;
*GetBigLittleCoreIDs*; *GetBigLittleCoreIDs*;
*MaceVersion*; *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: local:
*; *;
}; };
...@@ -42,7 +42,7 @@ cc_binary( ...@@ -42,7 +42,7 @@ cc_binary(
deps = [ deps = [
"//external:gflags_nothreads", "//external:gflags_nothreads",
"//mace/codegen:generated_mace_engine_factory", "//mace/codegen:generated_mace_engine_factory",
"//mace/codegen:generated_models",
"//mace/libmace:libmace_dynamic", "//mace/libmace:libmace_dynamic",
"//mace/utils:utils",
], ],
) )
...@@ -11,6 +11,9 @@ build --copt=-D_GLIBCXX_USE_C99_MATH_TR1 ...@@ -11,6 +11,9 @@ build --copt=-D_GLIBCXX_USE_C99_MATH_TR1
build --copt=-DMACE_OBFUSCATE_LITERALS build --copt=-DMACE_OBFUSCATE_LITERALS
build --copt=-DGEMMLOWP_USE_OPENMP build --copt=-DGEMMLOWP_USE_OPENMP
# Usage example: bazel build --config symbol_hidden
build:symbol_hidden --copt=-fvisibility=hidden
# Usage example: bazel build --config android # Usage example: bazel build --config android
build:android --crosstool_top=//external:android/crosstool build:android --crosstool_top=//external:android/crosstool
build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain build:android --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
...@@ -23,7 +26,6 @@ build:optimization --linkopt=-Wl,--strip-all ...@@ -23,7 +26,6 @@ build:optimization --linkopt=-Wl,--strip-all
build:optimization --copt=-ffunction-sections build:optimization --copt=-ffunction-sections
build:optimization --copt=-fdata-sections build:optimization --copt=-fdata-sections
build:optimization --linkopt=-Wl,--gc-sections build:optimization --linkopt=-Wl,--gc-sections
build:optimization --copt=-fvisibility=hidden
# Address sanitizer # Address sanitizer
build:asan --strip=never build:asan --strip=never
......
...@@ -44,16 +44,16 @@ fi ...@@ -44,16 +44,16 @@ fi
# build static libraries # build static libraries
echo "build static lib for armeabi-v7a + cpu_gpu_dsp" 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 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/ cp third_party/nnlib/*so $LIB_DIR/armeabi-v7a/cpu_gpu_dsp/
echo "build static lib for armeabi-v7a + cpu_gpu" 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/ cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/armeabi-v7a/cpu_gpu/
echo "build static lib for arm64-v8a + 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/ cp bazel-genfiles/mace/libmace/libmace.a $LIB_DIR/arm64-v8a/cpu_gpu/
if [[ "$OSTYPE" != "darwin"* ]];then if [[ "$OSTYPE" != "darwin"* ]];then
......
...@@ -786,7 +786,8 @@ def build_model_lib(configs, address_sanitizer): ...@@ -786,7 +786,8 @@ def build_model_lib(configs, address_sanitizer):
abi=target_abi, abi=target_abi,
hexagon_mode=hexagon_mode, hexagon_mode=hexagon_mode,
enable_opencl=get_opencl_mode(configs), 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) 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, ...@@ -880,8 +881,10 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer,
sh.rm("-rf", build_tmp_binary_dir) sh.rm("-rf", build_tmp_binary_dir)
os.makedirs(build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir)
symbol_hidden = True
mace_run_target = MACE_RUN_STATIC_TARGET mace_run_target = MACE_RUN_STATIC_TARGET
if mace_lib_type == MACELibType.dynamic: if mace_lib_type == MACELibType.dynamic:
symbol_hidden = False
mace_run_target = MACE_RUN_DYNAMIC_TARGET mace_run_target = MACE_RUN_DYNAMIC_TARGET
build_arg = "" build_arg = ""
if configs[YAMLKeyword.model_graph_format] == ModelFormat.code: if configs[YAMLKeyword.model_graph_format] == ModelFormat.code:
...@@ -897,6 +900,7 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer, ...@@ -897,6 +900,7 @@ def build_mace_run(configs, target_abi, enable_openmp, address_sanitizer,
enable_openmp=enable_openmp, enable_openmp=enable_openmp,
enable_opencl=get_opencl_mode(configs), enable_opencl=get_opencl_mode(configs),
address_sanitizer=address_sanitizer, address_sanitizer=address_sanitizer,
symbol_hidden=symbol_hidden,
extra_args=build_arg extra_args=build_arg
) )
sh_commands.update_mace_run_binary(build_tmp_binary_dir, sh_commands.update_mace_run_binary(build_tmp_binary_dir,
...@@ -924,6 +928,7 @@ def build_quantize_stat(configs): ...@@ -924,6 +928,7 @@ def build_quantize_stat(configs):
quantize_stat_target, quantize_stat_target,
abi=ABIType.host, abi=ABIType.host,
enable_openmp=True, enable_openmp=True,
symbol_hidden=True,
extra_args=build_arg extra_args=build_arg
) )
...@@ -943,36 +948,39 @@ def build_example(configs, target_abi, enable_openmp, mace_lib_type): ...@@ -943,36 +948,39 @@ def build_example(configs, target_abi, enable_openmp, mace_lib_type):
sh.rm("-rf", build_tmp_binary_dir) sh.rm("-rf", build_tmp_binary_dir)
os.makedirs(build_tmp_binary_dir) os.makedirs(build_tmp_binary_dir)
symbol_hidden = True
libmace_target = LIBMACE_STATIC_TARGET libmace_target = LIBMACE_STATIC_TARGET
if mace_lib_type == MACELibType.dynamic: if mace_lib_type == MACELibType.dynamic:
symbol_hidden = False
libmace_target = LIBMACE_SO_TARGET libmace_target = LIBMACE_SO_TARGET
sh_commands.bazel_build(libmace_target, sh_commands.bazel_build(libmace_target,
abi=target_abi, abi=target_abi,
enable_openmp=enable_openmp, enable_openmp=enable_openmp,
enable_opencl=get_opencl_mode(configs), 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): if os.path.exists(LIB_CODEGEN_DIR):
sh.rm("-rf", LIB_CODEGEN_DIR) sh.rm("-rf", LIB_CODEGEN_DIR)
sh.mkdir("-p", LIB_CODEGEN_DIR) sh.mkdir("-p", LIB_CODEGEN_DIR)
build_arg = "" 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: if mace_lib_type == MACELibType.dynamic:
example_target = EXAMPLE_DYNAMIC_TARGET example_target = EXAMPLE_DYNAMIC_TARGET
sh.cp("-f", LIBMACE_DYNAMIC_PATH, LIB_CODEGEN_DIR) sh.cp("-f", LIBMACE_DYNAMIC_PATH, LIB_CODEGEN_DIR)
else: 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 example_target = EXAMPLE_STATIC_TARGET
sh.cp("-f", LIBMACE_STATIC_PATH, LIB_CODEGEN_DIR)
sh_commands.bazel_build(example_target, sh_commands.bazel_build(example_target,
abi=target_abi, abi=target_abi,
...@@ -1325,11 +1333,6 @@ def print_package_summary(package_path): ...@@ -1325,11 +1333,6 @@ def print_package_summary(package_path):
def run_mace(flags): def run_mace(flags):
configs = format_model_config(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]) clear_build_dirs(configs[YAMLKeyword.library_name])
......
...@@ -279,6 +279,7 @@ def bazel_build(target, ...@@ -279,6 +279,7 @@ def bazel_build(target,
enable_neon=True, enable_neon=True,
enable_opencl=True, enable_opencl=True,
address_sanitizer=False, address_sanitizer=False,
symbol_hidden=False,
extra_args=""): extra_args=""):
print("* Build %s with ABI %s" % (target, abi)) print("* Build %s with ABI %s" % (target, abi))
if abi == "host": if abi == "host":
...@@ -307,6 +308,8 @@ def bazel_build(target, ...@@ -307,6 +308,8 @@ def bazel_build(target,
bazel_args += ("--config", "asan") bazel_args += ("--config", "asan")
else: else:
bazel_args += ("--config", "optimization") bazel_args += ("--config", "optimization")
if symbol_hidden:
bazel_args += ("--config", "symbol_hidden")
if extra_args: if extra_args:
bazel_args += (extra_args, ) bazel_args += (extra_args, )
print bazel_args print bazel_args
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册