aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/.github/actions/check-runtime-library/action.yml
blob: 32d2246f92704c14db3f815fdf8e1c9221b27eff (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
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-Objdump {
            $objdump = Get-Command 'objdump' -ErrorAction SilentlyContinue
            if ($objdump) {
                return $objdump.Path
            }
            $objdump = 'C:\ProgramData\chocolatey\bin\objdump.exe'
            if (Test-Path $objdump -Type Leaf) {
                return $objdump
            }
            $objdump = 'C:\mingw64\bin\objdump.exe'
            if (Test-Path $objdump -Type Leaf) {
                return $objdump
            }
            return 'objdump'
        }

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

            $objdump = 'objdump'
            if ($env:CI_HOST_WINDOWS) {
                $objdump = Get-Objdump
            }

            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,

                [ValidateNotNull()]
                [string[]] $Forbidden
            )

            echo 'Linked libraries:'
            echo $Actual

            $unexpected = $Actual | ?{$_ -in $Forbidden}

            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_forbidden = @('msvcrt.dll')
            if ($env:CI_MINGW) {
                if (!$env:CI_HOST_WINDOWS) {
                    $windows_forbidden = @()
                }
            }
            # Linking libstdc++ statically on Cygwin is broken, see the
            # cygwin_static_libstdc++.yml workflow.
            $cygwin_forbidden = @('msvcrt.dll')
            $linux_forbidden = @()

            if ($env:CI_TARGET_CYGWIN) {
                Do-ValidateLinkedLibraries `
                    -Actual $Actual `
                    -Forbidden $cygwin_forbidden
            } elseif ($env:CI_TARGET_WINDOWS) {
                Do-ValidateLinkedLibraries `
                    -Actual $Actual `
                    -Forbidden $windows_forbidden
            } elseif ($env:CI_TARGET_LINUX) {
                Do-ValidateLinkedLibraries `
                    -Actual $Actual `
                    -Forbidden $linux_forbidden
            } 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