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