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