0

I tried with this example, but nothing happens:

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)

For example, if I change the CMAKE_ANDROID_ARCH_ABI to anything else, nothing happens. It is like CMake is ignoring the NDK part of the code.

But If I change the project to another location, different things happen:

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)

The error:

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

I have no experience with CMake, I think that the problem is the order of the things. Can anyone help me?

Kevin
  • 16,549
  • 8
  • 60
  • 74

2 Answers2

1

Setting all of these variables (such as CMAKE_SYSTEM_NAME, CMAKE_SYSTEM_VERSION, CMAKE_ANDROID_ARCH_ABI, etc.) should happen in the toolchain file. You may certainly experience some nasty CMake behavior by putting these in the CMakeLists.txt file itself. There is even a sample toolchain file in the CMake documentation you linked here.

Also, the CMAKE_TOOLCHAIN_FILE variable should be set on the command line when you call cmake, not in the CMake file itself. This reduces your CMakeLists.txt file to something like this:

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)

Then, you should call cmake, specifying the toolchain file to use, like this:

cmake -DCMAKE_TOOLCHAIN_FILE=/home/icarolima/Android/Sdk/ndk/21.3.6528147/build/cmake/android.toolchain.cmake ..
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • So if I want to specify the ABI, what can I do? Also, calling CMake with these arguments and this CMakeLists.txt gives me the following error: `CMake Error at CMakeLists.txt:27 (message): Verilator was not found. Either install it, or set the VERILATOR_ROOT environment variable` But this error doesn't happen when I do not set the `CMAKE_TOOLCHAIN_FILE`. – Ícaro Lima Aug 27 '20 at 08:54
  • @ÍcaroDantasdeAraújoLima As suggested in my response, you should set all of these variables (such as `CMAKE_ANDROID_ARCH_ABI`) in the **toolchain** file. Also, as the error says, you must the Verilator software on your machine first, ensuring it matches the compiler/architecture you are using within CMake. – Kevin Aug 27 '20 at 12:31
  • I discovered what is happening: I already have Verilator installed, the problem is described [here](https://stackoverflow.com/a/35806563/9188829). – Ícaro Lima Aug 27 '20 at 12:47
  • I will mark your answer as accepted and then I will edit my question with the complete solution, thank you very much! – Ícaro Lima Aug 27 '20 at 12:48
  • 1
    @ÍcaroDantasdeAraújoLima Glad it is working! If you have a more complete solution, please consider writing your own answer post to document the full solution separately from your question. Feel free to mark yours as *accepted* as well! – Kevin Aug 27 '20 at 12:51
0

So, just to clarify, the way I solved it can be seen here: Dockerfile, and here: sandbox_template.

Thanks for the answers @squareskittles!