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 == "-fwhole-program-vtables" { 42 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use whole_program_vtables instead", flag) 43 } else if flag == "-Weverything" { 44 if !ctx.Config().IsEnvTrue("ANDROID_TEMPORARILY_ALLOW_WEVERYTHING") { 45 ctx.PropertyErrorf(prop, "-Weverything is not allowed in Android.bp files. "+ 46 "Build with `m ANDROID_TEMPORARILY_ALLOW_WEVERYTHING=true` to experiment locally with -Weverything.") 47 } 48 } else if strings.HasPrefix(flag, "-target ") || strings.HasPrefix(flag, "--target ") || 49 strings.HasPrefix(flag, "-target=") || strings.HasPrefix(flag, "--target=") { 50 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use the correct target soong rule.", flag) 51 } else if strings.Contains(flag, " ") { 52 args := strings.Split(flag, " ") 53 if args[0] == "-include" { 54 if len(args) > 2 { 55 ctx.PropertyErrorf(prop, "`-include` only takes one argument: `%s`", flag) 56 } 57 path := filepath.Clean(args[1]) 58 if strings.HasPrefix("/", path) { 59 ctx.PropertyErrorf(prop, "Path must not be an absolute path: %s", flag) 60 } else if strings.HasPrefix("../", path) { 61 ctx.PropertyErrorf(prop, "Path must not start with `../`: `%s`. Use include_dirs to -include from a different directory", flag) 62 } 63 } else if args[0] == "-mllvm" { 64 if len(args) > 2 { 65 ctx.PropertyErrorf(prop, "`-mllvm` only takes one argument: `%s`", flag) 66 } 67 } else if args[0] == "-Xclang" { 68 if len(args) > 2 { 69 ctx.PropertyErrorf(prop, "`-Xclang` only takes one argument: `%s`", flag) 70 } 71 } else if strings.HasPrefix(flag, "-D") && strings.Contains(flag, "=") { 72 // Do nothing in this case. 73 // For now, we allow space characters in -DNAME=def form to allow use cases 74 // like -DNAME="value with string". Later, this check should be done more 75 // correctly to prevent multi flag cases like -DNAME=value -O2. 76 } else { 77 ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) 78 } 79 } 80 } 81} 82 83// Check for bad ldflags and suggest alternatives. Only use this for flags 84// explicitly passed by the user, since these flags may be used internally. 85func CheckBadLinkerFlags(ctx BaseModuleContext, prop string, flags []string) { 86 for _, flag := range flags { 87 flag = strings.TrimSpace(flag) 88 89 if !strings.HasPrefix(flag, "-") { 90 ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) 91 } else if strings.HasPrefix(flag, "-l") { 92 if ctx.Host() { 93 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs or host_ldlibs instead", flag) 94 } else { 95 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs instead", flag) 96 } 97 } else if strings.HasPrefix(flag, "-L") { 98 ctx.PropertyErrorf(prop, "Bad flag: `%s` is not allowed", flag) 99 } else if strings.HasPrefix(flag, "-Wl,--version-script") { 100 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use version_script instead", flag) 101 } else if flag == "-T" || strings.HasPrefix(flag, "--script") { 102 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use linker_scripts instead", flag) 103 } else if flag == "--coverage" { 104 ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag) 105 } else if strings.Contains(flag, " ") { 106 args := strings.Split(flag, " ") 107 if args[0] == "-z" { 108 if len(args) > 2 { 109 ctx.PropertyErrorf(prop, "`-z` only takes one argument: `%s`", flag) 110 } 111 } else { 112 ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) 113 } 114 } 115 } 116} 117 118// Check for bad host_ldlibs 119func CheckBadHostLdlibs(ctx ModuleContext, prop string, flags []string) { 120 allowedLdlibs := ctx.toolchain().AvailableLibraries() 121 122 if !ctx.Host() { 123 panic("Invalid call to CheckBadHostLdlibs") 124 } 125 126 for _, flag := range flags { 127 flag = strings.TrimSpace(flag) 128 129 // TODO: Probably should just redo this property to prefix -l in Soong 130 if !strings.HasPrefix(flag, "-l") && !strings.HasPrefix(flag, "-framework") { 131 ctx.PropertyErrorf(prop, "Invalid flag: `%s`, must start with `-l` or `-framework`", flag) 132 } else if !inList(flag, allowedLdlibs) { 133 ctx.PropertyErrorf(prop, "Host library `%s` not available", flag) 134 } 135 } 136} 137 138// Check for bad clang tidy flags 139func CheckBadTidyFlags(ctx ModuleContext, prop string, flags []string) { 140 for _, flag := range flags { 141 flag = strings.TrimSpace(flag) 142 143 if !strings.HasPrefix(flag, "-") { 144 ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) 145 } else if strings.HasPrefix(flag, "-fix") { 146 ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, since it could cause multiple writes to the same source file", flag) 147 } else if strings.HasPrefix(flag, "-checks=") { 148 ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, use `tidy_checks` property instead", flag) 149 } else if strings.Contains(flag, " ") { 150 ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) 151 } 152 } 153} 154 155// Check for bad clang tidy checks 156func CheckBadTidyChecks(ctx ModuleContext, prop string, checks []string) { 157 for _, check := range checks { 158 if strings.Contains(check, " ") { 159 ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain spaces", check) 160 } else if strings.Contains(check, ",") { 161 ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain commas. Split each entry into it's own string instead", check) 162 } 163 } 164} 165