Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | // This file contains utility functions to check for bad or illegal cflags |
| 18 | // specified by a module |
| 19 | |
| 20 | import ( |
| 21 | "path/filepath" |
| 22 | "strings" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/cc/config" |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 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. |
Colin Cross | 76fada0 | 2016-07-27 10:31:13 -0700 | [diff] [blame] | 29 | func CheckBadCompilerFlags(ctx BaseModuleContext, prop string, flags []string) { |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 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) |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 37 | } else if inList(flag, config.IllegalFlags) { |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 38 | ctx.PropertyErrorf(prop, "Illegal flag `%s`", flag) |
Dan Willemsen | 17ff636 | 2017-02-14 13:03:54 -0800 | [diff] [blame] | 39 | } else if flag == "--coverage" { |
| 40 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag) |
Yi Kong | bc220ca | 2020-09-23 01:39:32 +0800 | [diff] [blame] | 41 | } else if flag == "-fwhole-program-vtables" { |
| 42 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use whole_program_vtables instead", flag) |
Pirama Arumuga Nainar | 50858a7 | 2024-08-27 05:54:05 +0000 | [diff] [blame] | 43 | } else if flag == "-gsplit-dwarf" { |
| 44 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, soong cannot track dependencies to split dwarf debuginfo", flag) |
Pirama Arumuga Nainar | 0cfbefb | 2024-08-30 18:57:22 +0000 | [diff] [blame] | 45 | } else if flag == "-fno-integrated-as" { |
| 46 | ctx.PropertyErrorf("Bad flag: `%s` is disallowed as it may invoke the `as` from the build host", flag) |
Colin Cross | 39ef52f | 2019-11-12 09:59:12 -0800 | [diff] [blame] | 47 | } else if flag == "-Weverything" { |
| 48 | if !ctx.Config().IsEnvTrue("ANDROID_TEMPORARILY_ALLOW_WEVERYTHING") { |
| 49 | ctx.PropertyErrorf(prop, "-Weverything is not allowed in Android.bp files. "+ |
| 50 | "Build with `m ANDROID_TEMPORARILY_ALLOW_WEVERYTHING=true` to experiment locally with -Weverything.") |
| 51 | } |
Colin Cross | 7c4a40a | 2024-07-08 10:50:16 -0700 | [diff] [blame] | 52 | } else if strings.HasPrefix(flag, "-target ") || strings.HasPrefix(flag, "--target ") || |
| 53 | strings.HasPrefix(flag, "-target=") || strings.HasPrefix(flag, "--target=") { |
AdityaK | f6fbaac | 2024-02-01 15:42:56 -0800 | [diff] [blame] | 54 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use the correct target soong rule.", flag) |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 55 | } else if strings.Contains(flag, " ") { |
| 56 | args := strings.Split(flag, " ") |
| 57 | if args[0] == "-include" { |
| 58 | if len(args) > 2 { |
| 59 | ctx.PropertyErrorf(prop, "`-include` only takes one argument: `%s`", flag) |
| 60 | } |
| 61 | path := filepath.Clean(args[1]) |
| 62 | if strings.HasPrefix("/", path) { |
| 63 | ctx.PropertyErrorf(prop, "Path must not be an absolute path: %s", flag) |
| 64 | } else if strings.HasPrefix("../", path) { |
| 65 | ctx.PropertyErrorf(prop, "Path must not start with `../`: `%s`. Use include_dirs to -include from a different directory", flag) |
| 66 | } |
Yi Kong | 6a0f506 | 2023-03-07 15:41:24 +0900 | [diff] [blame] | 67 | } else if args[0] == "-mllvm" { |
| 68 | if len(args) > 2 { |
| 69 | ctx.PropertyErrorf(prop, "`-mllvm` only takes one argument: `%s`", flag) |
| 70 | } |
Colin Cross | 7c4a40a | 2024-07-08 10:50:16 -0700 | [diff] [blame] | 71 | } else if args[0] == "-Xclang" { |
| 72 | if len(args) > 2 { |
| 73 | ctx.PropertyErrorf(prop, "`-Xclang` only takes one argument: `%s`", flag) |
| 74 | } |
Jiyong Park | d08b697 | 2017-09-26 10:50:54 +0900 | [diff] [blame] | 75 | } else if strings.HasPrefix(flag, "-D") && strings.Contains(flag, "=") { |
| 76 | // Do nothing in this case. |
| 77 | // For now, we allow space characters in -DNAME=def form to allow use cases |
| 78 | // like -DNAME="value with string". Later, this check should be done more |
| 79 | // correctly to prevent multi flag cases like -DNAME=value -O2. |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 80 | } else { |
| 81 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Check for bad ldflags and suggest alternatives. Only use this for flags |
| 88 | // explicitly passed by the user, since these flags may be used internally. |
Colin Cross | 76fada0 | 2016-07-27 10:31:13 -0700 | [diff] [blame] | 89 | func CheckBadLinkerFlags(ctx BaseModuleContext, prop string, flags []string) { |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 90 | for _, flag := range flags { |
| 91 | flag = strings.TrimSpace(flag) |
| 92 | |
| 93 | if !strings.HasPrefix(flag, "-") { |
| 94 | ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) |
| 95 | } else if strings.HasPrefix(flag, "-l") { |
| 96 | if ctx.Host() { |
| 97 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs or host_ldlibs instead", flag) |
| 98 | } else { |
| 99 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs instead", flag) |
| 100 | } |
| 101 | } else if strings.HasPrefix(flag, "-L") { |
| 102 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not allowed", flag) |
Dan Willemsen | 58f9bb1 | 2016-06-03 13:54:46 -0700 | [diff] [blame] | 103 | } else if strings.HasPrefix(flag, "-Wl,--version-script") { |
| 104 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use version_script instead", flag) |
David Brazdil | 958c957 | 2022-04-22 15:17:54 +0100 | [diff] [blame] | 105 | } else if flag == "-T" || strings.HasPrefix(flag, "--script") { |
| 106 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use linker_scripts instead", flag) |
Dan Willemsen | 17ff636 | 2017-02-14 13:03:54 -0800 | [diff] [blame] | 107 | } else if flag == "--coverage" { |
| 108 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag) |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 109 | } else if strings.Contains(flag, " ") { |
Colin Cross | 42d1ba2 | 2016-09-07 13:15:25 -0700 | [diff] [blame] | 110 | args := strings.Split(flag, " ") |
| 111 | if args[0] == "-z" { |
| 112 | if len(args) > 2 { |
| 113 | ctx.PropertyErrorf(prop, "`-z` only takes one argument: `%s`", flag) |
| 114 | } |
| 115 | } else { |
| 116 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) |
| 117 | } |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Check for bad host_ldlibs |
| 123 | func CheckBadHostLdlibs(ctx ModuleContext, prop string, flags []string) { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 124 | allowedLdlibs := ctx.toolchain().AvailableLibraries() |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 125 | |
| 126 | if !ctx.Host() { |
| 127 | panic("Invalid call to CheckBadHostLdlibs") |
| 128 | } |
| 129 | |
| 130 | for _, flag := range flags { |
| 131 | flag = strings.TrimSpace(flag) |
| 132 | |
| 133 | // TODO: Probably should just redo this property to prefix -l in Soong |
Josh Gao | 92da2b5 | 2016-09-01 11:51:27 -0700 | [diff] [blame] | 134 | if !strings.HasPrefix(flag, "-l") && !strings.HasPrefix(flag, "-framework") { |
| 135 | ctx.PropertyErrorf(prop, "Invalid flag: `%s`, must start with `-l` or `-framework`", flag) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 136 | } else if !inList(flag, allowedLdlibs) { |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 137 | ctx.PropertyErrorf(prop, "Host library `%s` not available", flag) |
| 138 | } |
| 139 | } |
| 140 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 141 | |
| 142 | // Check for bad clang tidy flags |
| 143 | func CheckBadTidyFlags(ctx ModuleContext, prop string, flags []string) { |
| 144 | for _, flag := range flags { |
| 145 | flag = strings.TrimSpace(flag) |
| 146 | |
| 147 | if !strings.HasPrefix(flag, "-") { |
| 148 | ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) |
| 149 | } else if strings.HasPrefix(flag, "-fix") { |
| 150 | ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, since it could cause multiple writes to the same source file", flag) |
| 151 | } else if strings.HasPrefix(flag, "-checks=") { |
| 152 | ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, use `tidy_checks` property instead", flag) |
| 153 | } else if strings.Contains(flag, " ") { |
| 154 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Check for bad clang tidy checks |
| 160 | func CheckBadTidyChecks(ctx ModuleContext, prop string, checks []string) { |
| 161 | for _, check := range checks { |
| 162 | if strings.Contains(check, " ") { |
| 163 | ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain spaces", check) |
| 164 | } else if strings.Contains(check, ",") { |
| 165 | ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain commas. Split each entry into it's own string instead", check) |
| 166 | } |
| 167 | } |
| 168 | } |