Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the meson project version be assigned dynamically?

I am new to Meson so please forgive me if this is a stupid question.

Simple Version of the Question:

I want to be able to assign a dynamic version number to the meson project version at build time. Essentially meson.project_version()=my_dynamic_var or project( 'my_cool_project', 'c', version : my_dynamic_var') (which of course won't work).

I would rather not pre-process the file if I don't have to.

Some background if anybody cares:

My build system dynamically comes up with a version number for the project. In my case, it is using a bash script. I have no problem getting that version into my top level meson.build file using run_command and scraping stdout from there. I have read that using doing it this way is bad form so if there is another way to do this.. I am all ears.

I am also able to create and pass the correct -DPRODUCT_VERSION="<my_dynamic_var>" via add_global_arguments so I COULD just settle for that.. but I would like the meson project itself to carry the same version for the logs and so I can use meson.project_version() to get the version in subprojects for languages other than c/c++.

like image 370
Señor CMasMas Avatar asked Dec 05 '19 18:12

Señor CMasMas


1 Answers

The short answer, as noted in comments to the question, appears to be no. There is no direct way to set the version dynamically in the project call.

However, there are some work arounds, and the first looks promising for the simple case:

(1) use meson rewriting capability

$ meson rewrite kwargs set project / version 1.0.0

Then obviously use an environment variable instead of 1.0.0.

(2) write a wrapper script which reads the version from the environment and substitutes it into your meson.build file in the project call.

(3) adopt conan.io and have your meson files generated.

(4) use build options. This option, while not as good as (1) might work for other work flows.

Here's how option (4) works.

  • create a meson_options.txt file in your meson root directory
  • add the following line:

    option('version', type : 'string', value : '0.0.0', description : 'project version')

  • then create a meson.build file that reads this option.

    project('my_proj', 'cpp')
    version = get_option('version')
    message(version)
    
    conf_data = configuration_data()
    conf_data.set('version', version)
    

When you go to generate your project, you have an extra step of setting options.

$ meson build && cd build
$ meson configure -Dversion=$BUILD_VERSION

Now the version is available as a build option, then we use a configuration_data object to make it available for substitution into header/source files (which you might want to get it into shared libraries or what not).

configure_file(
  input : 'config.hpp.in',
  output : 'config.hpp',
  configuration : conf_data
)

And config.hpp.in looks something like this:

#pragma once
#include <string>

const static std::string VERSION = "@version@";

When we do the configure_file call, @version@ will get substituted for the version string we set in the meson configure step.

So this way is pretty convoluted, but like I said, you may still end up doing some of it, e.g. to print copyright info and what not.

like image 110
paxos1977 Avatar answered Dec 03 '22 21:12

paxos1977