Search in sources :

Example 6 with WorkspaceAttributeMapper

use of com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper in project bazel by bazelbuild.

the class MavenJarFunction method getServer.

private static MavenServerValue getServer(Rule rule, Environment env) throws RepositoryFunctionException, InterruptedException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    boolean hasRepository = mapper.isAttributeValueExplicitlySpecified("repository");
    boolean hasServer = mapper.isAttributeValueExplicitlySpecified("server");
    if (hasRepository && hasServer) {
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), rule + " specifies both " + "'repository' and 'server', which are mutually exclusive options"), Transience.PERSISTENT);
    }
    try {
        if (hasRepository) {
            return MavenServerValue.createFromUrl(mapper.get("repository", Type.STRING));
        } else {
            String serverName = DEFAULT_SERVER;
            if (hasServer) {
                serverName = mapper.get("server", Type.STRING);
            }
            return (MavenServerValue) env.getValue(MavenServerValue.key(serverName));
        }
    } catch (EvalException e) {
        throw new RepositoryFunctionException(e, Transience.PERSISTENT);
    }
}
Also used : EvalException(com.google.devtools.build.lib.syntax.EvalException) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)

Example 7 with WorkspaceAttributeMapper

use of com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper in project bazel by bazelbuild.

the class MavenServerFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, RepositoryFunctionException {
    String repository = (String) skyKey.argument();
    Rule repositoryRule = null;
    try {
        repositoryRule = RepositoryFunction.getRule(repository, env);
    } catch (RepositoryNotFoundException ex) {
    // Ignored. We throw a new one below.
    }
    BlazeDirectories directories = PrecomputedValue.BLAZE_DIRECTORIES.get(env);
    if (env.valuesMissing()) {
        return null;
    }
    String serverName;
    String url;
    Map<String, FileValue> settingsFiles;
    boolean foundRepoRule = repositoryRule != null && repositoryRule.getRuleClass().equals(MavenServerRule.NAME);
    if (!foundRepoRule) {
        if (repository.equals(MavenServerValue.DEFAULT_ID)) {
            settingsFiles = getDefaultSettingsFile(directories, env);
            serverName = MavenServerValue.DEFAULT_ID;
            url = MavenConnector.getMavenCentralRemote().getUrl();
        } else {
            throw new RepositoryFunctionException(new IOException("Could not find maven repository " + repository), Transience.TRANSIENT);
        }
    } else {
        WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(repositoryRule);
        serverName = repositoryRule.getName();
        try {
            url = mapper.get("url", Type.STRING);
            if (!mapper.isAttributeValueExplicitlySpecified("settings_file")) {
                settingsFiles = getDefaultSettingsFile(directories, env);
            } else {
                PathFragment settingsFilePath = new PathFragment(mapper.get("settings_file", Type.STRING));
                RootedPath settingsPath = RootedPath.toRootedPath(directories.getWorkspace().getRelative(settingsFilePath), PathFragment.EMPTY_FRAGMENT);
                FileValue fileValue = (FileValue) env.getValue(FileValue.key(settingsPath));
                if (fileValue == null) {
                    return null;
                }
                if (!fileValue.exists()) {
                    throw new RepositoryFunctionException(new IOException("Could not find settings file " + settingsPath), Transience.TRANSIENT);
                }
                settingsFiles = ImmutableMap.<String, FileValue>builder().put(USER_KEY, fileValue).build();
            }
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    }
    if (settingsFiles == null) {
        return null;
    }
    Fingerprint fingerprint = new Fingerprint();
    try {
        for (Map.Entry<String, FileValue> entry : settingsFiles.entrySet()) {
            fingerprint.addString(entry.getKey());
            Path path = entry.getValue().realRootedPath().asPath();
            if (path.exists()) {
                fingerprint.addBoolean(true);
                fingerprint.addBytes(path.getDigest());
            } else {
                fingerprint.addBoolean(false);
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    byte[] fingerprintBytes = fingerprint.digestAndReset();
    if (settingsFiles.isEmpty()) {
        return new MavenServerValue(serverName, url, new Server(), fingerprintBytes);
    }
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    if (settingsFiles.containsKey(SYSTEM_KEY)) {
        request.setGlobalSettingsFile(settingsFiles.get(SYSTEM_KEY).realRootedPath().asPath().getPathFile());
    }
    if (settingsFiles.containsKey(USER_KEY)) {
        request.setUserSettingsFile(settingsFiles.get(USER_KEY).realRootedPath().asPath().getPathFile());
    }
    DefaultSettingsBuilder builder = (new DefaultSettingsBuilderFactory()).newInstance();
    SettingsBuildingResult result;
    try {
        result = builder.build(request);
    } catch (SettingsBuildingException e) {
        throw new RepositoryFunctionException(new IOException("Error parsing settings files: " + e.getMessage()), Transience.TRANSIENT);
    }
    if (!result.getProblems().isEmpty()) {
        throw new RepositoryFunctionException(new IOException("Errors interpreting settings file: " + Arrays.toString(result.getProblems().toArray())), Transience.PERSISTENT);
    }
    Settings settings = result.getEffectiveSettings();
    Server server = settings.getServer(serverName);
    server = server == null ? new Server() : server;
    return new MavenServerValue(serverName, url, server, fingerprintBytes);
}
Also used : FileValue(com.google.devtools.build.lib.skyframe.FileValue) Server(org.apache.maven.settings.Server) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Settings(org.apache.maven.settings.Settings) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Fingerprint(com.google.devtools.build.lib.util.Fingerprint) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) RepositoryNotFoundException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryNotFoundException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) DefaultSettingsBuilder(org.apache.maven.settings.building.DefaultSettingsBuilder) MavenServerRule(com.google.devtools.build.lib.bazel.rules.workspace.MavenServerRule) Rule(com.google.devtools.build.lib.packages.Rule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) Nullable(javax.annotation.Nullable)

Example 8 with WorkspaceAttributeMapper

use of com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper in project bazel by bazelbuild.

the class AndroidSdkRepositoryFunction method fetch.

@Override
public RepositoryDirectoryValue.Builder fetch(Rule rule, Path outputDirectory, BlazeDirectories directories, Environment env, Map<String, String> markerData) throws SkyFunctionException, InterruptedException {
    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);
    FileSystem fs = directories.getOutputBase().getFileSystem();
    Path androidSdkPath;
    if (attributes.isAttributeValueExplicitlySpecified("path")) {
        androidSdkPath = fs.getPath(getTargetPath(rule, directories.getWorkspace()));
    } else if (environ.get(PATH_ENV_VAR) != null) {
        androidSdkPath = fs.getPath(getAndroidHomeEnvironmentVar(directories.getWorkspace(), environ));
    } else {
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), "Either the path attribute of android_sdk_repository or the ANDROID_HOME environment " + " variable must be set."), Transience.PERSISTENT);
    }
    if (!symlinkLocalRepositoryContents(outputDirectory, androidSdkPath)) {
        return null;
    }
    DirectoryListingValue platformsDirectoryValue = AndroidRepositoryUtils.getDirectoryListing(androidSdkPath, PLATFORMS_DIR, env);
    if (platformsDirectoryValue == null) {
        return null;
    }
    ImmutableSortedSet<Integer> apiLevels = AndroidRepositoryUtils.getApiLevels(platformsDirectoryValue.getDirents());
    if (apiLevels.isEmpty()) {
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), "android_sdk_repository requires that at least one Android SDK Platform is installed " + "in the Android SDK. Please install an Android SDK Platform through the " + "Android SDK manager."), Transience.PERSISTENT);
    }
    Integer defaultApiLevel;
    if (attributes.isAttributeValueExplicitlySpecified("api_level")) {
        try {
            defaultApiLevel = attributes.get("api_level", Type.INTEGER);
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
        if (!apiLevels.contains(defaultApiLevel)) {
            throw new RepositoryFunctionException(new EvalException(rule.getLocation(), String.format("Android SDK api level %s was requested but it is not installed in the Android " + "SDK at %s. The api levels found were %s. Please choose an available api " + "level or install api level %s from the Android SDK Manager.", defaultApiLevel, androidSdkPath, apiLevels.toString(), defaultApiLevel)), Transience.PERSISTENT);
        }
    } else {
        // If the api_level attribute is not explicitly set, we select the highest api level that is
        // available in the SDK.
        defaultApiLevel = apiLevels.first();
    }
    String buildToolsDirectory;
    if (attributes.isAttributeValueExplicitlySpecified("build_tools_version")) {
        try {
            buildToolsDirectory = attributes.get("build_tools_version", Type.STRING);
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    } else {
        // If the build_tools_version attribute is not explicitly set, we select the highest version
        // installed in the SDK.
        DirectoryListingValue directoryValue = AndroidRepositoryUtils.getDirectoryListing(androidSdkPath, BUILD_TOOLS_DIR, env);
        if (directoryValue == null) {
            return null;
        }
        buildToolsDirectory = getNewestBuildToolsDirectory(rule, directoryValue.getDirents());
    }
    // android_sdk_repository.build_tools_version is technically actually the name of the
    // directory in $sdk/build-tools. Most of the time this is just the actual build tools
    // version, but for preview build tools, the directory is something like 24.0.0-preview, and
    // the actual version is something like "24 rc3". The android_sdk rule in the template needs
    // the real version.
    String buildToolsVersion;
    if (buildToolsDirectory.contains("-preview")) {
        Properties sourceProperties = getBuildToolsSourceProperties(outputDirectory, buildToolsDirectory, env);
        if (env.valuesMissing()) {
            return null;
        }
        buildToolsVersion = sourceProperties.getProperty("Pkg.Revision");
    } else {
        buildToolsVersion = buildToolsDirectory;
    }
    try {
        assertValidBuildToolsVersion(rule, buildToolsVersion);
    } catch (EvalException e) {
        throw new RepositoryFunctionException(e, Transience.PERSISTENT);
    }
    ImmutableSortedSet<PathFragment> androidDeviceSystemImageDirs = getAndroidDeviceSystemImageDirs(androidSdkPath, env);
    if (androidDeviceSystemImageDirs == null) {
        return null;
    }
    StringBuilder systemImageDirsList = new StringBuilder();
    for (PathFragment systemImageDir : androidDeviceSystemImageDirs) {
        systemImageDirsList.append(String.format("        \"%s\",\n", systemImageDir));
    }
    String template = getStringResource("android_sdk_repository_template.txt");
    String buildFile = template.replace("%repository_name%", rule.getName()).replace("%build_tools_version%", buildToolsVersion).replace("%build_tools_directory%", buildToolsDirectory).replace("%api_levels%", Iterables.toString(apiLevels)).replace("%default_api_level%", String.valueOf(defaultApiLevel)).replace("%system_image_dirs%", systemImageDirsList);
    // All local maven repositories that are shipped in the Android SDK.
    // TODO(ajmichael): Create SkyKeys so that if the SDK changes, this function will get rerun.
    Iterable<Path> localMavenRepositories = ImmutableList.of(outputDirectory.getRelative("extras/android/m2repository"), outputDirectory.getRelative("extras/google/m2repository"));
    try {
        SdkMavenRepository sdkExtrasRepository = SdkMavenRepository.create(Iterables.filter(localMavenRepositories, new Predicate<Path>() {

            @Override
            public boolean apply(@Nullable Path path) {
                return path.isDirectory();
            }
        }));
        sdkExtrasRepository.writeBuildFiles(outputDirectory);
        buildFile = buildFile.replace("%exported_files%", sdkExtrasRepository.getExportsFiles(outputDirectory));
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    writeBuildFile(outputDirectory, buildFile);
    return RepositoryDirectoryValue.builder().setPath(outputDirectory);
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) EvalException(com.google.devtools.build.lib.syntax.EvalException) IOException(java.io.IOException) Properties(java.util.Properties) Predicate(com.google.common.base.Predicate) DirectoryListingValue(com.google.devtools.build.lib.skyframe.DirectoryListingValue) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) Nullable(javax.annotation.Nullable)

Example 9 with WorkspaceAttributeMapper

use of com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper in project bazel by bazelbuild.

the class HttpDownloader method download.

/** Validates native repository rule attributes and calls the other download method. */
public Path download(Rule rule, Path outputDirectory, ExtendedEventHandler eventHandler, Map<String, String> clientEnv) throws RepositoryFunctionException, InterruptedException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    List<URL> urls = new ArrayList<>();
    String sha256;
    String type;
    try {
        String urlString = Strings.nullToEmpty(mapper.get("url", Type.STRING));
        if (!urlString.isEmpty()) {
            try {
                URL url = new URL(urlString);
                if (!HttpUtils.isUrlSupportedByDownloader(url)) {
                    throw new EvalException(rule.getAttributeLocation("url"), "Unsupported protocol: " + url.getProtocol());
                }
                urls.add(url);
            } catch (MalformedURLException e) {
                throw new EvalException(rule.getAttributeLocation("url"), e.toString());
            }
        }
        List<String> urlStrings = MoreObjects.firstNonNull(mapper.get("urls", Type.STRING_LIST), ImmutableList.<String>of());
        if (!urlStrings.isEmpty()) {
            if (!urls.isEmpty()) {
                throw new EvalException(rule.getAttributeLocation("url"), "Don't set url if urls is set");
            }
            try {
                for (String urlString2 : urlStrings) {
                    URL url = new URL(urlString2);
                    if (!HttpUtils.isUrlSupportedByDownloader(url)) {
                        throw new EvalException(rule.getAttributeLocation("urls"), "Unsupported protocol: " + url.getProtocol());
                    }
                    urls.add(url);
                }
            } catch (MalformedURLException e) {
                throw new EvalException(rule.getAttributeLocation("urls"), e.toString());
            }
        }
        if (urls.isEmpty()) {
            throw new EvalException(rule.getLocation(), "urls attribute not set");
        }
        sha256 = Strings.nullToEmpty(mapper.get("sha256", Type.STRING));
        if (!sha256.isEmpty() && !RepositoryCache.KeyType.SHA256.isValid(sha256)) {
            throw new EvalException(rule.getAttributeLocation("sha256"), "Invalid SHA256 checksum");
        }
        type = Strings.nullToEmpty(mapper.get("type", Type.STRING));
    } catch (EvalException e) {
        throw new RepositoryFunctionException(e, Transience.PERSISTENT);
    }
    try {
        return download(urls, sha256, Optional.of(type), outputDirectory, eventHandler, clientEnv);
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) EvalException(com.google.devtools.build.lib.syntax.EvalException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) URL(java.net.URL) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Aggregations

WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)9 EvalException (com.google.devtools.build.lib.syntax.EvalException)9 IOException (java.io.IOException)6 Path (com.google.devtools.build.lib.vfs.Path)4 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)3 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)3 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)3 Nullable (javax.annotation.Nullable)3 DirectoryListingValue (com.google.devtools.build.lib.skyframe.DirectoryListingValue)2 URL (java.net.URL)2 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)1 ProxyHelper (com.google.devtools.build.lib.bazel.repository.downloader.ProxyHelper)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