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) |
| 39 | } else if strings.Contains(flag, " ") { |
| 40 | args := strings.Split(flag, " ") |
| 41 | if args[0] == "-include" { |
| 42 | if len(args) > 2 { |
| 43 | ctx.PropertyErrorf(prop, "`-include` only takes one argument: `%s`", flag) |
| 44 | } |
| 45 | path := filepath.Clean(args[1]) |
| 46 | if strings.HasPrefix("/", path) { |
| 47 | ctx.PropertyErrorf(prop, "Path must not be an absolute path: %s", flag) |
| 48 | } else if strings.HasPrefix("../", path) { |
| 49 | ctx.PropertyErrorf(prop, "Path must not start with `../`: `%s`. Use include_dirs to -include from a different directory", flag) |
| 50 | } |
| 51 | } else { |
| 52 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Check for bad ldflags and suggest alternatives. Only use this for flags |
| 59 | // explicitly passed by the user, since these flags may be used internally. |
Colin Cross | 76fada0 | 2016-07-27 10:31:13 -0700 | [diff] [blame] | 60 | func CheckBadLinkerFlags(ctx BaseModuleContext, prop string, flags []string) { |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 61 | for _, flag := range flags { |
| 62 | flag = strings.TrimSpace(flag) |
| 63 | |
| 64 | if !strings.HasPrefix(flag, "-") { |
| 65 | ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag) |
| 66 | } else if strings.HasPrefix(flag, "-l") { |
| 67 | if ctx.Host() { |
| 68 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs or host_ldlibs instead", flag) |
| 69 | } else { |
| 70 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs instead", flag) |
| 71 | } |
| 72 | } else if strings.HasPrefix(flag, "-L") { |
| 73 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not allowed", flag) |
Dan Willemsen | 58f9bb1 | 2016-06-03 13:54:46 -0700 | [diff] [blame] | 74 | } else if strings.HasPrefix(flag, "-Wl,--version-script") { |
| 75 | ctx.PropertyErrorf(prop, "Bad flag: `%s`, use version_script instead", flag) |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 76 | } else if strings.Contains(flag, " ") { |
Colin Cross | 42d1ba2 | 2016-09-07 13:15:25 -0700 | [diff] [blame^] | 77 | args := strings.Split(flag, " ") |
| 78 | if args[0] == "-z" { |
| 79 | if len(args) > 2 { |
| 80 | ctx.PropertyErrorf(prop, "`-z` only takes one argument: `%s`", flag) |
| 81 | } |
| 82 | } else { |
| 83 | ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag) |
| 84 | } |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Check for bad host_ldlibs |
| 90 | func CheckBadHostLdlibs(ctx ModuleContext, prop string, flags []string) { |
| 91 | allowed_ldlibs := ctx.toolchain().AvailableLibraries() |
| 92 | |
| 93 | if !ctx.Host() { |
| 94 | panic("Invalid call to CheckBadHostLdlibs") |
| 95 | } |
| 96 | |
| 97 | for _, flag := range flags { |
| 98 | flag = strings.TrimSpace(flag) |
| 99 | |
| 100 | // TODO: Probably should just redo this property to prefix -l in Soong |
Josh Gao | 92da2b5 | 2016-09-01 11:51:27 -0700 | [diff] [blame] | 101 | if !strings.HasPrefix(flag, "-l") && !strings.HasPrefix(flag, "-framework") { |
| 102 | ctx.PropertyErrorf(prop, "Invalid flag: `%s`, must start with `-l` or `-framework`", flag) |
Dan Willemsen | 20acc5c | 2016-05-25 14:47:21 -0700 | [diff] [blame] | 103 | } else if !inList(flag, allowed_ldlibs) { |
| 104 | ctx.PropertyErrorf(prop, "Host library `%s` not available", flag) |
| 105 | } |
| 106 | } |
| 107 | } |