blob: 5faa302f6ee2441619d34a7003d417284c09482b [file] [log] [blame]
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.rules.cpp;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Map;
import javax.annotation.Nullable;
/** Wrapper around a map of bitcode files for purposes of caching its fingerprint. */
final class BitcodeFiles {
private final ImmutableMap<PathFragment, Artifact> files;
@Nullable private volatile byte[] fingerprint = null;
BitcodeFiles(Map<PathFragment, Artifact> files) {
this.files = ImmutableMap.copyOf(files);
}
@Nullable
Artifact lookup(PathFragment path) {
return files.get(path);
}
Iterable<Artifact> getFiles() {
return files.values();
}
void addToFingerprint(Fingerprint fp) {
if (fingerprint == null) {
synchronized (this) {
if (fingerprint == null) {
fingerprint = computeFingerprint();
}
}
}
fp.addBytes(fingerprint);
}
private byte[] computeFingerprint() {
Fingerprint fp = new Fingerprint();
for (PathFragment path : files.keySet()) {
fp.addPath(path);
}
return fp.digestAndReset();
}
}