Search in sources :

Example 1 with CrosstoolRelease

use of com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease in project bazel by bazelbuild.

the class AndroidNdkRepositoryFunction method fetch.

@Override
public RepositoryDirectoryValue.Builder fetch(Rule rule, Path outputDirectory, BlazeDirectories directories, Environment env, Map<String, String> markerData) throws InterruptedException, RepositoryFunctionException {
    Map<String, String> environ = declareEnvironmentDependencies(markerData, env, PATH_ENV_VAR_AS_LIST);
    if (environ == null) {
        return null;
    }
    prepareLocalRepositorySymlinkTree(rule, outputDirectory);
    WorkspaceAttributeMapper attributes = WorkspaceAttributeMapper.of(rule);
    PathFragment pathFragment;
    if (attributes.isAttributeValueExplicitlySpecified("path")) {
        pathFragment = getTargetPath(rule, directories.getWorkspace());
    } else if (environ.get(PATH_ENV_VAR) != null) {
        pathFragment = getAndroidNdkHomeEnvironmentVar(directories.getWorkspace(), environ);
    } else {
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), "Either the path attribute of android_ndk_repository or the ANDROID_NDK_HOME " + " environment variable must be set."), Transience.PERSISTENT);
    }
    Path ndkSymlinkTreeDirectory = outputDirectory.getRelative("ndk");
    try {
        ndkSymlinkTreeDirectory.createDirectory();
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    Path ndkHome = directories.getOutputBase().getFileSystem().getPath(pathFragment);
    if (!symlinkLocalRepositoryContents(ndkSymlinkTreeDirectory, ndkHome)) {
        return null;
    }
    String ruleName = rule.getName();
    // We need to fetch the NDK release info from the actual home to avoid cycle in the
    // dependency graph (the path relative to the repository root depends on the
    // repository being fetched).
    NdkRelease ndkRelease = getNdkRelease(ndkHome, env);
    if (env.valuesMissing()) {
        return null;
    }
    String apiLevelString;
    if (attributes.isAttributeValueExplicitlySpecified("api_level")) {
        try {
            apiLevelString = attributes.get("api_level", Type.INTEGER).toString();
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    } else {
        DirectoryListingValue platformsDirectoryValue = AndroidRepositoryUtils.getDirectoryListing(ndkHome, PLATFORMS_DIR, env);
        if (platformsDirectoryValue == null) {
            return null;
        }
        ImmutableSortedSet<Integer> apiLevels = AndroidRepositoryUtils.getApiLevels(platformsDirectoryValue.getDirents());
        if (apiLevels.isEmpty()) {
            // themselves.
            throw new RepositoryFunctionException(new EvalException(rule.getLocation(), "android_ndk_repository requires that at least one Android platform is present in " + "the Android NDK platforms directory. Please ensure that the path attribute " + "or the ANDROID_NDK_HOME environment variable points to a valid NDK."), Transience.PERSISTENT);
        }
        apiLevelString = apiLevels.first().toString();
    }
    // NDK minor revisions should be backwards compatible within a major revision, the crosstools
    // we generate don't care about the minor revision.
    NdkMajorRevision ndkMajorRevision;
    if (!ndkRelease.isValid) {
        String warningMessage = String.format("The revision of the Android NDK referenced by android_ndk_repository rule '%s' " + "could not be determined (the revision string found is '%s'). Defaulting to " + "revision %s.", ruleName, ndkRelease.rawRelease, AndroidNdkCrosstools.LATEST_KNOWN_REVISION.getKey());
        env.getListener().handle(Event.warn(warningMessage));
        ndkMajorRevision = AndroidNdkCrosstools.LATEST_KNOWN_REVISION.getValue();
    } else if (!AndroidNdkCrosstools.isKnownNDKRevision(ndkRelease)) {
        String warningMessage = String.format("The major revision of the Android NDK referenced by android_ndk_repository rule " + "'%s' is %s. The major revisions supported by Bazel are %s. Defaulting to " + "revision %s.", ruleName, ndkRelease.majorRevision, AndroidNdkCrosstools.KNOWN_NDK_MAJOR_REVISIONS.keySet(), AndroidNdkCrosstools.LATEST_KNOWN_REVISION.getKey());
        env.getListener().handle(Event.warn(warningMessage));
        ndkMajorRevision = AndroidNdkCrosstools.LATEST_KNOWN_REVISION.getValue();
    } else {
        ndkMajorRevision = AndroidNdkCrosstools.KNOWN_NDK_MAJOR_REVISIONS.get(ndkRelease.majorRevision);
    }
    ApiLevel apiLevel = ndkMajorRevision.apiLevel(env.getListener(), ruleName, apiLevelString);
    ImmutableList.Builder<CrosstoolStlPair> crosstoolsAndStls = ImmutableList.builder();
    try {
        String hostPlatform = AndroidNdkCrosstools.getHostPlatform(ndkRelease);
        NdkPaths ndkPaths = new NdkPaths(ruleName, hostPlatform, apiLevel);
        for (StlImpl stlImpl : StlImpls.get(ndkPaths)) {
            CrosstoolRelease crosstoolRelease = ndkMajorRevision.crosstoolRelease(ndkPaths, stlImpl, hostPlatform);
            crosstoolsAndStls.add(new CrosstoolStlPair(crosstoolRelease, stlImpl));
        }
    } catch (NdkCrosstoolsException e) {
        throw new RepositoryFunctionException(new IOException(e), Transience.PERSISTENT);
    }
    String buildFile = createBuildFile(ruleName, crosstoolsAndStls.build());
    writeBuildFile(outputDirectory, buildFile);
    return RepositoryDirectoryValue.builder().setPath(outputDirectory);
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) ToolPath(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath) Path(com.google.devtools.build.lib.vfs.Path) NdkRelease(com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkRelease) ImmutableList(com.google.common.collect.ImmutableList) StlImpl(com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) EvalException(com.google.devtools.build.lib.syntax.EvalException) IOException(java.io.IOException) NdkPaths(com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths) DirectoryListingValue(com.google.devtools.build.lib.skyframe.DirectoryListingValue) NdkMajorRevision(com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkMajorRevision) ApiLevel(com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.ApiLevel) CrosstoolRelease(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) NdkCrosstoolsException(com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.AndroidNdkCrosstools.NdkCrosstoolsException)

Example 2 with CrosstoolRelease

use of com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease in project bazel by bazelbuild.

the class AndroidNdkCrosstoolsTest method testAllToolchainsHaveRuntimesFilegroup.

@Test
public void testAllToolchainsHaveRuntimesFilegroup() {
    for (CrosstoolRelease crosstool : crosstoolReleases) {
        for (CToolchain toolchain : crosstool.getToolchainList()) {
            assertThat(toolchain.getDynamicRuntimesFilegroup()).isNotEmpty();
            assertThat(toolchain.getStaticRuntimesFilegroup()).isNotEmpty();
        }
    }
}
Also used : CToolchain(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain) CrosstoolRelease(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease) Test(org.junit.Test)

Example 3 with CrosstoolRelease

use of com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease in project bazel by bazelbuild.

the class AndroidNdkRepositoryFunction method createBuildFile.

private static String createBuildFile(String ruleName, List<CrosstoolStlPair> crosstools) {
    String buildFileTemplate = getTemplate("android_ndk_build_file_template.txt");
    String ccToolchainSuiteTemplate = getTemplate("android_ndk_cc_toolchain_suite_template.txt");
    String ccToolchainTemplate = getTemplate("android_ndk_cc_toolchain_template.txt");
    String stlFilegroupTemplate = getTemplate("android_ndk_stl_filegroup_template.txt");
    StringBuilder ccToolchainSuites = new StringBuilder();
    StringBuilder ccToolchainRules = new StringBuilder();
    StringBuilder stlFilegroups = new StringBuilder();
    for (CrosstoolStlPair crosstoolStlPair : crosstools) {
        // Create the cc_toolchain_suite rule
        CrosstoolRelease crosstool = crosstoolStlPair.crosstoolRelease;
        StringBuilder toolchainMap = new StringBuilder();
        for (CToolchain toolchain : crosstool.getToolchainList()) {
            toolchainMap.append(String.format("      \"%s|%s\": \":%s\",\n", toolchain.getTargetCpu(), toolchain.getCompiler(), toolchain.getToolchainIdentifier()));
        }
        String toolchainName = createToolchainName(crosstoolStlPair.stlImpl.getName());
        ccToolchainSuites.append(ccToolchainSuiteTemplate.replace("%toolchainName%", toolchainName).replace("%toolchainMap%", toolchainMap.toString().trim()).replace("%crosstoolReleaseProto%", crosstool.toString()));
        // Create the cc_toolchain rules
        for (CToolchain toolchain : crosstool.getToolchainList()) {
            ccToolchainRules.append(createCcToolchainRule(ccToolchainTemplate, toolchain));
        }
        // Create the STL file group rules
        for (Map.Entry<String, String> entry : crosstoolStlPair.stlImpl.getFilegroupNamesAndFilegroupFileGlobPatterns().entrySet()) {
            stlFilegroups.append(stlFilegroupTemplate.replace("%name%", entry.getKey()).replace("%fileGlobPattern%", entry.getValue()));
        }
    }
    return buildFileTemplate.replace("%ruleName%", ruleName).replace("%ccToolchainSuites%", ccToolchainSuites).replace("%ccToolchainRules%", ccToolchainRules).replace("%stlFilegroups%", stlFilegroups);
}
Also used : CToolchain(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain) CrosstoolRelease(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease) Map(java.util.Map)

Example 4 with CrosstoolRelease

use of com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease in project bazel by bazelbuild.

the class CrosstoolConfigurationLoader method findCrosstoolConfiguration.

private static CrosstoolFile findCrosstoolConfiguration(ConfigurationEnvironment env, Label crosstoolTop) throws IOException, InvalidConfigurationException, InterruptedException {
    CrosstoolProto crosstoolProto = getCrosstoolProtofromBuildFile(env, crosstoolTop);
    if (crosstoolProto == null) {
        crosstoolProto = getCrosstoolProtoFromCrosstoolFile(env, crosstoolTop);
    }
    if (crosstoolProto == null) {
        throw new InvalidConfigurationException("The crosstool_top you specified was resolved to '" + crosstoolTop + "', which does not contain a CROSSTOOL file. " + "You can use a crosstool from the depot by specifying its label.");
    } else {
        // Do this before we read the data, so if it changes, we get a different MD5 the next time.
        // Alternatively, we could calculate the MD5 of the contents, which we also read, but this
        // is faster if the file comes from a file system with md5 support.
        final CrosstoolProto finalProto = crosstoolProto;
        String md5 = BaseEncoding.base16().lowerCase().encode(finalProto.getMd5());
        CrosstoolConfig.CrosstoolRelease release;
        try {
            release = crosstoolReleaseCache.get(md5, new Callable<CrosstoolRelease>() {

                @Override
                public CrosstoolRelease call() throws Exception {
                    return toReleaseConfiguration(finalProto.getName(), finalProto.getContents());
                }
            });
        } catch (ExecutionException e) {
            throw new InvalidConfigurationException(e);
        }
        return new CrosstoolFile(finalProto.getName(), release, md5);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) CrosstoolRelease(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease) CrosstoolConfig(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig)

Example 5 with CrosstoolRelease

use of com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease in project bazel by bazelbuild.

the class AndroidNdkCrosstoolsTest method testPathsExist.

@Test
public void testPathsExist() throws Exception {
    for (CrosstoolRelease crosstool : crosstoolReleases) {
        for (CToolchain toolchain : crosstool.getToolchainList()) {
            // Test that all tool paths exist.
            for (ToolPath toolpath : toolchain.getToolPathList()) {
                assertThat(ndkFiles).contains(toolpath.getPath());
            }
            // Test that all cxx_builtin_include_directory paths exist.
            for (String includeDirectory : toolchain.getCxxBuiltinIncludeDirectoryList()) {
                // Special case for builtin_sysroot.
                if (!includeDirectory.equals("%sysroot%/usr/include")) {
                    String path = NdkPaths.stripRepositoryPrefix(includeDirectory);
                    assertThat(ndkDirectories).contains(path);
                }
            }
            // Test that the builtin_sysroot path exists.
            {
                String builtinSysroot = NdkPaths.stripRepositoryPrefix(toolchain.getBuiltinSysroot());
                assertThat(ndkDirectories).contains(builtinSysroot);
            }
            // Test that all include directories added through unfiltered_cxx_flag exist.
            for (String flag : toolchain.getUnfilteredCxxFlagList()) {
                if (!flag.equals("-isystem")) {
                    flag = NdkPaths.stripRepositoryPrefix(flag);
                    assertThat(ndkDirectories).contains(flag);
                }
            }
        }
    }
}
Also used : CToolchain(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain) ToolPath(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath) CrosstoolRelease(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease) Test(org.junit.Test)

Aggregations

CrosstoolRelease (com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease)6 CToolchain (com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain)4 Test (org.junit.Test)3 ToolPath (com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.ToolPath)2 Function (com.google.common.base.Function)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)1 NdkCrosstoolsException (com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.AndroidNdkCrosstools.NdkCrosstoolsException)1 ApiLevel (com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.ApiLevel)1 NdkMajorRevision (com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkMajorRevision)1 NdkPaths (com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths)1 NdkRelease (com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkRelease)1 StlImpl (com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl)1 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)1 DirectoryListingValue (com.google.devtools.build.lib.skyframe.DirectoryListingValue)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1 Path (com.google.devtools.build.lib.vfs.Path)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)1