use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.
the class NewLocalRepositoryFunction method fetch.
@Override
public RepositoryDirectoryValue.Builder fetch(Rule rule, Path outputDirectory, BlazeDirectories directories, Environment env, Map<String, String> markerData) throws SkyFunctionException, InterruptedException {
NewRepositoryBuildFileHandler buildFileHandler = new NewRepositoryBuildFileHandler(directories.getWorkspace());
if (!buildFileHandler.prepareBuildFile(rule, env)) {
return null;
}
PathFragment pathFragment = getTargetPath(rule, directories.getWorkspace());
FileSystem fs = directories.getOutputBase().getFileSystem();
Path path = fs.getPath(pathFragment);
RootedPath dirPath = RootedPath.toRootedPath(fs.getRootDirectory(), path);
try {
FileValue dirFileValue = (FileValue) env.getValueOrThrow(FileValue.key(dirPath), IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class);
if (dirFileValue == null) {
return null;
}
if (!dirFileValue.exists()) {
throw new RepositoryFunctionException(new IOException("Expected directory at " + dirPath.asPath().getPathString() + " but it does not exist."), Transience.PERSISTENT);
}
if (!dirFileValue.isDirectory()) {
// Someone tried to create a local repository from a file.
throw new RepositoryFunctionException(new IOException("Expected directory at " + dirPath.asPath().getPathString() + " but it is not a directory."), Transience.PERSISTENT);
}
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.PERSISTENT);
} catch (FileSymlinkException e) {
throw new RepositoryFunctionException(new IOException(e), Transience.PERSISTENT);
} catch (InconsistentFilesystemException e) {
throw new RepositoryFunctionException(new IOException(e), Transience.PERSISTENT);
}
// fetch() creates symlinks to each child under 'path' and DiffAwareness handles checking all
// of these files and directories for changes. However, if a new file/directory is added
// directly under 'path', Bazel doesn't know that this has to be symlinked in. Thus, this
// creates a dependency on the contents of the 'path' directory.
SkyKey dirKey = DirectoryListingValue.key(dirPath);
DirectoryListingValue directoryValue;
try {
directoryValue = (DirectoryListingValue) env.getValueOrThrow(dirKey, InconsistentFilesystemException.class);
} catch (InconsistentFilesystemException e) {
throw new RepositoryFunctionException(new IOException(e), Transience.PERSISTENT);
}
if (directoryValue == null) {
return null;
}
// Link x/y/z to /some/path/to/y/z.
if (!symlinkLocalRepositoryContents(outputDirectory, path)) {
return null;
}
buildFileHandler.finishBuildFile(outputDirectory);
// If someone specified *new*_local_repository, we can assume they didn't want the existing
// repository info.
Path workspaceFile = outputDirectory.getRelative("WORKSPACE");
if (workspaceFile.exists()) {
try {
workspaceFile.delete();
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
}
}
createWorkspaceFile(outputDirectory, rule.getTargetKind(), rule.getName());
return RepositoryDirectoryValue.builder().setPath(outputDirectory).setSourceDir(directoryValue);
}
use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.
the class PathWindowsTest method assertStartsWithReturnsOnWindows.
private static void assertStartsWithReturnsOnWindows(boolean expected, String ancestor, String descendant) {
FileSystem windowsFileSystem = new WindowsFileSystem();
Path parent = windowsFileSystem.getPath(ancestor);
Path child = windowsFileSystem.getPath(descendant);
assertThat(child.startsWith(parent)).isEqualTo(expected);
}
use of com.google.devtools.build.lib.vfs.FileSystem 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);
}
use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.
the class TreeArtifactBuildTest method createTreeArtifact.
private Artifact createTreeArtifact(String name) {
FileSystem fs = scratch.getFileSystem();
Path execRoot = fs.getPath(TestUtils.tmpDir());
PathFragment execPath = new PathFragment("out").getRelative(name);
Path path = execRoot.getRelative(execPath);
return new SpecialArtifact(path, Root.asDerivedRoot(execRoot, execRoot.getRelative("out")), execPath, ALL_OWNER, SpecialArtifactType.TREE);
}
use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.
the class BlazeRuntime method newRuntime.
/**
* Creates a new blaze runtime, given the install and output base directories.
*
* <p>Note: This method can and should only be called once per startup, as it also creates the
* filesystem object that will be used for the runtime. So it should only ever be called from the
* main method of the Blaze program.
*
* @param args Blaze startup options.
*
* @return a new BlazeRuntime instance initialized with the given filesystem and directories, and
* an error string that, if not null, describes a fatal initialization failure that makes
* this runtime unsuitable for real commands
*/
private static BlazeRuntime newRuntime(Iterable<BlazeModule> blazeModules, List<String> args, Runnable abruptShutdownHandler) throws AbruptExitException, OptionsParsingException {
OptionsProvider options = parseOptions(blazeModules, args);
for (BlazeModule module : blazeModules) {
module.globalInit(options);
}
BlazeServerStartupOptions startupOptions = options.getOptions(BlazeServerStartupOptions.class);
String productName = startupOptions.productName.toLowerCase(Locale.US);
if (startupOptions.oomMoreEagerlyThreshold != 100) {
new RetainedHeapLimiter(startupOptions.oomMoreEagerlyThreshold).install();
}
PathFragment workspaceDirectory = startupOptions.workspaceDirectory;
PathFragment installBase = startupOptions.installBase;
PathFragment outputBase = startupOptions.outputBase;
// Must be before first use of JNI.
maybeForceJNI(installBase);
// are mandatory options, despite the comment in their declarations.
if (installBase == null || !installBase.isAbsolute()) {
// (includes "" default case)
throw new IllegalArgumentException("Bad --install_base option specified: '" + installBase + "'");
}
if (outputBase != null && !outputBase.isAbsolute()) {
// (includes "" default case)
throw new IllegalArgumentException("Bad --output_base option specified: '" + outputBase + "'");
}
FileSystem fs = null;
for (BlazeModule module : blazeModules) {
FileSystem moduleFs = module.getFileSystem(options);
if (moduleFs != null) {
Preconditions.checkState(fs == null, "more than one module returns a file system");
fs = moduleFs;
}
}
if (fs == null) {
fs = fileSystemImplementation();
}
Path.setFileSystemForSerialization(fs);
SubprocessBuilder.setSubprocessFactory(subprocessFactoryImplementation());
Path installBasePath = fs.getPath(installBase);
Path outputBasePath = fs.getPath(outputBase);
Path workspaceDirectoryPath = null;
if (!workspaceDirectory.equals(PathFragment.EMPTY_FRAGMENT)) {
workspaceDirectoryPath = fs.getPath(workspaceDirectory);
}
ServerDirectories serverDirectories = new ServerDirectories(installBasePath, outputBasePath, startupOptions.installMD5);
Clock clock = BlazeClock.instance();
BlazeRuntime.Builder runtimeBuilder = new BlazeRuntime.Builder().setProductName(productName).setServerDirectories(serverDirectories).setStartupOptionsProvider(options).setClock(clock).setAbruptShutdownHandler(abruptShutdownHandler).setEventBusExceptionHandler(startupOptions.fatalEventBusExceptions || !BlazeVersionInfo.instance().isReleasedBlaze() ? new BlazeRuntime.BugReportingExceptionHandler() : new BlazeRuntime.RemoteExceptionHandler());
if (System.getenv("TEST_TMPDIR") != null && System.getenv("NO_CRASH_ON_LOGGING_IN_TEST") == null) {
LoggingUtil.installRemoteLogger(getTestCrashLogger());
}
runtimeBuilder.addBlazeModule(new BuiltinCommandModule());
for (BlazeModule blazeModule : blazeModules) {
runtimeBuilder.addBlazeModule(blazeModule);
}
BlazeRuntime runtime = runtimeBuilder.build();
BlazeDirectories directories = new BlazeDirectories(serverDirectories, workspaceDirectoryPath, startupOptions.deepExecRoot, productName);
BinTools binTools;
try {
binTools = BinTools.forProduction(directories);
} catch (IOException e) {
throw new AbruptExitException("Cannot enumerate embedded binaries: " + e.getMessage(), ExitCode.LOCAL_ENVIRONMENTAL_ERROR);
}
runtime.initWorkspace(directories, binTools);
if (startupOptions.useCustomExitCodeOnAbruptExit) {
CustomExitCodePublisher.setAbruptExitStatusFileDir(serverDirectories.getOutputBase());
}
AutoProfiler.setClock(runtime.getClock());
BugReport.setRuntime(runtime);
return runtime;
}
Aggregations