I'd like to do what is shown in this answer (http://stackoverflow.com/a/12368262/5136448) at stackoverflow. Is this something I can have added to the build process through a C::B dialog somewhere, or is this something that would necessitate using a custom Makefile for the project?
Have you tried to use a backtick expression?
Code::Blocks doesn't include a Makefile with projects, it seems to just sort of do it transparently in the background, so I can't just edit a file somewhere. What I'm asking is, is there a place in the project dialog / build options somewhere that I can place things that would normally go in a Makefile, into the Code::Blocks' build process? Or, in order to do this, do I need to change the project to use a custom Makefile? (meaning, I have to set up the wxWidgets stuff semi-manually; which is fine, I do it in a Makefile on a Linux project I have).
Your question is pretty vague. C::B's build system has most of the makefile features, so the things described in the stackoverflow post are possible.
In place of $(GIT_VERSION) you might have to use a backtick expression (`git blabla.... `).
See this http://wiki.codeblocks.org/index.php/Variable_expansion
The only problem might happen because C::B caches the output of backtick expressions. But this is fixable.
I'm trying to add those two lines to the pre-build steps and I cannot for the life of me get it to work. It always returns failed for the first line. I've tried:
GIT_VERSION := $(shell git describe --abbrev=4 --dirty --always --tags)
GIT_VERSION := `git describe --abbrev=4 --dirty --always --tags`
GIT_VERSION := {cmd /c git describe --abbrev=4 --dirty --always --tags}
GIT_VERSION := {git describe --abbrev=4 --dirty --always --tags}
Output of the first one shows it removing the $(shell part, but it leaves the trailing ) in there.
I've also tried only having different variations of the second line in the pre-build steps and adding the first one to the custom variables tab (the next one over from pre/post build tab), but that didn't work either (it does substitute the variable for the command, but it still fails).
I'm obviously misunderstanding how this works. I figured it would be added to pre-build steps exactly as you would have it in a Makefile, but I guess not. This is on Windows 10, svn 10703, and I've add git's bin directory to the system path.
Have you read the link I've posted?
None of the four lines is valid in codeblocks!
Also you don't have to use a pre-build step for this.
I did read the links, I still couldn't quite gather what I was supposed to do.
Something like - in the defines section MYDEFINE=`command-that-will-produce-the-required-output`. You can probably use custom variable or this, too.
I ended up going to the #defines tab of the Compiler Settings and putting this there:
BUILD_VERSION=\"`git describe --abbrev=5 --dirty --always --tags`\"
which worked as I needed it to.
Just keep in mind that the output of backtick expressions are cached and you have to restart cb in order to get a new value!
I see what you mean about the backtick expression being cached. The output outside of codeblocks changes, but the expression is still the old value in codeblocks. You mentioned this was fixable in an earlier post; how would I go about that? I'm googling, but not finding much.
I don't want to have to restart C::B each time I add a new commit to git (so that the expression gets updated). Is that the only way?
At the moment this is the only way.
The code must be changed to fix this problem.
Maybe I'll add a per-project mechanism somewhere to my local build of C::B (checkbox, or box with list of expressions not to cache, etc). When I was googling the backtick caching thing a little while ago, I did come across a post of yours from a few years back saying it was best to cache it for situations like generating the wxconfig flags/libs, etc.
You can also try to use scripting: http://wiki.codeblocks.org/index.php?title=Scripting_commands
the "ExecuteAndGetOutput" command should help.
For more reading:
http://wiki.codeblocks.org/index.php/Variable_expansion#Script_expansion
greetings
In fact, I'm planning to fix this problem.
I have an idea, I just need to test it a bit more, because this part of C::B is a bit fragile.
Quote from: BlueHazzard on April 10, 2016, 08:48:40 PM
You can also try to use scripting: http://wiki.codeblocks.org/index.php?title=Scripting_commands
the "ExecuteAndGetOutput" command should help.
For more reading:
http://wiki.codeblocks.org/index.php/Variable_expansion#Script_expansion
greetings
Trying to get this working, but having trouble.
Previously, I had this (although I had to recompile C::B to not use the backtick cache; I've since changed it back so I can try the scripting) under the
#defines tab of project compiler settings, which works:
BUILD_VERSION=\"`git describe --abbrev=5 --dirty --always --tags"`\"Following the example in the C::B project file (I think it's under
Other compiler options) I changed it to this:
BUILD_VERSION=\"[[IO.ExecuteAndGetOutput(_T("git describe --abbrev=5 --dirty --always --tags"))]]\"But it always comes out blank. I tried adding .ToStdString().c_str(), but it complained that they were unknown or undefined or something.
So following this example from the C::B project itself:
[[if (GetCompilerFactory().GetCompilerVersionString(_T("gcc")) >= _T("4.8.0")) print(_T("-Wno-unused-local-typedefs"));]]I tried changing mine to this:
BUILD_VERSION=\"[[print(_T(IO.ExecuteAndGetOutput(_T("git describe --abbrev=5 --dirty --always --tags"))))]]\"but it fails, with a messagebox saying:
Filename: CommandLine
Error: Incorrect function argument
Details: (Details were blank). So, I'm not sure how to do this since finding examples regarding the [[]] ability is turning up next to nothing. I couldn't figure how to to use the "allowInsecureScripts" constant to see if the macro was defined, and I tried grepping for the macro itself but it was nowhere to be found except in sc_io.cpp, where it's only checked. Regardless, I removed the #ifndef and recompiled C::B just to test if ExecuteAndGetOutput was returning blank because of security, but it still returns blank.
I added this
TEST="[[print(IO.ExecuteAndGetOutput(_T("git --version")).RemoveLast(1))]]"
into Project->Build options->#defines
and it seems to work
greetings
[EDIT:] Corrected the code!!!! No ticks!!!
I added the RemoveLast(1) part to my version (and removed the _T() part in print surrounding the ExecuteAndGetOutput()) and that seems to have been the issue. I never would have guessed that. Looking at the code, it seemed that it was just returning the plain result string already, but clearly not. Thanks.
To clear things up:
RemoveLast(1) is needed because ?git? (or ExecuteAndGetOutput?) gives a "\n" at the end and this will interfere with the gcc command
_T() is only needed if you give native strings (closured in "") to a wxWidgets function
print is needed to transfer the string value outside the [[]] script environment
greetings
[EDIT:]
To develop this code you can use the scripting console from view->Scripting Console