aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/.github/actions/check-runtime-library/action.yml
blob: 9a572b8e6328d6a05cc2d2f630576701fd20e2ec (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
name: Verify runtime library linkage
description: Verify runtime library linkage (static or shared)
inputs:
  path:
    description: Installation directory
    required: true
runs:
  using: composite
  steps:
    - run: |
        New-Variable path -Value '${{ inputs.path }}' -Option Constant

        function Parse-DllLine {
            param(
                [Parameter(Mandatory=$true)]
                [string] $Line
            )

            if ($Line -match '^\s*DLL Name:\s*(?<dll>\S+)\s*$') {
                $Matches.dll
            } else {
                throw "Line doesn't match the pattern: $Line"
            }
        }

        function Parse-PeHeaders {
            param(
                [Parameter(ValueFromPipeline)]
                [string[]] $Output
            )

            process {
                $Output | Select-String 'DLL Name:' -SimpleMatch -CaseSensitive | %{
                    Parse-DllLine $_
                }
            }
        }

        function Parse-NeededLine {
            param(
                [Parameter(Mandatory=$true)]
                [string] $Line
            )

            if ($Line -match '^\s*NEEDED\s*(?<dll>\S+)\s*$') {
                $Matches.dll
            } else {
                throw "Line doesn't match the pattern: $Line"
            }
        }

        function Parse-ElfHeaders {
            param(
                [Parameter(ValueFromPipeline)]
                [string[]] $Output
            )

            process {
                $Output | Select-String 'NEEDED' -SimpleMatch -CaseSensitive | %{
                    Parse-NeededLine $_
                }
            }
        }

        function Get-LinkedLibraries {
            param(
                [Parameter(Mandatory=$true)]
                [string] $ExePath
            )

            $objdump = 'objdump'
            if ($env:CI_HOST_WINDOWS) {
                $objdump = 'C:\ProgramData\chocolatey\bin\objdump.exe'
            }

            if ($env:CI_TARGET_PE) {
                & $objdump -x $ExePath | Parse-PeHeaders | echo
            }
            if ($env:CI_TARGET_ELF) {
                & $objdump -x $ExePath | Parse-ElfHeaders | echo
            }
        }

        function Do-ValidateLinkedLibraries {
            param(
                [Parameter(Mandatory=$true)]
                [ValidateNotNull()]
                [string[]] $Actual,

                [Parameter(Mandatory=$true)]
                [ValidateNotNull()]
                [string[]] $Required,

                [Parameter(Mandatory=$true)]
                [ValidateNotNull()]
                [string[]] $Optional
            )

            echo 'Linked libraries:'
            echo $Actual

            $missing = $Required | ?{$_ -notin $Actual}

            if ($missing.Count -gt 0) {
                throw "Doesn't link to the following libraries: $($missing -join ', ')"
            }

            $unexpected = $Actual | ?{$_ -notin $Required -and $_ -notin $Optional}

            if ($unexpected.Count -gt 0) {
                throw "Links to the following unexpected libraries: $($unexpected -join ', ')"
            }
        }

        function Validate-LinkedLibraries {
            param(
                [Parameter(Mandatory=$true)]
                [ValidateNotNull()]
                [string[]] $Actual
            )

            $windows_required = @('KERNEL32.dll')
            if ($env:CI_MINGW) {
                if ($env:CI_HOST_WINDOWS) {
                    # This is only true for windows-2022 images.
                    $windows_required += 'ucrtbase.dll'
                } else {
                    $windows_required += 'msvcrt.dll'
                }
            }
            # Linking libstdc++ statically on Cygwin is broken, see the
            # cygwin_static_libstdc++.yml workflow.
            $cygwin_required = $windows_required + @('cygwin1.dll','cygstdc++-6.dll')
            $linux_required = @('libc.so.6')

            $windows_optional = @('baz.dll')
            if ($env:CI_MINGW) {
                $windows_optional = @('libbaz.dll','USER32.dll')
            }
            $cygwin_optional = @('cygbaz.dll')
            $linux_optional = @('libbaz.so','ld-linux.so.2','ld-linux-x86-64.so.2','libm.so.6')

            if ($env:CI_TARGET_CYGWIN) {
                Do-ValidateLinkedLibraries `
                    -Actual $Actual `
                    -Required $cygwin_required `
                    -Optional $cygwin_optional
            } elseif ($env:CI_TARGET_WINDOWS) {
                Do-ValidateLinkedLibraries `
                    -Actual $Actual `
                    -Required $windows_required `
                    -Optional $windows_optional
            } elseif ($env:CI_TARGET_LINUX) {
                Do-ValidateLinkedLibraries `
                    -Actual $Actual `
                    -Required $linux_required `
                    -Optional $linux_optional
            } else {
                throw 'Where am I?'
            }
        }

        $exe_path = (Join-Path $path 'bin' 'foo') + $env:CI_EXE_EXT
        $libraries = Get-LinkedLibraries $exe_path
        Validate-LinkedLibraries $libraries
      shell: pwsh