1// Copyright 2016 Google Inc. All rights reserved.
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 cc
16
17// This file contains utility functions to check for bad or illegal cflags
18// specified by a module
19
20import (
21	"path/filepath"
22	"strings"
23
24	"android/soong/cc/config"
25)
26
27// Check for invalid c/conly/cpp/asflags and suggest alternatives. Only use this
28// for flags explicitly passed by the user, since these flags may be used internally.
29func CheckBadCompilerFlags(ctx BaseModuleContext, prop string, flags []string) {
30	for _, flag := range flags {
31		flag = strings.TrimSpace(flag)
32
33		if !strings.HasPrefix(flag, "-") {
34			ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag)
35		} else if strings.HasPrefix(flag, "-I") || strings.HasPrefix(flag, "-isystem") {
36			ctx.PropertyErrorf(prop, "Bad flag `%s`, use local_include_dirs or include_dirs instead", flag)
37		} else if inList(flag, config.IllegalFlags) {
38			ctx.PropertyErrorf(prop, "Illegal flag `%s`", flag)
39		} else if flag == "--coverage" {
40			ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag)
41		} else if flag == "-Weverything" {
42			if !ctx.Config().IsEnvTrue("ANDROID_TEMPORARILY_ALLOW_WEVERYTHING") {
43				ctx.PropertyErrorf(prop, "-Weverything is not allowed in Android.bp files.  "+
44					"Build with `m ANDROID_TEMPORARILY_ALLOW_WEVERYTHING=true` to experiment locally with -Weverything.")
45			}
46		} else if strings.Contains(flag, " ") {
47			args := strings.Split(flag, " ")
48			if args[0] == "-include" {
49				if len(args) > 2 {
50					ctx.PropertyErrorf(prop, "`-include` only takes one argument: `%s`", flag)
51				}
52				path := filepath.Clean(args[1])
53				if strings.HasPrefix("/", path) {
54					ctx.PropertyErrorf(prop, "Path must not be an absolute path: %s", flag)
55				} else if strings.HasPrefix("../", path) {
56					ctx.PropertyErrorf(prop, "Path must not start with `../`: `%s`. Use include_dirs to -include from a different directory", flag)
57				}
58			} else if strings.HasPrefix(flag, "-D") && strings.Contains(flag, "=") {
59				// Do nothing in this case.
60				// For now, we allow space characters in -DNAME=def form to allow use cases
61				// like -DNAME="value with string". Later, this check should be done more
62				// correctly to prevent multi flag cases like -DNAME=value -O2.
63			} else {
64				ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag)
65			}
66		}
67	}
68}
69
70// Check for bad ldflags and suggest alternatives. Only use this for flags
71// explicitly passed by the user, since these flags may be used internally.
72func CheckBadLinkerFlags(ctx BaseModuleContext, prop string, flags []string) {
73	for _, flag := range flags {
74		flag = strings.TrimSpace(flag)
75
76		if !strings.HasPrefix(flag, "-") {
77			ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag)
78		} else if strings.HasPrefix(flag, "-l") {
79			if ctx.Host() {
80				ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs or host_ldlibs instead", flag)
81			} else {
82				ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs instead", flag)
83			}
84		} else if strings.HasPrefix(flag, "-L") {
85			ctx.PropertyErrorf(prop, "Bad flag: `%s` is not allowed", flag)
86		} else if strings.HasPrefix(flag, "-Wl,--version-script") {
87			ctx.PropertyErrorf(prop, "Bad flag: `%s`, use version_script instead", flag)
88		} else if flag == "--coverage" {
89			ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag)
90		} else if strings.Contains(flag, " ") {
91			args := strings.Split(flag, " ")
92			if args[0] == "-z" {
93				if len(args) > 2 {
94					ctx.PropertyErrorf(prop, "`-z` only takes one argument: `%s`", flag)
95				}
96			} else {
97				ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag)
98			}
99		}
100	}
101}
102
103// Check for bad host_ldlibs
104func CheckBadHostLdlibs(ctx ModuleContext, prop string, flags []string) {
105	allowed_ldlibs := ctx.toolchain().AvailableLibraries()
106
107	if !ctx.Host() {
108		panic("Invalid call to CheckBadHostLdlibs")
109	}
110
111	for _, flag := range flags {
112		flag = strings.TrimSpace(flag)
113
114		// TODO: Probably should just redo this property to prefix -l in Soong
115		if !strings.HasPrefix(flag, "-l") && !strings.HasPrefix(flag, "-framework") {
116			ctx.PropertyErrorf(prop, "Invalid flag: `%s`, must start with `-l` or `-framework`", flag)
117		} else if !inList(flag, allowed_ldlibs) {
118			ctx.PropertyErrorf(prop, "Host library `%s` not available", flag)
119		}
120	}
121}
122
123// Check for bad clang tidy flags
124func CheckBadTidyFlags(ctx ModuleContext, prop string, flags []string) {
125	for _, flag := range flags {
126		flag = strings.TrimSpace(flag)
127
128		if !strings.HasPrefix(flag, "-") {
129			ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag)
130		} else if strings.HasPrefix(flag, "-fix") {
131			ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, since it could cause multiple writes to the same source file", flag)
132		} else if strings.HasPrefix(flag, "-checks=") {
133			ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, use `tidy_checks` property instead", flag)
134		} else if strings.Contains(flag, " ") {
135			ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag)
136		}
137	}
138}
139
140// Check for bad clang tidy checks
141func CheckBadTidyChecks(ctx ModuleContext, prop string, checks []string) {
142	for _, check := range checks {
143		if strings.Contains(check, " ") {
144			ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain spaces", check)
145		} else if strings.Contains(check, ",") {
146			ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain commas. Split each entry into it's own string instead", check)
147		}
148	}
149}
150