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.BaseContext) []string {
25	var cflags []string
26
27	if ctx.AConfig().IsEnvTrue("FORCE_BUILD_LLVM_DISABLE_NDEBUG") {
28		cflags = append(cflags, "-D_DEBUG", "-UNDEBUG")
29	}
30
31	return cflags
32}
33
34func deviceFlags(ctx android.BaseContext) []string {
35	var cflags []string
36
37	return cflags
38}
39
40func hostFlags(ctx android.BaseContext) []string {
41	var cflags []string
42
43	if ctx.AConfig().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.AConfig().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.AConfig().IsEnvTrue("FORCE_BUILD_LLVM_COMPONENTS") {
83		forceBuild = true
84	}
85	if len(ctx.AConfig().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			}
96		}
97		p := &props{}
98		p.Target.Host.Enabled = proptools.BoolPtr(false)
99		ctx.AppendProperties(p)
100	}
101}
102
103func init() {
104	android.RegisterModuleType("llvm_defaults", llvmDefaultsFactory)
105	android.RegisterModuleType("force_build_llvm_components_defaults", forceBuildLlvmComponentsDefaultsFactory)
106}
107
108func llvmDefaultsFactory() android.Module {
109	module := cc.DefaultsFactory()
110	android.AddLoadHook(module, llvmDefaults)
111
112	return module
113}
114
115func forceBuildLlvmComponentsDefaultsFactory() android.Module {
116	module := cc.DefaultsFactory()
117	android.AddLoadHook(module, forceBuildLlvmComponents)
118	return module
119}
120