version.cmake 3.0 KB
Newer Older
Y
Yu Yang 已提交
1 2 3
# Get the latest git tag.
set(PADDLE_VERSION $ENV{PADDLE_VERSION})
set(tmp_version "HEAD")
M
minqiyang 已提交
4
set(TAG_VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(\\.(a|b|rc)\\.[0-9]+)?")
5
set(COMMIT_VERSION_REGEX "[0-9a-f]+[0-9a-f]+[0-9a-f]+[0-9a-f]+[0-9a-f]+")
6
while("${PADDLE_VERSION}" STREQUAL "")
7
  # Check current branch name
Y
Yu Yang 已提交
8
  execute_process(
9
    COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref ${tmp_version}
10
    WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
11 12
    OUTPUT_VARIABLE GIT_BRANCH_NAME
    RESULT_VARIABLE GIT_BRANCH_RESULT
Y
Yu Yang 已提交
13
    ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
14
  if(NOT ${GIT_BRANCH_RESULT})
15
    execute_process(
16 17
      COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 --always
              ${tmp_version}
18 19 20 21
      WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
      OUTPUT_VARIABLE GIT_TAG_NAME
      RESULT_VARIABLE GIT_RESULT
      ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
22
    if(NOT ${GIT_RESULT})
23
      # Check if current branch is release branch
24
      if(${GIT_BRANCH_NAME} MATCHES "release/${TAG_VERSION_REGEX}")
25
        # Check the tag is a correct version
26
        if(${GIT_TAG_NAME} MATCHES "${COMMIT_VERSION_REGEX}")
27 28
          # if no tag was found, set PADDLE_VERSION to 0.0.0 to represent latest
          set(PADDLE_VERSION "0.0.0")
29
        elseif(${GIT_TAG_NAME} MATCHES "v${TAG_VERSION_REGEX}")
30
          string(REPLACE "v" "" PADDLE_VERSION ${GIT_TAG_NAME})
31
        else() # otherwise, get the previous git tag name.
32 33
          set(tmp_version "${GIT_TAG_NAME}~1")
        endif()
M
minqiyang 已提交
34
      else()
35 36 37 38 39 40
        execute_process(
          COMMAND ${GIT_EXECUTABLE} describe --exact-match --tags ${tmp_version}
          WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
          OUTPUT_VARIABLE GIT_EXACT_TAG_NAME
          RESULT_VARIABLE GIT_EXACT_TAG_RESULT
          ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
41
        if(NOT ${GIT_EXACT_TAG_NAME})
42
          # Check if current branch is tag branch
43
          if(${GIT_EXACT_TAG_NAME} MATCHES "v${TAG_VERSION_REGEX}")
44 45
            string(REPLACE "v" "" PADDLE_VERSION ${GIT_EXACT_TAG_NAME})
          else()
46
            set(PADDLE_VERSION "0.0.0")
47 48
          endif()
        else()
49 50
          # otherwise, we always set PADDLE_VERSION to 0.0.0 to represent latest
          set(PADDLE_VERSION "0.0.0")
51
        endif()
52 53
      endif()
    else()
54
      set(PADDLE_VERSION "0.0.0")
55
      message(WARNING "Cannot add paddle version from git tag")
Y
Yu Yang 已提交
56 57
    endif()
  else()
58
    set(PADDLE_VERSION "0.0.0")
59
    message(WARNING "Cannot add paddle version for wrong git branch result")
Y
Yu Yang 已提交
60 61 62
  endif()
endwhile()

63 64
string(REPLACE "-" "." PADDLE_VER_LIST ${PADDLE_VERSION})
string(REPLACE "." ";" PADDLE_VER_LIST ${PADDLE_VER_LIST})
65 66 67 68 69 70
list(GET PADDLE_VER_LIST 0 PADDLE_MAJOR_VER)
list(GET PADDLE_VER_LIST 1 PADDLE_MINOR_VER)
list(GET PADDLE_VER_LIST 2 PADDLE_PATCH_VER)
math(EXPR PADDLE_VERSION_INTEGER "${PADDLE_MAJOR_VER} * 1000000
    + ${PADDLE_MINOR_VER} * 1000 + ${PADDLE_PATCH_VER}")

71
add_definitions(-DPADDLE_VERSION=${PADDLE_VERSION})
72
add_definitions(-DPADDLE_VERSION_INTEGER=${PADDLE_VERSION_INTEGER})
Y
Yu Yang 已提交
73
message(STATUS "Paddle version is ${PADDLE_VERSION}")