Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. 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 | package main |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "bytes" |
| 20 | "html/template" |
| 21 | "io/ioutil" |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 22 | "reflect" |
| 23 | "sort" |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 26 | "github.com/google/blueprint/bootstrap/bpdoc" |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 29 | type moduleTypeTemplateData struct { |
| 30 | Name string |
| 31 | Synopsis string |
| 32 | Properties []bpdoc.Property |
| 33 | } |
| 34 | |
| 35 | // The properties in this map are displayed first, according to their rank. |
| 36 | // TODO(jungjw): consider providing module type-dependent ranking |
| 37 | var propertyRank = map[string]int{ |
| 38 | "name": 0, |
| 39 | "src": 1, |
| 40 | "srcs": 2, |
| 41 | "defautls": 3, |
| 42 | "host_supported": 4, |
| 43 | "device_supported": 5, |
| 44 | } |
| 45 | |
| 46 | // For each module type, extract its documentation and convert it to the template data. |
| 47 | func moduleTypeDocsToTemplates(ctx *android.Context) ([]moduleTypeTemplateData, error) { |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 48 | moduleTypeFactories := android.ModuleTypeFactories() |
| 49 | bpModuleTypeFactories := make(map[string]reflect.Value) |
| 50 | for moduleType, factory := range moduleTypeFactories { |
| 51 | bpModuleTypeFactories[moduleType] = reflect.ValueOf(factory) |
| 52 | } |
| 53 | |
| 54 | packages, err := bootstrap.ModuleTypeDocs(ctx.Context, bpModuleTypeFactories) |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 55 | if err != nil { |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 56 | return []moduleTypeTemplateData{}, err |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 57 | } |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 58 | var moduleTypeList []*bpdoc.ModuleType |
| 59 | for _, pkg := range packages { |
| 60 | moduleTypeList = append(moduleTypeList, pkg.ModuleTypes...) |
| 61 | } |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 62 | |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 63 | result := make([]moduleTypeTemplateData, 0) |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 64 | |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 65 | // Combine properties from all PropertyStruct's and reorder them -- first the ones |
| 66 | // with rank, then the rest of the properties in alphabetic order. |
| 67 | for _, m := range moduleTypeList { |
| 68 | item := moduleTypeTemplateData{ |
| 69 | Name: m.Name, |
| 70 | Synopsis: m.Text, |
| 71 | Properties: make([]bpdoc.Property, 0), |
| 72 | } |
| 73 | props := make([]bpdoc.Property, 0) |
| 74 | for _, propStruct := range m.PropertyStructs { |
| 75 | props = append(props, propStruct.Properties...) |
| 76 | } |
| 77 | sort.Slice(props, func(i, j int) bool { |
| 78 | if rankI, ok := propertyRank[props[i].Name]; ok { |
| 79 | if rankJ, ok := propertyRank[props[j].Name]; ok { |
| 80 | return rankI < rankJ |
| 81 | } else { |
| 82 | return true |
| 83 | } |
| 84 | } |
| 85 | if _, ok := propertyRank[props[j].Name]; ok { |
| 86 | return false |
| 87 | } |
| 88 | return props[i].Name < props[j].Name |
| 89 | }) |
| 90 | // Eliminate top-level duplicates. TODO(jungjw): improve bpdoc to handle this. |
| 91 | previousPropertyName := "" |
| 92 | for _, prop := range props { |
| 93 | if prop.Name == previousPropertyName { |
| 94 | oldProp := &item.Properties[len(item.Properties)-1].Properties |
| 95 | bpdoc.CollapseDuplicateProperties(oldProp, &prop.Properties) |
| 96 | } else { |
| 97 | item.Properties = append(item.Properties, prop) |
| 98 | } |
| 99 | previousPropertyName = prop.Name |
| 100 | } |
| 101 | result = append(result, item) |
| 102 | } |
| 103 | sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name }) |
| 104 | return result, err |
| 105 | } |
| 106 | |
| 107 | func writeDocs(ctx *android.Context, filename string) error { |
| 108 | buf := &bytes.Buffer{} |
| 109 | |
| 110 | // We need a module name getter/setter function because I couldn't |
| 111 | // find a way to keep it in a variable defined within the template. |
| 112 | currentModuleName := "" |
| 113 | data, err := moduleTypeDocsToTemplates(ctx) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 117 | tmpl, err := template.New("file").Funcs(map[string]interface{}{ |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 118 | "setModule": func(moduleName string) string { |
| 119 | currentModuleName = moduleName |
| 120 | return "" |
| 121 | }, |
| 122 | "getModule": func() string { |
| 123 | return currentModuleName |
| 124 | }, |
| 125 | }).Parse(fileTemplate) |
| 126 | if err == nil { |
| 127 | err = tmpl.Execute(buf, data) |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 128 | } |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 129 | if err == nil { |
| 130 | err = ioutil.WriteFile(filename, buf.Bytes(), 0666) |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 131 | } |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 132 | return err |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const ( |
| 136 | fileTemplate = ` |
| 137 | <html> |
| 138 | <head> |
| 139 | <title>Build Docs</title> |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 140 | <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"> |
| 141 | <style> |
| 142 | .accordion,.simple{margin-left:1.5em;text-indent:-1.5em;margin-top:.25em} |
| 143 | .collapsible{border-width:0 0 0 1;margin-left:.25em;padding-left:.25em;border-style:solid; |
| 144 | border-color:grey;display:none;} |
| 145 | span.fixed{display: block; float: left; clear: left; width: 1em;} |
| 146 | ul { |
| 147 | list-style-type: none; |
| 148 | margin: 0; |
| 149 | padding: 0; |
| 150 | width: 30ch; |
| 151 | background-color: #f1f1f1; |
| 152 | position: fixed; |
| 153 | height: 100%; |
| 154 | overflow: auto; |
| 155 | } |
| 156 | li a { |
| 157 | display: block; |
| 158 | color: #000; |
| 159 | padding: 8px 16px; |
| 160 | text-decoration: none; |
| 161 | } |
| 162 | |
| 163 | li a.active { |
| 164 | background-color: #4CAF50; |
| 165 | color: white; |
| 166 | } |
| 167 | |
| 168 | li a:hover:not(.active) { |
| 169 | background-color: #555; |
| 170 | color: white; |
| 171 | } |
| 172 | </style> |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 173 | </head> |
| 174 | <body> |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 175 | {{- /* Fixed sidebar with module types */ -}} |
| 176 | <ul> |
| 177 | <li><h3>Module Types:</h3></li> |
| 178 | {{range $moduleType := .}}<li><a href="#{{$moduleType.Name}}">{{$moduleType.Name}}</a></li> |
| 179 | {{end -}} |
| 180 | </ul> |
| 181 | {{/* Main panel with H1 section per module type */}} |
| 182 | <div style="margin-left:30ch;padding:1px 16px;"> |
| 183 | <H1>Soong Modules Reference</H1> |
| 184 | The latest versions of Android use the Soong build system, which greatly simplifies build |
| 185 | configuration over the previous Make-based system. This site contains the generated reference |
| 186 | files for the Soong build system. |
| 187 | <p> |
| 188 | See the <a href=https://source.android.com/setup/build/build-system>Android Build System</a> |
| 189 | description for an overview of Soong and examples for its use. |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 190 | |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 191 | {{range $imodule, $moduleType := .}} |
| 192 | {{setModule $moduleType.Name}} |
| 193 | <p> |
| 194 | <h2 id="{{$moduleType.Name}}">{{$moduleType.Name}}</h2> |
| 195 | {{if $moduleType.Synopsis }}{{$moduleType.Synopsis}}{{else}}<i>Missing synopsis</i>{{end}} |
| 196 | {{- /* Comma-separated list of module attributes' links module attributes */ -}} |
| 197 | <div class="breadcrumb"> |
| 198 | {{range $i,$prop := $moduleType.Properties }} |
| 199 | {{ if gt $i 0 }}, {{end -}} |
| 200 | <a href=#{{getModule}}.{{$prop.Name}}>{{$prop.Name}}</a> |
| 201 | {{- end -}} |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 202 | </div> |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame^] | 203 | {{- /* Property description */ -}} |
| 204 | {{- template "properties" $moduleType.Properties -}} |
| 205 | {{- end -}} |
| 206 | |
| 207 | {{define "properties" -}} |
| 208 | {{range .}} |
| 209 | {{if .Properties -}} |
| 210 | <div class="accordion" id="{{getModule}}.{{.Name}}"> |
| 211 | <span class="fixed">⊕</span><b>{{.Name}}</b> |
| 212 | {{- range .OtherNames -}}, {{.}}{{- end -}} |
| 213 | </div> |
| 214 | <div class="collapsible"> |
| 215 | {{- .Text}} {{range .OtherTexts}}{{.}}{{end}} |
| 216 | {{template "properties" .Properties -}} |
| 217 | </div> |
| 218 | {{- else -}} |
| 219 | <div class="simple" id="{{getModule}}.{{.Name}}"> |
| 220 | <span class="fixed"> </span><b>{{.Name}} {{range .OtherNames}}, {{.}}{{end -}}</b> |
| 221 | {{- if .Text -}}{{.Text}}{{- end -}} |
| 222 | {{- with .OtherTexts -}}{{.}}{{- end -}}<i>{{.Type}}</i> |
| 223 | {{- if .Default -}}<i>Default: {{.Default}}</i>{{- end -}} |
| 224 | </div> |
| 225 | {{- end}} |
| 226 | {{- end -}} |
| 227 | {{- end -}} |
| 228 | |
| 229 | </div> |
| 230 | <script> |
| 231 | accordions = document.getElementsByClassName('accordion'); |
| 232 | for (i=0; i < accordions.length; ++i) { |
| 233 | accordions[i].addEventListener("click", function() { |
| 234 | var panel = this.nextElementSibling; |
| 235 | var child = this.firstElementChild; |
| 236 | if (panel.style.display === "block") { |
| 237 | panel.style.display = "none"; |
| 238 | child.textContent = '\u2295'; |
| 239 | } else { |
| 240 | panel.style.display = "block"; |
| 241 | child.textContent = '\u2296'; |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | </script> |
| 246 | </body> |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 247 | ` |
| 248 | ) |