|
| 1 | +#!/usr/bin/env instantfpc |
| 2 | + |
| 3 | +program Make; |
| 4 | +{$mode objfpc}{$H+} |
| 5 | +{$unitpath /usr/lib64/lazarus/components/lazutils} |
| 6 | +uses |
| 7 | + Classes, |
| 8 | + SysUtils, |
| 9 | + StrUtils, |
| 10 | + FileUtil, |
| 11 | + Zipper, |
| 12 | + fphttpclient, |
| 13 | + RegExpr, |
| 14 | + openssl, |
| 15 | + opensslsockets, |
| 16 | + Process; |
| 17 | + |
| 18 | +const |
| 19 | + Target: string = 'test'; |
| 20 | + Dependencies: array of string = ('BGRABitmap'); |
| 21 | + |
| 22 | +type |
| 23 | + TLog = (audit, info, error); |
| 24 | + Output = record |
| 25 | + Success: boolean; |
| 26 | + Output: string; |
| 27 | + end; |
| 28 | + |
| 29 | + procedure OutLog(Knd: TLog; Msg: string); |
| 30 | + begin |
| 31 | + case Knd of |
| 32 | + error: Writeln(stderr, #27'[31m', Msg, #27'[0m'); |
| 33 | + info: Writeln(stderr, #27'[32m', Msg, #27'[0m'); |
| 34 | + audit: Writeln(stderr, #27'[33m', Msg, #27'[0m'); |
| 35 | + end; |
| 36 | + end; |
| 37 | + |
| 38 | + function CheckModules: Output; |
| 39 | + begin |
| 40 | + if FileExists('.gitmodules') then |
| 41 | + if RunCommand('git', ['submodule', 'update', '--init', '--recursive', |
| 42 | + '--force', '--remote'], Result.Output) then |
| 43 | + OutLog(info, Result.Output); |
| 44 | + end; |
| 45 | + |
| 46 | + function AddPackage(Path: string): Output; |
| 47 | + begin |
| 48 | + with TRegExpr.Create do |
| 49 | + begin |
| 50 | + Expression := |
| 51 | + {$IFDEF MSWINDOWS} |
| 52 | + '(cocoa|x11|_template)' |
| 53 | + {$ELSE} |
| 54 | + '(cocoa|gdi|_template)' |
| 55 | + {$ENDIF} |
| 56 | + ; |
| 57 | + if not Exec(Path) and RunCommand('lazbuild', ['--add-package-link', Path], |
| 58 | + Result.Output) then |
| 59 | + OutLog(audit, 'added ' + Path); |
| 60 | + Free; |
| 61 | + end; |
| 62 | + end; |
| 63 | + |
| 64 | + function BuildProject(Path: string): Output; |
| 65 | + var |
| 66 | + Line: string; |
| 67 | + begin |
| 68 | + OutLog(audit, 'build from ' + Path); |
| 69 | + try |
| 70 | + Result.Success := RunCommand('lazbuild', ['--build-all', '--recursive', |
| 71 | + '--no-write-project', Path], Result.Output); |
| 72 | + if Result.Success then |
| 73 | + for Line in SplitString(Result.Output, LineEnding) do |
| 74 | + begin |
| 75 | + if ContainsStr(Line, 'Linking') then |
| 76 | + begin |
| 77 | + Result.Output := SplitString(Line, ' ')[2]; |
| 78 | + OutLog(info, ' to ' + Result.Output); |
| 79 | + break; |
| 80 | + end; |
| 81 | + end |
| 82 | + else |
| 83 | + begin |
| 84 | + ExitCode += 1; |
| 85 | + for Line in SplitString(Result.Output, LineEnding) do |
| 86 | + with TRegExpr.Create do |
| 87 | + begin |
| 88 | + Expression := '(Fatal|Error):'; |
| 89 | + if Exec(Line) then |
| 90 | + OutLog(error, #10 + Line); |
| 91 | + Free; |
| 92 | + end; |
| 93 | + end; |
| 94 | + except |
| 95 | + on E: Exception do |
| 96 | + OutLog(error, E.ClassName + #13#10 + E.Message); |
| 97 | + end; |
| 98 | + end; |
| 99 | + |
| 100 | + function RunTest(Path: string): Output; |
| 101 | + var |
| 102 | + Temp: string; |
| 103 | + begin |
| 104 | + Result := BuildProject(Path); |
| 105 | + Temp:= Result.Output; |
| 106 | + if Result.Success then |
| 107 | + try |
| 108 | + if not RunCommand(Temp, ['--all', '--format=plain', '--progress'], Result.Output) then |
| 109 | + begin |
| 110 | + ExitCode += 1; |
| 111 | + OutLog(error, Result.Output); |
| 112 | + end; |
| 113 | + except |
| 114 | + on E: Exception do |
| 115 | + OutLog(error, E.ClassName + #13#10 + E.Message); |
| 116 | + end; |
| 117 | + end; |
| 118 | + |
| 119 | + function InstallOPM(Path: string): string; |
| 120 | + var |
| 121 | + OutFile, Uri: string; |
| 122 | + Zip: TStream; |
| 123 | + begin |
| 124 | + Result := |
| 125 | + {$IFDEF MSWINDOWS} |
| 126 | + GetEnvironmentVariable('APPDATA') + '\.lazarus\onlinepackagemanager\packages\' |
| 127 | + {$ELSE} |
| 128 | + GetEnvironmentVariable('HOME') + '/.lazarus/onlinepackagemanager/packages/' |
| 129 | + {$ENDIF} |
| 130 | + + Path; |
| 131 | + OutFile := GetTempFileName; |
| 132 | + Uri := 'https://packages.lazarus-ide.org/' + Path + '.zip'; |
| 133 | + if not DirectoryExists(Result) then |
| 134 | + begin |
| 135 | + Zip := TFileStream.Create(OutFile, fmCreate or fmOpenWrite); |
| 136 | + with TFPHttpClient.Create(nil) do |
| 137 | + begin |
| 138 | + try |
| 139 | + AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); |
| 140 | + AllowRedirect := True; |
| 141 | + Get(Uri, Zip); |
| 142 | + OutLog(audit, 'Download from ' + Uri + ' to ' + OutFile); |
| 143 | + finally |
| 144 | + Free; |
| 145 | + Zip.Free; |
| 146 | + end; |
| 147 | + end; |
| 148 | + CreateDir(Result); |
| 149 | + with TUnZipper.Create do |
| 150 | + begin |
| 151 | + try |
| 152 | + FileName := OutFile; |
| 153 | + OutputPath := Result; |
| 154 | + Examine; |
| 155 | + UnZipAllFiles; |
| 156 | + OutLog(audit, 'Unzip from ' + OutFile + ' to ' + Result); |
| 157 | + finally |
| 158 | + Free; |
| 159 | + end; |
| 160 | + end; |
| 161 | + DeleteFile(OutFile); |
| 162 | + end; |
| 163 | + end; |
| 164 | + |
| 165 | + procedure BuildAll; |
| 166 | + var |
| 167 | + Each: string; |
| 168 | + List: TStringList; |
| 169 | + begin |
| 170 | + CheckModules; |
| 171 | + InitSSLInterface; |
| 172 | + List := FindAllFiles(GetCurrentDir, '*.lpk', True); |
| 173 | + try |
| 174 | + for Each in Dependencies do |
| 175 | + List.AddStrings(FindAllFiles(InstallOPM(Each), '*.lpk', True)); |
| 176 | + for Each in List do |
| 177 | + AddPackage(Each); |
| 178 | + List := FindAllFiles(Target, '*.lpi', True); |
| 179 | + for Each in List do |
| 180 | + if not ContainsStr(Each, 'zengl') then |
| 181 | + if ContainsStr(ReadFileToString(ReplaceStr(Each, '.lpi', '.lpr')), |
| 182 | + 'consoletestrunner') then |
| 183 | + RunTest(Each) |
| 184 | + else |
| 185 | + BuildProject(Each); |
| 186 | + finally |
| 187 | + List.Free; |
| 188 | + end; |
| 189 | + end; |
| 190 | + |
| 191 | +begin |
| 192 | + if ParamCount <> 0 then |
| 193 | + case ParamStr(1) of |
| 194 | + 'build': BuildAll; |
| 195 | + else OutLog(audit, 'Nothing!'); |
| 196 | + end; |
| 197 | + if ExitCode <> 0 then |
| 198 | + OutLog(error, #10 + 'Errors: ' + IntToStr(ExitCode)) |
| 199 | + else |
| 200 | + OutLog(info, #10 + 'Errors: ' + IntToStr(ExitCode)); |
| 201 | +end. |
0 commit comments