Chinaunix首页 | 论坛 | 博客
  • 博客访问: 963758
  • 博文数量: 109
  • 博客积分: 554
  • 博客等级: 中士
  • 技术积分: 2577
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 12:49
文章分类

全部博文(109)

文章存档

2019年(5)

2016年(7)

2015年(9)

2014年(1)

2013年(71)

2012年(16)

分类: 嵌入式

2013-08-14 11:16:50

其实,一直以来,我们编译KVM(Linux kernel)生成的RPM包中的kernel版本总是带有一个“莫名其妙”的加号(+),其实我知道大概是因为我们修改了Linux.git(或 kvm.git)中的一些文件。但是我们只是修改了一下Makefile,让我们做RPM包是方便而已,一般我也没有在编译时修改其他的源代码文件,所以 我想把这个加号去掉,对其进行了简单的研究,问题已经搞定了,记录如下吧。

kernel版本出现一个加号(plug sign)的原因可能是如下两点,当然前提是使用Linux的GIT repository,且CONFIG_LOCALVERSION_AUTO和LOCALVERSION都没有设置。
(1)如果当前repository的commit ID不是某一个tag,则默认有一个加号。因为在最上层的Makefile中只有该repository中最近一次tag的版本信息,需要用加号(+)来标识它并非一个tag(如:3.5.0)。
(2)如果当前repository的commit ID刚好是一个tag,且其中有GIT管理下的文件被改动,这默认有一个加号(+)。
如果想避免这个烦人的加号,可以将scripts/setlocalversion脚本中带有’ echo “+” ‘和’ res=”$res${scm:++}” ‘的这两行删掉即可。
To avoid the additional plus sign in kernel release version, just remove ‘ echo “+” ‘ line and ‘ res=”$res${scm:++}” ‘ line in scripts/setlocalversion file.

首先,Linux的顶层的Makefile关于版本的信息,以及对”make kernelrelease”的定义如下:


  1. VERSION = 3
  2. PATCHLEVEL = 5
  3. SUBLEVEL = 0
  4. EXTRAVERSION =
  5. NAME = Saber-toothed Squirrel
  6. #..............
  7.         @echo ' kernelrelease - Output the release version string'
  8.         @echo ' kernelversion - Output the version stored in Makefile'
  9. #..............
  10. kernelrelease:
  11.         @echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))"
  12. kernelversion:
  13.         @echo $(KERNELVERSION)
然后,我们执行“make kernelrelease”则会生成kernel发布的版本信息,如下:

  1. 然后,我们执行“make kernelrelease”则会生成kernel发布的版本信息,如下:
注意,这个加号其实是作为本地的版本号加进去的,详见scripts/setlocalversion这个脚本,有如下的部分重要内容。

  1. scm_version()
  2. {
  3.         local short
  4.         short=false
  5.         cd "$srctree"
  6.         if test -e .scmversion; then
  7.                 cat .scmversion
  8.                 return
  9.         fi
  10.         if test "$1" = "--short"; then
  11.                 short=true
  12.         fi
  13.         # Check for git and a git repo.
  14.         if test -d .git && head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
  15.                 # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  16.                 # it, because this version is defined in the top level Makefile.
  17.                 if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
  18.                         # If only the short version is requested, don't bother
  19.                         # running further git commands
  20.                         if $short; then
  21.                                 echo "+"
  22.                                 return
  23.                         fi
  24.                         # If we are past a tagged commit (like
  25.                         # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
  26.                         if atag="`git describe 2>/dev/null`"; then
  27.                                 echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
  28.                         # If we don't have a tag at all we print -g{commitish}.
  29.                         else
  30.                                 printf '%s%s' -g $head
  31.                         fi
  32.                 fi
  33.         if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
  34.                         printf '%s' -dirty
  35.                 fi
  36.                 # All done with git
  37.                 return
  38.         fi
  39. #................
  40. }
  41. #................
  42. # CONFIG_LOCALVERSION and LOCALVERSION (if set)
  43. res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
  44. # scm version string if not at a tagged commit
  45. if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
  46.         # full scm version string
  47.         res="$res$(scm_version)"
  48. else
  49.         # append a plus sign if the repository is not in a clean
  50.         # annotated or signed tagged state (as git describe only
  51.         # looks at signed or annotated tags - git tag -a/-s) and
  52.         # LOCALVERSION= is not specified
  53.         if test "${LOCALVERSION+set}" != "set"; then
  54.                 scm=$(scm_version --short)
  55.                 res="$res${scm:++}"
  56.         fi
  57. fi
  58. echo "$res"
如下的一些信息可以方便你理解上面的这段脚本。

  1. [root@jay-linux linux.git]# git rev-parse --verify --short HEAD
  2. 28a33cb
  3. [root@jay-linux linux.git]# git describe --exact-match 2>/dev/null
  4. v3.5
  5. [root@jay-linux linux.git]# git diff-index --name-only HEAD
  6. mm/oom_kill.c
  7. [root@jay-linux linux.git]# man git
  8.     #...........
  9.  git-rev-parse(1)
  10.          Pick out and massage parameters.
  11.  git-describe(1)
  12.          Show the most recent tag that is reachable from a commit.
  13.  git-diff-index(1)
  14.          Compares content and mode of blobs between the index and repository.
  15.     #............

另外,对于setlocalversion中的一个语法解释一下:
${var:-value1} 在变量var不为空时,保持var原有的值不变;如果var变量未设置或者为空,这表达式结果为value1,但是变量var的值并不改变(未设置或为空)。
${var:+value1} 在变量var不为空时,表达式结果为value1;如果var变量未设置或者为空,这表达式结果为空。${var+value1}的效果一样。
${var:=value1} 在变量var不为空时,保持var原有的值不变;如果var变量未设置或者为空,这表达式结果为value1,变量var也被赋值为value1。
${var:?value1} 在变量var未设置或为空时,脚本会退出并抛出一个错误信息(包含value1)。

另外,如下一篇博客也研究了这个问题,供大家参考。

http://blog.csdn.net/adaptiver/article/details/7225980

转载地址:
阅读(3858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~