github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/cc/makevars.go (about)

     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  import (
    18  	"fmt"
    19  	"sort"
    20  	"strings"
    21  	"sync"
    22  
    23  	"android/soong/android"
    24  	"android/soong/cc/config"
    25  )
    26  
    27  const (
    28  	modulesAddedWall          = "ModulesAddedWall"
    29  	modulesUsingWnoError      = "ModulesUsingWnoError"
    30  	modulesMissingProfileFile = "ModulesMissingProfileFile"
    31  )
    32  
    33  func init() {
    34  	android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
    35  }
    36  
    37  func getNamedMapForConfig(config android.Config, name string) *sync.Map {
    38  	return config.Once(name, func() interface{} {
    39  		return &sync.Map{}
    40  	}).(*sync.Map)
    41  }
    42  
    43  func makeStringOfKeys(ctx android.MakeVarsContext, setName string) string {
    44  	set := getNamedMapForConfig(ctx.Config(), setName)
    45  	keys := []string{}
    46  	set.Range(func(key interface{}, value interface{}) bool {
    47  		keys = append(keys, key.(string))
    48  		return true
    49  	})
    50  	sort.Strings(keys)
    51  	return strings.Join(keys, " ")
    52  }
    53  
    54  func makeStringOfWarningAllowedProjects() string {
    55  	allProjects := append([]string{}, config.WarningAllowedProjects...)
    56  	allProjects = append(allProjects, config.WarningAllowedOldProjects...)
    57  	sort.Strings(allProjects)
    58  	// Makefile rules use pattern "path/%" to match module paths.
    59  	if len(allProjects) > 0 {
    60  		return strings.Join(allProjects, "% ") + "%"
    61  	} else {
    62  		return ""
    63  	}
    64  }
    65  
    66  func makeVarsProvider(ctx android.MakeVarsContext) {
    67  	ctx.Strict("LLVM_RELEASE_VERSION", "${config.ClangShortVersion}")
    68  	ctx.Strict("LLVM_PREBUILTS_VERSION", "${config.ClangVersion}")
    69  	ctx.Strict("LLVM_PREBUILTS_BASE", "${config.ClangBase}")
    70  	ctx.Strict("LLVM_PREBUILTS_PATH", "${config.ClangBin}")
    71  	ctx.Strict("CLANG", "${config.ClangBin}/clang")
    72  	ctx.Strict("CLANG_CXX", "${config.ClangBin}/clang++")
    73  	ctx.Strict("LLVM_AS", "${config.ClangBin}/llvm-as")
    74  	ctx.Strict("LLVM_LINK", "${config.ClangBin}/llvm-link")
    75  	ctx.Strict("PATH_TO_CLANG_TIDY", "${config.ClangBin}/clang-tidy")
    76  	ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(config.ClangUnknownCflags, " "))
    77  
    78  	ctx.Strict("RS_LLVM_PREBUILTS_VERSION", "${config.RSClangVersion}")
    79  	ctx.Strict("RS_LLVM_PREBUILTS_BASE", "${config.RSClangBase}")
    80  	ctx.Strict("RS_LLVM_PREBUILTS_PATH", "${config.RSLLVMPrebuiltsPath}")
    81  	ctx.Strict("RS_LLVM_INCLUDES", "${config.RSIncludePath}")
    82  	ctx.Strict("RS_CLANG", "${config.RSLLVMPrebuiltsPath}/clang")
    83  	ctx.Strict("RS_LLVM_AS", "${config.RSLLVMPrebuiltsPath}/llvm-as")
    84  	ctx.Strict("RS_LLVM_LINK", "${config.RSLLVMPrebuiltsPath}/llvm-link")
    85  
    86  	ctx.Strict("GLOBAL_CFLAGS_NO_OVERRIDE", "${config.NoOverrideGlobalCflags}")
    87  	ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${config.ClangExtraNoOverrideCflags}")
    88  	ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "")
    89  	ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
    90  	ctx.Strict("NDK_PREBUILT_SHARED_LIBRARIES", strings.Join(ndkPrebuiltSharedLibs, " "))
    91  
    92  	ctx.Strict("BOARD_VNDK_VERSION", ctx.DeviceConfig().VndkVersion())
    93  
    94  	ctx.Strict("VNDK_CORE_LIBRARIES", strings.Join(vndkCoreLibraries, " "))
    95  	ctx.Strict("VNDK_SAMEPROCESS_LIBRARIES", strings.Join(vndkSpLibraries, " "))
    96  	ctx.Strict("LLNDK_LIBRARIES", strings.Join(llndkLibraries, " "))
    97  	ctx.Strict("VNDK_PRIVATE_LIBRARIES", strings.Join(vndkPrivateLibraries, " "))
    98  
    99  	// Filter vendor_public_library that are exported to make
   100  	exportedVendorPublicLibraries := []string{}
   101  	ctx.SingletonContext().VisitAllModules(func(module android.Module) {
   102  		if ccModule, ok := module.(*Module); ok {
   103  			baseName := ccModule.BaseModuleName()
   104  			if inList(baseName, vendorPublicLibraries) && module.ExportedToMake() {
   105  				if !inList(baseName, exportedVendorPublicLibraries) {
   106  					exportedVendorPublicLibraries = append(exportedVendorPublicLibraries, baseName)
   107  				}
   108  			}
   109  		}
   110  	})
   111  	sort.Strings(exportedVendorPublicLibraries)
   112  	ctx.Strict("VENDOR_PUBLIC_LIBRARIES", strings.Join(exportedVendorPublicLibraries, " "))
   113  
   114  	sort.Strings(lsdumpPaths)
   115  	ctx.Strict("LSDUMP_PATHS", strings.Join(lsdumpPaths, " "))
   116  
   117  	ctx.Strict("ANDROID_WARNING_ALLOWED_PROJECTS", makeStringOfWarningAllowedProjects())
   118  	ctx.Strict("SOONG_MODULES_ADDED_WALL", makeStringOfKeys(ctx, modulesAddedWall))
   119  	ctx.Strict("SOONG_MODULES_USING_WNO_ERROR", makeStringOfKeys(ctx, modulesUsingWnoError))
   120  	ctx.Strict("SOONG_MODULES_MISSING_PGO_PROFILE_FILE", makeStringOfKeys(ctx, modulesMissingProfileFile))
   121  
   122  	ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS", strings.Join(asanCflags, " "))
   123  	ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS", strings.Join(asanLdflags, " "))
   124  	ctx.Strict("ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES", strings.Join(asanLibs, " "))
   125  
   126  	ctx.Strict("CFI_EXTRA_CFLAGS", strings.Join(cfiCflags, " "))
   127  	ctx.Strict("CFI_EXTRA_LDFLAGS", strings.Join(cfiLdflags, " "))
   128  
   129  	ctx.Strict("INTEGER_OVERFLOW_EXTRA_CFLAGS", strings.Join(intOverflowCflags, " "))
   130  
   131  	ctx.Strict("DEFAULT_C_STD_VERSION", config.CStdVersion)
   132  	ctx.Strict("DEFAULT_CPP_STD_VERSION", config.CppStdVersion)
   133  	ctx.Strict("DEFAULT_GCC_CPP_STD_VERSION", config.GccCppStdVersion)
   134  	ctx.Strict("EXPERIMENTAL_C_STD_VERSION", config.ExperimentalCStdVersion)
   135  	ctx.Strict("EXPERIMENTAL_CPP_STD_VERSION", config.ExperimentalCppStdVersion)
   136  
   137  	ctx.Strict("DEFAULT_GLOBAL_TIDY_CHECKS", "${config.TidyDefaultGlobalChecks}")
   138  	ctx.Strict("DEFAULT_LOCAL_TIDY_CHECKS", joinLocalTidyChecks(config.DefaultLocalTidyChecks))
   139  	ctx.Strict("DEFAULT_TIDY_HEADER_DIRS", "${config.TidyDefaultHeaderDirs}")
   140  
   141  	ctx.Strict("AIDL_CPP", "${aidlCmd}")
   142  
   143  	ctx.Strict("RS_GLOBAL_INCLUDES", "${config.RsGlobalIncludes}")
   144  
   145  	nativeHelperIncludeFlags, err := ctx.Eval("${config.CommonNativehelperInclude}")
   146  	if err != nil {
   147  		panic(err)
   148  	}
   149  	nativeHelperIncludes, nativeHelperSystemIncludes := splitSystemIncludes(ctx, nativeHelperIncludeFlags)
   150  	if len(nativeHelperSystemIncludes) > 0 {
   151  		panic("native helper may not have any system includes")
   152  	}
   153  	ctx.Strict("JNI_H_INCLUDE", strings.Join(nativeHelperIncludes, " "))
   154  
   155  	includeFlags, err := ctx.Eval("${config.CommonGlobalIncludes}")
   156  	if err != nil {
   157  		panic(err)
   158  	}
   159  	includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
   160  	ctx.StrictRaw("SRC_HEADERS", strings.Join(includes, " "))
   161  	ctx.StrictRaw("SRC_SYSTEM_HEADERS", strings.Join(systemIncludes, " "))
   162  
   163  	sort.Strings(ndkMigratedLibs)
   164  	ctx.Strict("NDK_MIGRATED_LIBS", strings.Join(ndkMigratedLibs, " "))
   165  
   166  	hostTargets := ctx.Config().Targets[android.Host]
   167  	makeVarsToolchain(ctx, "", hostTargets[0])
   168  	if len(hostTargets) > 1 {
   169  		makeVarsToolchain(ctx, "2ND_", hostTargets[1])
   170  	}
   171  
   172  	crossTargets := ctx.Config().Targets[android.HostCross]
   173  	if len(crossTargets) > 0 {
   174  		makeVarsToolchain(ctx, "", crossTargets[0])
   175  		if len(crossTargets) > 1 {
   176  			makeVarsToolchain(ctx, "2ND_", crossTargets[1])
   177  		}
   178  	}
   179  
   180  	deviceTargets := ctx.Config().Targets[android.Device]
   181  	makeVarsToolchain(ctx, "", deviceTargets[0])
   182  	if len(deviceTargets) > 1 {
   183  		makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
   184  	}
   185  }
   186  
   187  func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
   188  	target android.Target) {
   189  	var typePrefix string
   190  	switch target.Os.Class {
   191  	case android.Host:
   192  		typePrefix = "HOST_"
   193  	case android.HostCross:
   194  		typePrefix = "HOST_CROSS_"
   195  	case android.Device:
   196  		typePrefix = "TARGET_"
   197  	}
   198  	makePrefix := secondPrefix + typePrefix
   199  
   200  	toolchain := config.FindToolchain(target.Os, target.Arch)
   201  
   202  	var productExtraCflags string
   203  	var productExtraLdflags string
   204  
   205  	hod := "Host"
   206  	if target.Os.Class == android.Device {
   207  		hod = "Device"
   208  	}
   209  
   210  	if target.Os.Class == android.Host && ctx.Config().HostStaticBinaries() {
   211  		productExtraLdflags += "-static"
   212  	}
   213  
   214  	ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{
   215  		toolchain.Cflags(),
   216  		"${config.CommonGlobalCflags}",
   217  		fmt.Sprintf("${config.%sGlobalCflags}", hod),
   218  		toolchain.ToolchainCflags(),
   219  		productExtraCflags,
   220  	}, " "))
   221  	ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", strings.Join([]string{
   222  		"${config.CommonGlobalConlyflags}",
   223  	}, " "))
   224  	ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
   225  		"${config.CommonGlobalCppflags}",
   226  		fmt.Sprintf("${config.%sGlobalCppflags}", hod),
   227  		toolchain.Cppflags(),
   228  	}, " "))
   229  	ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
   230  		fmt.Sprintf("${config.%sGlobalLdflags}", hod),
   231  		toolchain.Ldflags(),
   232  		toolchain.ToolchainLdflags(),
   233  		productExtraLdflags,
   234  	}, " "))
   235  
   236  	includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
   237  	if err != nil {
   238  		panic(err)
   239  	}
   240  	includes, systemIncludes := splitSystemIncludes(ctx, includeFlags)
   241  	ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Join(includes, " "))
   242  	ctx.StrictRaw(makePrefix+"C_SYSTEM_INCLUDES", strings.Join(systemIncludes, " "))
   243  
   244  	if target.Arch.ArchType == android.Arm {
   245  		flags, err := toolchain.InstructionSetFlags("arm")
   246  		if err != nil {
   247  			panic(err)
   248  		}
   249  		ctx.Strict(makePrefix+"arm_CFLAGS", flags)
   250  
   251  		flags, err = toolchain.InstructionSetFlags("thumb")
   252  		if err != nil {
   253  			panic(err)
   254  		}
   255  		ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
   256  	}
   257  
   258  	if toolchain.ClangSupported() {
   259  		clangPrefix := secondPrefix + "CLANG_" + typePrefix
   260  		clangExtras := "-target " + toolchain.ClangTriple()
   261  		clangExtras += " -B" + config.ToolPath(toolchain)
   262  
   263  		ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{
   264  			toolchain.ClangCflags(),
   265  			"${config.CommonClangGlobalCflags}",
   266  			fmt.Sprintf("${config.%sClangGlobalCflags}", hod),
   267  			toolchain.ToolchainClangCflags(),
   268  			clangExtras,
   269  			productExtraCflags,
   270  		}, " "))
   271  		ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{
   272  			"${config.CommonClangGlobalCppflags}",
   273  			fmt.Sprintf("${config.%sGlobalCppflags}", hod),
   274  			toolchain.ClangCppflags(),
   275  		}, " "))
   276  		ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{
   277  			fmt.Sprintf("${config.%sGlobalLdflags}", hod),
   278  			toolchain.ClangLdflags(),
   279  			toolchain.ToolchainClangLdflags(),
   280  			productExtraLdflags,
   281  			clangExtras,
   282  		}, " "))
   283  
   284  		if target.Os.Class == android.Device {
   285  			ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(config.AddressSanitizerRuntimeLibrary(toolchain), ".so"))
   286  			ctx.Strict(secondPrefix+"UBSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain), ".so"))
   287  			ctx.Strict(secondPrefix+"UBSAN_MINIMAL_RUNTIME_LIBRARY", strings.TrimSuffix(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(toolchain), ".a"))
   288  			ctx.Strict(secondPrefix+"TSAN_RUNTIME_LIBRARY", strings.TrimSuffix(config.ThreadSanitizerRuntimeLibrary(toolchain), ".so"))
   289  		}
   290  
   291  		// This is used by external/gentoo/...
   292  		ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
   293  			toolchain.ClangTriple())
   294  
   295  		ctx.Strict(makePrefix+"CLANG_SUPPORTED", "true")
   296  	} else {
   297  		ctx.Strict(makePrefix+"CLANG_SUPPORTED", "")
   298  	}
   299  
   300  	ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
   301  	ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
   302  
   303  	if target.Os == android.Darwin {
   304  		ctx.Strict(makePrefix+"AR", "${config.MacArPath}")
   305  	} else {
   306  		ctx.Strict(makePrefix+"AR", "${config.ClangBin}/llvm-ar")
   307  		ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf"))
   308  		ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
   309  	}
   310  
   311  	if target.Os == android.Windows {
   312  		ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
   313  	}
   314  
   315  	if target.Os.Class == android.Device {
   316  		ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
   317  		ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
   318  		ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
   319  		ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion())
   320  		ctx.Strict(makePrefix+"NDK_GCC_VERSION", toolchain.GccVersion())
   321  		ctx.Strict(makePrefix+"NDK_TRIPLE", toolchain.ClangTriple())
   322  	}
   323  
   324  	if target.Os.Class == android.Host || target.Os.Class == android.HostCross {
   325  		ctx.Strict(makePrefix+"AVAILABLE_LIBRARIES", strings.Join(toolchain.AvailableLibraries(), " "))
   326  	}
   327  
   328  	ctx.Strict(makePrefix+"TOOLCHAIN_ROOT", toolchain.GccRoot())
   329  	ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, ""))
   330  	ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix())
   331  	ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix())
   332  }
   333  
   334  func splitSystemIncludes(ctx android.MakeVarsContext, val string) (includes, systemIncludes []string) {
   335  	flags, err := ctx.Eval(val)
   336  	if err != nil {
   337  		panic(err)
   338  	}
   339  
   340  	extract := func(flags string, dirs []string, prefix string) (string, []string, bool) {
   341  		if strings.HasPrefix(flags, prefix) {
   342  			flags = strings.TrimPrefix(flags, prefix)
   343  			flags = strings.TrimLeft(flags, " ")
   344  			s := strings.SplitN(flags, " ", 2)
   345  			dirs = append(dirs, s[0])
   346  			if len(s) > 1 {
   347  				return strings.TrimLeft(s[1], " "), dirs, true
   348  			}
   349  			return "", dirs, true
   350  		} else {
   351  			return flags, dirs, false
   352  		}
   353  	}
   354  
   355  	flags = strings.TrimLeft(flags, " ")
   356  	for flags != "" {
   357  		found := false
   358  		flags, includes, found = extract(flags, includes, "-I")
   359  		if !found {
   360  			flags, systemIncludes, found = extract(flags, systemIncludes, "-isystem ")
   361  		}
   362  		if !found {
   363  			panic(fmt.Errorf("Unexpected flag in %q", flags))
   364  		}
   365  	}
   366  
   367  	return includes, systemIncludes
   368  }
   369  
   370  func joinLocalTidyChecks(checks []config.PathBasedTidyCheck) string {
   371  	rets := make([]string, len(checks))
   372  	for i, check := range config.DefaultLocalTidyChecks {
   373  		rets[i] = check.PathPrefix + ":" + check.Checks
   374  	}
   375  	return strings.Join(rets, " ")
   376  }