xref: /aosp_15_r20/external/llvm/soong/tblgen.go (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker// Copyright (C) 2016 The Android Open Source Project
2*9880d681SAndroid Build Coastguard Worker//
3*9880d681SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*9880d681SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*9880d681SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*9880d681SAndroid Build Coastguard Worker//
7*9880d681SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*9880d681SAndroid Build Coastguard Worker//
9*9880d681SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*9880d681SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*9880d681SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9880d681SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*9880d681SAndroid Build Coastguard Worker// limitations under the License.
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Workerpackage llvm
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Workerimport (
18*9880d681SAndroid Build Coastguard Worker	"path/filepath"
19*9880d681SAndroid Build Coastguard Worker	"strings"
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker	"android/soong/android"
22*9880d681SAndroid Build Coastguard Worker	"android/soong/genrule"
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker	"github.com/google/blueprint"
25*9880d681SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
26*9880d681SAndroid Build Coastguard Worker)
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Workerfunc init() {
29*9880d681SAndroid Build Coastguard Worker	android.RegisterModuleType("llvm_tblgen", llvmTblgenFactory)
30*9880d681SAndroid Build Coastguard Worker}
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Workervar (
33*9880d681SAndroid Build Coastguard Worker	pctx = android.NewPackageContext("android/soong/llvm")
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker	llvmTblgen = pctx.HostBinToolVariable("llvmTblgen", "llvm-tblgen")
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker	tblgenRule = pctx.StaticRule("tblgenRule", blueprint.RuleParams{
38*9880d681SAndroid Build Coastguard Worker		Depfile:     "${out}.d",
39*9880d681SAndroid Build Coastguard Worker		Deps:        blueprint.DepsGCC,
40*9880d681SAndroid Build Coastguard Worker		Command:     "${llvmTblgen} ${includes} ${genopt} -d ${out}.d -o ${out} ${in}",
41*9880d681SAndroid Build Coastguard Worker		CommandDeps: []string{"${llvmTblgen}"},
42*9880d681SAndroid Build Coastguard Worker		Description: "LLVM TableGen $in => $out",
43*9880d681SAndroid Build Coastguard Worker	}, "includes", "genopt")
44*9880d681SAndroid Build Coastguard Worker)
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Workertype tblgenProperties struct {
47*9880d681SAndroid Build Coastguard Worker	In   *string
48*9880d681SAndroid Build Coastguard Worker	Outs []string
49*9880d681SAndroid Build Coastguard Worker}
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Workertype tblgen struct {
52*9880d681SAndroid Build Coastguard Worker	android.ModuleBase
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker	properties tblgenProperties
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker	exportedHeaderDirs android.Paths
57*9880d681SAndroid Build Coastguard Worker	generatedHeaders   android.Paths
58*9880d681SAndroid Build Coastguard Worker}
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Workervar _ genrule.SourceFileGenerator = (*tblgen)(nil)
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Workerfunc (t *tblgen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
63*9880d681SAndroid Build Coastguard Worker	in := android.PathForModuleSrc(ctx, proptools.String(t.properties.In))
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker	includes := []string{
66*9880d681SAndroid Build Coastguard Worker		"-I " + ctx.ModuleDir(),
67*9880d681SAndroid Build Coastguard Worker		"-I external/llvm/include",
68*9880d681SAndroid Build Coastguard Worker		"-I external/llvm/lib/Target",
69*9880d681SAndroid Build Coastguard Worker		"-I " + filepath.Dir(in.String()),
70*9880d681SAndroid Build Coastguard Worker	}
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker	for _, o := range t.properties.Outs {
73*9880d681SAndroid Build Coastguard Worker		out := android.PathForModuleGen(ctx, o)
74*9880d681SAndroid Build Coastguard Worker		generator := outToGenerator(ctx, o)
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker		ctx.ModuleBuild(pctx, android.ModuleBuildParams{
77*9880d681SAndroid Build Coastguard Worker			Rule:   tblgenRule,
78*9880d681SAndroid Build Coastguard Worker			Input:  in,
79*9880d681SAndroid Build Coastguard Worker			Output: out,
80*9880d681SAndroid Build Coastguard Worker			Args: map[string]string{
81*9880d681SAndroid Build Coastguard Worker				"includes": strings.Join(includes, " "),
82*9880d681SAndroid Build Coastguard Worker				"genopt":   generator,
83*9880d681SAndroid Build Coastguard Worker			},
84*9880d681SAndroid Build Coastguard Worker		})
85*9880d681SAndroid Build Coastguard Worker		t.generatedHeaders = append(t.generatedHeaders, out)
86*9880d681SAndroid Build Coastguard Worker	}
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker	t.exportedHeaderDirs = append(t.exportedHeaderDirs, android.PathForModuleGen(ctx, ""))
89*9880d681SAndroid Build Coastguard Worker}
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Workerfunc outToGenerator(ctx android.ModuleContext, out string) string {
92*9880d681SAndroid Build Coastguard Worker	out = filepath.Base(out)
93*9880d681SAndroid Build Coastguard Worker	switch {
94*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenRegisterInfo.inc"):
95*9880d681SAndroid Build Coastguard Worker		return "-gen-register-info"
96*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenInstrInfo.inc"):
97*9880d681SAndroid Build Coastguard Worker		return "-gen-instr-info"
98*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenAsmWriter.inc"):
99*9880d681SAndroid Build Coastguard Worker		return "-gen-asm-writer"
100*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenAsmWriter1.inc"):
101*9880d681SAndroid Build Coastguard Worker		return "-gen-asm-writer -asmwriternum=1"
102*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenAsmMatcher.inc"):
103*9880d681SAndroid Build Coastguard Worker		return "-gen-asm-matcher"
104*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenCodeEmitter.inc"):
105*9880d681SAndroid Build Coastguard Worker		return "-gen-emitter"
106*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenMCCodeEmitter.inc"):
107*9880d681SAndroid Build Coastguard Worker		return "-gen-emitter"
108*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenMCPseudoLowering.inc"):
109*9880d681SAndroid Build Coastguard Worker		return "-gen-pseudo-lowering"
110*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenDAGISel.inc"):
111*9880d681SAndroid Build Coastguard Worker		return "-gen-dag-isel"
112*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenDisassemblerTables.inc"):
113*9880d681SAndroid Build Coastguard Worker		return "-gen-disassembler"
114*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenSystemOperands.inc"):
115*9880d681SAndroid Build Coastguard Worker		return "-gen-searchable-tables"
116*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenEDInfo.inc"):
117*9880d681SAndroid Build Coastguard Worker		return "-gen-enhanced-disassembly-info"
118*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenFastISel.inc"):
119*9880d681SAndroid Build Coastguard Worker		return "-gen-fast-isel"
120*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenSubtargetInfo.inc"):
121*9880d681SAndroid Build Coastguard Worker		return "-gen-subtarget"
122*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenCallingConv.inc"):
123*9880d681SAndroid Build Coastguard Worker		return "-gen-callingconv"
124*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenIntrinsics.inc"):
125*9880d681SAndroid Build Coastguard Worker		return "-gen-intrinsics"
126*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "GenDecoderTables.inc"):
127*9880d681SAndroid Build Coastguard Worker		return "-gen-arm-decoder"
128*9880d681SAndroid Build Coastguard Worker	case strings.HasSuffix(out, "Options.inc"):
129*9880d681SAndroid Build Coastguard Worker		return "-gen-opt-parser-defs"
130*9880d681SAndroid Build Coastguard Worker	case out == "Attributes.inc", out == "AttributesCompatFunc.inc":
131*9880d681SAndroid Build Coastguard Worker		return "-gen-attrs"
132*9880d681SAndroid Build Coastguard Worker	case out == "Intrinsics.gen":
133*9880d681SAndroid Build Coastguard Worker		return "-gen-intrinsic"
134*9880d681SAndroid Build Coastguard Worker	}
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker	ctx.ModuleErrorf("couldn't map output file %q to a generator", out)
137*9880d681SAndroid Build Coastguard Worker	return ""
138*9880d681SAndroid Build Coastguard Worker}
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Workerfunc (t *tblgen) DepsMutator(ctx android.BottomUpMutatorContext) {
141*9880d681SAndroid Build Coastguard Worker}
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Workerfunc (t *tblgen) GeneratedHeaderDirs() android.Paths {
144*9880d681SAndroid Build Coastguard Worker	return t.exportedHeaderDirs
145*9880d681SAndroid Build Coastguard Worker}
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Workerfunc (t *tblgen) GeneratedSourceFiles() android.Paths {
148*9880d681SAndroid Build Coastguard Worker	return nil
149*9880d681SAndroid Build Coastguard Worker}
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Workerfunc (t *tblgen) GeneratedDeps() android.Paths {
152*9880d681SAndroid Build Coastguard Worker	return t.generatedHeaders
153*9880d681SAndroid Build Coastguard Worker}
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Workerfunc llvmTblgenFactory() android.Module {
156*9880d681SAndroid Build Coastguard Worker	t := &tblgen{}
157*9880d681SAndroid Build Coastguard Worker	t.AddProperties(&t.properties)
158*9880d681SAndroid Build Coastguard Worker	android.InitAndroidModule(t)
159*9880d681SAndroid Build Coastguard Worker	return t
160*9880d681SAndroid Build Coastguard Worker}
161