3333import com .google .devtools .build .lib .buildtool .BuildRequest ;
3434import com .google .devtools .build .lib .concurrent .ExecutorUtil ;
3535import com .google .devtools .build .lib .concurrent .ThreadSafety .ThreadHostile ;
36+ import com .google .devtools .build .lib .events .Event ;
3637import com .google .devtools .build .lib .exec .ExecutorBuilder ;
3738import com .google .devtools .build .lib .exec .ExecutorLifecycleListener ;
3839import com .google .devtools .build .lib .exec .ModuleActionContextRegistry ;
3940import com .google .devtools .build .lib .includescanning .IncludeParser .Inclusion ;
4041import com .google .devtools .build .lib .rules .cpp .CppIncludeExtractionContext ;
4142import com .google .devtools .build .lib .rules .cpp .CppIncludeScanningContext ;
43+ import com .google .devtools .build .lib .rules .cpp .CppOptions ;
4244import com .google .devtools .build .lib .rules .cpp .IncludeScanner .IncludeScanningHeaderData ;
4345import com .google .devtools .build .lib .rules .cpp .SwigIncludeScanningContext ;
4446import com .google .devtools .build .lib .runtime .BlazeModule ;
6668import java .util .concurrent .ThreadPoolExecutor ;
6769import java .util .concurrent .TimeUnit ;
6870import java .util .function .Supplier ;
71+ import javax .annotation .Nullable ;
6972
7073/**
7174 * Module that provides implementations of {@link CppIncludeExtractionContext},
7477public class IncludeScanningModule extends BlazeModule {
7578 private static final GoogleLogger logger = GoogleLogger .forEnclosingClass ();
7679
77- private static final PathFragment INCLUDE_HINTS_FILENAME =
78- PathFragment .create ("tools/cpp/INCLUDE_HINTS" );
79-
8080 private final MutableSupplier <SpawnIncludeScanner > spawnIncludeScannerSupplier =
8181 new MutableSupplier <>();
8282 private final MutableSupplier <ArtifactFactory > artifactFactory = new MutableSupplier <>();
8383 private IncludeScannerLifecycleManager lifecycleManager ;
8484
85+ @ Nullable
8586 protected PathFragment getIncludeHintsFilename () {
86- return INCLUDE_HINTS_FILENAME ;
87+ return null ;
8788 }
8889
8990 @ Override
@@ -106,7 +107,8 @@ public void registerActionContexts(
106107 @ ThreadHostile
107108 public void executorInit (CommandEnvironment env , BuildRequest request , ExecutorBuilder builder ) {
108109 lifecycleManager =
109- new IncludeScannerLifecycleManager (env , request , spawnIncludeScannerSupplier );
110+ new IncludeScannerLifecycleManager (
111+ env , request , spawnIncludeScannerSupplier , getIncludeHintsFilename () != null );
110112 builder .addExecutorLifecycleListener (lifecycleManager );
111113 }
112114
@@ -119,6 +121,11 @@ public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command)
119121
120122 @ Override
121123 public void beforeCommand (CommandEnvironment env ) {
124+ CppOptions cppOptions = env .getOptions ().getOptions (CppOptions .class );
125+ if (cppOptions != null && cppOptions .experimentalIncludeScanning ) {
126+ env .getReporter ()
127+ .handle (Event .warn ("Include scanning enabled. This feature is unsupported." ));
128+ }
122129 artifactFactory .set (env .getSkyframeBuildView ().getArtifactFactory ());
123130 }
124131
@@ -137,10 +144,13 @@ public void workspaceInit(
137144
138145 @ VisibleForTesting
139146 public static ImmutableMap <SkyFunctionName , SkyFunction > getSkyFunctions (
140- PathFragment includeHintsFile ) {
141- return ImmutableMap .of (
142- IncludeScanningSkyFunctions .INCLUDE_HINTS ,
143- new IncludeHintsFunction (includeHintsFile ));
147+ @ Nullable PathFragment includeHintsFile ) {
148+ ImmutableMap .Builder <SkyFunctionName , SkyFunction > skyFunctions = ImmutableMap .builder ();
149+ if (includeHintsFile != null ) {
150+ skyFunctions .put (
151+ IncludeScanningSkyFunctions .INCLUDE_HINTS , new IncludeHintsFunction (includeHintsFile ));
152+ }
153+ return skyFunctions .buildOrThrow ();
144154 }
145155
146156 /**
@@ -225,6 +235,7 @@ public void extractSwigIncludes(
225235 private static final class IncludeScannerLifecycleManager implements ExecutorLifecycleListener {
226236 private final CommandEnvironment env ;
227237 private final IncludeScanningOptions options ;
238+ private final boolean useIncludeHints ;
228239
229240 private final Supplier <SpawnIncludeScanner > spawnScannerSupplier ;
230241 private IncludeScannerSupplier includeScannerSupplier ;
@@ -233,9 +244,11 @@ private static final class IncludeScannerLifecycleManager implements ExecutorLif
233244 IncludeScannerLifecycleManager (
234245 CommandEnvironment env ,
235246 BuildRequest buildRequest ,
236- MutableSupplier <SpawnIncludeScanner > spawnScannerSupplier ) {
247+ MutableSupplier <SpawnIncludeScanner > spawnScannerSupplier ,
248+ boolean useIncludeHints ) {
237249 this .env = env ;
238250 this .options = buildRequest .getOptions (IncludeScanningOptions .class );
251+ this .useIncludeHints = useIncludeHints ;
239252
240253 spawnScannerSupplier .set (
241254 new SpawnIncludeScanner (
@@ -258,26 +271,32 @@ private SwigIncludeScanningContextImpl getSwigActionContext() {
258271 public void executionPhaseStarting (
259272 ActionGraph actionGraph , Supplier <ImmutableSet <Artifact >> topLevelArtifacts )
260273 throws AbruptExitException , InterruptedException {
261- try {
262- includeScannerSupplier .init (
263- new IncludeParser (
264- new IncludeParser .Hints (
265- (IncludeParser .HintsRules )
266- env .getSkyframeExecutor ()
267- .evaluateSkyKeyForExecutionSetup (
268- env .getReporter (), IncludeHintsFunction .INCLUDE_HINTS_KEY ),
269- env .getSkyframeBuildView ().getArtifactFactory ())));
270- } catch (ExecException e ) {
271- throw new AbruptExitException (
272- DetailedExitCode .of (
273- FailureDetail .newBuilder ()
274- .setMessage ("could not initialize include hints: " + e .getMessage ())
275- .setIncludeScanning (
276- IncludeScanning .newBuilder ()
277- .setCode (IncludeScanning .Code .INITIALIZE_INCLUDE_HINTS_ERROR ))
278- .build ()),
279- e );
274+ IncludeParser .HintsRules hintsRules ;
275+ if (useIncludeHints ) {
276+ try {
277+ hintsRules =
278+ (IncludeParser .HintsRules )
279+ env .getSkyframeExecutor ()
280+ .evaluateSkyKeyForExecutionSetup (
281+ env .getReporter (), IncludeHintsFunction .INCLUDE_HINTS_KEY );
282+ } catch (ExecException e ) {
283+ throw new AbruptExitException (
284+ DetailedExitCode .of (
285+ FailureDetail .newBuilder ()
286+ .setMessage ("could not initialize include hints: " + e .getMessage ())
287+ .setIncludeScanning (
288+ IncludeScanning .newBuilder ()
289+ .setCode (IncludeScanning .Code .INITIALIZE_INCLUDE_HINTS_ERROR ))
290+ .build ()),
291+ e );
292+ }
293+ } else {
294+ hintsRules = IncludeParser .HintsRules .EMPTY ;
280295 }
296+ includeScannerSupplier .init (
297+ new IncludeParser (
298+ new IncludeParser .Hints (
299+ hintsRules , env .getSkyframeBuildView ().getArtifactFactory ())));
281300 }
282301
283302 @ Override
0 commit comments