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