utils.iss.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. { Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. SPDX-License-Identifier: Apache-2.0 }
  3. { ------------------------------ Helper functions from libcmdlinerunner.dll ------------------------------ }
  4. function ProcStart(cmdline, workdir: string): Longword;
  5. external 'proc_start@files:cmdlinerunner.dll cdecl';
  6. function ProcGetExitCode(inst: Longword): DWORD;
  7. external 'proc_get_exit_code@files:cmdlinerunner.dll cdecl';
  8. function ProcGetOutput(inst: Longword; dest: PAnsiChar; sz: DWORD): DWORD;
  9. external 'proc_get_output@files:cmdlinerunner.dll cdecl';
  10. procedure ProcEnd(inst: Longword);
  11. external 'proc_end@files:cmdlinerunner.dll cdecl';
  12. { ------------------------------ WinAPI functions ------------------------------ }
  13. #ifdef UNICODE
  14. #define AW "W"
  15. #else
  16. #define AW "A"
  17. #endif
  18. function SetEnvironmentVariable(lpName: string; lpValue: string): BOOL;
  19. external 'SetEnvironmentVariable{#AW}@kernel32.dll stdcall';
  20. { ------------------------------ Functions to query the registry ------------------------------ }
  21. { Utility to search in HKLM and HKCU for an installation path. Looks in both 64-bit & 32-bit registry. }
  22. function GetInstallPath(key, valuename : String) : String;
  23. var
  24. value: String;
  25. begin
  26. Result := '';
  27. if RegQueryStringValue(HKEY_LOCAL_MACHINE, key, valuename, value) then
  28. begin
  29. Result := value;
  30. exit;
  31. end;
  32. if RegQueryStringValue(HKEY_CURRENT_USER, key, valuename, value) then
  33. begin
  34. Result := value;
  35. exit;
  36. end;
  37. { This is 32-bit setup running on 64-bit Windows, but ESP-IDF can use 64-bit tools also }
  38. if IsWin64 and RegQueryStringValue(HKLM64, key, valuename, value) then
  39. begin
  40. Result := value;
  41. exit;
  42. end;
  43. if IsWin64 and RegQueryStringValue(HKCU64, key, valuename, value) then
  44. begin
  45. Result := value;
  46. exit;
  47. end;
  48. end;
  49. { ------------------------------ Function to exit from the installer ------------------------------ }
  50. procedure AbortInstallation(Message: String);
  51. begin
  52. MsgBox(Message, mbError, MB_OK);
  53. Abort();
  54. end;
  55. { ------------------------------ File system related functions ------------------------------ }
  56. function DirIsEmpty(DirName: String): Boolean;
  57. var
  58. FindRec: TFindRec;
  59. begin
  60. Result := True;
  61. if FindFirst(DirName+'\*', FindRec) then begin
  62. try
  63. repeat
  64. if (FindRec.Name <> '.') and (FindRec.Name <> '..') then begin
  65. Result := False;
  66. break;
  67. end;
  68. until not FindNext(FindRec);
  69. finally
  70. FindClose(FindRec);
  71. end;
  72. end;
  73. end;
  74. type
  75. TFindFileCallback = procedure(Filename: String);
  76. procedure FindFileRecursive(Directory: string; FileName: string; Callback: TFindFileCallback);
  77. var
  78. FindRec: TFindRec;
  79. FilePath: string;
  80. begin
  81. if FindFirst(Directory + '\*', FindRec) then
  82. begin
  83. try
  84. repeat
  85. if (FindRec.Name = '.') or (FindRec.Name = '..') then
  86. continue;
  87. FilePath := Directory + '\' + FindRec.Name;
  88. if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
  89. begin
  90. FindFileRecursive(FilePath, FileName, Callback);
  91. end else if CompareText(FindRec.Name, FileName) = 0 then
  92. begin
  93. Callback(FilePath);
  94. end;
  95. until not FindNext(FindRec);
  96. finally
  97. FindClose(FindRec);
  98. end;
  99. end;
  100. end;
  101. { ------------------------------ Version related functions ------------------------------ }
  102. function VersionExtractMajorMinor(Version: String; var Major: Integer; var Minor: Integer): Boolean;
  103. var
  104. Delim: Integer;
  105. MajorStr, MinorStr: String;
  106. OrigVersion, ExpectedPrefix: String;
  107. begin
  108. Result := False;
  109. OrigVersion := Version;
  110. Delim := Pos('.', Version);
  111. if Delim = 0 then exit;
  112. MajorStr := Version;
  113. Delete(MajorStr, Delim, Length(MajorStr));
  114. Delete(Version, 1, Delim);
  115. Major := StrToInt(MajorStr);
  116. Delim := Pos('.', Version);
  117. if Delim = 0 then Delim := Length(MinorStr);
  118. MinorStr := Version;
  119. Delete(MinorStr, Delim, Length(MinorStr));
  120. Delete(Version, 1, Delim);
  121. Minor := StrToInt(MinorStr);
  122. { Sanity check }
  123. ExpectedPrefix := IntToStr(Major) + '.' + IntToStr(Minor);
  124. if Pos(ExpectedPrefix, OrigVersion) <> 1 then
  125. begin
  126. Log('VersionExtractMajorMinor: version=' + OrigVersion + ', expected=' + ExpectedPrefix);
  127. exit;
  128. end;
  129. Result := True;
  130. end;