回显到 Makefile 中的同一行

发布于 2024-11-18 15:33:17 字数 508 浏览 2 评论 0原文

我正在尝试在 Windows XP 中使用 git describe 为 GIT 创建一个 version.c 文件。为此,我在 makefile 中调用 gitdescribe ,如下所示:

@echo #include "version.h" > $(path)/version.c  
@echo const char * build_ver = ^" >> $(path)/version.c
git describe >> $(path)/version.c

我的问题是我无法使用 echo 打印到同一行,因此我可以获得类似的内容: const char * build_ver = "v1.1-4-g00a6d8f"

我已经看到了一些从 git 获取版本号的其他方法,但是使用 awk 或 perl 并不是真正的选择,因为我不能假设它们将安装在特定系统。

我尝试将它分配给一个变量,但它抱怨 createProcess。

我将不胜感激任何帮助。

I am trying to create a version.c file for GIT using git describe in Windows XP. For this I am calling git describe in my makefile as follows:

@echo #include "version.h" > $(path)/version.c  
@echo const char * build_ver = ^" >> $(path)/version.c
git describe >> $(path)/version.c

My problem is that I haven't been able to use echo to print to the same line so I can get something like:
const char * build_ver = "v1.1-4-g00a6d8f"

I have seen some other ways to get the version number from git, but using awk or perl is not really an option since I can't assume they will be installed on a particular system.

I've tried assigning it to a variable but it complains about createProcess.

I'd appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜`诱少女 2024-11-25 15:33:17

请参阅 man 1 echo:您可能正在寻找 -n开关。

如果您的 echo 不支持,请尝试 printf

编辑:由于您似乎都不可用,请尝试在同一行上回显所有内容;类似于:

echo 'const char *build_ver = "' `git describe` '";'

编辑 2:适用于 Linux / GNU make 的示例 makefile:

$ cat foo.make

build:
        @echo 'const char *build_ver = "'`git describe`'";'

$ make -f foo.make
const char *build_ver = "2008.07.01-102166-1620-g89db338";

See man 1 echo: you're probably looking for the -n switch.

If your echo doesn't support that, try printf.

EDIT: since neither seem to be available to you, try echoing everything on the same line; something like:

echo 'const char *build_ver = "' `git describe` '";'

EDIT 2: sample makefile that works on Linux / GNU make:

$ cat foo.make

build:
        @echo 'const char *build_ver = "'`git describe`'";'

$ make -f foo.make
const char *build_ver = "2008.07.01-102166-1620-g89db338";

且行且努力 2024-11-25 15:33:17

这是我使用的一些代码的示例,它类似地为您创建带有构建时间和 git 版本的 ac 文件。这还包括将“gitdescribe”输出获取到bat文件变量中的技巧。

:: vss1_version.bat
:: This batch file will create a version c file that 
:: will contain the build date and time along with
:: version information extracted from GIT. 
:: This file expects git to be installed and excessable 
:: from the path

if "%1" NEQ "" goto deploy
echo usage: vss1_version [filename] 
echo i.e. vss1_version .\deploy\vss1\VSS1_VERSION.c
exit /b 1

:deploy

set VSS1_VERSION_FILENAME=%1

:: Delete the current file. 
rm -f -v %VSS1_VERSION_FILENAME%

:: Get time and day nicely formatted
set current_time=%time%
set ver_hour=%current_time:~0,2%
if "%ver_hour:~0,1%" == " " set ver_hour=0%ver_hour:~1,1%
set ver_min=%current_time:~3,2%
if "%ver_min:~0,1%" == " " set ver_min=0%ver_min:~1,1%
set ver_secs=%current_time:~6,2%
if "%ver_secs:~0,1%" == " " set ver_secs=0%ver_secs:~1,1%
set current_date=%date%
set ver_year=%current_date:~-4%
set ver_month=%current_date:~4,2%
if "%ver_month:~0,1%" == " " set ver_month=0%ver_month:~1,1%
set ver_day=%current_date:~7,2%
if "%ver_day:~0,1%" == " " set ver_day=0%ver_day:~1,1%
set datetimef=%ver_year%/%ver_month%/%ver_day%-%ver_hour%:%ver_min%:%ver_secs%

:: Format the date in YYYY\MM\DD-HH:MM:SS.ss
echo const char * build_time = ^"%datetimef%^" ; >> %VSS1_VERSION_FILENAME%

:: Get the output from a program into a local variable fo reuse later
:: Get the "git describe" information into a local variable GITVERSION
FOR /F "tokens=*" %%i in ('git describe --dirt^=*') do SET GITVERSION=%%i 

:: Put the git version information into the file
echo const char * build_ver =  ^"%GITVERSION%^" ; >> %VSS1_VERSION_FILENAME%

echo Created %VSS1_VERSION_FILENAME%
echo %datetimef%
echo %GITVERSION%

set VSS1_VERSION_FILENAME= 
set GITVERSION=
set ver_hour=
set ver_min=
set ver_secs=
set ver_year=
set ver_month=
set ver_day=
set datetimef=
set current_time=
set current_date=

:: Make sure we exit cleanly to the calling batch script will continue
exit /b 1

Here's an example of some code that I use that similarly creates a c file for you with build time and git version. This also includes the trick to get the "git describe" output into a bat file variable.

:: vss1_version.bat
:: This batch file will create a version c file that 
:: will contain the build date and time along with
:: version information extracted from GIT. 
:: This file expects git to be installed and excessable 
:: from the path

if "%1" NEQ "" goto deploy
echo usage: vss1_version [filename] 
echo i.e. vss1_version .\deploy\vss1\VSS1_VERSION.c
exit /b 1

:deploy

set VSS1_VERSION_FILENAME=%1

:: Delete the current file. 
rm -f -v %VSS1_VERSION_FILENAME%

:: Get time and day nicely formatted
set current_time=%time%
set ver_hour=%current_time:~0,2%
if "%ver_hour:~0,1%" == " " set ver_hour=0%ver_hour:~1,1%
set ver_min=%current_time:~3,2%
if "%ver_min:~0,1%" == " " set ver_min=0%ver_min:~1,1%
set ver_secs=%current_time:~6,2%
if "%ver_secs:~0,1%" == " " set ver_secs=0%ver_secs:~1,1%
set current_date=%date%
set ver_year=%current_date:~-4%
set ver_month=%current_date:~4,2%
if "%ver_month:~0,1%" == " " set ver_month=0%ver_month:~1,1%
set ver_day=%current_date:~7,2%
if "%ver_day:~0,1%" == " " set ver_day=0%ver_day:~1,1%
set datetimef=%ver_year%/%ver_month%/%ver_day%-%ver_hour%:%ver_min%:%ver_secs%

:: Format the date in YYYY\MM\DD-HH:MM:SS.ss
echo const char * build_time = ^"%datetimef%^" ; >> %VSS1_VERSION_FILENAME%

:: Get the output from a program into a local variable fo reuse later
:: Get the "git describe" information into a local variable GITVERSION
FOR /F "tokens=*" %%i in ('git describe --dirt^=*') do SET GITVERSION=%%i 

:: Put the git version information into the file
echo const char * build_ver =  ^"%GITVERSION%^" ; >> %VSS1_VERSION_FILENAME%

echo Created %VSS1_VERSION_FILENAME%
echo %datetimef%
echo %GITVERSION%

set VSS1_VERSION_FILENAME= 
set GITVERSION=
set ver_hour=
set ver_min=
set ver_secs=
set ver_year=
set ver_month=
set ver_day=
set datetimef=
set current_time=
set current_date=

:: Make sure we exit cleanly to the calling batch script will continue
exit /b 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文