【问题标题】:Generate an incbin with NDK from an asset使用 NDK 从资产生成 incbin
【发布时间】:2021-04-25 18:25:21
【问题描述】:

我正在尝试调整 Stockfish 12 国际象棋引擎以在 React Native 中使用(仅适用于 Android,因为我没有适用于 IOS 的设备)。我受到项目 DroidFish 的启发。

但我面临一个重要问题

  • DroidFishApp 设法通过在资产文件夹中包含所需的 nn-baeb9ef2d183.nnue 来处理新的神经网络模型
  • 但是他们设法通过使用 Android.mk 格式来做到这一点,而在我的构建中我使用的是现代方式:CMakeLists.txt 文件(另外,我在尝试使用 Android.mk 脚本时遇到了问题,可能是因为处理原生库的 React-Native 方式)

所以当我尝试编译时,我收到一条错误消息,提示找不到 nn-baeb9ef2d183.nnue 的 incbin。

所以我的问题是我的以下问题:我需要适应什么来生成这个 incbin 文件?

这是我的 android/src/app/build.gradle(有一个 externalNativeBuild 块)

buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault
  def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['StockfishAndroid_kotlinVersion']

  repositories {
    google()
    jcenter()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['StockfishAndroid_' + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['StockfishAndroid_' + name]).toInteger()
}

android {
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  buildToolsVersion getExtOrDefault('buildToolsVersion')
  defaultConfig {
    minSdkVersion 16
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    versionCode 1
    versionName "1.0"
    
    externalNativeBuild {
        cmake {
            cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
            abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
        }
    }
    
  }
  
  externalNativeBuild {
      cmake {
          path "../cpp/CMakeLists.txt"
          version "3.10.2"
      }
  }
  
  buildTypes {
    release {
      minifyEnabled false
    }
  }
  lintOptions {
    disable 'GradleCompatible'
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

repositories {
  mavenCentral()
  jcenter()
  google()

  def found = false
  def defaultDir = null
  def androidSourcesName = 'React Native sources'

  if (rootProject.ext.has('reactNativeAndroidRoot')) {
    defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
  } else {
    defaultDir = new File(
            projectDir,
            '/../../../node_modules/react-native/android'
    )
  }

  if (defaultDir.exists()) {
    maven {
      url defaultDir.toString()
      name androidSourcesName
    }

    logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
    found = true
  } else {
    def parentDir = rootProject.projectDir

    1.upto(5, {
      if (found) return true
      parentDir = parentDir.parentFile

      def androidSourcesDir = new File(
              parentDir,
              'node_modules/react-native'
      )

      def androidPrebuiltBinaryDir = new File(
              parentDir,
              'node_modules/react-native/android'
      )

      if (androidPrebuiltBinaryDir.exists()) {
        maven {
          url androidPrebuiltBinaryDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
        found = true
      } else if (androidSourcesDir.exists()) {
        maven {
          url androidSourcesDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
        found = true
      }
    })
  }

  if (!found) {
    throw new GradleException(
            "${project.name}: unable to locate React Native android sources. " +
                    "Ensure you have you installed React Native as a dependency in your project and try again."
    )
  }
}

def kotlin_version = getExtOrDefault('kotlinVersion')

dependencies {
  // noinspection GradleDynamicVersion
  api 'com.facebook.react:react-native:+'
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

这是我的主要 CMakeLists.txt

cmake_minimum_required(VERSION 3.10.0)

set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 17)

include(stockfish/CMakeLists.txt)

add_library(stockfish_cpp
            SHARED
            nativeutil
            stockfish
)

set_target_properties(stockfish_cpp PROPERTIES LINKER_LANGUAGE CXX)

# Specifies a path to native header files.
include_directories(
            .
)

这是我的鱼/CMakeLists.txt

project(stockfish)

add_library(stockfish SHARED
./stockfish/benchmark.cpp
./stockfish/bitbase.cpp
./stockfish/bitboard.cpp
./stockfish/endgame.cpp
./stockfish/evaluate.cpp
./stockfish/main.cpp
./stockfish/material.cpp
./stockfish/misc.cpp
./stockfish/movegen.cpp
./stockfish/movepick.cpp
./stockfish/pawns.cpp
./stockfish/position.cpp
./stockfish/psqt.cpp
./stockfish/search.cpp
./stockfish/thread.cpp
./stockfish/timeman.cpp
./stockfish/tt.cpp
./stockfish/tune.cpp
./stockfish/uci.cpp
./stockfish/ucioption.cpp
./stockfish/nnue/evaluate_nnue.cpp
./stockfish/nnue/features/half_kp.cpp
./stockfish/syzygy/tbprobe.cpp
)

include_directories(
    ./stockfish
    ./stockfish/incbin
    ./stockfish/nnue
    ./stockfish/nnue/architectures
    ./stockfish/nnue/features
    ./stockfish/nnue/layers
    ./stockfish/syzygy
)

这是我的 nativeutil/CMakeLists.txt

project(nativeutil)

add_library(nativeutil SHARED
    ./nativeutil/nativeutil.cpp
  ./nativeutil/cpu_features/src/cpuinfo_aarch64.c
  ./nativeutil/cpu_features/src/cpuinfo_arm.c
  ./nativeutil/cpu_features/src/cpuinfo_x86.c
  ./nativeutil/cpu_features/src/filesystem.c
  ./nativeutil/cpu_features/src/hwcaps.c
  ./nativeutil/cpu_features/src/stack_line_reader.c
  ./nativeutil/cpu_features/src/string_view.c
  ./nativeutil/cpu_features/src/unix_features_aggregator.c
)

include_directories(
   ./nativeutil/cpu_features/include
   ./nativeutil/cpu_features/include/internal
)

这里是终端输出的摘录

$> react-native run-android
... (omitted)

> Build command failed.
  Error while executing process /home/laurent/Android/Sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/android/.cxx/cmake/debug/armeabi-v7a stockfish stockfish_cpp}
  ninja: Entering directory `/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/android/.cxx/cmake/debug/armeabi-v7a'
  [1/2] Building CXX object CMakeFiles/stockfish.dir/stockfish/evaluate.cpp.o
  FAILED: CMakeFiles/stockfish.dir/stockfish/evaluate.cpp.o 
  /home/laurent/Android/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi16 --gcc-toolchain=/home/laurent/Android/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/laurent/Android/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/linux-x86_64/sysroot  -Dstockfish_EXPORTS -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish/incbin -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish/nnue -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish/nnue/architectures -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish/nnue/features -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish/nnue/layers -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/./stockfish/syzygy -I/home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/. -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security  -O2 -frtti -fexceptions -Wall -fstack-protector-all -O0 -fno-limit-debug-info  -fPIC   -std=gnu++1z -MD -MT CMakeFiles/stockfish.dir/stockfish/evaluate.cpp.o -MF CMakeFiles/stockfish.dir/stockfish/evaluate.cpp.o.d -o CMakeFiles/stockfish.dir/stockfish/evaluate.cpp.o -c /home/laurent/Documents/Programmation/ProjetsPersos/ReactNative/Plugins/react-native-stockfish-android/cpp/stockfish/evaluate.cpp
  <inline asm>:6:9: error: Could not find incbin file 'nn-baeb9ef2d183.nnue'
  .incbin "nn-baeb9ef2d183.nnue"
          ^
  1 error generated.
  ninja: build stopped: subcommand failed.
  


我也看过 this previous topic :但解决方案仍然依赖于 Android.mk

【问题讨论】:

    标签: android c++ react-native cmake android-ndk


    【解决方案1】:

    其实这不是 CMake 的问题:我不得不在the source evaluate.h 中更改 nnue 文件的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2017-02-25
      • 2019-11-07
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2012-11-10
      相关资源
      最近更新 更多