// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EpicGames.Core;
using UnrealBuildBase;
namespace UnrealBuildTool
{
///
/// Utility functions for querying native projects (ie. those found via a .uprojectdirs query)
///
public class NativeProjects : NativeProjectsBase
{
///
/// Cached map of target names to the project file they belong to
///
static Dictionary? CachedTargetNameToProjectFile;
///
/// Clear our cached properties. Generally only needed if your script has modified local files...
///
public static void ClearCache()
{
ClearCacheBase();
CachedTargetNameToProjectFile = null;
}
///
/// Get the project folder for the given target name
///
/// Name of the target of interest
/// The project filename
/// True if the target was found
public static bool TryGetProjectForTarget(string InTargetName, [NotNullWhen(true)] out FileReference? OutProjectFileName)
{
if(CachedTargetNameToProjectFile == null)
{
lock(LockObject)
{
if(CachedTargetNameToProjectFile == null)
{
Dictionary TargetNameToProjectFile = new Dictionary();
foreach(FileReference ProjectFile in EnumerateProjectFiles())
{
foreach (DirectoryReference ExtensionDir in Unreal.GetExtensionDirs(ProjectFile.Directory))
{
DirectoryReference SourceDirectory = DirectoryReference.Combine(ExtensionDir, "Source");
if (DirectoryLookupCache.DirectoryExists(SourceDirectory))
{
FindTargetFiles(SourceDirectory, TargetNameToProjectFile, ProjectFile);
}
DirectoryReference IntermediateSourceDirectory = DirectoryReference.Combine(ExtensionDir, "Intermediate", "Source");
if (DirectoryLookupCache.DirectoryExists(IntermediateSourceDirectory))
{
FindTargetFiles(IntermediateSourceDirectory, TargetNameToProjectFile, ProjectFile);
}
}
}
CachedTargetNameToProjectFile = TargetNameToProjectFile;
}
}
}
return CachedTargetNameToProjectFile.TryGetValue(InTargetName, out OutProjectFileName);
}
}
}