Skip to content

Commit

Permalink
Compiler locators: set the compiler name to full path of the executable
Browse files Browse the repository at this point in the history
With this change, compilers are unlikely to "get lost" when a new version is out since
they are usually installed under the same location

Signed-off-by: Eran Ifrah <eran@codelite.org>
  • Loading branch information
eranif committed Nov 23, 2024
1 parent 558bfbd commit b2e856f
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 179 deletions.
119 changes: 39 additions & 80 deletions Plugin/CompilerLocator/CompilerLocatorCLANG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "globals.h"
#include "procutils.h"

#include <optional>
#include <wx/regex.h>

#ifdef __WXMSW__
Expand Down Expand Up @@ -99,15 +100,15 @@ bool CompilerLocatorCLANG::Locate()
wxFileName clang(d.fullpath);
if (clang.GetFullName() == "clang") {
// keep all the paths were we found clang
set.insert(clang.GetPath());
set.insert(clang.GetFullPath());
}
}
}

clDEBUG() << "Scan completed. Found" << set.size() << "compilers" << endl;

for (const auto& path : set) {
AddCompiler(path, wxEmptyString, wxEmptyString);
AddCompiler(path);
}
clDEBUG() << "Locate completed!" << endl;
return true;
Expand All @@ -133,99 +134,63 @@ CompilerPtr CompilerLocatorCLANG::Locate(const wxString& folder)
return NULL;
}

void CompilerLocatorCLANG::AddTool(CompilerPtr compiler, const wxString& toolname, const wxString& toolpath,
void CompilerLocatorCLANG::AddTool(CompilerPtr compiler, const wxString& name, const wxFileName& fntool,
const wxString& extraArgs)
{
wxString tool = toolpath;
wxString tool = fntool.GetFullPath();
::WrapWithQuotes(tool);
if (!extraArgs.IsEmpty()) {
tool << " " << extraArgs;
}
compiler->SetTool(toolname, tool);
compiler->SetTool(name, tool);
}

void CompilerLocatorCLANG::AddTools(CompilerPtr compiler, const wxString& installFolder, const wxString& suffix)
namespace
{
// add normalize path
compiler->SetInstallationPath(wxFileName(installFolder, "").GetPath());
wxFileName toolFile(installFolder, "");
#ifdef __WXMSW__
toolFile.SetExt("exe");
#endif
/// Given clang full path, try to locate `toolname` at the same location
/// For example if `clang` is `/usr/bin/clang-17` and `toolname` is `clang++`
/// This method will try to locate `/usr/bin/clang++-17`
std::optional<wxFileName> GetTool(const wxFileName& clang, const wxString& toolname)
{
wxString fullpath = clang.GetFullPath();
fullpath.Replace("clang", toolname);
wxFileName fn(fullpath);
if (fn.FileExists()) {
return fn;
} else {
return {};
}
}
} // namespace

toolFile.SetName("clang++" + suffix);
AddTool(compiler, "CXX", toolFile.GetFullPath());
AddTool(compiler, "LinkerName", toolFile.GetFullPath());
void CompilerLocatorCLANG::AddTools(CompilerPtr compiler, const wxFileName& clang)
{
// add normalize path
compiler->SetInstallationPath(clang.GetPath());
AddTool(compiler, "CXX", GetTool(clang, "clang++").value_or(clang));
AddTool(compiler, "LinkerName", GetTool(clang, "clang++").value_or(clang));

#ifdef __WXMAC__
AddTool(compiler, "SharedObjectLinkerName", toolFile.GetFullPath(), "-dynamiclib -fPIC");
AddTool(compiler, "SharedObjectLinkerName", GetTool(clang, "clang++").value_or(clang), "-dynamiclib -fPIC");
#else
AddTool(compiler, "SharedObjectLinkerName", toolFile.GetFullPath(), "-shared -fPIC");
AddTool(compiler, "SharedObjectLinkerName", GetTool(clang, "clang++").value_or(clang), "-shared -fPIC");
#endif

toolFile.SetName("clang" + suffix);
AddTool(compiler, "CC", toolFile.GetFullPath());
AddTool(compiler, "CC", clang);

// Add the archive tool
toolFile.SetName("llvm-ar" + suffix);
if (toolFile.FileExists()) {
AddTool(compiler, "AR", toolFile.GetFullPath(), "rcu");

} else {
toolFile.SetName("ar");
AddTool(compiler, "AR", toolFile.GetFullPath(), "rcu");
}

#ifdef __WXMSW__
toolFile.SetName("llvm-windres");
if (toolFile.FileExists()) {
AddTool(compiler, "ResourceCompiler", toolFile.GetFullPath());
} else {
AddTool(compiler, "ResourceCompiler", "windres.exe");
}
#else
AddTool(compiler, "ResourceCompiler", "");
#endif

// Add the assembler tool
toolFile.SetName("llvm-as" + suffix);
if (toolFile.FileExists()) {
AddTool(compiler, "AS", toolFile.GetFullPath());

} else {
toolFile.SetName("as");
AddTool(compiler, "AS", toolFile.GetFullPath());
}
AddTool(compiler, "AR", GetTool(clang, "llvm-ar").value_or(wxFileName("/usr/bin/ar")), "rcu");
AddTool(compiler, "AS", GetTool(clang, "llvm-as").value_or(wxFileName("/usr/bin/as")), wxEmptyString);
AddTool(compiler, "ResourceCompiler", {});

wxString makeExtraArgs;
if (wxThread::GetCPUCount() > 1) {
makeExtraArgs << "-j" << wxThread::GetCPUCount();
}

#ifdef __WXMSW__
toolFile.SetName("mingw32-make");
if (toolFile.FileExists()) {
AddTool(compiler, "MAKE", toolFile.GetFullPath(), makeExtraArgs + " SHELL=cmd.exe");
} else {
AddTool(compiler, "MAKE", "mingw32-make.exe", makeExtraArgs);
}
#else
AddTool(compiler, "MAKE", "make", makeExtraArgs);
#endif
AddTool(compiler, "MAKE", wxFileName("/usr/bin/make"), makeExtraArgs);
}

wxString CompilerLocatorCLANG::GetCompilerFullName(const wxString& clangBinary)
{
wxString fullname;
wxString version_string = ProcUtils::GrepCommandOutput({ clangBinary, "--version" }, "version");
version_string = version_string.AfterLast(' ');
version_string.Trim().Trim(false);
fullname = wxFileName(clangBinary).GetFullName().Capitalize();
if (!version_string.empty()) {
fullname << "-" << version_string;
}
return fullname;
}
wxString CompilerLocatorCLANG::GetCompilerFullName(const wxString& clangBinary) { return clangBinary; }

void CompilerLocatorCLANG::CheckUninstRegKey(const wxString& displayName, const wxString& installFolder,
const wxString& displayVersion)
Expand All @@ -235,19 +200,13 @@ void CompilerLocatorCLANG::CheckUninstRegKey(const wxString& displayName, const
wxUnusedVar(displayName);
}

CompilerPtr CompilerLocatorCLANG::AddCompiler(const wxString& clangFolder, const wxString& name, const wxString& suffix)
CompilerPtr CompilerLocatorCLANG::AddCompiler(const wxFileName& clang)
{
CompilerPtr compiler(new Compiler(NULL));
compiler->SetCompilerFamily(COMPILER_FAMILY_CLANG);
compiler->SetGenerateDependeciesFile(true);
if (name.IsEmpty()) {
// get the compiler version
wxFileName clang(clangFolder, "clang" + suffix);
compiler->SetName(GetCompilerFullName(clang.GetFullPath()));
} else {
compiler->SetName(name);
}
compiler->SetName(clang.GetFullPath());
m_compilers.push_back(compiler);
AddTools(compiler, clangFolder, suffix);
AddTools(compiler, clang);
return compiler;
}
24 changes: 11 additions & 13 deletions Plugin/CompilerLocator/CompilerLocatorCLANG.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@

class WXDLLIMPEXP_SDK CompilerLocatorCLANG : public ICompilerLocator
{
protected:
void AddTools(CompilerPtr compiler, const wxString& installFolder, const wxString& suffix = "");
void AddTool(CompilerPtr compiler, const wxString& toolname, const wxString& toolpath,
const wxString& extraArgs = "");
wxString GetCompilerFullName(const wxString& clangBinary);
virtual void CheckUninstRegKey(const wxString& displayName, const wxString& installFolder,
const wxString& displayVersion);
CompilerPtr AddCompiler(const wxString& clangFolder, const wxString& name = "", const wxString& suffix = "");

public:
CompilerLocatorCLANG();
virtual ~CompilerLocatorCLANG();
~CompilerLocatorCLANG() override;
bool Locate() override;
CompilerPtr Locate(const wxString& folder) override;

public:
virtual bool Locate();
virtual CompilerPtr Locate(const wxString& folder);
protected:
void AddTools(CompilerPtr compiler, const wxFileName& clang);
void AddTool(CompilerPtr compiler, const wxString& name, const wxFileName& fntool,
const wxString& extraArgs = wxEmptyString);
wxString GetCompilerFullName(const wxString& clangBinary);
void CheckUninstRegKey(const wxString& displayName, const wxString& installFolder,
const wxString& displayVersion) override;
CompilerPtr AddCompiler(const wxFileName& clang);
};

#endif // COMPILERLOCATORCLANG_H
Loading

0 comments on commit b2e856f

Please sign in to comment.