create_lts.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2021 The Abseil Authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """A script to do source transformations to create a new LTS release.
  17. Usage: ./create_lts.py YYYYMMDD
  18. """
  19. import sys
  20. def ReplaceStringsInFile(filename, replacement_dict):
  21. """Performs textual replacements in a file.
  22. Rewrites filename with the keys in replacement_dict replaced with
  23. their values. This function assumes the file can fit in memory.
  24. Args:
  25. filename: the filename to perform the replacement on
  26. replacement_dict: a dictionary of key strings to be replaced with their
  27. values
  28. Raises:
  29. Exception: A failure occured
  30. """
  31. f = open(filename, 'r')
  32. content = f.read()
  33. f.close()
  34. for key, value in replacement_dict.items():
  35. original = content
  36. content = content.replace(key, value)
  37. if content == original:
  38. raise Exception('Failed to find {} in {}'.format(key, filename))
  39. f = open(filename, 'w')
  40. f.write(content)
  41. f.close()
  42. def StripContentBetweenTags(filename, strip_begin_tag, strip_end_tag):
  43. """Strip contents from a file.
  44. Rewrites filename with by removing all content between
  45. strip_begin_tag and strip_end_tag, including the tags themselves.
  46. Args:
  47. filename: the filename to perform the replacement on
  48. strip_begin_tag: the start of the content to be removed
  49. strip_end_tag: the end of the content to be removed
  50. Raises:
  51. Exception: A failure occured
  52. """
  53. f = open(filename, 'r')
  54. content = f.read()
  55. f.close()
  56. while True:
  57. begin = content.find(strip_begin_tag)
  58. if begin == -1:
  59. break
  60. end = content.find(strip_end_tag, begin + len(strip_begin_tag))
  61. if end == -1:
  62. raise Exception('{}: imbalanced strip begin ({}) and '
  63. 'end ({}) tags'.format(filename, strip_begin_tag,
  64. strip_end_tag))
  65. content = content.replace(content[begin:end + len(strip_end_tag)], '')
  66. f = open(filename, 'w')
  67. f.write(content)
  68. f.close()
  69. def main(argv):
  70. if len(argv) != 2:
  71. print('Usage: {} YYYYMMDD'.format(sys.argv[0], file=sys.stderr))
  72. sys.exit(1)
  73. datestamp = sys.argv[1]
  74. if len(datestamp) != 8 or not datestamp.isdigit():
  75. raise Exception(
  76. 'datestamp={} is not in the YYYYMMDD format'.format(datestamp))
  77. # Replacement directives go here.
  78. ReplaceStringsInFile(
  79. 'absl/base/config.h', {
  80. '#undef ABSL_LTS_RELEASE_VERSION':
  81. '#define ABSL_LTS_RELEASE_VERSION {}'.format(datestamp),
  82. '#undef ABSL_LTS_RELEASE_PATCH_LEVEL':
  83. '#define ABSL_LTS_RELEASE_PATCH_LEVEL 0'
  84. })
  85. ReplaceStringsInFile(
  86. 'absl/base/options.h', {
  87. '#define ABSL_OPTION_USE_INLINE_NAMESPACE 0':
  88. '#define ABSL_OPTION_USE_INLINE_NAMESPACE 1',
  89. '#define ABSL_OPTION_INLINE_NAMESPACE_NAME head':
  90. '#define ABSL_OPTION_INLINE_NAMESPACE_NAME lts_{}'.format(
  91. datestamp)
  92. })
  93. ReplaceStringsInFile(
  94. 'CMakeLists.txt', {
  95. 'project(absl LANGUAGES CXX)':
  96. 'project(absl LANGUAGES CXX VERSION {})'.format(datestamp)
  97. })
  98. # Set the SOVERSION to YYMM.0.0 - The first 0 means we only have ABI
  99. # compatible changes, and the second 0 means we can increment it to
  100. # mark changes as ABI-compatible, for patch releases. Note that we
  101. # only use the last two digits of the year and the month because the
  102. # MacOS linker requires the first part of the SOVERSION to fit into
  103. # 16 bits.
  104. # https://www.sicpers.info/2013/03/how-to-version-a-mach-o-library/
  105. ReplaceStringsInFile(
  106. 'CMake/AbseilHelpers.cmake',
  107. {'SOVERSION 0': 'SOVERSION "{}.0.0"'.format(datestamp[2:6])})
  108. StripContentBetweenTags('CMakeLists.txt', '# absl:lts-remove-begin',
  109. '# absl:lts-remove-end')
  110. if __name__ == '__main__':
  111. main(sys.argv)