Highlighted
Absent Member.
Absent Member.
1546 views

Setting Environment Variables prior to build

I am using Visual COBOL v2.3 on Red Hat Enterprise Linux 64 bit. I am trying to execute a script which contains the setting of various environment variables required during build. I have tried to use the build configuration Events by both sourcing the script or simply executing the script. In both cases the environment variables are not set at build time. Setting environment variable one at a time within the project will not work because the script is built dynamically. How can this be accomplished?

0 Likes
1 Reply
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Setting Environment Variables prior to build

Setting variables in a sub-process doesn't work because environment variable settings are inherited by child processes, but not propagated up to parents. So they only affect the child process and its descendants.

Visual Studio projects are executed by MSBUILD, or the same processor running under the IDE. There's nothing in stock MSBUILD that sets environment variables, as far as I know.

Here's an old StackOverflow post that shows how to add a task that sets an environment variable:

social.msdn.microsoft.com/.../how-to-set-envrionment-variables-in-msbuild-file

There's also a NuGet package for it:

www.nuget.org/.../MSBuild.SetEnvVariable

That could be adapted to read a file (the one you build dynamically) and apply the settings it finds in it. It's not a trivial change, but it's doable.

Another alternative would be to create a script that 1) processes your settings and 2) invokes the COBOL compiler. Then use that from Makefile projects. But that gives up the advantages of using COBOL projects.

Or build from outside the IDE using a script like this:

   call set-my-variables.cmd

   msbuild myproject.cblproj (or mysolution.sln)

If you want to build from the IDE, you could create a makefile project that runs that script. There are various options for msbuild that you can use to set the build configuration (debug/release), etc.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.