github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/llvm/expand_cmake_vars.py (about)

     1  # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ==============================================================================
    15  
    16  """Expands CMake variables in a text file."""
    17  
    18  from __future__ import absolute_import
    19  from __future__ import division
    20  from __future__ import print_function
    21  
    22  import re
    23  import sys
    24  
    25  _CMAKE_DEFINE_REGEX = re.compile(r"\s*#cmakedefine\s+([A-Za-z_0-9]*)(\s.*)?$")
    26  _CMAKE_DEFINE01_REGEX = re.compile(r"\s*#cmakedefine01\s+([A-Za-z_0-9]*)")
    27  _CMAKE_VAR_REGEX = re.compile(r"\${([A-Za-z_0-9]*)}")
    28  
    29  
    30  def _parse_args(argv):
    31    """Parses arguments with the form KEY=VALUE into a dictionary."""
    32    result = {}
    33    for arg in argv:
    34      k, v = arg.split("=")
    35      result[k] = v
    36    return result
    37  
    38  
    39  def _expand_variables(input_str, cmake_vars):
    40    """Expands ${VARIABLE}s in 'input_str', using dictionary 'cmake_vars'.
    41  
    42    Args:
    43      input_str: the string containing ${VARIABLE} expressions to expand.
    44      cmake_vars: a dictionary mapping variable names to their values.
    45  
    46    Returns:
    47      The expanded string.
    48    """
    49    def replace(match):
    50      if match.group(1) in cmake_vars:
    51        return cmake_vars[match.group(1)]
    52      return ""
    53    return _CMAKE_VAR_REGEX.sub(replace, input_str)
    54  
    55  
    56  def _expand_cmakedefines(line, cmake_vars):
    57    """Expands #cmakedefine declarations, using a dictionary 'cmake_vars'."""
    58  
    59    # Handles #cmakedefine lines
    60    match = _CMAKE_DEFINE_REGEX.match(line)
    61    if match:
    62      name = match.group(1)
    63      suffix = match.group(2) or ""
    64      if name in cmake_vars:
    65        return "#define {}{}\n".format(name,
    66                                       _expand_variables(suffix, cmake_vars))
    67      else:
    68        return "/* #undef {} */\n".format(name)
    69  
    70    # Handles #cmakedefine01 lines
    71    match = _CMAKE_DEFINE01_REGEX.match(line)
    72    if match:
    73      name = match.group(1)
    74      value = cmake_vars.get(name, "0")
    75      return "#define {} {}\n".format(name, value)
    76  
    77    # Otherwise return the line unchanged.
    78    return _expand_variables(line, cmake_vars)
    79  
    80  
    81  def main():
    82    cmake_vars = _parse_args(sys.argv[1:])
    83    for line in sys.stdin:
    84      sys.stdout.write(_expand_cmakedefines(line, cmake_vars))
    85  
    86  
    87  if __name__ == "__main__":
    88    main()