6

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++.

Señor CMasMas
  • 4,290
  • 2
  • 14
  • 21
  • 1
    I don't get it. If you can get your version with `run_command()`, (something like this, https://gitlab.freedesktop.org/spice/spice-gtk/commit/b18b6e0b0a607130ae2ac1ca29fe208827f4d59c , right?) what's your problem then? Are you looking for some other way because you read that's a bad way to do it? – Yasushi Shoji Dec 06 '19 at 03:10
  • No @Yasushi_Shoji, I can live with the "hack".. It isn't SO bad.. But what use is the meson project version if it can't be assigned and used dynamically? Who hard-codes version numbers? – Señor CMasMas Dec 06 '19 at 15:59
  • I do. Let me ask you this question. What do you do when you don't have `.git/`, like a tarball? – Yasushi Shoji Dec 07 '19 at 16:53
  • YYYY.MM.DD.(minutes_today or git_commit_count) for versions not done on a release branch. Nobody should be passing these around so ambiguity shouldn't matter. I would never ship a binary that didn't come from a release branch on the build server. – Señor CMasMas Dec 11 '19 at 18:04
  • Thanks for your help Yasushi. I am going to go ahead and say "not possible" until I figure the guts of meson out. – Señor CMasMas Dec 11 '19 at 18:05

2 Answers2

4

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.

oz10
  • 153,307
  • 27
  • 93
  • 128
  • Thanks! I also implemented a work around, but this is as good as any. I hope it helps someone in the future. – Señor CMasMas Feb 11 '20 at 23:45
  • 1
    Thanks a lot for this; in meson 0.61.99, we can define version on the initial meson configuration: `meson -Dversion="$(git describe --abbrev=0)" build` – kevr Jan 26 '22 at 03:24
0

As of 0.60.3 you may directly assign version from run_command which means the following will work without any meson_options.txt.

project('randomName', 'cpp',
 version : run_command('git', 'rev-parse',  '--short', 'HEAD').stdout().strip(),
  default_options : [])

In particular, it is also possible to assign the result of a bash script, simply invoke it instead of git.

HaoZeke
  • 674
  • 9
  • 19