Jens Geyer | a4d458f | 2024-10-10 23:03:57 +0200 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # * |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # * |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | #---- failing tests -------------------------------------------- |
| 21 | |
| 22 | # expected to fail at Thrift Compiler |
| 23 | $FAIL_THRIFT = @( |
| 24 | "BrokenConstants.thrift", # intended to break |
| 25 | "DuplicateImportsTest.thrift", # subdir includes don't work here |
| 26 | "Include.thrift") # subdir includes don't work here |
| 27 | |
| 28 | # expected to fail at Delphi Compiler |
Jens Geyer | 0f0243e | 2024-10-20 17:52:28 +0200 | [diff] [blame] | 29 | $FAIL_DELPHI = @( |
| 30 | "Thrift5320.thrift" # this conflicts with Delphi scopes, but it's a bad practice testcase anyway |
| 31 | ) |
Jens Geyer | a4d458f | 2024-10-10 23:03:57 +0200 | [diff] [blame] | 32 | |
| 33 | # unexpected but known bugs (TODO: fix them) |
Jens Geyer | 0f0243e | 2024-10-20 17:52:28 +0200 | [diff] [blame] | 34 | $KNOWN_BUGS = @( |
| 35 | "IgnoreInitialismsTest.thrift", |
Jens Geyer | 0f0243e | 2024-10-20 17:52:28 +0200 | [diff] [blame] | 36 | "NameConflictTest.thrift" |
| 37 | ) |
Jens Geyer | a4d458f | 2024-10-10 23:03:57 +0200 | [diff] [blame] | 38 | |
| 39 | |
| 40 | |
| 41 | #---- functions -------------------------------------------- |
| 42 | |
| 43 | function FindThriftExe() { |
| 44 | # prefer debug over release over path |
| 45 | write-host -nonewline Looking for thrift.exe ... |
| 46 | $exe = "thrift.exe" |
| 47 | |
| 48 | # if we have a freshly compiled one it might be a better choice |
| 49 | @("Release","Debug") | foreach{ |
| 50 | if( test-path "$ROOTDIR\compiler\cpp\$_\thrift.exe") { $exe = "$ROOTDIR\compiler\cpp\$_\thrift.exe" } |
| 51 | if( test-path "$ROOTDIR\compiler\cpp\compiler\$_\thrift.exe") { $exe = "$ROOTDIR\compiler\cpp\$_\compiler\thrift.exe" } |
| 52 | } |
| 53 | |
| 54 | return $exe |
| 55 | } |
| 56 | |
| 57 | |
| 58 | function FindDcc32Exe() { |
| 59 | # prefer debug over release over path |
| 60 | write-host -nonewline Looking for dcc32.exe ... |
| 61 | $exe = "dcc32.exe" |
| 62 | |
| 63 | # TODO: add arbitraily complex code to locate a suitable dcc32.exe if it is not in the path |
| 64 | |
| 65 | return $exe |
| 66 | } |
| 67 | |
| 68 | |
| 69 | function InitializeFolder([string] $folder, [string] $pattern) { |
| 70 | #write-host $folder\$pattern |
| 71 | if(-not (test-path $folder)) { |
| 72 | new-item $folder -type directory | out-null |
| 73 | } |
| 74 | pushd $folder |
| 75 | remove-item $pattern #-recurse |
| 76 | popd |
| 77 | } |
| 78 | |
| 79 | |
| 80 | function CopyFilesFrom([string] $source, $text) { |
| 81 | #write-host "$source" |
| 82 | if( ($source -ne "") -and (test-path $source)) { |
| 83 | if( $text -ne $null) { |
| 84 | write-host -foregroundcolor yellow Copying $text ... |
| 85 | } |
| 86 | |
| 87 | pushd $source |
| 88 | # recurse dirs |
| 89 | gci . -directory | foreach { |
| 90 | CopyFilesFrom "$_" |
| 91 | } |
| 92 | # files within |
| 93 | gci *.thrift -file | foreach { |
| 94 | #write-host $_ |
| 95 | $name = $_.name |
| 96 | copy-item $_ "$TARGET\$name" |
| 97 | } |
| 98 | popd |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function TestIdlFile([string] $idl) { |
| 103 | # expected to fail at Thrift Compiler |
| 104 | $filter = $false |
| 105 | $FAIL_THRIFT | foreach { |
| 106 | if( $idl -eq $_) { |
| 107 | $filter = $true |
| 108 | write-host "Skipping $_" |
| 109 | } |
| 110 | } |
| 111 | if( $filter) { return $true } |
| 112 | |
| 113 | # compile IDL |
| 114 | #write-host -nonewline " $idl" |
| 115 | InitializeFolder "$TARGET\gen-delphi" "*.pas" |
| 116 | &$THRIFT_EXE $VERBOSE -r --gen delphi:register_types,constprefix,events,xmldoc $idl | out-file "$TARGET\thrift.log" |
| 117 | if( -not $?) { |
| 118 | get-content "$TARGET\thrift.log" | out-default |
| 119 | write-host -foregroundcolor red "Thrift compilation failed: $idl" |
| 120 | return $false |
| 121 | } |
| 122 | |
| 123 | # generate project dile |
| 124 | $units = gci "$TARGET\gen-delphi\*.pas" |
| 125 | $lines = @() |
| 126 | $lines += "program $TESTAPP;" |
| 127 | $lines += "{`$APPTYPE CONSOLE}" |
| 128 | $lines += "" |
| 129 | $lines += "uses" |
| 130 | $units | foreach { $name = $_.name.Replace(".pas",""); $lines += " $name," } |
| 131 | $lines += " Windows, Classes, SysUtils;" |
| 132 | $lines += "" |
| 133 | $lines += "begin" |
| 134 | $lines += " Writeln('Successfully compiled!');" |
| 135 | $lines += " Writeln('List of units:');" |
| 136 | $units | foreach { $name = $_.name.Replace(".pas",""); $lines += " Writeln('- $name');" } |
| 137 | $lines += " Writeln;" |
| 138 | $lines += "" |
| 139 | $lines += "end." |
| 140 | $lines += "" |
| 141 | $lines | set-content "$TARGET\gen-delphi\$TESTAPP.dpr" |
| 142 | |
| 143 | # try to compile the DPR |
| 144 | # this should not throw any errors, warnings or hints |
| 145 | $exe = "$TARGET\gen-delphi\$TESTAPP.exe" |
| 146 | if( test-path $exe) { remove-item $exe } |
| 147 | &$DCC32_EXE -B "$TARGET\gen-delphi\$TESTAPP" -U"$UNITSEARCH" -I"$UNITSEARCH" -N"$OUTDCU" -E"$TARGET\gen-delphi" | out-file "$TARGET\compile.log" |
| 148 | if( -not (test-path $exe)) { |
| 149 | |
| 150 | # expected to fail at Thrift Compiler |
| 151 | $filter = $false |
| 152 | $FAIL_DELPHI | foreach { |
| 153 | if( $idl -eq $_) { |
| 154 | $filter = $true |
| 155 | write-host ("Delphi compilation failed at "+$idl+" - as expected") |
| 156 | } |
| 157 | } |
| 158 | $KNOWN_BUGS | foreach { |
| 159 | if( $idl -eq $_) { |
| 160 | $filter = $true |
| 161 | write-host ("Delphi compilation failed at "+$idl+" - known issue (TODO)") |
| 162 | } |
| 163 | } |
| 164 | if( $filter) { return $true } |
| 165 | |
| 166 | get-content "$TARGET\compile.log" | out-default |
| 167 | write-host -foregroundcolor red "Delphi compilation failed: $idl" |
| 168 | return $false |
| 169 | } |
| 170 | |
| 171 | # The compiled program is now executed. If it hangs or crashes, we |
| 172 | # have a serious problem with the generated code. Expected output |
| 173 | # is "Successfully compiled:" followed by a list of generated units. |
| 174 | &"$exe" | out-file "$TARGET\exec.log" |
| 175 | if( -not $?) { |
| 176 | get-content "$TARGET\exec.log" | out-default |
| 177 | write-host -foregroundcolor red "Test execution failed: $idl" |
| 178 | return $false |
| 179 | } |
| 180 | return $true |
| 181 | } |
| 182 | |
| 183 | #---- main ------------------------------------------------- |
| 184 | # CONFIGURATION BEGIN |
| 185 | # configuration settings, adjust as necessary to meet your system setup |
| 186 | $MY_THRIFT_FILES = "" |
| 187 | $VERBOSE = "" # set any Thrift compiler debug/verbose flag you want |
| 188 | |
| 189 | # init |
| 190 | $ROOTDIR = $PSScriptRoot + "\..\..\..\.." |
| 191 | |
| 192 | # try to find thrift.exe |
| 193 | $THRIFT_EXE = FindThriftExe |
| 194 | &$THRIFT_EXE -version |
| 195 | if( -not $?) { |
| 196 | write-host -foregroundcolor red Missing thrift.exe |
| 197 | exit 1 |
| 198 | } |
| 199 | |
| 200 | # try to find dcc32.exe |
| 201 | $DCC32_EXE = FindDcc32Exe |
| 202 | &$DCC32_EXE --version |
| 203 | if( -not $?) { |
| 204 | write-host -foregroundcolor red Missing dcc32.exe |
| 205 | exit 1 |
| 206 | } |
| 207 | |
| 208 | |
| 209 | # some helpers |
| 210 | $TARGET = "$ROOTDIR\..\thrift-testing" |
| 211 | $TESTAPP = "TestProject" |
| 212 | $UNITSEARCH = "$ROOTDIR\lib\pas\src;$ROOTDIR\lib\delphi\src" |
| 213 | $OUTDCU = "$TARGET\dcu" |
| 214 | |
| 215 | # create and/or empty target dirs |
| 216 | InitializeFolder "$TARGET" "*.thrift" |
| 217 | InitializeFolder "$TARGET\gen-delphi" "*.pas" |
| 218 | InitializeFolder "$OUTDCU" "*.dcu" |
| 219 | |
| 220 | # recurse through thrift WC and "my thrift files" folder |
| 221 | # copies all .thrift files into thrift-testing |
| 222 | CopyFilesFrom "$ROOTDIR" "Thrift IDL files" |
| 223 | CopyFilesFrom "$MY_THRIFT_FILES" "Custom IDL files" |
| 224 | |
| 225 | # codegen and compile all thrift files, one by one to prevent side effects |
| 226 | $count = 0 |
| 227 | write-host -foregroundcolor yellow Running codegen tests .. |
| 228 | try { |
| 229 | pushd "$TARGET" |
| 230 | gci *.thrift -file | foreach { |
| 231 | $count += 1 |
| 232 | $ok = TestIdlFile $_.name |
| 233 | if( -not $ok) { |
| 234 | throw "TEST FAILED" # automated tests |
| 235 | popd; pause; pushd "$TARGET" # interactive / debug |
| 236 | } |
| 237 | } |
| 238 | write-host -foregroundcolor green "Success ($count tests executed)" |
| 239 | exit 0 |
| 240 | } catch { |
| 241 | write-host -foregroundcolor red $_ |
| 242 | exit 1 |
| 243 | } finally { |
| 244 | popd |
| 245 | } |
| 246 | |
| 247 | #eof |