1*333d2b36SAndroid Build Coastguard Worker// Copyright 2016 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage cc 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Worker// This file contains utility functions to check for bad or illegal cflags 18*333d2b36SAndroid Build Coastguard Worker// specified by a module 19*333d2b36SAndroid Build Coastguard Worker 20*333d2b36SAndroid Build Coastguard Workerimport ( 21*333d2b36SAndroid Build Coastguard Worker "path/filepath" 22*333d2b36SAndroid Build Coastguard Worker "strings" 23*333d2b36SAndroid Build Coastguard Worker 24*333d2b36SAndroid Build Coastguard Worker "android/soong/cc/config" 25*333d2b36SAndroid Build Coastguard Worker) 26*333d2b36SAndroid Build Coastguard Worker 27*333d2b36SAndroid Build Coastguard Worker// Check for invalid c/conly/cpp/asflags and suggest alternatives. Only use this 28*333d2b36SAndroid Build Coastguard Worker// for flags explicitly passed by the user, since these flags may be used internally. 29*333d2b36SAndroid Build Coastguard Workerfunc CheckBadCompilerFlags(ctx BaseModuleContext, prop string, flags []string) { 30*333d2b36SAndroid Build Coastguard Worker for _, flag := range flags { 31*333d2b36SAndroid Build Coastguard Worker flag = strings.TrimSpace(flag) 32*333d2b36SAndroid Build Coastguard Worker 33*333d2b36SAndroid Build Coastguard Worker if !strings.HasPrefix(flag, "-") { 34*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) 35*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-I") || strings.HasPrefix(flag, "-isystem") { 36*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag `%s`, use local_include_dirs or include_dirs instead", flag) 37*333d2b36SAndroid Build Coastguard Worker } else if inList(flag, config.IllegalFlags) { 38*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Illegal flag `%s`", flag) 39*333d2b36SAndroid Build Coastguard Worker } else if flag == "--coverage" { 40*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag) 41*333d2b36SAndroid Build Coastguard Worker } else if flag == "-fwhole-program-vtables" { 42*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use whole_program_vtables instead", flag) 43*333d2b36SAndroid Build Coastguard Worker } else if flag == "-gsplit-dwarf" { 44*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, soong cannot track dependencies to split dwarf debuginfo", flag) 45*333d2b36SAndroid Build Coastguard Worker } else if flag == "-fno-integrated-as" { 46*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("Bad flag: `%s` is disallowed as it may invoke the `as` from the build host", flag) 47*333d2b36SAndroid Build Coastguard Worker } else if flag == "-Weverything" { 48*333d2b36SAndroid Build Coastguard Worker if !ctx.Config().IsEnvTrue("ANDROID_TEMPORARILY_ALLOW_WEVERYTHING") { 49*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "-Weverything is not allowed in Android.bp files. "+ 50*333d2b36SAndroid Build Coastguard Worker "Build with `m ANDROID_TEMPORARILY_ALLOW_WEVERYTHING=true` to experiment locally with -Weverything.") 51*333d2b36SAndroid Build Coastguard Worker } 52*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-target ") || strings.HasPrefix(flag, "--target ") || 53*333d2b36SAndroid Build Coastguard Worker strings.HasPrefix(flag, "-target=") || strings.HasPrefix(flag, "--target=") { 54*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use the correct target soong rule.", flag) 55*333d2b36SAndroid Build Coastguard Worker } else if strings.Contains(flag, " ") { 56*333d2b36SAndroid Build Coastguard Worker args := strings.Split(flag, " ") 57*333d2b36SAndroid Build Coastguard Worker if args[0] == "-include" { 58*333d2b36SAndroid Build Coastguard Worker if len(args) > 2 { 59*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "`-include` only takes one argument: `%s`", flag) 60*333d2b36SAndroid Build Coastguard Worker } 61*333d2b36SAndroid Build Coastguard Worker path := filepath.Clean(args[1]) 62*333d2b36SAndroid Build Coastguard Worker if strings.HasPrefix("/", path) { 63*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Path must not be an absolute path: %s", flag) 64*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix("../", path) { 65*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Path must not start with `../`: `%s`. Use include_dirs to -include from a different directory", flag) 66*333d2b36SAndroid Build Coastguard Worker } 67*333d2b36SAndroid Build Coastguard Worker } else if args[0] == "-mllvm" { 68*333d2b36SAndroid Build Coastguard Worker if len(args) > 2 { 69*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "`-mllvm` only takes one argument: `%s`", flag) 70*333d2b36SAndroid Build Coastguard Worker } 71*333d2b36SAndroid Build Coastguard Worker } else if args[0] == "-Xclang" { 72*333d2b36SAndroid Build Coastguard Worker if len(args) > 2 { 73*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "`-Xclang` only takes one argument: `%s`", flag) 74*333d2b36SAndroid Build Coastguard Worker } 75*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-D") && strings.Contains(flag, "=") { 76*333d2b36SAndroid Build Coastguard Worker // Do nothing in this case. 77*333d2b36SAndroid Build Coastguard Worker // For now, we allow space characters in -DNAME=def form to allow use cases 78*333d2b36SAndroid Build Coastguard Worker // like -DNAME="value with string". Later, this check should be done more 79*333d2b36SAndroid Build Coastguard Worker // correctly to prevent multi flag cases like -DNAME=value -O2. 80*333d2b36SAndroid Build Coastguard Worker } else { 81*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) 82*333d2b36SAndroid Build Coastguard Worker } 83*333d2b36SAndroid Build Coastguard Worker } 84*333d2b36SAndroid Build Coastguard Worker } 85*333d2b36SAndroid Build Coastguard Worker} 86*333d2b36SAndroid Build Coastguard Worker 87*333d2b36SAndroid Build Coastguard Worker// Check for bad ldflags and suggest alternatives. Only use this for flags 88*333d2b36SAndroid Build Coastguard Worker// explicitly passed by the user, since these flags may be used internally. 89*333d2b36SAndroid Build Coastguard Workerfunc CheckBadLinkerFlags(ctx BaseModuleContext, prop string, flags []string) { 90*333d2b36SAndroid Build Coastguard Worker for _, flag := range flags { 91*333d2b36SAndroid Build Coastguard Worker flag = strings.TrimSpace(flag) 92*333d2b36SAndroid Build Coastguard Worker 93*333d2b36SAndroid Build Coastguard Worker if !strings.HasPrefix(flag, "-") { 94*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) 95*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-l") { 96*333d2b36SAndroid Build Coastguard Worker if ctx.Host() { 97*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs or host_ldlibs instead", flag) 98*333d2b36SAndroid Build Coastguard Worker } else { 99*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs instead", flag) 100*333d2b36SAndroid Build Coastguard Worker } 101*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-L") { 102*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s` is not allowed", flag) 103*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-Wl,--version-script") { 104*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use version_script instead", flag) 105*333d2b36SAndroid Build Coastguard Worker } else if flag == "-T" || strings.HasPrefix(flag, "--script") { 106*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use linker_scripts instead", flag) 107*333d2b36SAndroid Build Coastguard Worker } else if flag == "--coverage" { 108*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag) 109*333d2b36SAndroid Build Coastguard Worker } else if strings.Contains(flag, " ") { 110*333d2b36SAndroid Build Coastguard Worker args := strings.Split(flag, " ") 111*333d2b36SAndroid Build Coastguard Worker if args[0] == "-z" { 112*333d2b36SAndroid Build Coastguard Worker if len(args) > 2 { 113*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "`-z` only takes one argument: `%s`", flag) 114*333d2b36SAndroid Build Coastguard Worker } 115*333d2b36SAndroid Build Coastguard Worker } else { 116*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) 117*333d2b36SAndroid Build Coastguard Worker } 118*333d2b36SAndroid Build Coastguard Worker } 119*333d2b36SAndroid Build Coastguard Worker } 120*333d2b36SAndroid Build Coastguard Worker} 121*333d2b36SAndroid Build Coastguard Worker 122*333d2b36SAndroid Build Coastguard Worker// Check for bad host_ldlibs 123*333d2b36SAndroid Build Coastguard Workerfunc CheckBadHostLdlibs(ctx ModuleContext, prop string, flags []string) { 124*333d2b36SAndroid Build Coastguard Worker allowedLdlibs := ctx.toolchain().AvailableLibraries() 125*333d2b36SAndroid Build Coastguard Worker 126*333d2b36SAndroid Build Coastguard Worker if !ctx.Host() { 127*333d2b36SAndroid Build Coastguard Worker panic("Invalid call to CheckBadHostLdlibs") 128*333d2b36SAndroid Build Coastguard Worker } 129*333d2b36SAndroid Build Coastguard Worker 130*333d2b36SAndroid Build Coastguard Worker for _, flag := range flags { 131*333d2b36SAndroid Build Coastguard Worker flag = strings.TrimSpace(flag) 132*333d2b36SAndroid Build Coastguard Worker 133*333d2b36SAndroid Build Coastguard Worker // TODO: Probably should just redo this property to prefix -l in Soong 134*333d2b36SAndroid Build Coastguard Worker if !strings.HasPrefix(flag, "-l") && !strings.HasPrefix(flag, "-framework") { 135*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Invalid flag: `%s`, must start with `-l` or `-framework`", flag) 136*333d2b36SAndroid Build Coastguard Worker } else if !inList(flag, allowedLdlibs) { 137*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Host library `%s` not available", flag) 138*333d2b36SAndroid Build Coastguard Worker } 139*333d2b36SAndroid Build Coastguard Worker } 140*333d2b36SAndroid Build Coastguard Worker} 141*333d2b36SAndroid Build Coastguard Worker 142*333d2b36SAndroid Build Coastguard Worker// Check for bad clang tidy flags 143*333d2b36SAndroid Build Coastguard Workerfunc CheckBadTidyFlags(ctx ModuleContext, prop string, flags []string) { 144*333d2b36SAndroid Build Coastguard Worker for _, flag := range flags { 145*333d2b36SAndroid Build Coastguard Worker flag = strings.TrimSpace(flag) 146*333d2b36SAndroid Build Coastguard Worker 147*333d2b36SAndroid Build Coastguard Worker if !strings.HasPrefix(flag, "-") { 148*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) 149*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-fix") { 150*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, since it could cause multiple writes to the same source file", flag) 151*333d2b36SAndroid Build Coastguard Worker } else if strings.HasPrefix(flag, "-checks=") { 152*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, use `tidy_checks` property instead", flag) 153*333d2b36SAndroid Build Coastguard Worker } else if strings.Contains(flag, " ") { 154*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) 155*333d2b36SAndroid Build Coastguard Worker } 156*333d2b36SAndroid Build Coastguard Worker } 157*333d2b36SAndroid Build Coastguard Worker} 158*333d2b36SAndroid Build Coastguard Worker 159*333d2b36SAndroid Build Coastguard Worker// Check for bad clang tidy checks 160*333d2b36SAndroid Build Coastguard Workerfunc CheckBadTidyChecks(ctx ModuleContext, prop string, checks []string) { 161*333d2b36SAndroid Build Coastguard Worker for _, check := range checks { 162*333d2b36SAndroid Build Coastguard Worker if strings.Contains(check, " ") { 163*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain spaces", check) 164*333d2b36SAndroid Build Coastguard Worker } else if strings.Contains(check, ",") { 165*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain commas. Split each entry into it's own string instead", check) 166*333d2b36SAndroid Build Coastguard Worker } 167*333d2b36SAndroid Build Coastguard Worker } 168*333d2b36SAndroid Build Coastguard Worker} 169