github.com/matrixorigin/matrixone@v1.2.0/proto/gen.sh (about)

     1  #!/bin/bash
     2  #
     3  # Generate all MatrixOne protobuf bindings. Run from repository root.
     4  #
     5  set -ex
     6  
     7  program_exists() {
     8    if command -v "${1}" > /dev/null 2>&1; then
     9      echo "ok"
    10    else
    11      echo "fail"
    12    fi
    13  }
    14  
    15  VENDOR_DIR="$PWD/vendor"
    16  PB_DIR="$PWD/pkg/pb"
    17  PROTOC_DIR="$PWD/proto"
    18  PROTO_SYNTAX_VERSION='3'
    19  PROTOC_VERSION='21.1'
    20  GOGOPROTOBUF_VERSION='1.'
    21  
    22  if [ "${GOPATH}" == "" ];then
    23    GOPATH=`go env GOPATH`
    24  fi
    25  echo "GOPATH: ${GOPATH}"
    26  
    27  res=$(program_exists goimports)
    28  echo "res: ${res}"
    29  if [ "${res}" == "ok" ];then
    30    echo "goimports exist"
    31  else
    32    echo "install goimports"
    33    go install golang.org/x/tools/cmd/goimports@latest
    34  fi
    35  
    36  res=$(program_exists protoc)
    37  echo "res: ${res}"
    38  if [ "${res}" == "ok" ];then
    39    echo "protoc exist"
    40    version=$(protoc --version)
    41    if [[ "${version}" == *"${PROTO_SYNTAX_VERSION}.${PROTOC_VERSION}"* ]];then
    42      echo "protoc version matches ${PROTOC_VERSION}"
    43    else
    44      echo "protoc version does not match"
    45    fi
    46    if [ -f ${GOPATH}/bin/protoc ]; then
    47      ${GOPATH}/bin/protoc --version
    48    else
    49      ln -s $(which protoc) ${GOPATH}/bin/protoc
    50      ${GOPATH}/bin/protoc --version
    51    fi
    52  
    53  else
    54    echo "install protoc"
    55    if [ "$(uname)" == "Darwin" ];then
    56      OS='osx'
    57    elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ];then
    58      OS='linux'
    59    else
    60      echo "unsupported os, failed"
    61      exit 1
    62    fi
    63    if [[ "$(uname -m)" == *"x86"* ]];then
    64      ARCH='x86'
    65    elif [[ "$(uname -m)" == *"arm"* ]];then
    66      ARCH='aarch'
    67    elif [[ "$(uname -m)" == *"aarch"* ]];then
    68      ARCH='aarch'
    69    else
    70      echo "unsupported arch, failed"
    71      exit 1
    72    fi
    73    if [[ "$(getconf LONG_BIT)" == "64" ]];then
    74      SYSTEM_BIT='64'
    75    elif [[ "$(getconf LONG_BIT)" == "32" ]];then
    76      SYSTEM_BIT='32'
    77    else
    78      echo "unsupported system bit, failed"
    79      exit 1
    80    fi
    81    PROTOC_ZIP="protoc-${PROTOC_VERSION}-${OS}-${ARCH}_${SYSTEM_BIT}.zip"
    82    curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/${PROTOC_ZIP}
    83    unzip -o $PROTOC_ZIP -d "${GOPATH}/" bin/protoc
    84    unzip -o $PROTOC_ZIP -d "${GOPATH}/" 'include/*'
    85    ${GOPATH}/bin/protoc --version
    86  fi
    87  
    88  res1=$(program_exists protoc-gen-gogofast)
    89  res2=$(program_exists protoc-gen-gogofaster)
    90  echo "res1: ${res1}, res2: ${res2}"
    91  if [ "${res1}" == "ok" -a "${res2}" == "ok" ];then
    92    echo "protoc-gen-gogofast and protoc-gen-gogofaster exist"
    93  else
    94    echo "install protoc-gen-gogofast"
    95    if [ -f protobuf/ ];then rm -rf protobuf/;fi
    96    git clone https://github.com/gogo/protobuf.git
    97    cd protobuf
    98    git checkout v1.3.2
    99    cd protoc-gen-gogofast
   100    go build -o $GOPATH/bin/protoc-gen-gogofast
   101    cd ..
   102    cd protoc-gen-gogofaster
   103    go build -o $GOPATH/bin/protoc-gen-gogofaster
   104    cd ../..
   105  fi
   106  
   107  
   108  for file in `ls $PROTOC_DIR/*.proto`
   109  do
   110    outArgName="gogofast_out"
   111    # For query.proto and statsinfo.proto, extra fields are redundant.
   112    if echo $file | egrep "query.proto|statsinfo.proto" >/dev/null; then
   113      outArgName="gogofaster_out"
   114    fi
   115  	dir=$(basename $file .proto)
   116  	mkdir -p $PB_DIR/$dir
   117  	${GOPATH}/bin/protoc -I=.:$PROTOC_DIR:$VENDOR_DIR --$outArgName=paths=source_relative:./pkg/pb/$dir  $file
   118      goimports -w $PB_DIR/$dir/*pb.go
   119  done
   120  
   121  
   122  # Generate pb file for each package's own
   123  for fp in $(find pkg -name protogen.sh)
   124  do
   125      cd $(dirname ${fp})
   126      bash $(basename ${fp})
   127      cd - > /dev/null
   128  done
   129  
   130  if [ -f protobuf/ ];then rm -rf protobuf/;fi