mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.46k stars 1.58k forks source link

Cannot get Meson to work with Android NDK cross-compilation on Windows #4493

Open Antonz0 opened 5 years ago

Antonz0 commented 5 years ago

Hi.

I'm trying to cross-compile some third-party library (it's libepoxy from https://github.com/anholt/libepoxy/releases , latest stable version 1.5.3 ) for Android on Windows using Meson (also latest version 0.48.1) with no success.

Here's my cross config file (I took one example from http://dev-smart.com/cross-compiling-with-android/ and adapted it for my needs):

[binaries] c = 'C:/TEMP/android_toolchain-arm/bin/clang70.exe' cpp = 'C:/TEMP/android_toolchain-arm/bin/clang70++.exe' ar = 'C:/TEMP/android_toolchain-arm/bin/arm-linux-androideabi-ar.exe' ld = 'C:/TEMP/android_toolchain-arm/bin/arm-linux-androideabi-ld.exe' strip = 'C:/TEMP/android_toolchain-arm/bin/arm-linux-androideabi-strip.exe'

[properties] root = 'C:/TEMP/android_toolchain-arm/sysroot' needs_exe_wrapper = false

[host_machine] system = 'android-arm' cpu_family = 'arm' cpu = 'armv7a' endian = 'little'

Android toolchain got extracted into C:/TEMP/android_toolchain-arm folder using standard methods (code from helper batch file to do it: %ANDROID_NDK_HOME%/build/tools/make_standalone_toolchain.py --arch arm --stl libc++ --api 16 -v --install-dir "C:/TEMP/android_toolchain-arm" ). I tried putting it to the simplest absolute path as possible. Tried various variants of specifying 'c' (arm-linux-androideabi-clang.cmd, clang.cmd, clang70.exe) and 'cpp' (arm-linux-androideabi-clang++.cmd, clang++.cmd, clang70++.exe) compilers in "bin" folder within toolchain folder - no luck.

I'm invoking Meson from source root folder using following command:

meson --warnlevel 3 --backend ninja --default-library static --cross-file "../Build_Android_Meson_Config.txt" build

Here's what I'm getting every time in a log:

Build started at 2018-11-08T14:45:36.772105 Main binary: C:\Program Files\Meson\meson.exe Python system: Windows The Meson build system Version: 0.48.1 Source dir: C:\TEMP\libepoxy\origin Build dir: C:\TEMP\libepoxy\origin\build Build type: cross build Project name: libepoxy Project version: 1.5.3

meson.build:1:0: ERROR: Unknown compiler(s): ['cl', 'cc', 'gcc', 'clang'] The follow exceptions were encountered: Running "cl /?" gave "[WinError 2] Не удается найти указанный файл" Running "cc --version" gave "[WinError 2] Не удается найти указанный файл" Running "gcc --version" gave "[WinError 2] Не удается найти указанный файл" Running "clang --version" gave "[WinError 2] Не удается найти указанный файл"

"Не удается найти указанный файл" means "Cannot find file specified". It looks that Meson just ignores cross compilers that I'm trying to specify and just attempts to use some default set... And it actually parses cross-file because when I'm trying to set something syntactically wrong Meson reports an error about it.

When I'm building this library on MacOS X (some paths to toolchain executables are different here, of course) it works just fine. Here's example of cross-config file I use on MacOS X:

[binaries] name = 'android' c = '/tmp/android_toolchain-arm/bin/arm-linux-androideabi-clang' cpp = '/tmp/android_toolchain-arm/bin/arm-linux-androideabi-clang++' ar = '/tmp/android_toolchain-arm/bin/arm-linux-androideabi-ar' ld = '/tmp/android_toolchain-arm/bin/arm-linux-androideabi-ld' strip = '/tmp/android_toolchain-arm/bin/arm-linux-androideabi-strip'

[properties] root = '/tmp/android_toolchain-arm'

[host_machine] system = 'android-arm' cpu_family = 'arm' cpu = 'armv7a' endian = 'little'

Antonz0 commented 5 years ago

After adding direct path to cl.exe compiler to PATH (just got curious): C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin I'm getting this error instead:

meson.build:1:0: ERROR: Compiler cl can not compile programs.

So it's really looks that Meson on Windows is too happy with default compilers and just don't wants to use ones specified for cross-compilation for some reason. Or I'm doing it wrong.

jpakkane commented 5 years ago

Meson's cross compilation works slightly differently from other build systems. It requires both a native and a cross compiler so you can build tools such as code generators and use them during the build. The solution to this is to have a native compiler in the path, too. For your case the simplest way is to launch the builds from VS developer tools command console so it uses VS, but any native compiler in path will suffice.

alexbriskin commented 5 years ago

I have a similar problem with cross-compilation for an embedded target with Infenion Tricore architecture. I did some digging and debugging. I found several inconsistencies in the path handling of the cross-file in general. Specifically, I found a problematic line in environment.py parse_datafile function. The following line changes a windows path

res = eval(value, {'__builtins__': None}, {'true': True, 'false': False})

The line converts \t in the path to hex and removes double backslashes, but not all of them. After replacing the line with below code the cross-compilation does not work because of other errors.

res = value

Also, I'd like to suggest a better way of handling crosscompilation and adding support for new cross-compilers. @jpakkane would you be interested in something like that?

jpakkane commented 5 years ago

res = value

This does not work in general, because we need to convert strings such as ['foo', 'bar'] into actual arrays. That's why the eval is there.

In general this is exactly the reason why you'd want to use / as a path separator, even on Windows. Since backslash is a quote character, it will cause breakage all the time. That being said we should be able to handle those better. Would preprocessing by swapping all backslashes to regular slashes be enough or is there breakage potential?

alexbriskin commented 5 years ago

Thanks switching to / worked on windows, but I still get an "Unknown compiler(s)" error emitted from a subproject. Can you suggest a solution?

alexbriskin commented 5 years ago

The Meson build system Version: 0.48.999 Source dir: C:\Users\alex.briskin\all_work\delta_pc Build dir: C:\Users\alex.briskin\all_work\delta_pc\cross Build type: cross build Project name: delta_pc Project version: 0.0.1 Native C compiler: gcc (gcc 6.3.0 "gcc (MinGW.org GCC-6.3.0-1) 6.3.0") Cross C compiler: C:/HIGHTEC/toolchains/tricore/v4.9.1.0-infineon-2.0/bin/tricore-gcc.exe (gcc 4.9.4) Host machine cpu family: Tricore Host machine cpu: TC297 Target machine cpu family: Tricore Target machine cpu: TC297 Build machine cpu family: x86 Build machine cpu: x86

Executing subproject delta_core
Project name: delta_core
Project version: 0.0.1
Native C compiler: gcc (gcc 6.3.0 "gcc (MinGW.org GCC-6.3.0-1) 6.3.0")
Cross C compiler: C:/HIGHTEC/toolchains/tricore/v4.9.1.0-infineon-2.0/bin/tricore-gcc.exe (gcc 4.9.4)

subprojects\delta_core\meson.build:1:0: ERROR: Unknown compiler(s): ['cl', 'c++', 'g++', 'clang++', 'clang-cl'] The follow exceptions were encountered: Running "cl /?" gave "[WinError 2] The system cannot find the file specified" Running "c++ --version" gave "[WinError 2] The system cannot find the file specified" Running "g++ --version" gave "[WinError 2] The system cannot find the file specified" Running "clang++ --version" gave "[WinError 2] The system cannot find the file specified" Running "clang-cl /?" gave "[WinError 2] The system cannot find the file specified"

A full log can be found at C:\Users\alex.briskin\all_work\delta_pc\cross\meson-logs\meson-log.txt

jpakkane commented 5 years ago

You seem to be missing the C++ compiler from your MinGW installation. It does find the C compiler but not the C++ one.

mrquokka commented 4 years ago

Maybe some need fix of this problem sudo apt install g++