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 llvm
16
17import (
18	"android/soong/android"
19	"android/soong/cc"
20
21	"github.com/google/blueprint/proptools"
22)
23
24func globalFlags(ctx android.LoadHookContext) []string {
25	var cflags []string
26
27	if ctx.Config().IsEnvTrue("FORCE_BUILD_LLVM_DISABLE_NDEBUG") {
28		cflags = append(cflags, "-D_DEBUG", "-UNDEBUG")
29	}
30
31	return cflags
32}
33
34func deviceFlags(ctx android.LoadHookContext) []string {
35	var cflags []string
36
37	return cflags
38}
39
40func hostFlags(ctx android.LoadHookContext) []string {
41	var cflags []string
42
43	if ctx.Config().IsEnvTrue("FORCE_BUILD_LLVM_DEBUG") {
44		cflags = append(cflags, "-O0", "-g")
45	}
46
47	return cflags
48}
49
50func llvmDefaults(ctx android.LoadHookContext) {
51	type props struct {
52		Target struct {
53			Android struct {
54				Cflags  []string
55				Enabled *bool
56			}
57			Host struct {
58				Enabled *bool
59			}
60			Not_windows struct {
61				Cflags []string
62			}
63		}
64		Cflags []string
65	}
66
67	p := &props{}
68	p.Cflags = globalFlags(ctx)
69	p.Target.Android.Cflags = deviceFlags(ctx)
70	// Mingw fails to link binaries with lots of debug information
71	p.Target.Not_windows.Cflags = hostFlags(ctx)
72
73	if ctx.Config().IsEnvTrue("DISABLE_LLVM_DEVICE_BUILDS") {
74		p.Target.Android.Enabled = proptools.BoolPtr(false)
75	}
76
77	ctx.AppendProperties(p)
78}
79
80func forceBuildLlvmComponents(ctx android.LoadHookContext) {
81	forceBuild := false
82	if ctx.Config().IsEnvTrue("FORCE_BUILD_LLVM_COMPONENTS") {
83		forceBuild = true
84	}
85	if len(ctx.Config().SanitizeHost()) > 0 {
86		forceBuild = true
87	}
88
89	if !forceBuild {
90		type props struct {
91			Target struct {
92				Host struct {
93					Enabled *bool
94				}
95				Linux_bionic_arm64 struct {
96					Enabled *bool
97				}
98			}
99		}
100		p := &props{}
101		p.Target.Host.Enabled = proptools.BoolPtr(false)
102		p.Target.Linux_bionic_arm64.Enabled = proptools.BoolPtr(true)
103		ctx.AppendProperties(p)
104	}
105}
106
107func init() {
108	android.RegisterModuleType("llvm_defaults", llvmDefaultsFactory)
109	android.RegisterModuleType("force_build_llvm_components_defaults", forceBuildLlvmComponentsDefaultsFactory)
110}
111
112func llvmDefaultsFactory() android.Module {
113	module := cc.DefaultsFactory()
114	android.AddLoadHook(module, llvmDefaults)
115
116	return module
117}
118
119func forceBuildLlvmComponentsDefaultsFactory() android.Module {
120	module := cc.DefaultsFactory()
121	android.AddLoadHook(module, forceBuildLlvmComponents)
122	return module
123}
124