如何在 Visual Studio 预构建事件中添加 Fortran 项目的 git 版本信息

问题描述 投票:0回答:2

我正在尝试添加预构建事件以添加 Windows 上的 Fortran 项目的版本信息。预构建事件是创建 gitparams.h 文件,然后向其中写入一些 git 信息。我使用命令行编写文件并在 Version.F90 文件中使用它没有问题。

预构建命令:

echo | set /p dummyName=#define GITVERSION > gitparams.h
git describe --dirty --always --tags >> gitparams.h

echo | set /p dummyName=#define GITBRANCH >> gitparams.h
git branch --show-current >> gitparams.h

输出

gitparams.h

#define GITVERSION 61154c3-dirty
#define GITBRANCH main

Version.F90
文件中:

#include "gitparams.h"

#if defined(GITVERSION)
    Character(32) :: GlobalRevisionNumber = GITVERSION
#else
    Character(32) :: GlobalRevisionNumber = "unknown"
#endif

错误:

error #5143: Missing mandatory separating blankSyntax error, found IDENTIFIER 'C3' when expecting one of: :: ) , : \* \<END-OF-STATEMENT\> ; . (/ + - \] /) ' \*\* / // \> PRIVATE ...

一种解决方案是,对于带有特定字符或数字的值,在

gitparams.h
中添加引号,如下所示:

#define GITVERSION "61154c3-dirty"
#define GITBRANCH "main"

但是,我尝试了多种方法,但都没有得到我想要的。我对 bash 脚本不熟悉。有人可以帮我提供一些建议或解决方案吗?

这是在Jonny和Compo的帮助下最终的解决方案。

解决方案 1 基于 Jonny 的建议。

@echo off

: get the repository url into gitrepurl
for /f "delims=" %%i in ('git config --get remote.origin.url') do set gitrepurl=%%i

: get the branch into gitbranch
for /f "delims=" %%i in ('git branch --show-current') do set gitbranch=%%i

: get the version into gitversion
for /f "delims=" %%i in ('git describe --dirty --always --tags') do set gitversion=%%i

: get the commit time into gitcommittime
for /f "delims=" %%i in ('git log -1 --format^=%%cd --date^=format-local:"%%Y-%%m-%%dT%%H:%%M:%%S%%z"') do set gitcommittime=%%i

(
  echo | set /p dummyName=#define GITREPURL "%gitrepurl%"
  echo:
  echo | set /p dummyName=#define GITBRANCH "%gitbranch%"
  echo:
  echo | set /p dummyName=#define GITVERSION "%gitversion%"
  echo:
  echo | set /p dummyName=#define GITCOMMITTIME "%gitcommittime%"
  echo:
  echo | set /p dummyName=#define BUILDTIME "%date%T%TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%"
) > GitParams.h

@echo on

Compo建议后的解决方案2。

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Rem Define variable for the location of git executable
Set "git=R:\Git\bin\git.exe"

Rem Undefine possible existing variables used later
For %%G In (repurl branch version committime) Do Set "git%%G="

Rem Get the repository URL into GITREPURL
For /F "Delims=" %%G In ('^""%git%" config --get remote.origin.url 2^>NUL^"') Do Set "gitrepurl=%%G"

Rem Get the branch into GITBRANCH
For /F "delims=" %%G In ('^""%git%" branch --show-current 2^>NUL^"') Do Set "gitbranch=%%G"

Rem Get the version into GITVERSION
For /F "Delims=" %%G In ('^""%git%" describe --dirty --always --tags 2^>NUL^"') Do Set "gitversion=%%G"

Rem Get the commit time into GITCOMMITTIME
For /F "Delims=" %%G In ('^""%git%" log -1 --format^=%%cd --date^=format-local:"%%Y-%%m-%%dT%%H:%%M:%%S%%z" 2^>NUL^"') Do Set "gitcommittime=%%G"

Rem End script if any of the previous variable definitions failed
For %%G In (repurl branch version committime) Do If Not Defined git%%G GoTo :EOF

Rem Define variables for the date and time
Set "_d=%DATE%"
Set "_t=%TIME%"

Rem Create the header file
(
    Echo #define GITREPURL "%gitrepurl%"
    Echo #define GITBRANCH "%gitbranch%"
    Echo #define GITVERSION "%gitversion%"
    Echo #define GITCOMMITTIME "%gitcommittime%"
    Echo #define BUILDTIME "%_d%T%_t:~0,2%:%_t:~3,2%:%_t:~6,2%"
) >"GitParams.h"

@Echo on

谢谢,

丹尼尔

git visual-studio batch-file
2个回答
2
投票

这只是新代码最近编辑的固定版本:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Rem Define variable for the location of git executable
Set "git=P:\athTo\git.exe"

Rem Undefine possible existing variables used later
For %%G In (repurl branch version committime) Do Set "git%%G="

Rem Get the repository URL into GITREPURL
For /F "Delims=" %%G In ('^""%git%" config --get remote.origin.url 2^>NUL^"') Do Set "gitrepurl=%%G"

Rem Get the branch into GITBRANCH
For /F "delims=" %%G In ('^""%git%" branch --show-current 2^>NUL^"') Do Set "gitbranch=%%G"

Rem Get the version into GITVERSION
For /F "Delims=" %%G In ('^""%git%" describe --dirty --always --tags 2^>NUL^"') Do Set "gitversion=%%G"

Rem Get the commit time into GITCOMMITTIME
For /F "Delims=" %%G In ('^""%git%" log -1 --format^=%%cd --date^=format-local:"%%Y-%%m-%%dT%%T%%z" 2^>NUL^"') Do Set "gitcommittime=%%G"

Rem End script if any of the previous variable definitions failed
For %%G In (repurl branch version committime) Do If Not Defined git%%G GoTo :EOF

Define variables for the date and time
Set "_d=%DATE%"
Set "_t=%TIME%"

Rem Create the header file
(
    Echo #define GITREPURL "%gitrepurl%"
    Echo #define GITBRANCH "%gitbranch%"
    Echo #define GITVERSION "%gitversion%"
    Echo #define GITCOMMITTIME "%gitcommittime%"
    Echo #define BUILDTIME "%_d%T%_t:~0,2%:%_t:~3,2%:%_t:~6,2%"
) 1>"GitParams.h"

EndLocal

请记住将第五行的

P:\athTo
更改为 git 可执行文件的实际位置。


1
投票

我认为您混淆了

batch
bash

编辑:我没有意识到这是一个 Visual Studio 预构建命令。如果您不将其作为批处理脚本运行,则可能需要在

%%i
命令中将
%i
更改为
for

以下

batch
脚本不太漂亮,但应该可以工作。您需要将其命名为
gitparams.bat
之类的名称,并使用
start gitparams.bat
等命令提示符运行它。

@echo off

: get the version into gitversion
for /f "delims=" %%i in ('git describe --dirty --always --tags') do set gitversion=%%i

: get the branch into gitbranch
for /f "delims=" %%i in ('git branch --show-current') do set gitbranch=%%i

(
  echo | set /p dummyName=#define GITVERSION "%gitversion%"
  echo:
  echo | set /p dummyName=#define GITBRANCH "%gitbranch%"
) > gitparams.h

@echo on

gitparams.h
的内容应类似于:

#define GITVERSION "61154c3-dirty"
#define GITBRANCH "main"

我没有方便的带有标签的 git 存储库,因此可能涉及更多故障排除,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.