aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/action.yml
blob: 1a0af3c58bbb236a784f3e02fffa7723f08c0f0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Install Clang
description: Install Clang & LLVM

inputs:
  version:
    description: Version to install
    required: false
    default: latest
  platform:
    description: Target platform
    required: false
    default: x64
  cc:
    description: Set up cc/clang/c++/clang++ executables
    required: false
    default: 1

outputs:
  clang:
    description: clang binary name
    value: '${{ steps.install.outputs.clang }}'
  clangxx:
    description: clang++ binary name
    value: '${{ steps.install.outputs.clangxx }}'

runs:
  using: composite
  steps:
    - id: install
      env:
        ARG_VERSION: ${{ inputs.version }}
        ARG_PLATFORM: ${{ inputs.platform }}
      run: |
        $linux_host = '${{ runner.os }}' -eq 'Linux'
        $windows_host = '${{ runner.os }}' -eq 'Windows'

        $version = (Get-ChildItem env:ARG_VERSION).Value
        $latest = $version -eq 'latest'
        $x64 = (Get-ChildItem env:ARG_PLATFORM).Value -eq 'x64'

        function Locate-Choco {
            $path = Get-Command 'choco' -ErrorAction SilentlyContinue
            if ($path) {
                $path.Path
            } else {
                Join-Path (Get-ChildItem env:ProgramData).Value 'chocolatey' 'bin' 'choco'
            }
        }

        function Install-Package {
            param(
                [Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
                [string[]] $Packages
            )

            if ($script:linux_host) {
                sudo apt-get update
                sudo DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends $Packages
            } elseif ($script:windows_host) {
                $choco = Locate-Choco
                & $choco upgrade $Packages -y --no-progress --allow-downgrade
            } else {
                throw "Sorry, installing packages is unsupported on $script:os"
            }
        }

        function Format-UpstreamVersion {
            param(
                [Parameter(Mandatory=$true)]
                [string] $Version
            )

            switch -Exact ($Version) {
                # Since version 7, they dropped the .0 suffix. The earliest
                # version supported is 3.9 on Bionic; versions 4, 5 and 6 are
                # mapped to LLVM-friendly 4.0, 5.0 and 6.0.
                '4' { '4.0' }
                '5' { '5.0' }
                '6' { '6.0' }
                default { $Version }
            }
        }

        function Format-AptLine {
            param(
                [Parameter(Mandatory=$true)]
                [string] $Version
            )

            if (!(Get-Command lsb_release -ErrorAction SilentlyContinue)) {
                throw "Couldn't find lsb_release; LLVM only provides repositories for Debian/Ubuntu"
            }
            $codename = lsb_release -sc

            "deb http://apt.llvm.org/$codename/ llvm-toolchain-$codename-$Version main"
        }

        function Add-UpstreamRepo {
            param(
                [Parameter(Mandatory=$true)]
                [string] $Version
            )

            wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
            $apt_line = Format-AptLine $Version
            sudo add-apt-repository --yes --update $apt_line
        }

        $clang = 'clang'
        $clangxx = 'clang++'
        $lld = 'ld.lld'

        if ($linux_host) {
            $pkg_clang = 'clang'
            $pkg_clang_format = 'clang-format'
            $pkg_clang_tidy = 'clang-tidy'
            $pkg_llvm = 'llvm'
            $pkg_rt_dev = 'libclang-rt-dev'
            $pkg_gxx = 'g++'
            $pkg_lld = 'lld'

            if (!$latest) {
                $pkg_version = Format-UpstreamVersion $version
                Add-UpstreamRepo $pkg_version

                $pkg_clang = "$pkg_clang-$pkg_version"
                $pkg_clang_format = "$pkg_clang_format-$pkg_version"
                $pkg_clang = "$pkg_clang_tidy-$pkg_version"
                $pkg_llvm = "$pkg_llvm-$pkg_version"
                $pkg_rt_dev = "libclang-rt-$pkg_version-dev"
                $pkg_lld = "$pkg_lld-$pkg_version"

                $clang = "$clang-$pkg_version"
                $clangxx = "$clangxx-$pkg_version"
                $lld = "$lld-$pkg_version"
            }
            if (!$x64) {
                $pkg_gxx = 'g++-multilib'
            }

            $packages = $pkg_clang,$pkg_clang_format,$pkg_clang_tidy,$pkg_llvm,$pkg_gxx,$pkg_lld

            # The rt-dev library doesn't seem to exist for versions below 14:
            #     https://apt.llvm.org/jammy/pool/main/l/
            if ($latest -or [int]$version -ge 14) {
                if (Select-String -Path /etc/os-release -Pattern 'VERSION_CODENAME=jammy' -SimpleMatch -Quiet) {
                    if (-not $latest -and [int]$version -ne 15) {
                        # On Jammy, there's a file conflict between rt-dev and common-dev for version 15.
                        # Additionally, the whole installation thing was only checked on Github-hosted
                        # runners (Jammy & Noble at the moment of writing).
                        # _And_ there's no default libclang-rt-dev package on Jammy.
                        $packages += $pkg_rt_dev
                    }
                } else {
                    $packages += $pkg_rt_dev
                }
            }

            Install-Package $packages
        } elseif ($windows_host) {
            Install-Package llvm
            echo 'C:\Program Files\LLVM\bin' >> $env:GITHUB_PATH
        } else {
            throw "Sorry, installing Clang is unsupported on $os"
        }

        echo "clang=$clang"     >> $env:GITHUB_OUTPUT
        echo "clangxx=$clangxx" >> $env:GITHUB_OUTPUT
        echo "lld=$lld"         >> $env:GITHUB_OUTPUT
      shell: pwsh

    - env:
        ARG_CC: ${{ inputs.cc }}
        CLANG: ${{ steps.install.outputs.clang }}
        CLANGXX: ${{ steps.install.outputs.clangxx }}
      run: |
        $linux_host = '${{ runner.os }}' -eq 'Linux'
        $windows_host = '${{ runner.os }}' -eq 'Windows'

        $cc = (Get-ChildItem env:ARG_CC).Value -eq '1'

        $clang   = (Get-ChildItem env:CLANG).Value
        $clangxx = (Get-ChildItem env:CLANGXX).Value

        function Link-Exe {
            param(
                [Parameter(Mandatory=$true)]
                [string] $Exe,
                [Parameter(Mandatory=$true)]
                [string] $LinkName
            )

            $exe_path = (Get-Command $Exe).Path
            $link_dir = if ($script:windows_host) { Split-Path $exe_path } else { '/usr/local/bin' }
            $link_name = if ($script:windows_host) { "$LinkName.exe" } else { $LinkName }
            $link_path = Join-Path $link_dir $link_name
            echo "Creating link $link_path -> $exe_path"
            if ($script:linux_host) {
                sudo ln -f -s $exe_path $link_path
            } elseif ($script:windows_host) {
                New-Item -ItemType HardLink -Path $link_path -Value $exe_path -Force | Out-Null
            }
        }

        if ($cc) {
            Link-Exe $clang cc
            if ($clang -ne 'clang') {
                Link-Exe $clang 'clang'
            }
            Link-Exe $clangxx c++
            if ($clangxx -ne 'clang++') {
                Link-Exe $clangxx 'clang++'
            }
        }
      shell: pwsh

branding:
  icon: star
  color: green