Skip to content

Commit

Permalink
Fix clang-format binary
Browse files Browse the repository at this point in the history
  • Loading branch information
aarcangeli committed Feb 22, 2025
1 parent bd8efed commit 07c9f3c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 48 deletions.
37 changes: 20 additions & 17 deletions .github/workflows/update-clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
latest_tag=$(gh release view --repo llvm/llvm-project --json tagName --jq .tagName)
echo "Latest tag: $latest_tag"
branch_name="autoupdate-clang-$latest_tag"
current_version=$(cat src/main/resources/clang-format-tag.txt)
current_version=$(cat src/main/resources/llvm-tag.txt)
found_branch=$(gh api repos/llvm/llvm-project/branches/$branch_name --jq .name 2> /dev/null || true)
echo "Branch found: $found_branch"
# We need to update if: 1. The current branch is outdated and 2. There is no other branch with the same name
Expand Down Expand Up @@ -65,6 +65,9 @@ jobs:
if: steps.check-for-updates.outputs.update_needed == 'true'
run: |
latest_tag=${{ steps.check-for-updates.outputs.version }}
# Remove all existing clang-format binaries
rm -rf src/main/resources/clang-format-*
echo "Downloading windows binary"
echo "::group::Download Details"
Expand All @@ -75,27 +78,27 @@ jobs:
rm clang/clang+llvm-*
echo "::endgroup::"
echo "Downloading linux aarch64 binary"
echo "Downloading linux X64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "clang+llvm-*-aarch64-linux-gnu.tar.xz" --dir clang
tar -xvf clang/clang+llvm-*-aarch64-linux-gnu.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-linux-aarch64
mv clang/clang-format src/main/resources/clang-format-linux-aarch64/clang-format
chmod +x src/main/resources/clang-format-linux-aarch64/clang-format
rm clang/clang+llvm-*
gh release download $latest_tag --repo llvm/llvm-project --pattern "LLVM-*-Linux-X64.tar.xz" --dir clang
tar -xvf clang/LLVM-*-Linux-X64.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-linux-x64
mv clang/clang-format src/main/resources/clang-format-linux-x64/clang-format
chmod +x src/main/resources/clang-format-linux-x64/clang-format
rm clang/LLVM-*
echo "::endgroup::"
echo "Downloading linux armv7a binary"
echo "Downloading linux ARM64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "clang+llvm-*-armv7a-linux-*.tar.gz" --dir clang
tar -xvf clang/clang+llvm-*-armv7a-linux-*.tar.gz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-linux-armv7a
mv clang/clang-format src/main/resources/clang-format-linux-armv7a/clang-format
chmod +x src/main/resources/clang-format-linux-armv7a/clang-format
gh release download $latest_tag --repo llvm/llvm-project --pattern "LLVM-*-Linux-ARM64.tar.xz" --dir clang
tar -xvf clang/LLVM-*-Linux-ARM64.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-linux-arm64
mv clang/clang-format src/main/resources/clang-format-linux-arm64/clang-format
chmod +x src/main/resources/clang-format-linux-arm64/clang-format
rm clang/clang+llvm-*
echo "::endgroup::"
echo "Downloading macos x64 binary"
echo "Downloading macos X64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "LLVM-*-macOS-X64.tar.xz" --dir clang
tar -xvf clang/LLVM-*-macOS-X64.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
Expand All @@ -105,7 +108,7 @@ jobs:
rm clang/LLVM-*
echo "::endgroup::"
echo "Downloading macos arm64 binary"
echo "Downloading macos ARM64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "LLVM-*-macOS-ARM64.tar.xz" --dir clang
tar -xvf clang/LLVM-*-macOS-ARM64.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
Expand All @@ -121,7 +124,7 @@ jobs:
if: steps.check-for-updates.outputs.update_needed == 'true'
run: |
latest_tag=${{ steps.check-for-updates.outputs.version }}
echo $latest_tag > src/main/resources/clang-format-tag.txt
echo $latest_tag > src/main/resources/llvm-tag.txt
git config --global user.email "[email protected]"
git config --global user.name "Alessandro Arcangeli (bot)"
branch_name="autoupdate-clang-$latest_tag"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,43 @@ class ClangFormatServiceImpl : ClangFormatService, Disposable {
}

override fun getBuiltinPath(): BuiltinPath? {
val tempDir = Paths.get(PathManager.getPluginTempPath(), "clang-format-tools")
val version = ClangFormatCommons.readBuiltInVersion()
val versionMarkerString = "version-$version-${SystemInfo.OS_NAME}-${SystemInfo.OS_ARCH}"
val versionMarker = tempDir.resolve("version.txt")
try {
val tempDir = Paths.get(PathManager.getPluginTempPath(), "clang-format-tools")
val version = ClangFormatCommons.readBuiltInVersion()
val versionMarkerString = "version-$version-${SystemInfo.OS_NAME}-${SystemInfo.OS_ARCH}"
val versionMarker = tempDir.resolve("version.txt")

val outputFilename = if (SystemInfo.isWindows) "clang-format.exe" else "clang-format"
val outputFile = tempDir.resolve(outputFilename)
val outputFilename = if (SystemInfo.isWindows) "clang-format.exe" else "clang-format"
val outputFile = tempDir.resolve(outputFilename)

val currentVersion = runCatching { versionMarker.toFile().readText() }
val currentVersion = runCatching { versionMarker.toFile().readText() }

// Check if the file exists
if (Files.exists(outputFile) && Files.isExecutable(outputFile) && currentVersion.isSuccess && currentVersion.getOrNull() == versionMarkerString) {
return BuiltinPath(outputFile.toString(), version)
}
// Check if the file exists
if (Files.exists(outputFile) && Files.isExecutable(outputFile) && currentVersion.isSuccess && currentVersion.getOrNull() == versionMarkerString) {
return BuiltinPath(outputFile.toString(), version)
}

// Copy the file
val inputStream = ClangFormatCommons.getClangFormatPathFromResources() ?: return null
Files.createDirectories(tempDir)
Files.copy(inputStream, outputFile, StandardCopyOption.REPLACE_EXISTING)
// Copy the file
val path = ClangFormatCommons.getClangFormatPath() ?: return null
Files.createDirectories(tempDir)
Files.copy(path.openStream(), outputFile, StandardCopyOption.REPLACE_EXISTING)

// Make the file executable
if (!SystemInfo.isWindows) {
outputFile.toFile().setExecutable(true)
}
// Make the file executable
if (!SystemInfo.isWindows) {
outputFile.toFile().setExecutable(true)
}

// Write the version
versionMarker.toFile().writeText(versionMarkerString)
// Write the version
versionMarker.toFile().writeText(versionMarkerString)

tracker.incModificationCount()
tracker.incModificationCount()

return BuiltinPath(outputFile.toString(), version)
return BuiltinPath(outputFile.toString(), version)
}
catch (e: Exception) {
LOG.warn("Cannot copy clang-format", e)
return null
}
}

override fun getBuiltinPathTracker(): ModificationTracker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.build.FileNavigatable
import com.intellij.build.FilePosition
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.process.ProcessOutput
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
Expand All @@ -18,6 +19,7 @@ import com.intellij.util.PathUtil
import org.jetbrains.annotations.NonNls
import java.io.File
import java.io.InputStream

Check warning on line 21 in src/main/kotlin/com/github/aarcangeli/ideaclangformat/utils/ClangFormatCommons.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import java.net.URL
import java.util.regex.Pattern

// TODO: We should respect the user's settings for the file extensions
Expand All @@ -27,6 +29,8 @@ val knownCppExtensions = setOf(
"h", "hh", "hpp", "hxx", "inc", "inl", "ipp", "mpp", "pch", "tch", "tpp", "cuh",
)

private val LOG = Logger.getInstance(ClangFormatCommons::class.java)

object ClangFormatCommons {
private val CLANG_ERROR_PATTERN = Pattern.compile(
"(?<FileName>(?:[a-zA-Z]:|/)[^<>|?*:\\t]+):(?<LineNumber>\\d+):(?<Column>\\d+)\\s*:\\s*(?<Type>\\w+):\\s*(?<Message>.*)"
Expand Down Expand Up @@ -91,20 +95,33 @@ object ClangFormatCommons {
return ClangFormatError("Exit code ${output.exitCode} from ${commandLine.commandLineString}\n${stderr}")
}

fun getClangFormatPathFromResources(): InputStream? {
fun getClangFormatPath(): URL? {
LOG.info("Loading clang-format from resources for ${SystemInfo.OS_NAME}-${SystemInfo.OS_ARCH}")
val resourcePath = when {
SystemInfo.isWindows -> "/clang-format-win/clang-format.exe"
SystemInfo.isLinux && SystemInfo.isAarch64 -> "/clang-format-linux-aarch64/clang-format"
SystemInfo.isLinux -> "/clang-format-linux-armv7a/clang-format"
SystemInfo.isLinux && SystemInfo.OS_ARCH == "arm64" -> "/clang-format-linux-arm64/clang-format"
SystemInfo.isLinux && SystemInfo.OS_ARCH == "arm" -> "/clang-format-linux-armv7a/clang-format"
SystemInfo.isLinux -> "/clang-format-linux-x64/clang-format"
SystemInfo.isMac && SystemInfo.isAarch64 -> "/clang-format-macos-arm64/clang-format"
SystemInfo.isMac -> "/clang-format-macos-x64/clang-format"
else -> return null
}
return ClangFormatCommons::class.java.getResourceAsStream(resourcePath)
val resource = ClangFormatCommons::class.java.getResource(resourcePath)
if (resource != null) {
LOG.info("Loaded clang-format from $resourcePath")
return resource
}
// Some releases of LLVM don't have linux-arm64, so we fall back to linux-armv7a
if (SystemInfo.isLinux && SystemInfo.OS_ARCH == "arm64") {
LOG.info("Falling back to linux-armv7a")
return ClangFormatCommons::class.java.getResource("/clang-format-linux-armv7a/clang-format")
}
LOG.warn("Failed to load clang-format from $resourcePath")
return null
}

fun readBuiltInVersion(): String {
val inputStream = ClangFormatCommons::class.java.getResourceAsStream("/clang-format-tag.txt")
val inputStream = ClangFormatCommons::class.java.getResourceAsStream("/llvm-tag.txt")
val version = inputStream?.bufferedReader()?.use { it.readText() } ?: return "unknown"
return version.split("-")[1].trim()
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/clang-format-linux-aarch64/clang-format

This file was deleted.

Empty file modified src/main/resources/clang-format-linux-armv7a/clang-format
100755 → 100644
Empty file.
3 changes: 3 additions & 0 deletions src/main/resources/clang-format-linux-x64/clang-format
Git LFS file not shown
File renamed without changes.

0 comments on commit 07c9f3c

Please sign in to comment.