[Settings] Convert more settings to be generated
[WebKit-https.git] / Source / WebCore / Scripts / GenerateSettings / GenerateSettingsImplementationFile.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2017 Apple Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 # THE POSSIBILITY OF SUCH DAMAGE.
25
26 import os.path
27
28 from Settings import license, makeConditionalString, makePreferredConditional
29
30
31 def generateSettingsImplementationFile(outputDirectory, settings):
32     settingsByConditional = {}
33     unconditionalSettings = {}
34
35     for settingName in sorted(settings.iterkeys()):
36         setting = settings[settingName]
37         if setting.conditional:
38             if setting.conditional not in settingsByConditional:
39                 settingsByConditional[setting.conditional] = {}
40             settingsByConditional[setting.conditional][setting.name] = True
41         else:
42             unconditionalSettings[setting.name] = True
43
44     sortedUnconditionalSettingsNames = sorted(unconditionalSettings.iterkeys())
45     sortedConditionals = sorted(settingsByConditional.iterkeys())
46
47     outputPath = os.path.join(outputDirectory, "Settings.cpp")
48     outputFile = open(outputPath, 'w')
49     outputFile.write(license())
50
51     outputFile.write("#include \"config.h\"\n")
52     outputFile.write("#include \"Settings.h\"\n\n")
53
54     outputFile.write("#include \"Page.h\"\n")
55     outputFile.write("#include \"SettingsDefaultValues.h\"\n\n")
56
57     outputFile.write("namespace WebCore {\n\n")
58
59     outputFile.write("Ref<Settings> Settings::create(Page* page)\n")
60     outputFile.write("{\n")
61     outputFile.write("    return adoptRef(*new Settings(page));\n")
62     outputFile.write("}\n\n")
63
64     outputFile.write("Settings::Settings(Page* page)\n")
65     outputFile.write("    : SettingsBase(page)\n")
66
67     printInitializerList(outputFile, sortedUnconditionalSettingsNames, sortedConditionals, settingsByConditional, settings)
68
69     outputFile.write("{\n")
70     outputFile.write("}\n\n")
71
72     outputFile.write("Settings::~Settings()\n")
73     outputFile.write("{\n")
74     outputFile.write("}\n\n")
75
76     printSetterBodies(outputFile, sortedUnconditionalSettingsNames, sortedConditionals, settingsByConditional, settings)
77
78     outputFile.write("}\n")
79
80     outputFile.close()
81
82
83 def hasNonBoolInitializer(setting):
84     if setting.type == "bool":
85         return False
86     if not setting.initial:
87         return False
88     return True
89
90
91 def printNonBoolInitializer(outputFile, setting):
92     if not hasNonBoolInitializer(setting):
93         return
94     outputFile.write("    , m_" + setting.name + "(" + setting.initial + ")\n")
95
96
97 def hasBoolInitializer(setting):
98     if setting.type != "bool":
99         return False
100     if not setting.initial:
101         return False
102     return True
103
104
105 def printBoolInitializer(outputFile, setting):
106     if not hasBoolInitializer(setting):
107         return
108     outputFile.write("    , m_" + setting.name + "(" + setting.initial + ")\n")
109
110
111 def printInitializerList(outputFile, sortedUnconditionalSettingsNames, sortedConditionals, settingsByConditional, settings):
112     for unconditionalSettingName in sortedUnconditionalSettingsNames:
113         printNonBoolInitializer(outputFile, settings[unconditionalSettingName])
114
115     for conditional in sortedConditionals:
116         hasInitializer = False
117         for settingName in sorted(settingsByConditional[conditional].iterkeys()):
118             if hasNonBoolInitializer(settings[settingName]):
119                 hasInitializer = True
120                 break
121
122         if hasInitializer:
123             outputFile.write("#if " + makeConditionalString(conditional) + "\n")
124
125             for settingName in sorted(settingsByConditional[conditional].iterkeys()):
126                 printNonBoolInitializer(outputFile, settings[settingName])
127
128             outputFile.write("#endif\n")
129
130     for unconditionalSettingName in sortedUnconditionalSettingsNames:
131         printBoolInitializer(outputFile, settings[unconditionalSettingName])
132
133     for conditional in sortedConditionals:
134         hasInitializer = False
135         for settingName in sorted(settingsByConditional[conditional].iterkeys()):
136             if hasBoolInitializer(settings[settingName]):
137                 hasInitializer = True
138                 break
139
140         if hasInitializer:
141             outputFile.write("#if " + makeConditionalString(conditional) + "\n")
142
143             for settingName in sorted(settingsByConditional[conditional].iterkeys()):
144                 printBoolInitializer(outputFile, settings[settingName])
145
146             outputFile.write("#endif\n")
147
148
149 def printSetterBody(outputFile, setting):
150     if not setting.hasComplexSetter():
151         return
152
153     setterFunctionName = setting.setterFunctionName()
154     changeFunctionName = setting.setterChangeFunctionName()
155
156     if setting.typeIsValueType():
157         outputFile.write("void Settings::" + setterFunctionName + "(" + setting.type + " " + setting.name + ")\n")
158     else:
159         outputFile.write("void Settings::" + setterFunctionName + "(const " + setting.type + "& " + setting.name + ")\n")
160
161     outputFile.write("{\n")
162     outputFile.write("    if (m_" + setting.name + " == " + setting.name + ")\n")
163     outputFile.write("        return;\n")
164     outputFile.write("    m_" + setting.name + " = " + setting.name + ";\n")
165     outputFile.write("    " + changeFunctionName + "();\n")
166     outputFile.write("}\n\n")
167
168
169 def printSetterBodies(outputFile, sortedUnconditionalSettingsNames, sortedConditionals, settingsByConditional, settings):
170     for unconditionalSettingName in sortedUnconditionalSettingsNames:
171         setting = settings[unconditionalSettingName]
172         printSetterBody(outputFile, setting)
173
174     for conditional in sortedConditionals:
175         hasSetterBody = False
176         for settingName in sorted(settingsByConditional[conditional].iterkeys()):
177             setting = settings[settingName]
178             if setting.hasComplexSetter():
179                 hasSetterBody = True
180                 break
181
182         if hasSetterBody:
183             outputFile.write("#if " + makeConditionalString(conditional) + "\n\n")
184
185             for settingName in sorted(settingsByConditional[conditional].iterkeys()):
186                 printSetterBody(outputFile, settings[settingName])
187
188             outputFile.write("#endif\n\n")