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