Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[c2cpg] Fixes for system header auto discovery #5385

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ object IncludeAutoDiscovery {

private val VcVarsCommand = Seq("cmd.exe", "/C", "VC\\Auxiliary\\Build\\vcvars64.bat")

private val LanguageSetting = Map("LC_ALL" -> "C")

// Only check once
private var isGccAvailable: Option[Boolean] = None

Expand Down Expand Up @@ -79,20 +81,23 @@ object IncludeAutoDiscovery {
}

private def discoverPaths(command: Seq[String]): mutable.LinkedHashSet[Path] =
GccSpecificExternalCommand.run(command, ".") match {
GccSpecificExternalCommand.run(command, ".", LanguageSetting) match {
case Success(output) => extractPaths(output)
case Failure(exception) =>
logger.warn(s"Unable to discover system include paths. Running '$command' failed.", exception)
mutable.LinkedHashSet.empty
}

private def extractPaths(output: Seq[String]): mutable.LinkedHashSet[Path] = {
val startIndex =
output.indexWhere(_.contains("#include")) + 2
val endIndex =
if (IsWin) output.indexWhere(_.startsWith("End of search list.")) - 1
else output.indexWhere(_.startsWith("COMPILER_PATH")) - 1
mutable.LinkedHashSet.from(output.slice(startIndex, endIndex).map(p => Paths.get(p.trim).toRealPath()))
val startIndex = output.indexWhere(_.contains("#include")) + 2
val endIndex = output.indexWhere(_.startsWith("End of search list.")) - 1
mutable.LinkedHashSet.from(output.slice(startIndex, endIndex).map { pathString =>
val trimmedPathString = pathString.trim
val macSpecificFix = if (trimmedPathString.contains(" (") && trimmedPathString.endsWith(")")) {
trimmedPathString.substring(0, trimmedPathString.indexOf(" ("))
} else trimmedPathString
Paths.get(macSpecificFix).toRealPath()
})
}

private def discoverMSVCPaths(): mutable.LinkedHashSet[Path] = {
Expand Down
Loading