PREPROCESSOR DEFINE WITH CONTENT OF A BUILD VARIABLE (in MSBuild)

Sysprogs forums Forums VisualGDB PREPROCESSOR DEFINE WITH CONTENT OF A BUILD VARIABLE (in MSBuild)

This topic contains 11 replies, has 2 voices, and was last updated by  support 2 weeks, 6 days ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #22341

    SR
    Participant

    Hi,

    I have read the following post:

    PREPROCESSOR DEFINE WITH CONTENT OF A BUILD VARIABLE

    I want to set a preprocessor define with the hash of my current git revision. I managed to put the revision into a custom build variable (git rev-parse HEAD in a custom build step).

    and wondered how that can be accomplished when using VisualGDB with MSBuild.

    Regards,

    SR

    #22345

    support
    Keymaster

    Hi,

    If you are using MSBuild, you can script it by creating a custom MSBuild target (VisualGDB uses the same target system as regular MSBuild projects).

    You can find extensive documentation on creating MSBuild tasks here: https://docs.microsoft.com/en-us/visualstudio/msbuild/task-writing?view=vs-2017

    Also consider having a quick look through our version updating task used by our open-source plugins. You could create a similar C# task that would export an MSBuild variable and then reference this variable in the “Preprocessor Definitions” setting.

     

    #22365

    SR
    Participant

    To summarize for everyone wondering:

    1. Make new C# Library GitProperties.dll
    2. Add NuGet-Packages: Microsoft.Build.Framework & Microsoft.Build.Utilities
    3. Add Task Class
    1. Add the following lines at the end of your Project file:
    1. Add to your Project Settings –> C/C++ –> Preprocessor –> Preprocessor Defines:
    #22366

    SR
    Participant

    @Support: I have seen that you used a .props file instead of the project file for better reusability.

    I have tried this myself, but it didn’t work.

    What do i have to configure for MSBuild to automatically load this file?

    #22367

    SR
    Participant

    @Support: Have to correct myself:

    using <Target Name=”Build”> overrides the default build process and nothing works, but <Target Name=”BeforeBuild” BeforeTargets=”Build”> works neither in a .vcxproj.</span>

    • This reply was modified 3 weeks, 3 days ago by  SR.
    • This reply was modified 3 weeks, 3 days ago by  SR.
    #22370

    SR
    Participant

    I have again read the AutoVersion.props as you suggested and added

    But the GitPropertiesTarget isn’t invoked.

    Does the VisualGDB Embedded ToolChain override these dependencies?

    #22372

    support
    Keymaster

    Hi,

    Thanks for sharing this. Indeed the target name should not collide with any of the existing targets, as it would override the original target. Setting $(BuildDependsOn) to include your target name should be the right thing to do.

    If it doesn’t work, please try setting the MSBuild build log verbosity to “Diagnostic” via Tools->Options->Build and Run and then check that your target is being built, the task is being executed and returns the correct value and that it happens before the actual compilation phase.

    #22381

    SR
    Participant

    Hi,

    Using the MSBuild Diagnostic setting helped a lot to unterstand the procedure.

    I changes small things in the Task to make it more reliable, but it doesn’t seem to be the problem.

    My new Task:

    And my changes to Project.vcxproj

    And PreProcessor-Settings:

    Outputs this:

    To me it seems like the PreprocessorDefinitions get resolved before GitPropertiesTarget is triggered, as the variables seems to get set correctly, but not the PreprocessorDefinitions.

    PS: I have attached you a shortened version of MSBuild.log (due to your upload-size limit)

    • This reply was modified 3 weeks ago by  SR.
    #22383

    SR
    Participant

    Edit: Retrying the upload:

    Attachments:
    You must be logged in to view attached files.
    #22399

    support
    Keymaster

    Hi,

    If the PreprocessorDefinitions macro gets resolved before the custom tool has a chance to run, please consider a slightly different approach. Remove the reference to your variable from the regular PreprocessorDefinitions and add the following code inside your <Target> element after the invocation of the custom tool (please update it to reference the actual variables):

    As long as the code above is inside the “target” element, it will get evaluated after the tool is invoked and hence the variables will be resolved properly.

    #22408

    SR
    Participant

    Hi,

    I actually tried just that yesterday, but it failed with error:

    <ItemDefinitionGroup> is not allowed inside a target.

    But with the help of some google I figured that this works as desired.

    Thank you very much for your help!

    #22411

    support
    Keymaster

    Hi,

    Sorry about that, it should indeed be ItemGroup. When placed inside a target, it will work the same as ItemDefinitionGroup works normally.

    Good to know it works and let us know if you encounter any further issues.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.