generate_copts.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 """Generate Abseil compile compile option configs.
00003 
00004 Usage: <path_to_absl>/copts/generate_copts.py
00005 
00006 The configs are generated from copts.py.
00007 """
00008 
00009 from os import path
00010 import sys
00011 from copts import COPT_VARS
00012 
00013 
00014 # Helper functions
00015 def file_header_lines():
00016   return [
00017       "GENERATED! DO NOT MANUALLY EDIT THIS FILE.", "",
00018       "(1) Edit absl/copts/copts.py.",
00019       "(2) Run `python <path_to_absl>/copts/generate_copts.py`."
00020   ]
00021 
00022 
00023 def flatten(*lists):
00024   return [item for sublist in lists for item in sublist]
00025 
00026 
00027 def relative_filename(filename):
00028   return path.join(path.dirname(__file__), filename)
00029 
00030 
00031 # Style classes.  These contain all the syntactic styling needed to generate a
00032 # copt file for different build tools.
00033 class CMakeStyle(object):
00034   """Style object for CMake copts file."""
00035 
00036   def separator(self):
00037     return ""
00038 
00039   def list_introducer(self, name):
00040     return "list(APPEND " + name
00041 
00042   def list_closer(self):
00043     return ")\n"
00044 
00045   def docstring(self):
00046     return "\n".join((("# " + line).strip() for line in file_header_lines()))
00047 
00048   def filename(self):
00049     return "GENERATED_AbseilCopts.cmake"
00050 
00051 
00052 class StarlarkStyle(object):
00053   """Style object for Starlark copts file."""
00054 
00055   def separator(self):
00056     return ","
00057 
00058   def list_introducer(self, name):
00059     return name + " = ["
00060 
00061   def list_closer(self):
00062     return "]\n"
00063 
00064   def docstring(self):
00065     docstring_quotes = "\"\"\""
00066     return docstring_quotes + "\n".join(
00067         flatten(file_header_lines(), [docstring_quotes]))
00068 
00069   def filename(self):
00070     return "GENERATED_copts.bzl"
00071 
00072 
00073 def copt_list(name, arg_list, style):
00074   """Copt file generation."""
00075 
00076   make_line = lambda s: "    \"" + s + "\"" + style.separator()
00077   external_str_list = [make_line(s) for s in arg_list]
00078 
00079   return "\n".join(
00080       flatten(
00081           [style.list_introducer(name)],
00082           external_str_list,
00083           [style.list_closer()]))
00084 
00085 
00086 def generate_copt_file(style):
00087   """Creates a generated copt file using the given style object.
00088 
00089   Args:
00090     style: either StarlarkStyle() or CMakeStyle()
00091   """
00092   with open(relative_filename(style.filename()), "w") as f:
00093     f.write(style.docstring())
00094     f.write("\n")
00095     for var_name, arg_list in sorted(COPT_VARS.items()):
00096       f.write("\n")
00097       f.write(copt_list(var_name, arg_list, style))
00098 
00099 
00100 def main(argv):
00101   if len(argv) > 1:
00102     raise RuntimeError("generate_copts needs no command line args")
00103 
00104   generate_copt_file(StarlarkStyle())
00105   generate_copt_file(CMakeStyle())
00106 
00107 
00108 if __name__ == "__main__":
00109   main(sys.argv)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14