update-codecgen.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. # Copyright 2015 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. kube::golang::setup_env
  21. # The sort at the end makes sure we feed the topological sort a deterministic
  22. # list (since there aren't many dependencies).
  23. generated_files=($(
  24. find . -not \( \
  25. \( \
  26. -wholename './output' \
  27. -o -wholename './_output' \
  28. -o -wholename './staging' \
  29. -o -wholename './release' \
  30. -o -wholename './target' \
  31. -o -wholename '*/third_party/*' \
  32. -o -wholename '*/vendor/*' \
  33. -o -wholename '*/codecgen-*-1234.generated.go' \
  34. \) -prune \
  35. \) -name '*.generated.go' | sort -r))
  36. # We only work for deps within this prefix.
  37. my_prefix="k8s.io/kubernetes"
  38. # Register function to be called on EXIT to remove codecgen
  39. # binary and also to touch the files that should be regenerated
  40. # since they are first removed.
  41. # This is necessary to make the script work after previous failure.
  42. function cleanup {
  43. rm -f "${CODECGEN:-}"
  44. pushd "${KUBE_ROOT}" > /dev/null
  45. for (( i=0; i < number; i++ )); do
  46. touch "${generated_files[${i}]}" || true
  47. done
  48. popd > /dev/null
  49. }
  50. trap cleanup EXIT
  51. # Precompute dependencies for all directories.
  52. # Then sort all files in the dependency order.
  53. number=${#generated_files[@]}
  54. result=""
  55. for (( i=0; i<number; i++ )); do
  56. visited[${i}]=false
  57. file="${generated_files[${i}]/\.generated\.go/.go}"
  58. deps[${i}]=$(go list -f '{{range .Deps}}{{.}}{{"\n"}}{{end}}' ${file} | grep "^${my_prefix}")
  59. done
  60. ###echo "DBG: found $number generated files"
  61. ###for f in $(echo "${generated_files[@]}" | sort); do
  62. ### echo "DBG: $f"
  63. ###done
  64. # NOTE: depends function assumes that the whole repository is under
  65. # $my_prefix - it will NOT work if that is not true.
  66. function depends {
  67. rhs="$(dirname ${generated_files[$2]/#./${my_prefix}})"
  68. ###echo "DBG: does ${file} depend on ${rhs}?"
  69. for dep in ${deps[$1]}; do
  70. ###echo "DBG: checking against $dep"
  71. if [[ "${dep}" == "${rhs}" ]]; then
  72. ###echo "DBG: = yes"
  73. return 0
  74. fi
  75. done
  76. ###echo "DBG: = no"
  77. return 1
  78. }
  79. function tsort {
  80. visited[$1]=true
  81. local j=0
  82. for (( j=0; j<number; j++ )); do
  83. if ! ${visited[${j}]}; then
  84. if depends "$1" ${j}; then
  85. tsort $j
  86. fi
  87. fi
  88. done
  89. result="${result} $1"
  90. }
  91. echo "Building dependencies"
  92. for (( i=0; i<number; i++ )); do
  93. ###echo "DBG: considering ${generated_files[${i}]}"
  94. if ! ${visited[${i}]}; then
  95. ###echo "DBG: tsorting ${generated_files[${i}]}"
  96. tsort ${i}
  97. fi
  98. done
  99. index=(${result})
  100. haveindex=${index:-}
  101. if [[ -z ${haveindex} ]]; then
  102. echo No files found for $0
  103. echo A previous run of $0 may have deleted all the files and then crashed.
  104. echo Use 'touch' to create files named 'types.generated.go' listed as deleted in 'git status'
  105. exit 1
  106. fi
  107. echo "Building codecgen"
  108. make generated_files
  109. CODECGEN="${PWD}/codecgen_binary"
  110. go build -o "${CODECGEN}" ./vendor/github.com/ugorji/go/codec/codecgen
  111. # Running codecgen fails if some of the files doesn't compile.
  112. # Thus (since all the files are completely auto-generated and
  113. # not required for the code to be compilable, we first remove
  114. # them and the regenerate them.
  115. for (( i=0; i < number; i++ )); do
  116. rm -f "${generated_files[${i}]}"
  117. done
  118. # Generate files in the dependency order.
  119. for current in "${index[@]}"; do
  120. generated_file=${generated_files[${current}]}
  121. initial_dir=${PWD}
  122. file=${generated_file/\.generated\.go/.go}
  123. echo "processing ${file}"
  124. # codecgen work only if invoked from directory where the file
  125. # is located.
  126. pushd "$(dirname ${file})" > /dev/null
  127. base_file=$(basename "${file}")
  128. base_generated_file=$(basename "${generated_file}")
  129. # We use '-d 1234' flag to have a deterministic output every time.
  130. # The constant was just randomly chosen.
  131. ###echo "DBG: running ${CODECGEN} -d 1234 -o ${base_generated_file} ${base_file}"
  132. ${CODECGEN} -d 1234 -o "${base_generated_file}" "${base_file}"
  133. # Add boilerplate at the beginning of the generated file.
  134. sed 's/YEAR/2016/' "${initial_dir}/hack/boilerplate/boilerplate.go.txt" > "${base_generated_file}.tmp"
  135. cat "${base_generated_file}" >> "${base_generated_file}.tmp"
  136. mv "${base_generated_file}.tmp" "${base_generated_file}"
  137. popd > /dev/null
  138. done