From ad1d583d2228d95ed39dcf31882206491d20dd33 Mon Sep 17 00:00:00 2001 From: TonyWang222 Date: Thu, 1 Dec 2022 12:29:56 +0800 Subject: [PATCH] compile MSLite for ohos Signed-off-by: TonyWang222 --- .../device/cpu/kernel/nnacl/CMakeLists.txt | 5 +++- .../device/cpu/kernel/nnacl/nnacl_utils.c | 4 +-- mindspore/lite/CMakeLists.txt | 9 +++++++ mindspore/lite/build_lite.sh | 14 +++++++++- mindspore/lite/java/native/CMakeLists.txt | 3 ++- mindspore/lite/src/CMakeLists.txt | 8 +++++- mindspore/lite/src/common/log.cc | 26 ++++++++++++++++--- mindspore/lite/src/common/log.h | 6 +++++ mindspore/lite/src/common/utils.cc | 6 ++--- mindspore/lite/src/litert/cpu_info.cc | 4 +-- mindspore/lite/tools/benchmark/CMakeLists.txt | 11 +++++++- third_party/securec/CMakeLists.txt | 3 +++ 12 files changed, 83 insertions(+), 16 deletions(-) diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/CMakeLists.txt b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/CMakeLists.txt index 37343bc50d2..76728f59bef 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/CMakeLists.txt +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/CMakeLists.txt @@ -64,6 +64,9 @@ if(APPLE OR PLATFORM_ARM32 OR PLATFORM_ARM64 OR PLATFORM_MCU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer -fstrict-aliasing \ -ffunction-sections -fdata-sections -ffast-math") endif() + if(TARGET_OHOS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-inline-asm") + endif() elseif(NOT MSVC) if("${CMAKE_BUILD_TYPE}" STREQUAL "Release") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer -fstrict-aliasing -ffunction-sections \ @@ -80,7 +83,7 @@ if(NOT MSVC) endif() if(WIN32) if("${X86_64_SIMD}" STREQUAL "avx512") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f -fno-asynchronous-unwind-tables") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f -fno-asynchronous-unwind-tables -Wno-inline-asm") endif() endif() endif() diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/nnacl_utils.c b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/nnacl_utils.c index 37df381d2ea..f0f81b26ac5 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/nnacl_utils.c +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/nnacl_utils.c @@ -15,11 +15,11 @@ */ #include "nnacl/nnacl_utils.h" -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) #include #endif -#if defined(__ANDROID__) +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) uint32_t getHwCap(int hwcap_type) { uint32_t ret = getauxval(hwcap_type); return ret; diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index e796f435228..43d2ecb0787 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -279,6 +279,11 @@ elseif(TOOLCHAIN_NAME STREQUAL "mix210") elseif(TOOLCHAIN_NAME STREQUAL "ohos-lite") set(TARGET_OHOS_LITE on) SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE) +elseif(TOOLCHAIN_NAME STREQUAL "ohos") + set(TARGET_OHOS on) + add_compile_definitions(MS_COMPILE_OHOS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument -Wno-c++17-extensions") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-command-line-argument -Wno-c++17-extensions") endif() if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.3.0 @@ -602,6 +607,8 @@ endif() if(PLATFORM_ARM64) if(TARGET_HIMIX100 OR TARGET_MIX210 OR MACHINE_LINUX_ARM64) set(RUNTIME_COMPONENT_NAME "linux-aarch64") + elseif(TARGET_OHOS) + set(RUNTIME_COMPONENT_NAME "ohos-aarch64") else() set(RUNTIME_COMPONENT_NAME "android-aarch64") endif() @@ -611,6 +618,8 @@ elseif(PLATFORM_ARM32) set(RUNTIME_COMPONENT_NAME "linux-aarch32") elseif(TARGET_OHOS_LITE) set(RUNTIME_COMPONENT_NAME "ohos-aarch32") + elseif(TARGET_OHOS) + set(RUNTIME_COMPONENT_NAME "ohos-aarch32") endif() elseif(WIN32) if(CMAKE_SIZEOF_VOID_P EQUAL 4) diff --git a/mindspore/lite/build_lite.sh b/mindspore/lite/build_lite.sh index 2f333ec4911..5be82bde78b 100755 --- a/mindspore/lite/build_lite.sh +++ b/mindspore/lite/build_lite.sh @@ -329,6 +329,12 @@ build_lite() { LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DTOOLCHAIN_NAME=himix200" LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_MINDDATA_IMPLEMENT=off" LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_ENABLE_FP16=off -DMSLITE_ENABLE_TRAIN=off -DMSLITE_GPU_BACKEND=off" + elif [[ "${TOOLCHAIN_NAME}" == "ohos" ]]; then + pkg_name=mindspore-lite-${VERSION_STR}-ohos-arm32 + CMAKE_TOOLCHAIN_FILE=${OHOS_NDK}/build/cmake/ohos.toolchain.cmake + LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DCMAKE_OHOS_NDK=${OHOS_NDK} -DOHOS_ARCH=armeabi-v7a -DOHOS_STL=c++_static -DTOOLCHAIN_NAME=${TOOLCHAIN_NAME}" + LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_MINDDATA_IMPLEMENT=off" + LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_ENABLE_FP16=off -DMSLITE_ENABLE_TRAIN=off -DMSLITE_GPU_BACKEND=off" else # CPU : Android-aarch32 checkndk @@ -358,6 +364,12 @@ build_lite() { LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DTOOLCHAIN_NAME=mix210" LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_MINDDATA_IMPLEMENT=off" LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_ENABLE_FP16=on -DMSLITE_ENABLE_TRAIN=off -DMSLITE_GPU_BACKEND=off" + elif [[ "${TOOLCHAIN_NAME}" == "ohos" ]]; then + pkg_name=mindspore-lite-${VERSION_STR}-ohos-aarch64 + CMAKE_TOOLCHAIN_FILE=${OHOS_NDK}/build/cmake/ohos.toolchain.cmake + LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DCMAKE_OHOS_NDK=${OHOS_NDK} -DOHOS_ARCH=arm64-v8a -DOHOS_STL=c++_static -DTOOLCHAIN_NAME=${TOOLCHAIN_NAME}" + LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_MINDDATA_IMPLEMENT=off" + LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_ENABLE_FP16=on -DMSLITE_ENABLE_TRAIN=off -DMSLITE_GPU_BACKEND=off" else if [[ "${machine}" == "aarch64" ]]; then # CPU : Linux-aarch64 @@ -409,7 +421,7 @@ build_lite() { if [[ "X$MSLITE_COMPILE_TWICE" != "X" ]]; then LITE_CMAKE_ARGS="${LITE_CMAKE_ARGS} -DMSLITE_COMPILE_TWICE=${MSLITE_COMPILE_TWICE}" fi - if [[ "${local_lite_platform}" == "arm64" || "${local_lite_platform}" == "arm32" ]]; then + if [[ ("${local_lite_platform}" == "arm64" || "${local_lite_platform}" == "arm32") && "${TOOLCHAIN_NAME}" != "ohos" ]]; then echo "default link libc++_static.a, export MSLITE_ANDROID_STL=c++_shared to link libc++_shared.so" fi diff --git a/mindspore/lite/java/native/CMakeLists.txt b/mindspore/lite/java/native/CMakeLists.txt index 1735fd8ff02..7f748101e39 100644 --- a/mindspore/lite/java/native/CMakeLists.txt +++ b/mindspore/lite/java/native/CMakeLists.txt @@ -113,7 +113,7 @@ if(USE_GLOG) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/securec/include) include_directories(${CMAKE_BINARY_DIR}/jni_include) add_definitions(-DUSE_GLOG) - + set_property(SOURCE ${JNI_SRC} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_LITE) set(JNI_SRC ${JNI_SRC} ${TOP_DIR}/mindspore/core/utils/log_adapter.cc @@ -166,6 +166,7 @@ if(SUPPORT_TRAIN) ${JNI_TRAIN_SRC} ${TOP_DIR}/mindspore/core/utils/log_adapter.cc ) + set_property(SOURCE ${JNI_SRC} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_LITE) else() set(JNI_TRAIN_SRC ${JNI_TRAIN_SRC} diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 422dbdbd148..ddf9c4b0119 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -534,10 +534,16 @@ if(SUPPORT_NNAPI) endif() if(PLATFORM_ARM32 OR PLATFORM_ARM64 AND NOT TARGET_HIMIX - AND NOT TARGET_MIX210 AND NOT TARGET_OHOS_LITE AND NOT MACHINE_LINUX_ARM64) + AND NOT TARGET_MIX210 AND NOT TARGET_OHOS_LITE AND NOT MACHINE_LINUX_ARM64 AND NOT TARGET_OHOS) target_link_libraries(mindspore-lite log) target_link_libraries(mindspore-lite_static log) endif() + +if(TARGET_OHOS) + target_link_libraries(mindspore-lite hilog_ndk.z.so) + target_link_libraries(mindspore-lite_static hilog_ndk.z.so) +endif() + if(MSLITE_MINDDATA_IMPLEMENT STREQUAL "lite") target_link_libraries(mindspore-lite minddata_eager_mid minddata-lite) target_link_libraries(mindspore-lite_static minddata_eager_mid) diff --git a/mindspore/lite/src/common/log.cc b/mindspore/lite/src/common/log.cc index 14f529ee82f..962027536d9 100644 --- a/mindspore/lite/src/common/log.cc +++ b/mindspore/lite/src/common/log.cc @@ -17,13 +17,13 @@ #include #include -#if defined(ENABLE_ARM) && (defined(__ANDROID__) || defined(ANDROID)) +#if defined(__ANDROID__) #include #endif // namespace to support utils module definition namespace mindspore constexpr const char *ANDROID_LOG_TAG = "MS_LITE"; namespace mindspore { -#if defined(ENABLE_ARM) && (defined(__ANDROID__) || defined(ANDROID)) +#if defined(__ANDROID__) constexpr const char *ANDROID_LOG_TAG = "MS_LITE"; #endif @@ -55,7 +55,7 @@ bool IsPrint(int level) { return level >= ms_level; } -#if defined(ENABLE_ARM) && (defined(__ANDROID__) || defined(ANDROID)) +#if defined(__ANDROID__) static int GetAndroidLogLevel(LiteLogLevel level) { switch (level) { case LiteLogLevel::DEBUG: @@ -71,6 +71,22 @@ static int GetAndroidLogLevel(LiteLogLevel level) { } #endif +#ifdef MS_COMPILE_OHOS +void PrintHiLog(LiteLogLevel level, const char *file, int line, const char *func, const char *msg) { + if (level == LiteLogLevel::DEBUG) { + OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, FORMAT, file, line, func, msg); + } else if (level == LiteLogLevel::INFO) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, LOG_TAG, FORMAT, file, line, func, msg); + } else if (level == LiteLogLevel::WARNING) { + OH_LOG_Print(LOG_APP, LOG_WARN, LOG_DOMAIN, LOG_TAG, FORMAT, file, line, func, msg); + } else if (level == LiteLogLevel::ERROR) { + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, LOG_TAG, FORMAT, file, line, func, msg); + } else { + OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, LOG_TAG, FORMAT, file, line, func, msg); + } +} +#endif + const char *EnumStrForMsLogLevel(LiteLogLevel level) { if (level == LiteLogLevel::DEBUG) { return "DEBUG"; @@ -87,9 +103,11 @@ const char *EnumStrForMsLogLevel(LiteLogLevel level) { void LiteLogWriter::OutputLog(const std::ostringstream &msg) const { if (IsPrint(static_cast(log_level_))) { -#if defined(ENABLE_ARM) && (defined(__ANDROID__) || defined(ANDROID)) +#if defined(__ANDROID__) __android_log_print(GetAndroidLogLevel(log_level_), ANDROID_LOG_TAG, "[%s:%d] %s] %s", location_.file_, location_.line_, location_.func_, msg.str().c_str()); +#elif defined(MS_COMPILE_OHOS) + PrintHiLog(log_level_, location_.file_, location_.line_, location_.func_, msg.str().c_str()); #else printf("%s [%s:%d] %s] %s\n", EnumStrForMsLogLevel(log_level_), location_.file_, location_.line_, location_.func_, msg.str().c_str()); diff --git a/mindspore/lite/src/common/log.h b/mindspore/lite/src/common/log.h index 577013aa88e..43047a2b986 100644 --- a/mindspore/lite/src/common/log.h +++ b/mindspore/lite/src/common/log.h @@ -23,6 +23,12 @@ #include #include "utils/overload.h" +#ifdef MS_COMPILE_OHOS +#define LOG_DOMAIN 0x2102 +#define LOG_TAG "MS_Lite" +#define FORMAT "[%{public}s:%{public}d] %{public}s# %{public}s" +#include "hilog/log.h" +#endif // NOTICE: when relative path of 'log.h' changed, macro 'LITE_LOG_HEAR_FILE_REL_PATH' must be changed #ifndef LITE_LOG_HEAR_FILE_REL_PATH #define LITE_LOG_HEAR_FILE_REL_PATH "mindspore/lite/src/common/log.h" diff --git a/mindspore/lite/src/common/utils.cc b/mindspore/lite/src/common/utils.cc index 5f4328c0d99..1d51cce05c4 100644 --- a/mindspore/lite/src/common/utils.cc +++ b/mindspore/lite/src/common/utils.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) #include #include #endif @@ -141,7 +141,7 @@ std::vector Tokenize(const std::string &src, const std::string &del return tokens; } -#if defined(__ANDROID__) +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) uint32_t getHwCap(int hwcap_type) { uint32_t ret = getauxval(hwcap_type); return ret; @@ -151,7 +151,7 @@ uint32_t getHwCap(int hwcap_type) { bool IsSupportSDot() { bool status = false; #ifdef ENABLE_ARM64 -#if defined(__ANDROID__) +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) int hwcap_type = 16; uint32_t hwcap = getHwCap(hwcap_type); if (hwcap & HWCAP_ASIMDDP) { diff --git a/mindspore/lite/src/litert/cpu_info.cc b/mindspore/lite/src/litert/cpu_info.cc index c9a47dbd3a6..7510de1ae53 100644 --- a/mindspore/lite/src/litert/cpu_info.cc +++ b/mindspore/lite/src/litert/cpu_info.cc @@ -56,7 +56,7 @@ #endif #endif namespace mindspore::lite { -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) uint32_t CpuInfo::MidrSetPart(uint32_t part) { return ((part << ARM_CPU_PART_OFFSET) & ARM_CPU_PART_MASK) | (midr_ & ~ARM_CPU_PART_MASK); } @@ -152,7 +152,7 @@ bool CpuInfo::ArmIsSupportFp16() { } return false; #else -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) #ifdef ENABLE_ARM32 GetArmProcCpuInfo(&android_cpu_info_); midr_ = MidrSetPart(android_cpu_info_.cpu_part); diff --git a/mindspore/lite/tools/benchmark/CMakeLists.txt b/mindspore/lite/tools/benchmark/CMakeLists.txt index 7f78a961169..479d916c4d2 100644 --- a/mindspore/lite/tools/benchmark/CMakeLists.txt +++ b/mindspore/lite/tools/benchmark/CMakeLists.txt @@ -2,7 +2,11 @@ cmake_minimum_required(VERSION 3.12) project(Lite_benchmark) if(NOT MSLITE_ENABLE_CLOUD_FUSION_INFERENCE) - set(BENCHMARK_LINK_LIB mindspore-lite) + if(TARGET_OHOS) + set(BENCHMARK_LINK_LIB libmindspore-lite.so) + else() + set(BENCHMARK_LINK_LIB mindspore-lite) + endif() else() add_definitions(-DUSE_GLOG) set(BENCHMARK_LINK_LIB mindspore-extendrt) @@ -82,6 +86,11 @@ add_executable(benchmark ${C_SRC} ${COMMON_SRC}) +if(TARGET_OHOS) + target_link_directories(benchmark PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../src) + add_dependencies(benchmark mindspore-lite) +endif() + add_dependencies(benchmark fbs_src) target_link_libraries(benchmark ${BENCHMARK_LINK_LIB}) diff --git a/third_party/securec/CMakeLists.txt b/third_party/securec/CMakeLists.txt index 91fb33b720a..edf8ac56221 100644 --- a/third_party/securec/CMakeLists.txt +++ b/third_party/securec/CMakeLists.txt @@ -11,6 +11,9 @@ if(NOT MSVC) #add flags set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include -Werror") endif() +if(TARGET_OHOS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument") +endif() include_directories(./include) add_subdirectory(src)