Search in sources :

Example 26 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException 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 27 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException 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 28 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class AndroidSdkRepositoryFunction method getNewestBuildToolsDirectory.

/**
   * Gets the newest build tools directory according to {@link Revision}.
   *
   * @throws RepositoryFunctionException if none of the buildToolsDirectories are directories and
   *     have names that are parsable as build tools version.
   */
private static String getNewestBuildToolsDirectory(Rule rule, Dirents buildToolsDirectories) throws RepositoryFunctionException {
    String newestBuildToolsDirectory = null;
    Revision newestBuildToolsRevision = null;
    for (Dirent buildToolsDirectory : buildToolsDirectories) {
        if (buildToolsDirectory.getType() != Dirent.Type.DIRECTORY) {
            continue;
        }
        try {
            Revision buildToolsRevision = Revision.parseRevision(buildToolsDirectory.getName());
            if (newestBuildToolsRevision == null || buildToolsRevision.compareTo(newestBuildToolsRevision) > 0) {
                newestBuildToolsDirectory = buildToolsDirectory.getName();
                newestBuildToolsRevision = buildToolsRevision;
            }
        } catch (NumberFormatException e) {
        // Ignore unparsable build tools directories.
        }
    }
    if (newestBuildToolsDirectory == null) {
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), String.format("Bazel requires Android build tools version %s or newer but none are installed. " + "Please install a recent version through the Android SDK manager.", MIN_BUILD_TOOLS_REVISION)), Transience.PERSISTENT);
    }
    return newestBuildToolsDirectory;
}
Also used : Revision(com.android.repository.Revision) Dirent(com.google.devtools.build.lib.vfs.Dirent) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 29 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException 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)

Example 30 with EvalException

use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.

the class SkylarkRepositoryContext method getRootedPathFromLabel.

private static RootedPath getRootedPathFromLabel(Label label, Environment env) throws InterruptedException, EvalException {
    // Look for package.
    if (label.getPackageIdentifier().getRepository().isDefault()) {
        try {
            label = Label.create(label.getPackageIdentifier().makeAbsolute(), label.getName());
        } catch (LabelSyntaxException e) {
            // Can't happen because the input label is valid
            throw new AssertionError(e);
        }
    }
    SkyKey pkgSkyKey = PackageLookupValue.key(label.getPackageIdentifier());
    PackageLookupValue pkgLookupValue = (PackageLookupValue) env.getValue(pkgSkyKey);
    if (pkgLookupValue == null) {
        throw SkylarkRepositoryFunction.restart();
    }
    if (!pkgLookupValue.packageExists()) {
        throw new EvalException(Location.BUILTIN, "Unable to load package for " + label + ": not found.");
    }
    // And now for the file
    Path packageRoot = pkgLookupValue.getRoot();
    return RootedPath.toRootedPath(packageRoot, label.toPathFragment());
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) PackageLookupValue(com.google.devtools.build.lib.skyframe.PackageLookupValue) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Aggregations

EvalException (com.google.devtools.build.lib.syntax.EvalException)47 IOException (java.io.IOException)15 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)9 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)9 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 Label (com.google.devtools.build.lib.cmdline.Label)8 Path (com.google.devtools.build.lib.vfs.Path)8 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)7 Environment (com.google.devtools.build.lib.syntax.Environment)7 Map (java.util.Map)7 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)6 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 Nullable (javax.annotation.Nullable)6 FileValue (com.google.devtools.build.lib.skyframe.FileValue)5 ImmutableList (com.google.common.collect.ImmutableList)4 Artifact (com.google.devtools.build.lib.actions.Artifact)4 BaseFunction (com.google.devtools.build.lib.syntax.BaseFunction)3