1// Copyright 2017 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 androidmk 16 17import ( 18 "fmt" 19 "sort" 20 "strconv" 21 "strings" 22 23 mkparser "android/soong/androidmk/parser" 24 25 bpparser "github.com/google/blueprint/parser" 26) 27 28const ( 29 clearVarsPath = "__android_mk_clear_vars" 30 includeIgnoredPath = "__android_mk_include_ignored" 31) 32 33type bpVariable struct { 34 name string 35 variableType bpparser.Type 36} 37 38type variableAssignmentContext struct { 39 file *bpFile 40 prefix string 41 mkvalue *mkparser.MakeString 42 append bool 43} 44 45var trueValue = &bpparser.Bool{ 46 Value: true, 47} 48 49var rewriteProperties = map[string](func(variableAssignmentContext) error){ 50 // custom functions 51 "LOCAL_32_BIT_ONLY": local32BitOnly, 52 "LOCAL_AIDL_INCLUDES": localAidlIncludes, 53 "LOCAL_ASSET_DIR": localizePathList("asset_dirs"), 54 "LOCAL_C_INCLUDES": localIncludeDirs, 55 "LOCAL_EXPORT_C_INCLUDE_DIRS": exportIncludeDirs, 56 "LOCAL_JARJAR_RULES": localizePath("jarjar_rules"), 57 "LOCAL_LDFLAGS": ldflags, 58 "LOCAL_MODULE_CLASS": prebuiltClass, 59 "LOCAL_MODULE_STEM": stem, 60 "LOCAL_MODULE_HOST_OS": hostOs, 61 "LOCAL_RESOURCE_DIR": localizePathList("resource_dirs"), 62 "LOCAL_NOTICE_FILE": localizePathList("android_license_files"), 63 "LOCAL_SANITIZE": sanitize(""), 64 "LOCAL_SANITIZE_DIAG": sanitize("diag."), 65 "LOCAL_STRIP_MODULE": strip(), 66 "LOCAL_CFLAGS": cflags, 67 "LOCAL_PROTOC_FLAGS": protoLocalIncludeDirs, 68 "LOCAL_PROTO_JAVA_OUTPUT_PARAMS": protoOutputParams, 69 "LOCAL_UNINSTALLABLE_MODULE": invert("installable"), 70 "LOCAL_PROGUARD_ENABLED": proguardEnabled, 71 "LOCAL_MODULE_PATH": prebuiltModulePath, 72 "LOCAL_REPLACE_PREBUILT_APK_INSTALLED": prebuiltPreprocessed, 73 74 "LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG": invert("auto_gen_config"), 75 76 // composite functions 77 "LOCAL_MODULE_TAGS": includeVariableIf(bpVariable{"tags", bpparser.ListType}, not(valueDumpEquals("optional"))), 78 79 // skip functions 80 "LOCAL_ADDITIONAL_DEPENDENCIES": skip, // TODO: check for only .mk files? 81 "LOCAL_CPP_EXTENSION": skip, 82 "LOCAL_MODULE_SUFFIX": skip, // TODO 83 "LOCAL_PATH": skip, // Nothing to do, except maybe avoid the "./" in paths? 84 "LOCAL_PRELINK_MODULE": skip, // Already phased out 85 "LOCAL_BUILT_MODULE_STEM": skip, 86 "LOCAL_USE_AAPT2": skip, // Always enabled in Soong 87 "LOCAL_JAR_EXCLUDE_FILES": skip, // Soong never excludes files from jars 88 89 "LOCAL_ANNOTATION_PROCESSOR_CLASSES": skip, // Soong gets the processor classes from the plugin 90 "LOCAL_CTS_TEST_PACKAGE": skip, // Obsolete 91 "LOCAL_XTS_TEST_PACKAGE": skip, // Obsolete 92 "LOCAL_JACK_ENABLED": skip, // Obselete 93 "LOCAL_JACK_FLAGS": skip, // Obselete 94} 95 96// adds a group of properties all having the same type 97func addStandardProperties(propertyType bpparser.Type, properties map[string]string) { 98 for key, val := range properties { 99 rewriteProperties[key] = includeVariable(bpVariable{val, propertyType}) 100 } 101} 102 103func init() { 104 addStandardProperties(bpparser.StringType, 105 map[string]string{ 106 "LOCAL_MODULE": "name", 107 "LOCAL_CXX_STL": "stl", 108 "LOCAL_MULTILIB": "compile_multilib", 109 "LOCAL_ARM_MODE_HACK": "instruction_set", 110 "LOCAL_SDK_VERSION": "sdk_version", 111 "LOCAL_MIN_SDK_VERSION": "min_sdk_version", 112 "LOCAL_TARGET_SDK_VERSION": "target_sdk_version", 113 "LOCAL_NDK_STL_VARIANT": "stl", 114 "LOCAL_JAR_MANIFEST": "manifest", 115 "LOCAL_CERTIFICATE": "certificate", 116 "LOCAL_CERTIFICATE_LINEAGE": "lineage", 117 "LOCAL_PACKAGE_NAME": "name", 118 "LOCAL_MODULE_RELATIVE_PATH": "relative_install_path", 119 "LOCAL_PROTOC_OPTIMIZE_TYPE": "proto.type", 120 "LOCAL_MODULE_OWNER": "owner", 121 "LOCAL_RENDERSCRIPT_TARGET_API": "renderscript.target_api", 122 "LOCAL_JAVA_LANGUAGE_VERSION": "java_version", 123 "LOCAL_INSTRUMENTATION_FOR": "instrumentation_for", 124 "LOCAL_MANIFEST_FILE": "manifest", 125 126 "LOCAL_DEX_PREOPT_PROFILE_CLASS_LISTING": "dex_preopt.profile", 127 "LOCAL_TEST_CONFIG": "test_config", 128 "LOCAL_RRO_THEME": "theme", 129 }) 130 addStandardProperties(bpparser.ListType, 131 map[string]string{ 132 "LOCAL_SRC_FILES": "srcs", 133 "LOCAL_SRC_FILES_EXCLUDE": "exclude_srcs", 134 "LOCAL_HEADER_LIBRARIES": "header_libs", 135 "LOCAL_SHARED_LIBRARIES": "shared_libs", 136 "LOCAL_STATIC_LIBRARIES": "static_libs", 137 "LOCAL_WHOLE_STATIC_LIBRARIES": "whole_static_libs", 138 "LOCAL_SYSTEM_SHARED_LIBRARIES": "system_shared_libs", 139 "LOCAL_USES_LIBRARIES": "uses_libs", 140 "LOCAL_OPTIONAL_USES_LIBRARIES": "optional_uses_libs", 141 "LOCAL_ASFLAGS": "asflags", 142 "LOCAL_CLANG_ASFLAGS": "clang_asflags", 143 "LOCAL_COMPATIBILITY_SUPPORT_FILES": "data", 144 "LOCAL_CONLYFLAGS": "conlyflags", 145 "LOCAL_CPPFLAGS": "cppflags", 146 "LOCAL_REQUIRED_MODULES": "required", 147 "LOCAL_HOST_REQUIRED_MODULES": "host_required", 148 "LOCAL_TARGET_REQUIRED_MODULES": "target_required", 149 "LOCAL_OVERRIDES_MODULES": "overrides", 150 "LOCAL_LDLIBS": "host_ldlibs", 151 "LOCAL_CLANG_CFLAGS": "clang_cflags", 152 "LOCAL_YACCFLAGS": "yacc.flags", 153 "LOCAL_SANITIZE_RECOVER": "sanitize.recover", 154 "LOCAL_SOONG_LOGTAGS_FILES": "logtags", 155 "LOCAL_EXPORT_HEADER_LIBRARY_HEADERS": "export_header_lib_headers", 156 "LOCAL_EXPORT_SHARED_LIBRARY_HEADERS": "export_shared_lib_headers", 157 "LOCAL_EXPORT_STATIC_LIBRARY_HEADERS": "export_static_lib_headers", 158 "LOCAL_INIT_RC": "init_rc", 159 "LOCAL_VINTF_FRAGMENTS": "vintf_fragments", 160 "LOCAL_TIDY_FLAGS": "tidy_flags", 161 // TODO: This is comma-separated, not space-separated 162 "LOCAL_TIDY_CHECKS": "tidy_checks", 163 "LOCAL_RENDERSCRIPT_INCLUDES": "renderscript.include_dirs", 164 "LOCAL_RENDERSCRIPT_FLAGS": "renderscript.flags", 165 166 "LOCAL_JAVA_RESOURCE_DIRS": "java_resource_dirs", 167 "LOCAL_JAVA_RESOURCE_FILES": "java_resources", 168 "LOCAL_JAVACFLAGS": "javacflags", 169 "LOCAL_ERROR_PRONE_FLAGS": "errorprone.javacflags", 170 "LOCAL_DX_FLAGS": "dxflags", 171 "LOCAL_JAVA_LIBRARIES": "libs", 172 "LOCAL_STATIC_JAVA_LIBRARIES": "static_libs", 173 "LOCAL_JNI_SHARED_LIBRARIES": "jni_libs", 174 "LOCAL_AAPT_FLAGS": "aaptflags", 175 "LOCAL_PACKAGE_SPLITS": "package_splits", 176 "LOCAL_COMPATIBILITY_SUITE": "test_suites", 177 "LOCAL_OVERRIDES_PACKAGES": "overrides", 178 179 "LOCAL_ANNOTATION_PROCESSORS": "plugins", 180 181 "LOCAL_PROGUARD_FLAGS": "optimize.proguard_flags", 182 "LOCAL_PROGUARD_FLAG_FILES": "optimize.proguard_flags_files", 183 184 // These will be rewritten to libs/static_libs by bpfix, after their presence is used to convert 185 // java_library_static to android_library. 186 "LOCAL_SHARED_ANDROID_LIBRARIES": "android_libs", 187 "LOCAL_STATIC_ANDROID_LIBRARIES": "android_static_libs", 188 "LOCAL_ADDITIONAL_CERTIFICATES": "additional_certificates", 189 190 // Jacoco filters: 191 "LOCAL_JACK_COVERAGE_INCLUDE_FILTER": "jacoco.include_filter", 192 "LOCAL_JACK_COVERAGE_EXCLUDE_FILTER": "jacoco.exclude_filter", 193 194 "LOCAL_FULL_LIBS_MANIFEST_FILES": "additional_manifests", 195 196 // will be rewrite later to "license_kinds:" by byfix 197 "LOCAL_LICENSE_KINDS": "android_license_kinds", 198 // will be removed later by byfix 199 // TODO: does this property matter in the license module? 200 "LOCAL_LICENSE_CONDITIONS": "android_license_conditions", 201 "LOCAL_GENERATED_SOURCES": "generated_sources", 202 }) 203 204 addStandardProperties(bpparser.BoolType, 205 map[string]string{ 206 // Bool properties 207 "LOCAL_IS_HOST_MODULE": "host", 208 "LOCAL_CLANG": "clang", 209 "LOCAL_FORCE_STATIC_EXECUTABLE": "static_executable", 210 "LOCAL_NATIVE_COVERAGE": "native_coverage", 211 "LOCAL_NO_CRT": "nocrt", 212 "LOCAL_ALLOW_UNDEFINED_SYMBOLS": "allow_undefined_symbols", 213 "LOCAL_RTTI_FLAG": "rtti", 214 "LOCAL_PACK_MODULE_RELOCATIONS": "pack_relocations", 215 "LOCAL_TIDY": "tidy", 216 "LOCAL_USE_CLANG_LLD": "use_clang_lld", 217 "LOCAL_PROPRIETARY_MODULE": "proprietary", 218 "LOCAL_VENDOR_MODULE": "vendor", 219 "LOCAL_ODM_MODULE": "device_specific", 220 "LOCAL_PRODUCT_MODULE": "product_specific", 221 "LOCAL_PRODUCT_SERVICES_MODULE": "product_specific", 222 "LOCAL_SYSTEM_EXT_MODULE": "system_ext_specific", 223 "LOCAL_EXPORT_PACKAGE_RESOURCES": "export_package_resources", 224 "LOCAL_PRIVILEGED_MODULE": "privileged", 225 "LOCAL_AAPT_INCLUDE_ALL_RESOURCES": "aapt_include_all_resources", 226 "LOCAL_DONT_MERGE_MANIFESTS": "dont_merge_manifests", 227 "LOCAL_USE_EMBEDDED_NATIVE_LIBS": "use_embedded_native_libs", 228 "LOCAL_USE_EMBEDDED_DEX": "use_embedded_dex", 229 230 "LOCAL_DEX_PREOPT": "dex_preopt.enabled", 231 "LOCAL_DEX_PREOPT_APP_IMAGE": "dex_preopt.app_image", 232 "LOCAL_DEX_PREOPT_GENERATE_PROFILE": "dex_preopt.profile_guided", 233 234 "LOCAL_PRIVATE_PLATFORM_APIS": "platform_apis", 235 "LOCAL_JETIFIER_ENABLED": "jetifier", 236 237 "LOCAL_IS_UNIT_TEST": "unit_test", 238 239 "LOCAL_ENFORCE_USES_LIBRARIES": "enforce_uses_libs", 240 241 "LOCAL_CHECK_ELF_FILES": "check_elf_files", 242 }) 243} 244 245type listSplitFunc func(bpparser.Expression) (string, bpparser.Expression, error) 246 247func emptyList(value bpparser.Expression) bool { 248 if list, ok := value.(*bpparser.List); ok { 249 return len(list.Values) == 0 250 } 251 return false 252} 253 254func splitBpList(val bpparser.Expression, keyFunc listSplitFunc) (lists map[string]bpparser.Expression, err error) { 255 lists = make(map[string]bpparser.Expression) 256 257 switch val := val.(type) { 258 case *bpparser.Operator: 259 listsA, err := splitBpList(val.Args[0], keyFunc) 260 if err != nil { 261 return nil, err 262 } 263 264 listsB, err := splitBpList(val.Args[1], keyFunc) 265 if err != nil { 266 return nil, err 267 } 268 269 for k, v := range listsA { 270 if !emptyList(v) { 271 lists[k] = v 272 } 273 } 274 275 for k, vB := range listsB { 276 if emptyList(vB) { 277 continue 278 } 279 280 if vA, ok := lists[k]; ok { 281 expression := val.Copy().(*bpparser.Operator) 282 expression.Args = [2]bpparser.Expression{vA, vB} 283 lists[k] = expression 284 } else { 285 lists[k] = vB 286 } 287 } 288 case *bpparser.Variable: 289 key, value, err := keyFunc(val) 290 if err != nil { 291 return nil, err 292 } 293 if value.Type() == bpparser.ListType { 294 lists[key] = value 295 } else { 296 lists[key] = &bpparser.List{ 297 Values: []bpparser.Expression{value}, 298 } 299 } 300 case *bpparser.List: 301 for _, v := range val.Values { 302 key, value, err := keyFunc(v) 303 if err != nil { 304 return nil, err 305 } 306 l := lists[key] 307 if l == nil { 308 l = &bpparser.List{} 309 } 310 l.(*bpparser.List).Values = append(l.(*bpparser.List).Values, value) 311 lists[key] = l 312 } 313 default: 314 panic(fmt.Errorf("unexpected type %t", val)) 315 } 316 317 return lists, nil 318} 319 320// classifyLocalOrGlobalPath tells whether a file path should be interpreted relative to the current module (local) 321// or relative to the root of the source checkout (global) 322func classifyLocalOrGlobalPath(value bpparser.Expression) (string, bpparser.Expression, error) { 323 switch v := value.(type) { 324 case *bpparser.Variable: 325 if v.Name == "LOCAL_PATH" { 326 return "local", &bpparser.String{ 327 Value: ".", 328 }, nil 329 } else { 330 // TODO: Should we split variables? 331 return "global", value, nil 332 } 333 case *bpparser.Operator: 334 if v.Type() != bpparser.StringType { 335 return "", nil, fmt.Errorf("classifyLocalOrGlobalPath expected a string, got %s", v.Type()) 336 } 337 338 if v.Operator != '+' { 339 return "global", value, nil 340 } 341 342 firstOperand := v.Args[0] 343 secondOperand := v.Args[1] 344 345 if _, ok := firstOperand.(*bpparser.Operator); ok { 346 return "global", value, nil 347 } 348 349 if variable, ok := firstOperand.(*bpparser.Variable); !ok || variable.Name != "LOCAL_PATH" { 350 return "global", value, nil 351 } 352 353 local := secondOperand 354 if s, ok := secondOperand.(*bpparser.String); ok { 355 if strings.HasPrefix(s.Value, "/") { 356 s.Value = s.Value[1:] 357 } 358 } 359 return "local", local, nil 360 case *bpparser.String: 361 return "global", value, nil 362 default: 363 return "", nil, fmt.Errorf("classifyLocalOrGlobalPath expected a string, got %s", v.Type()) 364 365 } 366} 367 368// splitAndAssign splits a Make list into components and then 369// creates the corresponding variable assignments. 370func splitAndAssign(ctx variableAssignmentContext, splitFunc listSplitFunc, namesByClassification map[string]string) error { 371 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType) 372 if err != nil { 373 return err 374 } 375 376 lists, err := splitBpList(val, splitFunc) 377 if err != nil { 378 return err 379 } 380 381 var classifications []string 382 for classification := range namesByClassification { 383 classifications = append(classifications, classification) 384 } 385 sort.Strings(classifications) 386 387 for _, nameClassification := range classifications { 388 name := namesByClassification[nameClassification] 389 if component, ok := lists[nameClassification]; ok && !emptyList(component) { 390 err = setVariable(ctx.file, ctx.append, ctx.prefix, name, component, true) 391 if err != nil { 392 return err 393 } 394 } 395 } 396 return nil 397} 398 399func localIncludeDirs(ctx variableAssignmentContext) error { 400 return splitAndAssign(ctx, classifyLocalOrGlobalPath, map[string]string{"global": "include_dirs", "local": "local_include_dirs"}) 401} 402 403func exportIncludeDirs(ctx variableAssignmentContext) error { 404 // Add any paths that could not be converted to local relative paths to export_include_dirs 405 // anyways, they will cause an error if they don't exist and can be fixed manually. 406 return splitAndAssign(ctx, classifyLocalOrGlobalPath, map[string]string{"global": "export_include_dirs", "local": "export_include_dirs"}) 407} 408 409func local32BitOnly(ctx variableAssignmentContext) error { 410 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.BoolType) 411 if err != nil { 412 return err 413 } 414 boolValue, ok := val.(*bpparser.Bool) 415 if !ok { 416 return fmt.Errorf("value should evaluate to boolean literal") 417 } 418 if boolValue.Value { 419 thirtyTwo := &bpparser.String{ 420 Value: "32", 421 } 422 return setVariable(ctx.file, false, ctx.prefix, "compile_multilib", thirtyTwo, true) 423 } 424 return nil 425} 426 427func localAidlIncludes(ctx variableAssignmentContext) error { 428 return splitAndAssign(ctx, classifyLocalOrGlobalPath, map[string]string{"global": "aidl.include_dirs", "local": "aidl.local_include_dirs"}) 429} 430 431func localizePathList(attribute string) func(ctx variableAssignmentContext) error { 432 return func(ctx variableAssignmentContext) error { 433 paths, err := localizePaths(ctx) 434 if err == nil { 435 err = setVariable(ctx.file, ctx.append, ctx.prefix, attribute, paths, true) 436 } 437 return err 438 } 439} 440 441func localizePath(attribute string) func(ctx variableAssignmentContext) error { 442 return func(ctx variableAssignmentContext) error { 443 paths, err := localizePaths(ctx) 444 if err == nil { 445 pathList, ok := paths.(*bpparser.List) 446 if !ok { 447 panic("Expected list") 448 } 449 switch len(pathList.Values) { 450 case 0: 451 err = setVariable(ctx.file, ctx.append, ctx.prefix, attribute, &bpparser.List{}, true) 452 case 1: 453 err = setVariable(ctx.file, ctx.append, ctx.prefix, attribute, pathList.Values[0], true) 454 default: 455 err = fmt.Errorf("Expected single value for %s", attribute) 456 } 457 } 458 return err 459 } 460} 461 462// Convert the "full" paths (that is, from the top of the source tree) to the relative one 463// (from the directory containing the blueprint file) and set given attribute to it. 464// This is needed for some of makefile variables (e.g., LOCAL_RESOURCE_DIR). 465// At the moment only the paths of the `$(LOCAL_PATH)/foo/bar` format can be converted 466// (to `foo/bar` in this case) as we cannot convert a literal path without 467// knowing makefiles's location in the source tree. We just issue a warning in the latter case. 468func localizePaths(ctx variableAssignmentContext) (bpparser.Expression, error) { 469 bpvalue, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType) 470 var result bpparser.Expression 471 if err != nil { 472 return result, err 473 } 474 classifiedPaths, err := splitBpList(bpvalue, classifyLocalOrGlobalPath) 475 if err != nil { 476 return result, err 477 } 478 for pathClass, path := range classifiedPaths { 479 switch pathClass { 480 case "local": 481 result = path 482 default: 483 err = fmt.Errorf("Only $(LOCAL_PATH)/.. values are allowed") 484 } 485 } 486 return result, err 487} 488 489func stem(ctx variableAssignmentContext) error { 490 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.StringType) 491 if err != nil { 492 return err 493 } 494 varName := "stem" 495 496 if exp, ok := val.(*bpparser.Operator); ok && exp.Operator == '+' { 497 if variable, ok := exp.Args[0].(*bpparser.Variable); ok && variable.Name == "LOCAL_MODULE" { 498 varName = "suffix" 499 val = exp.Args[1] 500 } 501 } 502 503 return setVariable(ctx.file, ctx.append, ctx.prefix, varName, val, true) 504} 505 506func hostOs(ctx variableAssignmentContext) error { 507 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType) 508 if err != nil { 509 return err 510 } 511 512 inList := func(s string) bool { 513 for _, v := range val.(*bpparser.List).Values { 514 if v.(*bpparser.String).Value == s { 515 return true 516 } 517 } 518 return false 519 } 520 521 falseValue := &bpparser.Bool{ 522 Value: false, 523 } 524 525 if inList("windows") { 526 err = setVariable(ctx.file, ctx.append, "target.windows", "enabled", trueValue, true) 527 } 528 529 if !inList("linux") && err == nil { 530 err = setVariable(ctx.file, ctx.append, "target.linux_glibc", "enabled", falseValue, true) 531 } 532 533 if !inList("darwin") && err == nil { 534 err = setVariable(ctx.file, ctx.append, "target.darwin", "enabled", falseValue, true) 535 } 536 537 return err 538} 539 540func sanitize(sub string) func(ctx variableAssignmentContext) error { 541 return func(ctx variableAssignmentContext) error { 542 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType) 543 if err != nil { 544 return err 545 } 546 547 if _, ok := val.(*bpparser.List); !ok { 548 return fmt.Errorf("unsupported sanitize expression") 549 } 550 551 misc := &bpparser.List{} 552 553 for _, v := range val.(*bpparser.List).Values { 554 switch v := v.(type) { 555 case *bpparser.Variable, *bpparser.Operator: 556 ctx.file.errorf(ctx.mkvalue, "unsupported sanitize expression") 557 case *bpparser.String: 558 switch v.Value { 559 case "never", "address", "fuzzer", "thread", "undefined", "cfi": 560 bpTrue := &bpparser.Bool{ 561 Value: true, 562 } 563 err = setVariable(ctx.file, false, ctx.prefix, "sanitize."+sub+v.Value, bpTrue, true) 564 if err != nil { 565 return err 566 } 567 default: 568 misc.Values = append(misc.Values, v) 569 } 570 default: 571 return fmt.Errorf("sanitize expected a string, got %s", v.Type()) 572 } 573 } 574 575 if len(misc.Values) > 0 { 576 err = setVariable(ctx.file, false, ctx.prefix, "sanitize."+sub+"misc_undefined", misc, true) 577 if err != nil { 578 return err 579 } 580 } 581 582 return err 583 } 584} 585 586func strip() func(ctx variableAssignmentContext) error { 587 return func(ctx variableAssignmentContext) error { 588 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.StringType) 589 if err != nil { 590 return err 591 } 592 593 if _, ok := val.(*bpparser.String); !ok { 594 return fmt.Errorf("unsupported strip expression") 595 } 596 597 bpTrue := &bpparser.Bool{ 598 Value: true, 599 } 600 v := val.(*bpparser.String).Value 601 sub := (map[string]string{"false": "none", "true": "all", "keep_symbols": "keep_symbols"})[v] 602 if sub == "" { 603 return fmt.Errorf("unexpected strip option: %s", v) 604 } 605 return setVariable(ctx.file, false, ctx.prefix, "strip."+sub, bpTrue, true) 606 } 607} 608 609func prebuiltClass(ctx variableAssignmentContext) error { 610 class := ctx.mkvalue.Value(ctx.file.scope) 611 if _, ok := prebuiltTypes[class]; ok { 612 ctx.file.scope.Set("BUILD_PREBUILT", class) 613 } else { 614 // reset to default 615 ctx.file.scope.Set("BUILD_PREBUILT", "prebuilt") 616 } 617 return nil 618} 619 620func makeBlueprintStringAssignment(file *bpFile, prefix string, suffix string, value string) error { 621 val, err := makeVariableToBlueprint(file, mkparser.SimpleMakeString(value, mkparser.NoPos), bpparser.StringType) 622 if err == nil { 623 err = setVariable(file, false, prefix, suffix, val, true) 624 } 625 return err 626} 627 628// Assigns a given boolean value to a given variable in the result bp file. See 629// setVariable documentation for more information about prefix and name. 630func makeBlueprintBoolAssignment(ctx variableAssignmentContext, prefix, name string, value bool) error { 631 expressionValue, err := stringToBoolValue(strconv.FormatBool(value)) 632 if err == nil { 633 err = setVariable(ctx.file, false, prefix, name, expressionValue, true) 634 } 635 return err 636} 637 638// If variable is a literal variable name, return the name, otherwise return "" 639func varLiteralName(variable mkparser.Variable) string { 640 if len(variable.Name.Variables) == 0 { 641 return variable.Name.Strings[0] 642 } 643 return "" 644} 645 646func prebuiltModulePath(ctx variableAssignmentContext) error { 647 // Cannot handle appending 648 if ctx.append { 649 return fmt.Errorf("Cannot handle appending to LOCAL_MODULE_PATH") 650 } 651 // Analyze value in order to set the correct values for the 'device_specific', 652 // 'product_specific', 'system_ext_specific' 'vendor'/'soc_specific', 653 // 'system_ext_specific' attribute. Two cases are allowed: 654 // $(VAR)/<literal-value> 655 // $(PRODUCT_OUT)/$(TARGET_COPY_OUT_VENDOR)/<literal-value> 656 // The last case is equivalent to $(TARGET_OUT_VENDOR)/<literal-value> 657 // Map the variable name if present to `local_module_path_var` 658 // Map literal-path to local_module_path_fixed 659 varname := "" 660 fixed := "" 661 val := ctx.mkvalue 662 663 if len(val.Variables) == 1 && varLiteralName(val.Variables[0]) != "" && len(val.Strings) == 2 && val.Strings[0] == "" { 664 if varLiteralName(val.Variables[0]) == "PRODUCT_OUT" && val.Strings[1] == "/system/priv-app" { 665 return makeBlueprintBoolAssignment(ctx, "", "privileged", true) 666 } 667 fixed = val.Strings[1] 668 varname = val.Variables[0].Name.Strings[0] 669 // TARGET_OUT_OPTIONAL_EXECUTABLES puts the artifact in xbin, which is 670 // deprecated. TARGET_OUT_DATA_APPS install location will be handled 671 // automatically by Soong 672 if varname == "TARGET_OUT_OPTIONAL_EXECUTABLES" || varname == "TARGET_OUT_DATA_APPS" { 673 return nil 674 } 675 } else if len(val.Variables) == 2 && varLiteralName(val.Variables[0]) == "PRODUCT_OUT" && varLiteralName(val.Variables[1]) == "TARGET_COPY_OUT_VENDOR" && 676 len(val.Strings) == 3 && val.Strings[0] == "" && val.Strings[1] == "/" { 677 fixed = val.Strings[2] 678 varname = "TARGET_OUT_VENDOR" 679 } else { 680 return fmt.Errorf("LOCAL_MODULE_PATH value should start with $(<some-varaible>)/ or $(PRODUCT_OUT)/$(TARGET_COPY_VENDOR)/") 681 } 682 err := makeBlueprintStringAssignment(ctx.file, "local_module_path", "var", varname) 683 if err == nil && fixed != "" { 684 err = makeBlueprintStringAssignment(ctx.file, "local_module_path", "fixed", fixed) 685 } 686 return err 687} 688 689func ldflags(ctx variableAssignmentContext) error { 690 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType) 691 if err != nil { 692 return err 693 } 694 695 lists, err := splitBpList(val, func(value bpparser.Expression) (string, bpparser.Expression, error) { 696 // Anything other than "-Wl,--version_script," + LOCAL_PATH + "<path>" matches ldflags 697 exp1, ok := value.(*bpparser.Operator) 698 if !ok { 699 return "ldflags", value, nil 700 } 701 702 exp2, ok := exp1.Args[0].(*bpparser.Operator) 703 if !ok { 704 return "ldflags", value, nil 705 } 706 707 if s, ok := exp2.Args[0].(*bpparser.String); !ok || s.Value != "-Wl,--version-script," { 708 return "ldflags", value, nil 709 } 710 711 if v, ok := exp2.Args[1].(*bpparser.Variable); !ok || v.Name != "LOCAL_PATH" { 712 ctx.file.errorf(ctx.mkvalue, "Unrecognized version-script") 713 return "ldflags", value, nil 714 } 715 716 s, ok := exp1.Args[1].(*bpparser.String) 717 if !ok { 718 ctx.file.errorf(ctx.mkvalue, "Unrecognized version-script") 719 return "ldflags", value, nil 720 } 721 722 s.Value = strings.TrimPrefix(s.Value, "/") 723 724 return "version", s, nil 725 }) 726 if err != nil { 727 return err 728 } 729 730 if ldflags, ok := lists["ldflags"]; ok && !emptyList(ldflags) { 731 err = setVariable(ctx.file, ctx.append, ctx.prefix, "ldflags", ldflags, true) 732 if err != nil { 733 return err 734 } 735 } 736 737 if version_script, ok := lists["version"]; ok && !emptyList(version_script) { 738 if len(version_script.(*bpparser.List).Values) > 1 { 739 ctx.file.errorf(ctx.mkvalue, "multiple version scripts found?") 740 } 741 err = setVariable(ctx.file, false, ctx.prefix, "version_script", version_script.(*bpparser.List).Values[0], true) 742 if err != nil { 743 return err 744 } 745 } 746 747 return nil 748} 749 750func prebuiltPreprocessed(ctx variableAssignmentContext) error { 751 ctx.mkvalue = ctx.mkvalue.Clone() 752 return setVariable(ctx.file, false, ctx.prefix, "preprocessed", trueValue, true) 753} 754 755func cflags(ctx variableAssignmentContext) error { 756 // The Soong replacement for CFLAGS doesn't need the same extra escaped quotes that were present in Make 757 ctx.mkvalue = ctx.mkvalue.Clone() 758 ctx.mkvalue.ReplaceLiteral(`\"`, `"`) 759 return includeVariableNow(bpVariable{"cflags", bpparser.ListType}, ctx) 760} 761 762func protoOutputParams(ctx variableAssignmentContext) error { 763 // The Soong replacement for LOCAL_PROTO_JAVA_OUTPUT_PARAMS doesn't need "," 764 ctx.mkvalue = ctx.mkvalue.Clone() 765 ctx.mkvalue.ReplaceLiteral(`, `, ` `) 766 return includeVariableNow(bpVariable{"proto.output_params", bpparser.ListType}, ctx) 767} 768 769func protoLocalIncludeDirs(ctx variableAssignmentContext) error { 770 // The Soong replacement for LOCAL_PROTOC_FLAGS includes "--proto_path=$(LOCAL_PATH)/..." 771 ctx.mkvalue = ctx.mkvalue.Clone() 772 if len(ctx.mkvalue.Strings) >= 1 && strings.Contains(ctx.mkvalue.Strings[0], "--proto_path=") { 773 ctx.mkvalue.Strings[0] = strings.Replace(ctx.mkvalue.Strings[0], "--proto_path=", "", 1) 774 paths, err := localizePaths(ctx) 775 if err == nil { 776 err = setVariable(ctx.file, ctx.append, ctx.prefix, "proto.local_include_dirs", paths, true) 777 } 778 return err 779 } 780 return fmt.Errorf("Currently LOCAL_PROTOC_FLAGS only support with value '--proto_path=$(LOCAL_PATH)/...'") 781} 782 783func proguardEnabled(ctx variableAssignmentContext) error { 784 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.ListType) 785 if err != nil { 786 return err 787 } 788 789 list, ok := val.(*bpparser.List) 790 if !ok { 791 return fmt.Errorf("unsupported proguard expression") 792 } 793 794 set := func(prop string, value bool) { 795 bpValue := &bpparser.Bool{ 796 Value: value, 797 } 798 setVariable(ctx.file, false, ctx.prefix, prop, bpValue, true) 799 } 800 801 enable := false 802 803 for _, v := range list.Values { 804 s, ok := v.(*bpparser.String) 805 if !ok { 806 return fmt.Errorf("unsupported proguard expression") 807 } 808 809 switch s.Value { 810 case "disabled": 811 set("optimize.enabled", false) 812 case "obfuscation": 813 enable = true 814 set("optimize.obfuscate", true) 815 case "optimization": 816 enable = true 817 set("optimize.optimize", true) 818 case "full": 819 enable = true 820 case "custom": 821 set("optimize.no_aapt_flags", true) 822 enable = true 823 default: 824 return fmt.Errorf("unsupported proguard value %q", s) 825 } 826 } 827 828 if enable { 829 // This is only necessary for libraries which default to false, but we can't 830 // tell the difference between a library and an app here. 831 set("optimize.enabled", true) 832 } 833 834 return nil 835} 836 837func invert(name string) func(ctx variableAssignmentContext) error { 838 return func(ctx variableAssignmentContext) error { 839 val, err := makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpparser.BoolType) 840 if err != nil { 841 return err 842 } 843 844 val.(*bpparser.Bool).Value = !val.(*bpparser.Bool).Value 845 846 return setVariable(ctx.file, ctx.append, ctx.prefix, name, val, true) 847 } 848} 849 850// given a conditional, returns a function that will insert a variable assignment or not, based on the conditional 851func includeVariableIf(bpVar bpVariable, conditional func(ctx variableAssignmentContext) bool) func(ctx variableAssignmentContext) error { 852 return func(ctx variableAssignmentContext) error { 853 var err error 854 if conditional(ctx) { 855 err = includeVariableNow(bpVar, ctx) 856 } 857 return err 858 } 859} 860 861// given a variable, returns a function that will always insert a variable assignment 862func includeVariable(bpVar bpVariable) func(ctx variableAssignmentContext) error { 863 return includeVariableIf(bpVar, always) 864} 865 866func includeVariableNow(bpVar bpVariable, ctx variableAssignmentContext) error { 867 var val bpparser.Expression 868 var err error 869 val, err = makeVariableToBlueprint(ctx.file, ctx.mkvalue, bpVar.variableType) 870 if err == nil { 871 err = setVariable(ctx.file, ctx.append, ctx.prefix, bpVar.name, val, true) 872 } 873 return err 874} 875 876// given a function that returns a bool, returns a function that returns the opposite 877func not(conditional func(ctx variableAssignmentContext) bool) func(ctx variableAssignmentContext) bool { 878 return func(ctx variableAssignmentContext) bool { 879 return !conditional(ctx) 880 } 881} 882 883// returns a function that tells whether mkvalue.Dump equals the given query string 884func valueDumpEquals(textToMatch string) func(ctx variableAssignmentContext) bool { 885 return func(ctx variableAssignmentContext) bool { 886 return (ctx.mkvalue.Dump() == textToMatch) 887 } 888} 889 890func always(ctx variableAssignmentContext) bool { 891 return true 892} 893 894func skip(ctx variableAssignmentContext) error { 895 return nil 896} 897 898// Shorter suffixes of other suffixes must be at the end of the list 899var propertyPrefixes = []struct{ mk, bp string }{ 900 {"arm", "arch.arm"}, 901 {"arm64", "arch.arm64"}, 902 {"x86", "arch.x86"}, 903 {"x86_64", "arch.x86_64"}, 904 {"32", "multilib.lib32"}, 905 // 64 must be after x86_64 906 {"64", "multilib.lib64"}, 907 {"darwin", "target.darwin"}, 908 {"linux", "target.linux_glibc"}, 909 {"windows", "target.windows"}, 910} 911 912var conditionalTranslations = map[string]map[bool]string{ 913 "($(HOST_OS),darwin)": { 914 true: "target.darwin", 915 false: "target.not_darwin"}, 916 "($(HOST_OS), darwin)": { 917 true: "target.darwin", 918 false: "target.not_darwin"}, 919 "($(HOST_OS),windows)": { 920 true: "target.windows", 921 false: "target.not_windows"}, 922 "($(HOST_OS), windows)": { 923 true: "target.windows", 924 false: "target.not_windows"}, 925 "($(HOST_OS),linux)": { 926 true: "target.linux_glibc", 927 false: "target.not_linux_glibc"}, 928 "($(HOST_OS), linux)": { 929 true: "target.linux_glibc", 930 false: "target.not_linux_glibc"}, 931 "($(BUILD_OS),darwin)": { 932 true: "target.darwin", 933 false: "target.not_darwin"}, 934 "($(BUILD_OS), darwin)": { 935 true: "target.darwin", 936 false: "target.not_darwin"}, 937 "($(BUILD_OS),linux)": { 938 true: "target.linux_glibc", 939 false: "target.not_linux_glibc"}, 940 "($(BUILD_OS), linux)": { 941 true: "target.linux_glibc", 942 false: "target.not_linux_glibc"}, 943 "(,$(TARGET_BUILD_APPS))": { 944 false: "product_variables.unbundled_build"}, 945 "($(TARGET_BUILD_APPS),)": { 946 false: "product_variables.unbundled_build"}, 947 "($(TARGET_BUILD_PDK),true)": { 948 true: "product_variables.pdk"}, 949 "($(TARGET_BUILD_PDK), true)": { 950 true: "product_variables.pdk"}, 951} 952 953func mydir(args []string) []string { 954 return []string{"."} 955} 956 957func allFilesUnder(wildcard string) func(args []string) []string { 958 return func(args []string) []string { 959 dirs := []string{""} 960 if len(args) > 0 { 961 dirs = strings.Fields(args[0]) 962 } 963 964 paths := make([]string, len(dirs)) 965 for i := range paths { 966 paths[i] = fmt.Sprintf("%s/**/"+wildcard, dirs[i]) 967 } 968 return paths 969 } 970} 971 972func allSubdirJavaFiles(args []string) []string { 973 return []string{"**/*.java"} 974} 975 976func includeIgnored(args []string) []string { 977 return []string{includeIgnoredPath} 978} 979 980var moduleTypes = map[string]string{ 981 "BUILD_SHARED_LIBRARY": "cc_library_shared", 982 "BUILD_STATIC_LIBRARY": "cc_library_static", 983 "BUILD_HOST_SHARED_LIBRARY": "cc_library_host_shared", 984 "BUILD_HOST_STATIC_LIBRARY": "cc_library_host_static", 985 "BUILD_HEADER_LIBRARY": "cc_library_headers", 986 "BUILD_EXECUTABLE": "cc_binary", 987 "BUILD_HOST_EXECUTABLE": "cc_binary_host", 988 "BUILD_NATIVE_TEST": "cc_test", 989 "BUILD_HOST_NATIVE_TEST": "cc_test_host", 990 "BUILD_NATIVE_BENCHMARK": "cc_benchmark", 991 "BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host", 992 993 "BUILD_JAVA_LIBRARY": "java_library_installable", // will be rewritten to java_library by bpfix 994 "BUILD_STATIC_JAVA_LIBRARY": "java_library", 995 "BUILD_HOST_JAVA_LIBRARY": "java_library_host", 996 "BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik", 997 "BUILD_PACKAGE": "android_app", 998 "BUILD_RRO_PACKAGE": "runtime_resource_overlay", 999 1000 "BUILD_CTS_EXECUTABLE": "cc_binary", // will be further massaged by bpfix depending on the output path 1001 "BUILD_CTS_SUPPORT_PACKAGE": "cts_support_package", // will be rewritten to android_test by bpfix 1002 "BUILD_CTS_PACKAGE": "cts_package", // will be rewritten to android_test by bpfix 1003 "BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to java_library by bpfix 1004 "BUILD_CTS_HOST_JAVA_LIBRARY": "cts_host_java_library", // will be rewritten to java_library_host by bpfix 1005} 1006 1007var prebuiltTypes = map[string]string{ 1008 "SHARED_LIBRARIES": "cc_prebuilt_library_shared", 1009 "STATIC_LIBRARIES": "cc_prebuilt_library_static", 1010 "EXECUTABLES": "cc_prebuilt_binary", 1011 "JAVA_LIBRARIES": "java_import", 1012 "APPS": "android_app_import", 1013 "ETC": "prebuilt_etc", 1014} 1015 1016var soongModuleTypes = map[string]bool{} 1017 1018var includePathToModule = map[string]string{ 1019 // The content will be populated dynamically in androidScope below 1020} 1021 1022func mapIncludePath(path string) (string, bool) { 1023 if path == clearVarsPath || path == includeIgnoredPath { 1024 return path, true 1025 } 1026 module, ok := includePathToModule[path] 1027 return module, ok 1028} 1029 1030func androidScope() mkparser.Scope { 1031 globalScope := mkparser.NewScope(nil) 1032 globalScope.Set("CLEAR_VARS", clearVarsPath) 1033 globalScope.SetFunc("my-dir", mydir) 1034 globalScope.SetFunc("all-java-files-under", allFilesUnder("*.java")) 1035 globalScope.SetFunc("all-proto-files-under", allFilesUnder("*.proto")) 1036 globalScope.SetFunc("all-aidl-files-under", allFilesUnder("*.aidl")) 1037 globalScope.SetFunc("all-Iaidl-files-under", allFilesUnder("I*.aidl")) 1038 globalScope.SetFunc("all-logtags-files-under", allFilesUnder("*.logtags")) 1039 globalScope.SetFunc("all-subdir-java-files", allSubdirJavaFiles) 1040 globalScope.SetFunc("all-makefiles-under", includeIgnored) 1041 globalScope.SetFunc("first-makefiles-under", includeIgnored) 1042 globalScope.SetFunc("all-named-subdir-makefiles", includeIgnored) 1043 globalScope.SetFunc("all-subdir-makefiles", includeIgnored) 1044 1045 // The scope maps each known variable to a path, and then includePathToModule maps a path 1046 // to a module. We don't care what the actual path value is so long as the value in scope 1047 // is mapped, so we might as well use variable name as key, too. 1048 for varName, moduleName := range moduleTypes { 1049 path := varName 1050 globalScope.Set(varName, path) 1051 includePathToModule[path] = moduleName 1052 } 1053 for varName, moduleName := range prebuiltTypes { 1054 includePathToModule[varName] = moduleName 1055 } 1056 1057 return globalScope 1058} 1059