1*9356374aSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*9356374aSAndroid Build Coastguard Worker"""Generate Abseil compile compile option configs. 3*9356374aSAndroid Build Coastguard Worker 4*9356374aSAndroid Build Coastguard WorkerUsage: <path_to_absl>/copts/generate_copts.py 5*9356374aSAndroid Build Coastguard Worker 6*9356374aSAndroid Build Coastguard WorkerThe configs are generated from copts.py. 7*9356374aSAndroid Build Coastguard Worker""" 8*9356374aSAndroid Build Coastguard Worker 9*9356374aSAndroid Build Coastguard Workerfrom os import path 10*9356374aSAndroid Build Coastguard Workerimport sys 11*9356374aSAndroid Build Coastguard Workerfrom copts import COPT_VARS 12*9356374aSAndroid Build Coastguard Worker 13*9356374aSAndroid Build Coastguard Worker 14*9356374aSAndroid Build Coastguard Worker# Helper functions 15*9356374aSAndroid Build Coastguard Workerdef file_header_lines(): 16*9356374aSAndroid Build Coastguard Worker return [ 17*9356374aSAndroid Build Coastguard Worker "GENERATED! DO NOT MANUALLY EDIT THIS FILE.", "", 18*9356374aSAndroid Build Coastguard Worker "(1) Edit absl/copts/copts.py.", 19*9356374aSAndroid Build Coastguard Worker "(2) Run `python <path_to_absl>/copts/generate_copts.py`." 20*9356374aSAndroid Build Coastguard Worker ] 21*9356374aSAndroid Build Coastguard Worker 22*9356374aSAndroid Build Coastguard Worker 23*9356374aSAndroid Build Coastguard Workerdef flatten(*lists): 24*9356374aSAndroid Build Coastguard Worker return [item for sublist in lists for item in sublist] 25*9356374aSAndroid Build Coastguard Worker 26*9356374aSAndroid Build Coastguard Worker 27*9356374aSAndroid Build Coastguard Workerdef relative_filename(filename): 28*9356374aSAndroid Build Coastguard Worker return path.join(path.dirname(__file__), filename) 29*9356374aSAndroid Build Coastguard Worker 30*9356374aSAndroid Build Coastguard Worker 31*9356374aSAndroid Build Coastguard Worker# Style classes. These contain all the syntactic styling needed to generate a 32*9356374aSAndroid Build Coastguard Worker# copt file for different build tools. 33*9356374aSAndroid Build Coastguard Workerclass CMakeStyle(object): 34*9356374aSAndroid Build Coastguard Worker """Style object for CMake copts file.""" 35*9356374aSAndroid Build Coastguard Worker 36*9356374aSAndroid Build Coastguard Worker def separator(self): 37*9356374aSAndroid Build Coastguard Worker return "" 38*9356374aSAndroid Build Coastguard Worker 39*9356374aSAndroid Build Coastguard Worker def list_introducer(self, name): 40*9356374aSAndroid Build Coastguard Worker return "list(APPEND " + name 41*9356374aSAndroid Build Coastguard Worker 42*9356374aSAndroid Build Coastguard Worker def list_closer(self): 43*9356374aSAndroid Build Coastguard Worker return ")\n" 44*9356374aSAndroid Build Coastguard Worker 45*9356374aSAndroid Build Coastguard Worker def docstring(self): 46*9356374aSAndroid Build Coastguard Worker return "\n".join((("# " + line).strip() for line in file_header_lines())) 47*9356374aSAndroid Build Coastguard Worker 48*9356374aSAndroid Build Coastguard Worker def filename(self): 49*9356374aSAndroid Build Coastguard Worker return "GENERATED_AbseilCopts.cmake" 50*9356374aSAndroid Build Coastguard Worker 51*9356374aSAndroid Build Coastguard Worker 52*9356374aSAndroid Build Coastguard Workerclass StarlarkStyle(object): 53*9356374aSAndroid Build Coastguard Worker """Style object for Starlark copts file.""" 54*9356374aSAndroid Build Coastguard Worker 55*9356374aSAndroid Build Coastguard Worker def separator(self): 56*9356374aSAndroid Build Coastguard Worker return "," 57*9356374aSAndroid Build Coastguard Worker 58*9356374aSAndroid Build Coastguard Worker def list_introducer(self, name): 59*9356374aSAndroid Build Coastguard Worker return name + " = [" 60*9356374aSAndroid Build Coastguard Worker 61*9356374aSAndroid Build Coastguard Worker def list_closer(self): 62*9356374aSAndroid Build Coastguard Worker return "]\n" 63*9356374aSAndroid Build Coastguard Worker 64*9356374aSAndroid Build Coastguard Worker def docstring(self): 65*9356374aSAndroid Build Coastguard Worker docstring_quotes = "\"\"\"" 66*9356374aSAndroid Build Coastguard Worker return docstring_quotes + "\n".join( 67*9356374aSAndroid Build Coastguard Worker flatten(file_header_lines(), [docstring_quotes])) 68*9356374aSAndroid Build Coastguard Worker 69*9356374aSAndroid Build Coastguard Worker def filename(self): 70*9356374aSAndroid Build Coastguard Worker return "GENERATED_copts.bzl" 71*9356374aSAndroid Build Coastguard Worker 72*9356374aSAndroid Build Coastguard Worker 73*9356374aSAndroid Build Coastguard Workerdef copt_list(name, arg_list, style): 74*9356374aSAndroid Build Coastguard Worker """Copt file generation.""" 75*9356374aSAndroid Build Coastguard Worker 76*9356374aSAndroid Build Coastguard Worker make_line = lambda s: " \"" + s + "\"" + style.separator() 77*9356374aSAndroid Build Coastguard Worker external_str_list = [make_line(s) for s in arg_list] 78*9356374aSAndroid Build Coastguard Worker 79*9356374aSAndroid Build Coastguard Worker return "\n".join( 80*9356374aSAndroid Build Coastguard Worker flatten( 81*9356374aSAndroid Build Coastguard Worker [style.list_introducer(name)], 82*9356374aSAndroid Build Coastguard Worker external_str_list, 83*9356374aSAndroid Build Coastguard Worker [style.list_closer()])) 84*9356374aSAndroid Build Coastguard Worker 85*9356374aSAndroid Build Coastguard Worker 86*9356374aSAndroid Build Coastguard Workerdef generate_copt_file(style): 87*9356374aSAndroid Build Coastguard Worker """Creates a generated copt file using the given style object. 88*9356374aSAndroid Build Coastguard Worker 89*9356374aSAndroid Build Coastguard Worker Args: 90*9356374aSAndroid Build Coastguard Worker style: either StarlarkStyle() or CMakeStyle() 91*9356374aSAndroid Build Coastguard Worker """ 92*9356374aSAndroid Build Coastguard Worker with open(relative_filename(style.filename()), "w") as f: 93*9356374aSAndroid Build Coastguard Worker f.write(style.docstring()) 94*9356374aSAndroid Build Coastguard Worker f.write("\n") 95*9356374aSAndroid Build Coastguard Worker for var_name, arg_list in sorted(COPT_VARS.items()): 96*9356374aSAndroid Build Coastguard Worker f.write("\n") 97*9356374aSAndroid Build Coastguard Worker f.write(copt_list(var_name, arg_list, style)) 98*9356374aSAndroid Build Coastguard Worker 99*9356374aSAndroid Build Coastguard Worker 100*9356374aSAndroid Build Coastguard Workerdef main(argv): 101*9356374aSAndroid Build Coastguard Worker if len(argv) > 1: 102*9356374aSAndroid Build Coastguard Worker raise RuntimeError("generate_copts needs no command line args") 103*9356374aSAndroid Build Coastguard Worker 104*9356374aSAndroid Build Coastguard Worker generate_copt_file(StarlarkStyle()) 105*9356374aSAndroid Build Coastguard Worker generate_copt_file(CMakeStyle()) 106*9356374aSAndroid Build Coastguard Worker 107*9356374aSAndroid Build Coastguard Worker 108*9356374aSAndroid Build Coastguard Workerif __name__ == "__main__": 109*9356374aSAndroid Build Coastguard Worker main(sys.argv) 110