Muffo
Muffo

Reputation: 1743

Multiple targets with different configuration files in same meson.build?

My C application can be built with different modes that behaves differently depending on the content of config.h. In my meson.build file, I generate this header using the function configure_file().

Now I'd like to improve my build system to compile binaries for multiple modes in a single build. That means I'll have to manage different versions of config.h side by side.

My plan was to create different versions of config.h and save them in different folders. Then, customize the include directories for each Meson target to select the header for the mode I am building.

Unfortunately, I discovered that you cannot specify a subfolder in the output option of configure_file(). The file will always be created in the same location as the meson.build currently being executed.

This means I always have to include '.' and cannot differentiate between the different modes. Note: changing the #include directives in the C source files is not an option.

Is there a way to work around this in Meson?

Upvotes: 1

Views: 1333

Answers (1)

blubase
blubase

Reputation: 845

Let meson handle the configuration options natively instead of working around it by parsing options via config.h.

In a meson_options.txt file, you declare custom configuration options such as:

option('mode', type : 'combo', choices : ['one', 'two'], value : 'one')

Then, when you configure your build you can create individual build directories by calling:

meson buildOne -Dmode=one
ninja -C buildOne

and,

meson buildTwo -Dmode=two
ninja -C buildTwo

However, the above approach might not be desired?

Another possible method is to declare multiple subproject(). If your current meson.build is not too specific you could try the following.

Have a new main meson.build which deals with packaging, but also copies a template meson.build in a sub of the subproject directory, one for each mode. This might only require a few extra ../../ to compensate the different build structure.

The nice thing with the subproject declaration is, that you can override build options for that subproject. This would effectively mean, that you have separate build directories again, but only one main build directory, and only need to call meson once.

In this line of thought, you might refactor your application, and build only the different-mode parts in dedicated subprojects/libraries.

Upvotes: 1

Related Questions