blob: 253979e412695a8b074abd2834a45fbe887d65e8 [file] [log] [blame]
Colin Crosse87040b2017-12-11 15:52:26 -08001// 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
15package main
16
17import (
18 "android/soong/android"
19 "bytes"
20 "html/template"
21 "io/ioutil"
Jaewoong Jung6c296882019-02-20 07:12:30 -080022 "path/filepath"
Colin Cross7089c272019-01-25 22:43:35 -080023 "reflect"
24 "sort"
Colin Crosse87040b2017-12-11 15:52:26 -080025
26 "github.com/google/blueprint/bootstrap"
Colin Cross7089c272019-01-25 22:43:35 -080027 "github.com/google/blueprint/bootstrap/bpdoc"
Colin Crosse87040b2017-12-11 15:52:26 -080028)
29
Jaewoong Jung6c296882019-02-20 07:12:30 -080030type perPackageTemplateData struct {
31 Name string
32 Modules []moduleTypeTemplateData
33}
34
Sasha Smundakff483392019-02-07 12:10:56 -080035type moduleTypeTemplateData struct {
36 Name string
Jaewoong Jung238be382019-03-11 14:35:41 -070037 Synopsis template.HTML
Sasha Smundakff483392019-02-07 12:10:56 -080038 Properties []bpdoc.Property
39}
40
41// The properties in this map are displayed first, according to their rank.
42// TODO(jungjw): consider providing module type-dependent ranking
43var propertyRank = map[string]int{
44 "name": 0,
45 "src": 1,
46 "srcs": 2,
Liz Kammere7211dd2020-11-02 22:01:10 +000047 "exclude_srcs": 3,
48 "defaults": 4,
49 "host_supported": 5,
50 "device_supported": 6,
Sasha Smundakff483392019-02-07 12:10:56 -080051}
52
53// For each module type, extract its documentation and convert it to the template data.
Jaewoong Jung6c296882019-02-20 07:12:30 -080054func moduleTypeDocsToTemplates(moduleTypeList []*bpdoc.ModuleType) []moduleTypeTemplateData {
Sasha Smundakff483392019-02-07 12:10:56 -080055 result := make([]moduleTypeTemplateData, 0)
Colin Crosse87040b2017-12-11 15:52:26 -080056
Sasha Smundakff483392019-02-07 12:10:56 -080057 // Combine properties from all PropertyStruct's and reorder them -- first the ones
58 // with rank, then the rest of the properties in alphabetic order.
59 for _, m := range moduleTypeList {
60 item := moduleTypeTemplateData{
61 Name: m.Name,
62 Synopsis: m.Text,
63 Properties: make([]bpdoc.Property, 0),
64 }
65 props := make([]bpdoc.Property, 0)
66 for _, propStruct := range m.PropertyStructs {
67 props = append(props, propStruct.Properties...)
68 }
69 sort.Slice(props, func(i, j int) bool {
70 if rankI, ok := propertyRank[props[i].Name]; ok {
71 if rankJ, ok := propertyRank[props[j].Name]; ok {
72 return rankI < rankJ
73 } else {
74 return true
75 }
76 }
77 if _, ok := propertyRank[props[j].Name]; ok {
78 return false
79 }
80 return props[i].Name < props[j].Name
81 })
82 // Eliminate top-level duplicates. TODO(jungjw): improve bpdoc to handle this.
83 previousPropertyName := ""
84 for _, prop := range props {
85 if prop.Name == previousPropertyName {
86 oldProp := &item.Properties[len(item.Properties)-1].Properties
87 bpdoc.CollapseDuplicateProperties(oldProp, &prop.Properties)
88 } else {
89 item.Properties = append(item.Properties, prop)
90 }
91 previousPropertyName = prop.Name
92 }
93 result = append(result, item)
94 }
95 sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name })
Jaewoong Jung6c296882019-02-20 07:12:30 -080096 return result
Sasha Smundakff483392019-02-07 12:10:56 -080097}
98
Jingwen Chend8004ef2020-08-27 09:40:43 +000099func getPackages(ctx *android.Context) ([]*bpdoc.Package, error) {
Jaewoong Jung6c296882019-02-20 07:12:30 -0800100 moduleTypeFactories := android.ModuleTypeFactories()
101 bpModuleTypeFactories := make(map[string]reflect.Value)
102 for moduleType, factory := range moduleTypeFactories {
103 bpModuleTypeFactories[moduleType] = reflect.ValueOf(factory)
104 }
Jingwen Chend8004ef2020-08-27 09:40:43 +0000105 return bootstrap.ModuleTypeDocs(ctx.Context, bpModuleTypeFactories)
106}
Sasha Smundakff483392019-02-07 12:10:56 -0800107
Jingwen Chend8004ef2020-08-27 09:40:43 +0000108func writeDocs(ctx *android.Context, filename string) error {
109 packages, err := getPackages(ctx)
Sasha Smundakff483392019-02-07 12:10:56 -0800110 if err != nil {
111 return err
112 }
Jaewoong Jung6c296882019-02-20 07:12:30 -0800113
114 // Produce the top-level, package list page first.
Jaewoong Jung90e11552019-02-22 13:44:38 -0800115 tmpl := template.Must(template.Must(template.New("file").Parse(packageListTemplate)).Parse(copyBaseUrl))
Jaewoong Jung6c296882019-02-20 07:12:30 -0800116 buf := &bytes.Buffer{}
Jaewoong Jung90e11552019-02-22 13:44:38 -0800117 err = tmpl.Execute(buf, packages)
Sasha Smundakff483392019-02-07 12:10:56 -0800118 if err == nil {
119 err = ioutil.WriteFile(filename, buf.Bytes(), 0666)
Colin Crosse87040b2017-12-11 15:52:26 -0800120 }
Jaewoong Jung6c296882019-02-20 07:12:30 -0800121
122 // Now, produce per-package module lists with detailed information.
123 for _, pkg := range packages {
124 // We need a module name getter/setter function because I couldn't
125 // find a way to keep it in a variable defined within the template.
126 currentModuleName := ""
Jaewoong Jung90e11552019-02-22 13:44:38 -0800127 tmpl := template.Must(
128 template.Must(template.New("file").Funcs(map[string]interface{}{
129 "setModule": func(moduleName string) string {
130 currentModuleName = moduleName
131 return ""
132 },
133 "getModule": func() string {
134 return currentModuleName
135 },
136 }).Parse(perPackageTemplate)).Parse(copyBaseUrl))
Jaewoong Jung6c296882019-02-20 07:12:30 -0800137 buf := &bytes.Buffer{}
138 modules := moduleTypeDocsToTemplates(pkg.ModuleTypes)
139 data := perPackageTemplateData{Name: pkg.Name, Modules: modules}
140 err = tmpl.Execute(buf, data)
141 if err != nil {
142 return err
143 }
144 pkgFileName := filepath.Join(filepath.Dir(filename), pkg.Name+".html")
145 err = ioutil.WriteFile(pkgFileName, buf.Bytes(), 0666)
146 if err != nil {
147 return err
148 }
149 }
Sasha Smundakff483392019-02-07 12:10:56 -0800150 return err
Colin Crosse87040b2017-12-11 15:52:26 -0800151}
152
Jaewoong Jung6c296882019-02-20 07:12:30 -0800153// TODO(jungjw): Consider ordering by name.
Colin Crosse87040b2017-12-11 15:52:26 -0800154const (
Jaewoong Jung6c296882019-02-20 07:12:30 -0800155 packageListTemplate = `
156<html>
157<head>
158<title>Build Docs</title>
Jaewoong Jung6c296882019-02-20 07:12:30 -0800159<style>
160#main {
161 padding: 48px;
162}
163
164table{
165 table-layout: fixed;
166}
167
168td {
169 word-wrap:break-word;
170}
Jaewoong Jung5f867c02019-04-15 15:09:16 -0700171
172/* The following entries are copied from source.android.com's css file. */
173td,td code {
174 color: #202124
175}
176
177th,th code {
178 color: #fff;
179 font: 500 16px/24px Roboto,sans-serif
180}
181
182td,table.responsive tr:not(.alt) td td:first-child,table.responsive td tr:not(.alt) td:first-child {
183 background: rgba(255,255,255,.95);
184 vertical-align: top
185}
186
187td,td code {
188 padding: 7px 8px 8px
189}
190
191tr {
192 border: 0;
193 background: #78909c;
194 border-top: 1px solid #cfd8dc
195}
196
197th,td {
198 border: 0;
199 margin: 0;
200 text-align: left
201}
202
203th {
204 height: 48px;
205 padding: 8px;
206 vertical-align: middle
207}
208
209table {
210 border: 0;
211 border-collapse: collapse;
212 border-spacing: 0;
213 font: 14px/20px Roboto,sans-serif;
214 margin: 16px 0;
215 width: 100%
216}
217
218h1 {
219 color: #80868b;
220 font: 300 34px/40px Roboto,sans-serif;
221 letter-spacing: -0.01em;
222 margin: 40px 0 20px
223}
224
225h1,h2,h3,h4,h5,h6 {
226 overflow: hidden;
227 padding: 0;
228 text-overflow: ellipsis
229}
230
231:link,:visited {
232 color: #039be5;
233 outline: 0;
234 text-decoration: none
235}
236
237body,html {
238 color: #202124;
239 font: 400 16px/24px Roboto,sans-serif;
240 -moz-osx-font-smoothing: grayscale;
241 -webkit-font-smoothing: antialiased;
242 height: 100%;
243 margin: 0;
244 -webkit-text-size-adjust: 100%;
245 -moz-text-size-adjust: 100%;
246 -ms-text-size-adjust: 100%;
247 text-size-adjust: 100%
248}
249
250html {
251 -webkit-box-sizing: border-box;
252 box-sizing: border-box
253}
254
255*,*::before,*::after {
256 -webkit-box-sizing: inherit;
257 box-sizing: inherit
258}
259
260body,div,dl,dd,form,img,input,figure,menu {
261 margin: 0;
262 padding: 0
263}
Jaewoong Jung6c296882019-02-20 07:12:30 -0800264</style>
Jaewoong Jung90e11552019-02-22 13:44:38 -0800265{{template "copyBaseUrl"}}
Jaewoong Jung6c296882019-02-20 07:12:30 -0800266</head>
267<body>
268<div id="main">
269<H1>Soong Modules Reference</H1>
270The latest versions of Android use the Soong build system, which greatly simplifies build
271configuration over the previous Make-based system. This site contains the generated reference
272files for the Soong build system.
273
274<table class="module_types" summary="Table of Soong module types sorted by package">
275 <thead>
276 <tr>
277 <th style="width:20%">Package</th>
278 <th style="width:80%">Module types</th>
279 </tr>
280 </thead>
281 <tbody>
282 {{range $pkg := .}}
283 <tr>
284 <td>{{.Path}}</td>
285 <td>
286 {{range $i, $mod := .ModuleTypes}}{{if $i}}, {{end}}<a href="{{$pkg.Name}}.html#{{$mod.Name}}">{{$mod.Name}}</a>{{end}}
287 </td>
288 </tr>
289 {{end}}
290 </tbody>
291</table>
292</div>
293</body>
294</html>
295`
Jaewoong Jung6c296882019-02-20 07:12:30 -0800296
Jaewoong Jung6c296882019-02-20 07:12:30 -0800297 perPackageTemplate = `
Colin Crosse87040b2017-12-11 15:52:26 -0800298<html>
299<head>
300<title>Build Docs</title>
Sasha Smundakff483392019-02-07 12:10:56 -0800301<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
302<style>
303.accordion,.simple{margin-left:1.5em;text-indent:-1.5em;margin-top:.25em}
304.collapsible{border-width:0 0 0 1;margin-left:.25em;padding-left:.25em;border-style:solid;
305 border-color:grey;display:none;}
306span.fixed{display: block; float: left; clear: left; width: 1em;}
307ul {
308 list-style-type: none;
309 margin: 0;
310 padding: 0;
311 width: 30ch;
312 background-color: #f1f1f1;
313 position: fixed;
314 height: 100%;
315 overflow: auto;
316}
317li a {
318 display: block;
319 color: #000;
320 padding: 8px 16px;
321 text-decoration: none;
322}
323
324li a.active {
325 background-color: #4CAF50;
326 color: white;
327}
328
329li a:hover:not(.active) {
330 background-color: #555;
331 color: white;
332}
333</style>
Jaewoong Jung90e11552019-02-22 13:44:38 -0800334{{template "copyBaseUrl"}}
Colin Crosse87040b2017-12-11 15:52:26 -0800335</head>
336<body>
Sasha Smundakff483392019-02-07 12:10:56 -0800337{{- /* Fixed sidebar with module types */ -}}
338<ul>
Jaewoong Jung6c296882019-02-20 07:12:30 -0800339<li><h3>{{.Name}} package</h3></li>
Jaewoong Jungd10f4842019-02-27 11:15:00 -0800340{{range $moduleType := .Modules}}<li><a href="{{$.Name}}.html#{{$moduleType.Name}}">{{$moduleType.Name}}</a></li>
Sasha Smundakff483392019-02-07 12:10:56 -0800341{{end -}}
342</ul>
343{{/* Main panel with H1 section per module type */}}
344<div style="margin-left:30ch;padding:1px 16px;">
Jaewoong Jung6c296882019-02-20 07:12:30 -0800345{{range $moduleType := .Modules}}
Sasha Smundakff483392019-02-07 12:10:56 -0800346 {{setModule $moduleType.Name}}
347 <p>
348 <h2 id="{{$moduleType.Name}}">{{$moduleType.Name}}</h2>
349 {{if $moduleType.Synopsis }}{{$moduleType.Synopsis}}{{else}}<i>Missing synopsis</i>{{end}}
350 {{- /* Comma-separated list of module attributes' links module attributes */ -}}
351 <div class="breadcrumb">
352 {{range $i,$prop := $moduleType.Properties }}
353 {{ if gt $i 0 }},&nbsp;{{end -}}
Jaewoong Jungd10f4842019-02-27 11:15:00 -0800354 <a href={{$.Name}}.html#{{getModule}}.{{$prop.Name}}>{{$prop.Name}}</a>
Sasha Smundakff483392019-02-07 12:10:56 -0800355 {{- end -}}
Colin Crosse87040b2017-12-11 15:52:26 -0800356 </div>
Sasha Smundakff483392019-02-07 12:10:56 -0800357 {{- /* Property description */ -}}
358 {{- template "properties" $moduleType.Properties -}}
359{{- end -}}
360
361{{define "properties" -}}
362 {{range .}}
363 {{if .Properties -}}
364 <div class="accordion" id="{{getModule}}.{{.Name}}">
365 <span class="fixed">&#x2295</span><b>{{.Name}}</b>
366 {{- range .OtherNames -}}, {{.}}{{- end -}}
367 </div>
368 <div class="collapsible">
369 {{- .Text}} {{range .OtherTexts}}{{.}}{{end}}
370 {{template "properties" .Properties -}}
371 </div>
372 {{- else -}}
373 <div class="simple" id="{{getModule}}.{{.Name}}">
374 <span class="fixed">&nbsp;</span><b>{{.Name}} {{range .OtherNames}}, {{.}}{{end -}}</b>
Jaewoong Jung12c02a62019-03-12 13:28:25 -0700375 <i>{{.Type}}</i>
376 {{- if .Text -}}{{if ne .Text "\n"}}, {{end}}{{.Text}}{{- end -}}
377 {{- with .OtherTexts -}}{{.}}{{- end -}}
Sasha Smundakff483392019-02-07 12:10:56 -0800378 {{- if .Default -}}<i>Default: {{.Default}}</i>{{- end -}}
379 </div>
380 {{- end}}
381 {{- end -}}
382{{- end -}}
Sasha Smundakff483392019-02-07 12:10:56 -0800383</div>
384<script>
385 accordions = document.getElementsByClassName('accordion');
386 for (i=0; i < accordions.length; ++i) {
387 accordions[i].addEventListener("click", function() {
388 var panel = this.nextElementSibling;
389 var child = this.firstElementChild;
390 if (panel.style.display === "block") {
391 panel.style.display = "none";
392 child.textContent = '\u2295';
393 } else {
394 panel.style.display = "block";
395 child.textContent = '\u2296';
396 }
397 });
398 }
399</script>
400</body>
Colin Crosse87040b2017-12-11 15:52:26 -0800401`
Jaewoong Jung90e11552019-02-22 13:44:38 -0800402
403 copyBaseUrl = `
404{{define "copyBaseUrl"}}
405<script type="text/javascript">
406window.addEventListener('message', (e) => {
407 if (e != null && e.data != null && e.data.type === "SET_BASE" && e.data.base != null) {
408 const existingBase = document.querySelector('base');
409 if (existingBase != null) {
410 existingBase.parentElement.removeChild(existingBase);
411 }
412
413 const base = document.createElement('base');
414 base.setAttribute('href', e.data.base);
415 document.head.appendChild(base);
416 }
417});
418</script>
419{{end}}
420`
Colin Crosse87040b2017-12-11 15:52:26 -0800421)