Author Topic: [Squirrel] How to set variable value visible in project?  (Read 4246 times)

Offline DejaVu

  • Single posting newcomer
  • *
  • Posts: 4
[Squirrel] How to set variable value visible in project?
« on: August 26, 2015, 02:28:25 pm »
I've got build script which I use for several projects:
Code
// set build version (global variable):
buildVersion <- "1.0.0";

// generate build options (called by codeblocks while building):
function SetBuildOptions(base)
{
// enable all warnings:
base.AddCompilerOption(_T("-Wall"));

// enable C++11 standard:
base.AddCompilerOption(_T("-std=c++11"));

// set directory with libraries:
base.AddLibDir(_T("$(WORKSPACE_DIR)/../_tmp_/$(TARGET_NAME)-libs/"));

// set macro with version number:
local macroVersion = "-DBUILD_VERSION=\"" + buildVersion + "\"";
base.AddCompilerOption(_T(macroVersion));
}
I would like to set VERSION variable which will be visible in Project Options/Build Targets/Output Filename. Currently I have 'Output filename' set to value:
Code
$(WORKSPACE_DIR)/../bin/$(PROJECT_NAME)-$(VERSION)
Currently output file name doesn't contain $(VERSION) because variable is not set. I have tried to create global variable in build script but it didn't work. I have also spent few hours on looking solution in Google, but I didn't find solution for this issue.

Question: How to set environment variable from build script which will be visible in project settings?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: [Squirrel] How to set variable value visible in project?
« Reply #1 on: August 26, 2015, 05:16:15 pm »
Code
$(VERSION)

Do you want to use a CB Global variable or a CB custom variable?

Because your post seems to NOT clearly state which you want to use!


I know scripts can be used to create a CB custom variable; I have no idea if this is true for CB Global variables.
I also am NOT sure if environment variables are able to be created by scripting.
I would guess NOT because it likely would be a major security hole.

Tim S.
« Last Edit: August 26, 2015, 05:19:57 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline DejaVu

  • Single posting newcomer
  • *
  • Posts: 4
Re: [Squirrel] How to set variable value visible in project?
« Reply #2 on: August 26, 2015, 05:29:48 pm »
I don't care if variable will be global or local. All what I want to achieve is to create variable from build script which will be visible by Code::Blocks in project settings.

1. I don't want to declare any variable in GLOBAL IDE SETTINGS (solution must be portable via reposittory);
2. I don't want to declare any variable via Project Build Options/Custom Variables, because this variable is visible only per project;
3. I want to declare variable by build script, because all projects are using the same build script and changing version in one place will affect to all projects.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: [Squirrel] How to set variable value visible in project?
« Reply #3 on: August 26, 2015, 06:10:16 pm »
For what objects/classes have you tried the method "SetVar" and it failed to work?

http://wiki.codeblocks.org/index.php?title=Scripting_commands

It worked for me in the past to set a CB custom variable at the target level.
I never figured out if setting one at the project level was possible.

Edit: I have never used SetBuildOptions script before; I have just edited CB wizards.
Edit2: I am guessing you will need to call "GetParentProject"

Tim S.
« Last Edit: August 26, 2015, 06:41:51 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: [Squirrel] How to set variable value visible in project?
« Reply #4 on: August 26, 2015, 06:47:35 pm »
I just thought of a simpler solution if you fail to get the build script to work.

Just create a CB Project template with the value of the CB Custom variable set in there to the default value.

If that not good enough, the only other thing I know to try after the build script fails is to create a CB project wizard.

Tim S.
 
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: [Squirrel] How to set variable value visible in project?
« Reply #5 on: August 27, 2015, 07:30:12 am »
Another solution is to make use of envvars through the envvars plugin. It exposes scripting commands that you can use. However, your projects would depend on the presence of this plugin though.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline DejaVu

  • Single posting newcomer
  • *
  • Posts: 4
Re: [Squirrel] How to set variable value visible in project?
« Reply #6 on: August 28, 2015, 02:44:03 pm »
For what objects/classes have you tried the method "SetVar" and it failed to work?

http://wiki.codeblocks.org/index.php?title=Scripting_commands

It worked for me in the past to set a CB custom variable at the target level.
I never figured out if setting one at the project level was possible.

Edit: I have never used SetBuildOptions script before; I have just edited CB wizards.
Edit2: I am guessing you will need to call "GetParentProject"

Tim S.

Thanks! Your post solved my problem :) Final solution provided below:
Code
// set build version (global variable):
buildVersion <- "1.0.1";

// generate build options (called by codeblocks while building):
function SetBuildOptions(base)
{
// set value for variable $(VERSION) which is visible in project settings:
base.SetVar(_T("VERSION"),_T(buildVersion),false);

//(...)
}