使用 CMake 内置 NDK 构建 Verilator (C++)

Building Verilator (C++) with CMake built-in NDK

我尝试使用 this 示例,但没有任何反应:

cmake_minimum_required(VERSION 3.8)
project(cmake_simulator)

set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 21)
set(CMAKE_ANDROID_ARCH_ABI x86)
set(CMAKE_ANDROID_NDK /home/icarolima/Android/Sdk/ndk/21.3.6528147)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)

set(CMAKE_TOOLCHAIN_FILE /home/icarolima/Android/Sdk/ndk/21.3.6528147/build/cmake/android.toolchain.cmake)

find_package(verilator HINTS $ENV{VERILATOR_ROOT} ${VERILATOR_ROOT})
if (NOT verilator_FOUND)
  message(FATAL_ERROR "Verilator was not found. Either install it, or set the VERILATOR_ROOT environment variable")
endif()

# Create a new executable target that will contain all your sources
add_library(simulator SHARED simulator.cpp)

# Add the Verilated circuit to the target
verilate(simulator
  INCLUDE_DIRS "."
  SOURCES top.sv
  VERILATOR_ARGS -Wno-CASEINCOMPLETE -Wno-WIDTH -Wno-COMBDLY -cc +1800-2012ext+sv)

例如,如果我将 CMAKE_ANDROID_ARCH_ABI 更改为其他任何内容,则不会发生任何事情。就像 CMake 忽略了代码的 NDK 部分。

但是如果我将项目更改到另一个位置,则会发生不同的事情:

cmake_minimum_required(VERSION 3.8)

set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 21)
set(CMAKE_ANDROID_ARCH_ABI x86)
set(CMAKE_ANDROID_NDK /home/icarolima/Android/Sdk/ndk/21.3.6528147)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)

project(cmake_simulator)

set(CMAKE_TOOLCHAIN_FILE /home/icarolima/Android/Sdk/ndk/21.3.6528147/build/cmake/android.toolchain.cmake)

find_package(verilator HINTS $ENV{VERILATOR_ROOT} ${VERILATOR_ROOT})
if (NOT verilator_FOUND)
  message(FATAL_ERROR "Verilator was not found. Either install it, or set the VERILATOR_ROOT environment variable")
endif()

# Create a new executable target that will contain all your sources
add_library(simulator SHARED simulator.cpp)

# Add the Verilated circuit to the target
verilate(simulator
  INCLUDE_DIRS "."
  SOURCES top.sv
  VERILATOR_ARGS -Wno-CASEINCOMPLETE -Wno-WIDTH -Wno-COMBDLY -cc +1800-2012ext+sv)

错误:

CMake Error at /home/icarolima/Android/Sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/Platform/Android/Determine-Compiler-NDK.cmake:97 (message):
  Android: No toolchain for ABI 'x86' found in the NDK:

    /home/icarolima/Android/Sdk/ndk/21.3.6528147

我没有使用 CMake 的经验,我认为问题出在事物的顺序上。谁能帮帮我?

设置所有这些变量(例如 CMAKE_SYSTEM_NAMECMAKE_SYSTEM_VERSIONCMAKE_ANDROID_ARCH_ABI 等)应该在 toolchain 文件中进行.通过将它们放入 CMakeLists.txt 文件本身,您肯定会遇到一些令人讨厌的 CMake 行为。您链接的 CMake 文档中甚至有一个示例工具链文件 here

此外,当您调用 cmake 时,CMAKE_TOOLCHAIN_FILE 变量应该在 命令行 上设置,而不是在 CMake 文件本身中设置。这会将您的 CMakeLists.txt 文件缩减为如下内容:

cmake_minimum_required(VERSION 3.8)

project(cmake_simulator)
    
find_package(verilator HINTS $ENV{VERILATOR_ROOT} ${VERILATOR_ROOT})
if (NOT verilator_FOUND)
  message(FATAL_ERROR "Verilator was not found. Either install it, or set the VERILATOR_ROOT environment variable")
endif()

# Create a new executable target that will contain all your sources
add_library(simulator SHARED simulator.cpp)

# Add the Verilated circuit to the target
verilate(simulator
  INCLUDE_DIRS "."
  SOURCES top.sv
  VERILATOR_ARGS -Wno-CASEINCOMPLETE -Wno-WIDTH -Wno-COMBDLY -cc +1800-2012ext+sv)

然后,您应该调用 cmake,指定要使用的工具链文件,如下所示:

cmake -DCMAKE_TOOLCHAIN_FILE=/home/icarolima/Android/Sdk/ndk/21.3.6528147/build/cmake/android.toolchain.cmake ..

所以,为了澄清,我解决它的方法可以在这里看到:Dockerfile, and here: sandbox_template

感谢@squareskittles 的回答!