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 |
| 29 | $FAIL_DELPHI = @() |
| 30 | |
| 31 | # unexpected but known bugs (TODO: fix them) |
| 32 | $KNOWN_BUGS = @( |
| 33 | "ConstantsDemo.thrift", |
| 34 | "ConstOptionalField.thrift", |
| 35 | "IgnoreInitialismsTest.thrift", |
| 36 | "JavaTypes.thrift", |
| 37 | "JavaDefinitionOrderB.thrift", |
| 38 | "JavaDeepCopyTest.thrift", |
| 39 | "NameConflictTest.thrift", |
| 40 | "Types.thrift", |
| 41 | "Thrift5320.thrift", |
| 42 | "Service.thrift") |
| 43 | |
| 44 | |
| 45 | |
| 46 | #---- functions -------------------------------------------- |
| 47 | |
| 48 | function FindThriftExe() { |
| 49 | # prefer debug over release over path |
| 50 | write-host -nonewline Looking for thrift.exe ... |
| 51 | $exe = "thrift.exe" |
| 52 | |
| 53 | # if we have a freshly compiled one it might be a better choice |
| 54 | @("Release","Debug") | foreach{ |
| 55 | if( test-path "$ROOTDIR\compiler\cpp\$_\thrift.exe") { $exe = "$ROOTDIR\compiler\cpp\$_\thrift.exe" } |
| 56 | if( test-path "$ROOTDIR\compiler\cpp\compiler\$_\thrift.exe") { $exe = "$ROOTDIR\compiler\cpp\$_\compiler\thrift.exe" } |
| 57 | } |
| 58 | |
| 59 | return $exe |
| 60 | } |
| 61 | |
| 62 | |
| 63 | function FindDcc32Exe() { |
| 64 | # prefer debug over release over path |
| 65 | write-host -nonewline Looking for dcc32.exe ... |
| 66 | $exe = "dcc32.exe" |
| 67 | |
| 68 | # TODO: add arbitraily complex code to locate a suitable dcc32.exe if it is not in the path |
| 69 | |
| 70 | return $exe |
| 71 | } |
| 72 | |
| 73 | |
| 74 | function InitializeFolder([string] $folder, [string] $pattern) { |
| 75 | #write-host $folder\$pattern |
| 76 | if(-not (test-path $folder)) { |
| 77 | new-item $folder -type directory | out-null |
| 78 | } |
| 79 | pushd $folder |
| 80 | remove-item $pattern #-recurse |
| 81 | popd |
| 82 | } |
| 83 | |
| 84 | |
| 85 | function CopyFilesFrom([string] $source, $text) { |
| 86 | #write-host "$source" |
| 87 | if( ($source -ne "") -and (test-path $source)) { |
| 88 | if( $text -ne $null) { |
| 89 | write-host -foregroundcolor yellow Copying $text ... |
| 90 | } |
| 91 | |
| 92 | pushd $source |
| 93 | # recurse dirs |
| 94 | gci . -directory | foreach { |
| 95 | CopyFilesFrom "$_" |
| 96 | } |
| 97 | # files within |
| 98 | gci *.thrift -file | foreach { |
| 99 | #write-host $_ |
| 100 | $name = $_.name |
| 101 | copy-item $_ "$TARGET\$name" |
| 102 | } |
| 103 | popd |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | function TestIdlFile([string] $idl) { |
| 108 | # expected to fail at Thrift Compiler |
| 109 | $filter = $false |
| 110 | $FAIL_THRIFT | foreach { |
| 111 | if( $idl -eq $_) { |
| 112 | $filter = $true |
| 113 | write-host "Skipping $_" |
| 114 | } |
| 115 | } |
| 116 | if( $filter) { return $true } |
| 117 | |
| 118 | # compile IDL |
| 119 | #write-host -nonewline " $idl" |
| 120 | InitializeFolder "$TARGET\gen-delphi" "*.pas" |
| 121 | &$THRIFT_EXE $VERBOSE -r --gen delphi:register_types,constprefix,events,xmldoc $idl | out-file "$TARGET\thrift.log" |
| 122 | if( -not $?) { |
| 123 | get-content "$TARGET\thrift.log" | out-default |
| 124 | write-host -foregroundcolor red "Thrift compilation failed: $idl" |
| 125 | return $false |
| 126 | } |
| 127 | |
| 128 | # generate project dile |
| 129 | $units = gci "$TARGET\gen-delphi\*.pas" |
| 130 | $lines = @() |
| 131 | $lines += "program $TESTAPP;" |
| 132 | $lines += "{`$APPTYPE CONSOLE}" |
| 133 | $lines += "" |
| 134 | $lines += "uses" |
| 135 | $units | foreach { $name = $_.name.Replace(".pas",""); $lines += " $name," } |
| 136 | $lines += " Windows, Classes, SysUtils;" |
| 137 | $lines += "" |
| 138 | $lines += "begin" |
| 139 | $lines += " Writeln('Successfully compiled!');" |
| 140 | $lines += " Writeln('List of units:');" |
| 141 | $units | foreach { $name = $_.name.Replace(".pas",""); $lines += " Writeln('- $name');" } |
| 142 | $lines += " Writeln;" |
| 143 | $lines += "" |
| 144 | $lines += "end." |
| 145 | $lines += "" |
| 146 | $lines | set-content "$TARGET\gen-delphi\$TESTAPP.dpr" |
| 147 | |
| 148 | # try to compile the DPR |
| 149 | # this should not throw any errors, warnings or hints |
| 150 | $exe = "$TARGET\gen-delphi\$TESTAPP.exe" |
| 151 | if( test-path $exe) { remove-item $exe } |
| 152 | &$DCC32_EXE -B "$TARGET\gen-delphi\$TESTAPP" -U"$UNITSEARCH" -I"$UNITSEARCH" -N"$OUTDCU" -E"$TARGET\gen-delphi" | out-file "$TARGET\compile.log" |
| 153 | if( -not (test-path $exe)) { |
| 154 | |
| 155 | # expected to fail at Thrift Compiler |
| 156 | $filter = $false |
| 157 | $FAIL_DELPHI | foreach { |
| 158 | if( $idl -eq $_) { |
| 159 | $filter = $true |
| 160 | write-host ("Delphi compilation failed at "+$idl+" - as expected") |
| 161 | } |
| 162 | } |
| 163 | $KNOWN_BUGS | foreach { |
| 164 | if( $idl -eq $_) { |
| 165 | $filter = $true |
| 166 | write-host ("Delphi compilation failed at "+$idl+" - known issue (TODO)") |
| 167 | } |
| 168 | } |
| 169 | if( $filter) { return $true } |
| 170 | |
| 171 | get-content "$TARGET\compile.log" | out-default |
| 172 | write-host -foregroundcolor red "Delphi compilation failed: $idl" |
| 173 | return $false |
| 174 | } |
| 175 | |
| 176 | # The compiled program is now executed. If it hangs or crashes, we |
| 177 | # have a serious problem with the generated code. Expected output |
| 178 | # is "Successfully compiled:" followed by a list of generated units. |
| 179 | &"$exe" | out-file "$TARGET\exec.log" |
| 180 | if( -not $?) { |
| 181 | get-content "$TARGET\exec.log" | out-default |
| 182 | write-host -foregroundcolor red "Test execution failed: $idl" |
| 183 | return $false |
| 184 | } |
| 185 | return $true |
| 186 | } |
| 187 | |
| 188 | #---- main ------------------------------------------------- |
| 189 | # CONFIGURATION BEGIN |
| 190 | # configuration settings, adjust as necessary to meet your system setup |
| 191 | $MY_THRIFT_FILES = "" |
| 192 | $VERBOSE = "" # set any Thrift compiler debug/verbose flag you want |
| 193 | |
| 194 | # init |
| 195 | $ROOTDIR = $PSScriptRoot + "\..\..\..\.." |
| 196 | |
| 197 | # try to find thrift.exe |
| 198 | $THRIFT_EXE = FindThriftExe |
| 199 | &$THRIFT_EXE -version |
| 200 | if( -not $?) { |
| 201 | write-host -foregroundcolor red Missing thrift.exe |
| 202 | exit 1 |
| 203 | } |
| 204 | |
| 205 | # try to find dcc32.exe |
| 206 | $DCC32_EXE = FindDcc32Exe |
| 207 | &$DCC32_EXE --version |
| 208 | if( -not $?) { |
| 209 | write-host -foregroundcolor red Missing dcc32.exe |
| 210 | exit 1 |
| 211 | } |
| 212 | |
| 213 | |
| 214 | # some helpers |
| 215 | $TARGET = "$ROOTDIR\..\thrift-testing" |
| 216 | $TESTAPP = "TestProject" |
| 217 | $UNITSEARCH = "$ROOTDIR\lib\pas\src;$ROOTDIR\lib\delphi\src" |
| 218 | $OUTDCU = "$TARGET\dcu" |
| 219 | |
| 220 | # create and/or empty target dirs |
| 221 | InitializeFolder "$TARGET" "*.thrift" |
| 222 | InitializeFolder "$TARGET\gen-delphi" "*.pas" |
| 223 | InitializeFolder "$OUTDCU" "*.dcu" |
| 224 | |
| 225 | # recurse through thrift WC and "my thrift files" folder |
| 226 | # copies all .thrift files into thrift-testing |
| 227 | CopyFilesFrom "$ROOTDIR" "Thrift IDL files" |
| 228 | CopyFilesFrom "$MY_THRIFT_FILES" "Custom IDL files" |
| 229 | |
| 230 | # codegen and compile all thrift files, one by one to prevent side effects |
| 231 | $count = 0 |
| 232 | write-host -foregroundcolor yellow Running codegen tests .. |
| 233 | try { |
| 234 | pushd "$TARGET" |
| 235 | gci *.thrift -file | foreach { |
| 236 | $count += 1 |
| 237 | $ok = TestIdlFile $_.name |
| 238 | if( -not $ok) { |
| 239 | throw "TEST FAILED" # automated tests |
| 240 | popd; pause; pushd "$TARGET" # interactive / debug |
| 241 | } |
| 242 | } |
| 243 | write-host -foregroundcolor green "Success ($count tests executed)" |
| 244 | exit 0 |
| 245 | } catch { |
| 246 | write-host -foregroundcolor red $_ |
| 247 | exit 1 |
| 248 | } finally { |
| 249 | popd |
| 250 | } |
| 251 | |
| 252 | #eof |