-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide access to environment variables in build files #9
Comments
At the moment this is not possible. Something about this needs to be designed at some point. If you wish to set a default, you can do so with -D the first time you run Meson. So something like this meson .. -Dsomeoption=foobar or even meson .. -Dsomeoption=$SANDBOX |
👍 I am also in a need to use this. |
Not having this feature is a bit of an issue for me. I'm currently porting a build system from CMake to Meson, and in this use case I need to build against an installed SDK in Windows. In order to be able to automatically detect its location in the filesystem, the SDK sets its installed path as an environment variable during installation. The ability to set the default value in meson_options.txt to an existing environment variable (alongside an optional fallback value, perhaps) would be the only way to keep the current simplicity of building the project. |
It makes sense to allow default values of A workaround is also to use pymodule = import('python3')
python3 = pymodule.find_python()
res = run_command(python3, '-c', 'import os; print(os.environ["SDK_VARIABLE"])')
if res.returncode() == 0
sdk_var = res.stdout
else
# not found, do fallback
endif |
@nirbheek, I'm using the LunarG's Vulkan SDK, and it's technically assembled from a few open-source repositories. This is its main repository: KhronosGroup/Vulkan-LoaderAndValidationLayers. As for my use case, I only need to include the headers from the And thanks a lot for the interim solution, I'll try it out. |
It would be cool if we added support for the Vulkan SDK to Meson. Would you be interested in working on that? You know more about it than anyone else. It's really simple: inside I'd be happy to review and help you refine your PR of course. :) |
Sure, I guess I can give it a try. I'll get to it once I finish converting my CMake project to Meson. |
Great! Looking forward to your PR. 👍 |
For finding packages, is the problem that the package doesn't ship a pkg-config file, or is it that pkg-config is a Unix thing and not used on Windows? If it's a missing pkg-config file problem, why not write it for them? Should be preferrable to adding special casing to Meson. In the case of Vulkan, I see the dependency documentation has you covered now:
If it's a Windows problem (of not having something akin to pkg-config), then how on Earth are you supposed to find build dependencies on Windows in general? pkg-config supports Windows, but that of course only means that it could be used. |
For access to some environment variable from a meson.build, I've found this trick:
It is a little too verbose. But as it is possible, I think meson should propose some
-- Maurice |
Accessing environment variables from Meson scripts is intentionally not supported and will not be added in the foreseeable future. Environment variables are terrible for any sort of configuration because they are mutable global state. If your setup depends on environment variables being set, then running any sort of command that causes reconfiguration from a different terminal that does not have those envvars set (or from an IDE or any of a thousand of different options) breaks your setup in unobvious ways that are at worst incredibly difficult to debug. Using envvars for configuration is a code smell. Don't use them whenever possible. Convert those to project options or something similar instead. It is the only reliable solution. |
To add to this, if there are standard frameworks that require configuration via environment variables, the best way forward is:
|
Thank you for this guideline. In fact I discovered Meson thank to the very nice presentation from Jussi at INRIA-Saclay (France) last friday. Then I really wanted to try it, then if possible switch from cmake to meson ;-). I'm trying to expose here one typical use case I try to solve with meson. Let be a proprietary closed software (CPLEX from IBM) which propose some executables cplex, cplexamp, ... some static libraries et some includes directories. The same need could arise from some open source projet (say xlife++ for finite elements). We want to install several versions of cplex in a non intrusive manner:
In fact, we vant to switch compilation of one project from which use one cplex library version to another cplex library version without modifying the compiled project code (no cplex pathes should be hard coded in the project). For exemple let:
be two shell commands which update the standard PATH, LD_LIBRARY_PATH variables and position some others specifics environment variables like:
(and much others with unambiguous names) I can provide some In fact, I probably want to use a command like this in meson.build:
or:
But I don't know how to make this working. Perhars I should write one pkg-config for each -- Maurice |
The "correct" solution is to get upstream to add pkg-config files to their build system. This is not guaranteed to work or even if it does it takes a long time for this to appear. The same can be said about convincing upstream to build with Meson so you can use the dependency as a subproject. :) In the mean time there are a few choices you can do:
The latter works roughly by having a project option with the name of, say,
And get the library with something like:
You can change the value of |
Hmm, getting IBM to use Meson to build CPLEX would be great, but So what I understand is that the (my) best way to process is to write a Then when a user want to do some development with a specific set of tools, I'll ask them to call explicitly that set of tools in their shell:
This could add some variables (and update PATH, ...) but above all add pathes to the PKG_CONFIG_PATH variable pour some config file (cplex-X.Y.Z.cp, ...) Now I suppose a
Is it right? |
Pretty much apart from the fact that
|
Thank you versus much Jussi, I finally managed to make it work! For every platform, I'll have a directory with all versions of my package plus a link (e.g @cplex.pc) to prefered one (e.g cplex1280.pc). I need yet to clean up installation, clean up the .pc files and find good positions for pkgconfig paths. The shell commands:
(among other) complete the -- Maurice |
It would be nice to provide access to |
That value will generally only be available when the install script is run, not when meson is being run, so I don't see how that is useful? |
e.g.,
and have |
It is a common idiom to do something like: $ ./configure && make
$ DESTDIR=/foo/bar/dir make install And the meson equivalent is: $ meson _build && ninja -C _build
$ DESTDIR=/foo/bar/dir ninja -C _build install In this case your install script will fail. You should ideally use a wrapper script (python, preferably since that will always be available), and fetch |
It wouldn't fail if Meson expanded I can certainly (and already have) made a wrapper script, but I have to duplicate a bunch of path computation in it since I don't have access to Meson variables there. |
I have another use-case for reading an environment variable (maybe). I use
The issue arises when I try to build in a Fedora container.
There's probably more than one solution to that, but the one that seems the most straightforward to me would be to test if the environment variable If anyone has a better idea btw, you're welcome :) |
@elboulangero in this use-case you want the env variable check to be done when the test is run, not when the configuration is done, because in theory the configuration and the running could be done in different environments. So I'd recommend using a script that skips the test when you know that |
The "mesonic" and portable way of doing this is to have a standalone Python script that does all the inspection and validation needed and then either runs the test or returns error code 77 (which means the test was skipped). Then you would use it something like:
This has the added benefit that if you want to add more validation steps, adding them to the validator script is easy. |
@nirbheek @jpakkane I followed your suggestion and used a standalone script, it works great. https://gitlab.com/goodvibes/goodvibes/commit/6acdf51 Thanks a bunch! |
It appears that it's a long-standing Meson design design to not directly read environment variables, so it seems this issue should be closed. |
I ended up here trying to find a way to enable validation layers on a vulkan project. Normally you do that by exporting an env var. I saw in this thread that official support was added. The docs do shortly mention Vulkan but I assume this is for linking. To enable validation layers one must set the VK_LAYER_PATH env var. Now, I can just export the path to my .profile. However that only works on Linux and adds some cognitive load on the user of the system should they chose to build the project. Is it possible to have some support for the layers path in a similar fashion to I can try adding it myself with some guidance. It seems the vulkan class in ui.py would be the place to send the flag into, but trying to sneak around meson;s code I am not sure how to get the flag down there to enable the env var. |
It's only necessary to call 'systemd-tmpfiles --create' when building and installing from source on the host operating system. It's not needed when using a pre-built binary downstream package, because: * When 'meson install' is called as part of building the package, that's not when the temporary files need to be created. They need to be created when the binary package is later downloaded and installed by the user. * Downstream tools can sometimes handle it automatically. eg., on Fedora, the systemd RPM installs a trigger that tells RPM to call 'systemd-tmpfiles --create' automatically when a tmpfiles.d snippet is installed. It's also not needed when installing inside a toolbox container because the files that 'systemd-tmpfiles --create' is supposed to create are meant to be on the host. Downstream distributors set the DESTDIR environment variable when building their packages. Therefore, it's used to detect when a downstream package is being built. Unfortunately, environment variables are messy and, generally, Meson doesn't support accessing them inside its scripts [1]. Therefore, this adds a spurious build-time dependency on systemd for downstream distributors. However, that's probably not a big problem because all supported downstream operating systems are already expected to use systemd for the tmpfiles.d(5) snippets to work. [1] mesonbuild/meson#9 containers#955
It's only necessary to call 'systemd-tmpfiles --create' when building and installing from source on the host operating system. It's not needed when using a pre-built binary downstream package, because: * When 'meson install' is called as part of building the package, that's not when the temporary files need to be created. They need to be created when the binary package is later downloaded and installed by the user. * Downstream tools can sometimes handle it automatically. eg., on Fedora, the systemd RPM installs a trigger that tells RPM to call 'systemd-tmpfiles --create' automatically when a tmpfiles.d snippet is installed. It's also not needed when installing inside a toolbox container because the files that 'systemd-tmpfiles --create' is supposed to create are meant to be on the host. Downstream distributors set the DESTDIR environment variable when building their packages. Therefore, it's used to detect when a downstream package is being built. Unfortunately, environment variables are messy and, generally, Meson doesn't support accessing them inside its scripts [1]. Therefore, this adds a spurious build-time dependency on systemd for downstream distributors. However, that's probably not a big problem because all supported downstream operating systems are already expected to use systemd for the tmpfiles.d(5) snippets to work. [1] mesonbuild/meson#9 containers#955
I honestly agree, but sometimes there is no choice. For example the devkitPro toolchains have no official fixed location and are instead identified by the environment variables they set ( Since its only distributed via pacman and the maintainer has a history of blocking access to direct download if he deems the machine downloading it is not trustworthy its not possible to simply download the latest version on the fly. Building the toolchain from scratch is not an option either since the buildscripts are, according to the lead developer, not used for creating the pacman packages, are licenseless and build incomplete versions of the toolchain. Their official solution for using meson is a shell script that generates a cross file on the fly, but that is not a reliable solution since upstream does not commit to keeping it working nor can it easily be detected within Meson |
Sounds like a job for |
BUG: fix macOS version checks so things work in very old versions
How would I include the value of an environment variable in either my meson.build or meson_options.txt files? Is that even possible?
E.g.
option('someoption', type : 'string', value : ${SANDBOX})
The text was updated successfully, but these errors were encountered: