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