Search code examples
cmakecross-compilinghost

Find host package for cross-compiling with CMake


I'm involved in some project, developing a library in C language.

Unit-tests in our project run python scripts to generate data for testing.

When the project is compiled for Linux or Windows, call to find_package(Python3 COMPONENTS Interpreter Development NumPy) in CMakeLists.txt works pretty well: Python is successfully found, data are generated.

However, when the project is cross-compiled on Windows for some embedded platform, that call to find_package(Python3...) always fails.

Recently I've learned that it can be debugged using internal CMake variable CMAKE_FIND_DEBUG_MODE

So, I've wrapped the call to find_package in commands setting this variable to ON, then OFF.

Here is the relevant part of debug log:

CMake Debug Log at C:/Program Files/CMake/share/cmake-3.19/Modules/FindPython/Support.cmake:2681 (find_library):
  find_library called with the following settings:

    VAR: _Python3_LIBRARY_RELEASE
    NAMES: "python37"
           "python3.7"
    Documentation: Path to a library.
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 0
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 0
   
  find_library considered the following locations:

    C:/Anaconda3/envs/py37/lib/(lib)python37(\.so|\.a)
    C:/Anaconda3/envs/py37/lib/(lib)python3.7(\.so|\.a)
    C:/Anaconda3/envs/py37/libs/(lib)python37(\.so|\.a)
    C:/Anaconda3/envs/py37/libs/(lib)python3.7(\.so|\.a)
    C:/Anaconda3/envs/py37/lib/python3.7/config-3.7/(lib)python37(\.so|\.a)
    C:/Anaconda3/envs/py37/lib/python3.7/config-3.7/(lib)python3.7(\.so|\.a)
    C:/Anaconda3/envs/py37/(lib)python37(\.so|\.a)
    C:/Anaconda3/envs/py37/(lib)python3.7(\.so|\.a)
    ... other paths

  The item was not found.

Correct path to the python library is C:\Anaconda3\envs\py37\libs\python37.lib. Directory, containing this file, appears on the third place on the directory list above. So, if CMake would search for a file with .lib extension, it would find it.

However, extensions .lib or .dll never appear in the log.

At the same time, executable file of the python interpreter is found.

I think, it is because CMake tries to find python for the target platform. However, I need to run python on the host.

Is it possible to switch this behavior?

Search for "cmake find_host_package" brings only this macro wrapping original find_package, that is incorrect


Solution

  • Solved with removing "Development" from requested components:

    if(CMAKE_CROSSCOMPILING)
        find_package(Python3 COMPONENTS Interpreter NumPy)
    else()
        find_package(Python3 COMPONENTS Interpreter Development NumPy)
    endif()