Skip to content

Commit

Permalink
fix: fixed redundant build after opening sdkconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaaa committed Jan 7, 2025
1 parent fc3ab91 commit 3d6a0c1
Showing 1 changed file with 1 addition and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -48,10 +45,8 @@
import org.eclipse.cdt.core.envvar.EnvironmentVariable;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ElementChangedEvent;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.IBinaryContainer;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.resources.IConsole;
Expand All @@ -62,7 +57,6 @@
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -119,7 +113,6 @@ public class IDFBuildConfiguration extends CBuildConfiguration
* ErrorParserManager intermixes these streams making it impossible to parse for errors.<br>
* To work around that, we run cmake in advance with its dedicated working error parser.
*/
private boolean cmakeListsModified;
private ICMakeToolChainFile toolChainFile;
private String customBuildDir;
private IProgressMonitor monitor;
Expand Down Expand Up @@ -341,17 +334,7 @@ public IProject[] build(int kind, Map<String, String> args, IConsole console, IP
try
{
infoStream.write(String.format(Messages.CMakeBuildConfiguration_BuildingIn, buildDir.toString()));
boolean runCMake = cmakeListsModified;
if (!runCMake)
{
runCMake = generator.contentEquals(NINJA) ? !Files.exists(buildDir.resolve("build.ninja")) //$NON-NLS-1$
: !Files.exists(buildDir.resolve("CMakeFiles")); //$NON-NLS-1$
}

if (runCMake)
{
runCmakeCommand(console, monitor, project, generator, infoStream, buildDir);
}
runCmakeCommand(console, monitor, project, generator, infoStream, buildDir);
runCmakeBuildCommand(console, monitor, project, start, generator, infoStream, buildDir);
new ClangdConfigFileHandler().update(project);
new ClangFormatFileHandler(project).update();
Expand Down Expand Up @@ -501,9 +484,6 @@ private void runCmakeCommand(IConsole console, IProgressMonitor monitor, IProjec
deleteCMakeErrorMarkers(project);

infoStream.write(String.format(Messages.CMakeBuildConfiguration_Configuring, buildDir));
// clean output to make sure there is no content
// incompatible with current settings (cmake config would fail)
cleanBuildDirectory(buildDir);

List<String> command = new ArrayList<>();

Expand Down Expand Up @@ -570,8 +550,6 @@ private void runCmakeCommand(IConsole console, IProgressMonitor monitor, IProjec
epm.setOutputStream(console.getOutputStream());
watchProcess(new IConsoleParser[] { epm }, monitor);
}

cmakeListsModified = false;
}

private boolean checkToolChainFile(IConsole console)
Expand Down Expand Up @@ -731,102 +709,6 @@ public void clean(IConsole console, IProgressMonitor monitor) throws CoreExcepti
}
}

/**
* Recursively removes any files and directories found below the specified Path.
*/
private static void cleanDirectory(Path dir) throws IOException
{
SimpleFileVisitor<Path> deltor = new SimpleFileVisitor<>()
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
{
super.postVisitDirectory(dir, exc);
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
};
Path[] files = Files.list(dir).toArray(Path[]::new);
for (Path file : files)
{
Files.walkFileTree(file, deltor);
}
}

private void cleanBuildDirectory(Path buildDir) throws IOException
{
if (!Files.exists(buildDir))
return;
if (Files.isDirectory(buildDir))
cleanDirectory(buildDir);
// TODO: not a directory should we do something?
}

/**
* Overwritten to detect whether one of the CMakeLists.txt files in the project was modified since the last build.
*/
@Override
public void elementChanged(ElementChangedEvent event)
{
super.elementChanged(event);
// Only respond to post change events
if (event.getType() != ElementChangedEvent.POST_CHANGE)
return;
if (!cmakeListsModified)
{
processElementDelta(event.getDelta());
}
}

/**
* Processes the delta in order to detect whether one of the CMakeLists.txt files in the project has been modified
* and saved by the user since the last build.
*
* @return <code>true</code> to continue with delta processing, otherwise <code>false</code>
*/
private boolean processElementDelta(ICElementDelta delta)
{
if (delta == null)
{
return true;
}

if (delta.getKind() == ICElementDelta.CHANGED && ((delta.getFlags() & ICElementDelta.F_CONTENT) != 0))
{
// check for modified CMakeLists.txt file
IResourceDelta[] resourceDeltas = delta.getResourceDeltas();
if (resourceDeltas != null)
{
cmakeListsModified = Stream.of(resourceDeltas).map(t -> t.getResource())
.filter(t -> t.getType() == IResource.FILE
&& !t.getFullPath().toOSString().contains(IDFConstants.BUILD_FOLDER))
.map(t -> t.getName()).anyMatch(t -> t.equals("CMakeLists.txt") || t.endsWith(".cmake")); //$NON-NLS-1$ //$NON-NLS-2$

}
if (cmakeListsModified)
{
return false;
}
}

// recurse...
for (ICElementDelta child : delta.getAffectedChildren())
{
if (!processElementDelta(child))
{
return false; // stop processing
}
}
return true;
}

/**
* Overwritten since we do not parse console output to get scanner information.
*/
Expand Down

0 comments on commit 3d6a0c1

Please sign in to comment.