• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2016 The Android Open Source Project
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 clang
16
17import (
18	"path/filepath"
19	"strings"
20
21	"android/soong/android"
22	"android/soong/genrule"
23
24	"github.com/google/blueprint"
25	"github.com/google/blueprint/proptools"
26)
27
28func init() {
29	android.RegisterModuleType("clang_tblgen", clangTblgenFactory)
30}
31
32var (
33	pctx = android.NewPackageContext("android/soong/clang")
34
35	clangTblgen = pctx.HostBinToolVariable("clangTblgen", "clang-tblgen")
36
37	tblgenRule = pctx.StaticRule("tblgenRule", blueprint.RuleParams{
38		Depfile:     "${out}.d",
39		Deps:        blueprint.DepsGCC,
40		Command:     "${clangTblgen} ${includes} ${genopt} -d ${depfile} -o ${out} ${in}",
41		CommandDeps: []string{"${clangTblgen}"},
42		Description: "Clang TableGen $in => $out",
43	}, "includes", "depfile", "genopt")
44)
45
46type tblgenProperties struct {
47	In   *string
48	Outs []string
49}
50
51type tblgen struct {
52	android.ModuleBase
53
54	properties tblgenProperties
55
56	exportedHeaderDirs android.Paths
57	generatedHeaders   android.Paths
58}
59
60var _ genrule.SourceFileGenerator = (*tblgen)(nil)
61
62func (t *tblgen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
63	in := android.PathForModuleSrc(ctx, proptools.String(t.properties.In))
64
65	includes := []string{
66		"-I " + ctx.ModuleDir(),
67		"-I external/llvm/include",
68		"-I external/llvm/lib/Target",
69		"-I external/clang/include",
70		"-I " + filepath.Dir(in.String()),
71	}
72
73	for _, o := range t.properties.Outs {
74		out := android.PathForModuleGen(ctx, o)
75		generator := outToGenerator(ctx, o)
76
77		ctx.ModuleBuild(pctx, android.ModuleBuildParams{
78			Rule:   tblgenRule,
79			Input:  in,
80			Output: out,
81			Args: map[string]string{
82				"includes": strings.Join(includes, " "),
83				"genopt":   generator,
84			},
85		})
86		t.generatedHeaders = append(t.generatedHeaders, out)
87	}
88
89	t.exportedHeaderDirs = append(t.exportedHeaderDirs, android.PathForModuleGen(ctx, ""))
90}
91
92func outToGenerator(ctx android.ModuleContext, out string) string {
93	out = filepath.Base(out)
94	switch {
95	case out == "AttrDump.inc":
96		return "-gen-clang-attr-dump"
97	case out == "AttrImpl.inc":
98		return "-gen-clang-attr-impl"
99	case out == "AttrHasAttributeImpl.inc":
100		return "-gen-clang-attr-has-attribute-impl"
101	case out == "AttrList.inc":
102		return "-gen-clang-attr-list"
103	case out == "AttrSpellingListIndex.inc":
104		return "-gen-clang-attr-spelling-index"
105	case out == "AttrPCHRead.inc":
106		return "-gen-clang-attr-pch-read"
107	case out == "AttrPCHWrite.inc":
108		return "-gen-clang-attr-pch-write"
109	case out == "Attrs.inc":
110		return "-gen-clang-attr-classes"
111	case out == "AttrParserStringSwitches.inc":
112		return "-gen-clang-attr-parser-string-switches"
113	case out == "AttrVisitor.inc":
114		return "-gen-clang-attr-ast-visitor"
115	case out == "AttrParsedAttrKinds.inc":
116		return "-gen-clang-attr-parsed-attr-kinds"
117	case out == "AttrParsedAttrImpl.inc":
118		return "-gen-clang-attr-parsed-attr-impl"
119	case out == "AttrParsedAttrList.inc":
120		return "-gen-clang-attr-parsed-attr-list"
121	case out == "AttrTemplateInstantiate.inc":
122		return "-gen-clang-attr-template-instantiate"
123	case out == "Checkers.inc":
124		return "-gen-clang-sa-checkers"
125	case out == "CommentCommandInfo.inc":
126		return "-gen-clang-comment-command-info"
127	case out == "CommentCommandList.inc":
128		return "-gen-clang-comment-command-list"
129	case out == "CommentHTMLNamedCharacterReferences.inc":
130		return "-gen-clang-comment-html-named-character-references"
131	case out == "CommentHTMLTagsProperties.inc":
132		return "-gen-clang-comment-html-tags-properties"
133	case out == "CommentHTMLTags.inc":
134		return "-gen-clang-comment-html-tags"
135	case out == "CommentNodes.inc":
136		return "-gen-clang-comment-nodes"
137	case strings.HasPrefix(out, "Diagnostic") && strings.HasSuffix(out, "Kinds.inc"):
138		component := strings.TrimPrefix(strings.TrimSuffix(out, "Kinds.inc"), "Diagnostic")
139		return "-gen-clang-diags-defs -clang-component=" + component
140	case out == "DiagnosticGroups.inc":
141		return "-gen-clang-diag-groups"
142	case out == "DiagnosticIndexName.inc":
143		return "-gen-clang-diag-groups"
144	case out == "DeclNodes.inc":
145		return "-gen-clang-decl-nodes"
146	case out == "StmtNodes.inc":
147		return "-gen-clang-stmt-nodes"
148	case out == "arm_neon.inc":
149		return "-gen-arm-neon-sema"
150	case out == "arm_neon.h":
151		return "-gen-arm-neon"
152	}
153
154	ctx.ModuleErrorf("couldn't map output file %q to a generator", out)
155	return ""
156}
157
158func (t *tblgen) DepsMutator(ctx android.BottomUpMutatorContext) {
159}
160
161func (t *tblgen) GeneratedHeaderDirs() android.Paths {
162	return t.exportedHeaderDirs
163}
164
165func (t *tblgen) GeneratedSourceFiles() android.Paths {
166	return nil
167}
168
169func (t *tblgen) GeneratedDeps() android.Paths {
170	return t.generatedHeaders
171}
172
173func clangTblgenFactory() android.Module {
174	t := &tblgen{}
175
176	t.AddProperties(&t.properties)
177	android.InitAndroidModule(t)
178	return t
179}
180