blob: 5fb6e6ba23b07878e6b8bf8cfc93a4b30498d3d7 [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 Kammera93fbac2020-07-07 13:47:29 -070047 "defaults": 3,
Sasha Smundakff483392019-02-07 12:10:56 -080048 "host_supported": 4,
49 "device_supported": 5,
50}
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) {
Jaewoong Jung6c296882019-02-20 07:12:30 -080099 moduleTypeFactories := android.ModuleTypeFactories()
100 bpModuleTypeFactories := make(map[string]reflect.Value)
101 for moduleType, factory := range moduleTypeFactories {
102 bpModuleTypeFactories[moduleType] = reflect.ValueOf(factory)
103 }
Jingwen Chend8004ef2020-08-27 09:40:43 +0000104 return bootstrap.ModuleTypeDocs(ctx.Context, bpModuleTypeFactories)
105}
Sasha Smundakff483392019-02-07 12:10:56 -0800106
Jingwen Chend8004ef2020-08-27 09:40:43 +0000107func writeDocs(ctx *android.Context, filename string) error {
108 packages, err := getPackages(ctx)
Sasha Smundakff483392019-02-07 12:10:56 -0800109 if err != nil {
110 return err
111 }
Jaewoong Jung6c296882019-02-20 07:12:30 -0800112
113 // Produce the top-level, package list page first.
Jaewoong Jung90e11552019-02-22 13:44:38 -0800114 tmpl := template.Must(template.Must(template.New("file").Parse(packageListTemplate)).Parse(copyBaseUrl))
Jaewoong Jung6c296882019-02-20 07:12:30 -0800115 buf := &bytes.Buffer{}
Jaewoong Jung90e11552019-02-22 13:44:38 -0800116 err = tmpl.Execute(buf, packages)
Sasha Smundakff483392019-02-07 12:10:56 -0800117 if err == nil {
118 err = ioutil.WriteFile(filename, buf.Bytes(), 0666)
Colin Crosse87040b2017-12-11 15:52:26 -0800119 }
Jaewoong Jung6c296882019-02-20 07:12:30 -0800120
121 // Now, produce per-package module lists with detailed information.
122 for _, pkg := range packages {
123 // We need a module name getter/setter function because I couldn't
124 // find a way to keep it in a variable defined within the template.
125 currentModuleName := ""
Jaewoong Jung90e11552019-02-22 13:44:38 -0800126 tmpl := template.Must(
127 template.Must(template.New("file").Funcs(map[string]interface{}{
128 "setModule": func(moduleName string) string {
129 currentModuleName = moduleName
130 return ""
131 },
132 "getModule": func() string {
133 return currentModuleName
134 },
135 }).Parse(perPackageTemplate)).Parse(copyBaseUrl))
Jaewoong Jung6c296882019-02-20 07:12:30 -0800136 buf := &bytes.Buffer{}
137 modules := moduleTypeDocsToTemplates(pkg.ModuleTypes)
138 data := perPackageTemplateData{Name: pkg.Name, Modules: modules}
139 err = tmpl.Execute(buf, data)
140 if err != nil {
141 return err
142 }
143 pkgFileName := filepath.Join(filepath.Dir(filename), pkg.Name+".html")
144 err = ioutil.WriteFile(pkgFileName, buf.Bytes(), 0666)
145 if err != nil {
146 return err
147 }
148 }
Sasha Smundakff483392019-02-07 12:10:56 -0800149 return err
Colin Crosse87040b2017-12-11 15:52:26 -0800150}
151
Jaewoong Jung6c296882019-02-20 07:12:30 -0800152// TODO(jungjw): Consider ordering by name.
Colin Crosse87040b2017-12-11 15:52:26 -0800153const (
Jaewoong Jung6c296882019-02-20 07:12:30 -0800154 packageListTemplate = `
155<html>
156<head>
157<title>Build Docs</title>
Jaewoong Jung6c296882019-02-20 07:12:30 -0800158<style>
159#main {
160 padding: 48px;
161}
162
163table{
164 table-layout: fixed;
165}
166
167td {
168 word-wrap:break-word;
169}
Jaewoong Jung5f867c02019-04-15 15:09:16 -0700170
171/* The following entries are copied from source.android.com's css file. */
172td,td code {
173 color: #202124
174}
175
176th,th code {
177 color: #fff;
178 font: 500 16px/24px Roboto,sans-serif
179}
180
181td,table.responsive tr:not(.alt) td td:first-child,table.responsive td tr:not(.alt) td:first-child {
182 background: rgba(255,255,255,.95);
183 vertical-align: top
184}
185
186td,td code {
187 padding: 7px 8px 8px
188}
189
190tr {
191 border: 0;
192 background: #78909c;
193 border-top: 1px solid #cfd8dc
194}
195
196th,td {
197 border: 0;
198 margin: 0;
199 text-align: left
200}
201
202th {
203 height: 48px;
204 padding: 8px;
205 vertical-align: middle
206}
207
208table {
209 border: 0;
210 border-collapse: collapse;
211 border-spacing: 0;
212 font: 14px/20px Roboto,sans-serif;
213 margin: 16px 0;
214 width: 100%
215}
216
217h1 {
218 color: #80868b;
219 font: 300 34px/40px Roboto,sans-serif;
220 letter-spacing: -0.01em;
221 margin: 40px 0 20px
222}
223
224h1,h2,h3,h4,h5,h6 {
225 overflow: hidden;
226 padding: 0;
227 text-overflow: ellipsis
228}
229
230:link,:visited {
231 color: #039be5;
232 outline: 0;
233 text-decoration: none
234}
235
236body,html {
237 color: #202124;
238 font: 400 16px/24px Roboto,sans-serif;
239 -moz-osx-font-smoothing: grayscale;
240 -webkit-font-smoothing: antialiased;
241 height: 100%;
242 margin: 0;
243 -webkit-text-size-adjust: 100%;
244 -moz-text-size-adjust: 100%;
245 -ms-text-size-adjust: 100%;
246 text-size-adjust: 100%
247}
248
249html {
250 -webkit-box-sizing: border-box;
251 box-sizing: border-box
252}
253
254*,*::before,*::after {
255 -webkit-box-sizing: inherit;
256 box-sizing: inherit
257}
258
259body,div,dl,dd,form,img,input,figure,menu {
260 margin: 0;
261 padding: 0
262}
Jaewoong Jung6c296882019-02-20 07:12:30 -0800263</style>
Jaewoong Jung90e11552019-02-22 13:44:38 -0800264{{template "copyBaseUrl"}}
Jaewoong Jung6c296882019-02-20 07:12:30 -0800265</head>
266<body>
267<div id="main">
268<H1>Soong Modules Reference</H1>
269The latest versions of Android use the Soong build system, which greatly simplifies build
270configuration over the previous Make-based system. This site contains the generated reference
271files for the Soong build system.
272
273<table class="module_types" summary="Table of Soong module types sorted by package">
274 <thead>
275 <tr>
276 <th style="width:20%">Package</th>
277 <th style="width:80%">Module types</th>
278 </tr>
279 </thead>
280 <tbody>
281 {{range $pkg := .}}
282 <tr>
283 <td>{{.Path}}</td>
284 <td>
285 {{range $i, $mod := .ModuleTypes}}{{if $i}}, {{end}}<a href="{{$pkg.Name}}.html#{{$mod.Name}}">{{$mod.Name}}</a>{{end}}
286 </td>
287 </tr>
288 {{end}}
289 </tbody>
290</table>
291</div>
292</body>
293</html>
294`
Jaewoong Jung6c296882019-02-20 07:12:30 -0800295
Jaewoong Jung6c296882019-02-20 07:12:30 -0800296 perPackageTemplate = `
Colin Crosse87040b2017-12-11 15:52:26 -0800297<html>
298<head>
299<title>Build Docs</title>
Sasha Smundakff483392019-02-07 12:10:56 -0800300<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
301<style>
302.accordion,.simple{margin-left:1.5em;text-indent:-1.5em;margin-top:.25em}
303.collapsible{border-width:0 0 0 1;margin-left:.25em;padding-left:.25em;border-style:solid;
304 border-color:grey;display:none;}
305span.fixed{display: block; float: left; clear: left; width: 1em;}
306ul {
307 list-style-type: none;
308 margin: 0;
309 padding: 0;
310 width: 30ch;
311 background-color: #f1f1f1;
312 position: fixed;
313 height: 100%;
314 overflow: auto;
315}
316li a {
317 display: block;
318 color: #000;
319 padding: 8px 16px;
320 text-decoration: none;
321}
322
323li a.active {
324 background-color: #4CAF50;
325 color: white;
326}
327
328li a:hover:not(.active) {
329 background-color: #555;
330 color: white;
331}
332</style>
Jaewoong Jung90e11552019-02-22 13:44:38 -0800333{{template "copyBaseUrl"}}
Colin Crosse87040b2017-12-11 15:52:26 -0800334</head>
335<body>
Sasha Smundakff483392019-02-07 12:10:56 -0800336{{- /* Fixed sidebar with module types */ -}}
337<ul>
Jaewoong Jung6c296882019-02-20 07:12:30 -0800338<li><h3>{{.Name}} package</h3></li>
Jaewoong Jungd10f4842019-02-27 11:15:00 -0800339{{range $moduleType := .Modules}}<li><a href="{{$.Name}}.html#{{$moduleType.Name}}">{{$moduleType.Name}}</a></li>
Sasha Smundakff483392019-02-07 12:10:56 -0800340{{end -}}
341</ul>
342{{/* Main panel with H1 section per module type */}}
343<div style="margin-left:30ch;padding:1px 16px;">
Jaewoong Jung6c296882019-02-20 07:12:30 -0800344{{range $moduleType := .Modules}}
Sasha Smundakff483392019-02-07 12:10:56 -0800345 {{setModule $moduleType.Name}}
346 <p>
347 <h2 id="{{$moduleType.Name}}">{{$moduleType.Name}}</h2>
348 {{if $moduleType.Synopsis }}{{$moduleType.Synopsis}}{{else}}<i>Missing synopsis</i>{{end}}
349 {{- /* Comma-separated list of module attributes' links module attributes */ -}}
350 <div class="breadcrumb">
351 {{range $i,$prop := $moduleType.Properties }}
352 {{ if gt $i 0 }},&nbsp;{{end -}}
Jaewoong Jungd10f4842019-02-27 11:15:00 -0800353 <a href={{$.Name}}.html#{{getModule}}.{{$prop.Name}}>{{$prop.Name}}</a>
Sasha Smundakff483392019-02-07 12:10:56 -0800354 {{- end -}}
Colin Crosse87040b2017-12-11 15:52:26 -0800355 </div>
Sasha Smundakff483392019-02-07 12:10:56 -0800356 {{- /* Property description */ -}}
357 {{- template "properties" $moduleType.Properties -}}
358{{- end -}}
359
360{{define "properties" -}}
361 {{range .}}
362 {{if .Properties -}}
363 <div class="accordion" id="{{getModule}}.{{.Name}}">
364 <span class="fixed">&#x2295</span><b>{{.Name}}</b>
365 {{- range .OtherNames -}}, {{.}}{{- end -}}
366 </div>
367 <div class="collapsible">
368 {{- .Text}} {{range .OtherTexts}}{{.}}{{end}}
369 {{template "properties" .Properties -}}
370 </div>
371 {{- else -}}
372 <div class="simple" id="{{getModule}}.{{.Name}}">
373 <span class="fixed">&nbsp;</span><b>{{.Name}} {{range .OtherNames}}, {{.}}{{end -}}</b>
Jaewoong Jung12c02a62019-03-12 13:28:25 -0700374 <i>{{.Type}}</i>
375 {{- if .Text -}}{{if ne .Text "\n"}}, {{end}}{{.Text}}{{- end -}}
376 {{- with .OtherTexts -}}{{.}}{{- end -}}
Sasha Smundakff483392019-02-07 12:10:56 -0800377 {{- if .Default -}}<i>Default: {{.Default}}</i>{{- end -}}
378 </div>
379 {{- end}}
380 {{- end -}}
381{{- end -}}
Sasha Smundakff483392019-02-07 12:10:56 -0800382</div>
383<script>
384 accordions = document.getElementsByClassName('accordion');
385 for (i=0; i < accordions.length; ++i) {
386 accordions[i].addEventListener("click", function() {
387 var panel = this.nextElementSibling;
388 var child = this.firstElementChild;
389 if (panel.style.display === "block") {
390 panel.style.display = "none";
391 child.textContent = '\u2295';
392 } else {
393 panel.style.display = "block";
394 child.textContent = '\u2296';
395 }
396 });
397 }
398</script>
399</body>
Colin Crosse87040b2017-12-11 15:52:26 -0800400`
Jaewoong Jung90e11552019-02-22 13:44:38 -0800401
402 copyBaseUrl = `
403{{define "copyBaseUrl"}}
404<script type="text/javascript">
405window.addEventListener('message', (e) => {
406 if (e != null && e.data != null && e.data.type === "SET_BASE" && e.data.base != null) {
407 const existingBase = document.querySelector('base');
408 if (existingBase != null) {
409 existingBase.parentElement.removeChild(existingBase);
410 }
411
412 const base = document.createElement('base');
413 base.setAttribute('href', e.data.base);
414 document.head.appendChild(base);
415 }
416});
417</script>
418{{end}}
419`
Colin Crosse87040b2017-12-11 15:52:26 -0800420)