blob: f3ca9881c6609142498b5ef5748d6e8444d4d7ee [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.skyframe;
15
Kristina Chodorow73fa2032015-08-28 17:57:46 +000016import com.google.devtools.build.lib.cmdline.PackageIdentifier;
John Cater45932962017-09-06 21:57:46 +020017import com.google.devtools.build.lib.skyframe.PackageLookupValue.IncorrectRepositoryReferencePackageLookupValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.devtools.build.lib.vfs.PathFragment;
19import com.google.devtools.build.skyframe.SkyFunction;
20import com.google.devtools.build.skyframe.SkyKey;
21import com.google.devtools.build.skyframe.SkyValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022import javax.annotation.Nullable;
23
24/**
25 * SkyFunction for {@link ContainingPackageLookupValue}s.
26 */
27public class ContainingPackageLookupFunction implements SkyFunction {
28
29 @Override
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000030 public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031 PackageIdentifier dir = (PackageIdentifier) skyKey.argument();
Ulf Adams07dba942015-03-05 14:47:37 +000032 PackageLookupValue pkgLookupValue =
33 (PackageLookupValue) env.getValue(PackageLookupValue.key(dir));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034 if (pkgLookupValue == null) {
35 return null;
36 }
37
38 if (pkgLookupValue.packageExists()) {
39 return ContainingPackageLookupValue.withContainingPackage(dir, pkgLookupValue.getRoot());
40 }
41
John Cater45932962017-09-06 21:57:46 +020042 // Does the requested package cross into a sub-repository, which we should report via the
43 // correct package identifier?
44 if (pkgLookupValue instanceof IncorrectRepositoryReferencePackageLookupValue) {
45 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
46 (IncorrectRepositoryReferencePackageLookupValue) pkgLookupValue;
47 PackageIdentifier correctPackageIdentifier =
48 incorrectPackageLookupValue.getCorrectedPackageIdentifier();
49 return env.getValue(ContainingPackageLookupValue.key(correctPackageIdentifier));
50 }
51
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010052 PathFragment parentDir = dir.getPackageFragment().getParentDirectory();
53 if (parentDir == null) {
Michajlo Matijkiw11184592015-10-16 19:00:45 +000054 return ContainingPackageLookupValue.NONE;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 }
Miguel Alcon Pinto2627b9f2015-10-01 16:02:53 +000056 PackageIdentifier parentId = PackageIdentifier.create(dir.getRepository(), parentDir);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010057 return env.getValue(ContainingPackageLookupValue.key(parentId));
58 }
59
60 @Nullable
61 @Override
62 public String extractTag(SkyKey skyKey) {
63 return null;
64 }
65}