mesonbuild / meson

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

Another approach for cross-compilation ? #1034

Open Salamandar opened 7 years ago

Salamandar commented 7 years ago

I started using Meson some days ago, and I can say it's really awesome. I easily converted all of my "native" CMake projects to Meson. But I'm trying to convert a cross-compilation project and that's… Less awesome. Here are my 2 CMake Toolchain files :

ARCH is defined the first (or not) time CMake is called :

mkdir "build-libopencm3"
cd    "build-libopencm3"
cmake -DARCH=libopencm3 -DBOARD=stm32l4 ..

…And the toolchain file is included by

include("${CMAKE_CURRENT_SOURCE_DIR}/../toolchains/${ARCH}.cmake")

What CMake allows me to do in this toolchain file :

Simply said, we can use the whole CMake syntax. Why not with Meson ? I checked your source code and you're using the "simple" python configparser syntax, right ? For now I can only set global variables.

All that would be easier if the cross-file had a Meson syntax and if I could call "set_cross_file()" inside my project file.

Salamandar commented 7 years ago

PS : Why not define all this in my project file ? Because I have several boards with different firmwares and I don't want to define all that in all projects separately. I could

but that would only be a bad hotfix for something (i think) a lot of people need.

jpakkane commented 7 years ago

Meson's cross compilation is a bit different than in other build systems, because it tries to provide for more complex things (such as compiling stuff with both the native and cross compiler in a single invocation). I highly recommend that you take a look at the Arduino sample project which does roughly the same thing as your CMake setup seems to do (at least based on a cursory overview of the setup).

The main point of a cross file is to separate out everything "board specific" from the build setup itself. This yields two main benefits: first of all the build definitions get simpler and secondly the same cross files can be used with any other Meson project. That is, you write your cross definition once and then can just use it.

In your case you would write one cross file for each of your target boards that sets up compiler flags as needed and would have an extra property boardname. Then you can, if needed, access the boardname variable in your Meson build file with meson.get_cross_property and set up your targets accordingly.

Now, granted, this is a bit more work up front but allows you to drop the cross files to other projects and have them work out of the box.