idf_setup.iss.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. { Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. SPDX-License-Identifier: Apache-2.0 }
  3. { ------------------------------ Downloading ESP-IDF ------------------------------ }
  4. var
  5. IDFZIPFileVersion, IDFZIPFileName: String;
  6. function GetIDFPath(Unused: String): String;
  7. begin
  8. if IDFUseExisting then
  9. Result := IDFExistingPath
  10. else
  11. Result := IDFDownloadPath;
  12. end;
  13. function GetIDFZIPFileVersion(Version: String): String;
  14. var
  15. ReleaseVerPart: String;
  16. i: Integer;
  17. Found: Boolean;
  18. begin
  19. if WildCardMatch(Version, 'v*') or WildCardMatch(Version, 'v*-rc*') then
  20. Result := Version
  21. else if Version = 'master' then
  22. Result := ''
  23. else if WildCardMatch(Version, 'release/v*') then
  24. begin
  25. ReleaseVerPart := Version;
  26. Log('ReleaseVerPart=' + ReleaseVerPart)
  27. Delete(ReleaseVerPart, 1, Length('release/'));
  28. Log('ReleaseVerPart=' + ReleaseVerPart)
  29. Found := False;
  30. for i := 0 to GetArrayLength(IDFDownloadAvailableVersions) - 1 do
  31. begin
  32. if Pos(ReleaseVerPart, IDFDownloadAvailableVersions[i]) = 1 then
  33. begin
  34. Result := IDFDownloadAvailableVersions[i];
  35. Found := True;
  36. break;
  37. end;
  38. end;
  39. if not Found then
  40. Result := '';
  41. end;
  42. Log('GetIDFZIPFileVersion(' + Version + ')=' + Result);
  43. end;
  44. procedure IDFAddDownload();
  45. var
  46. Url, MirrorUrl: String;
  47. begin
  48. IDFZIPFileVersion := GetIDFZIPFileVersion(IDFDownloadVersion);
  49. if IDFZIPFileVersion <> '' then
  50. begin
  51. Url := 'https://github.com/espressif/esp-idf/releases/download/' + IDFZIPFileVersion + '/esp-idf-' + IDFZIPFileVersion + '.zip';
  52. MirrorUrl := 'https://dl.espressif.com/dl/esp-idf/releases/esp-idf-' + IDFZIPFileVersion + '.zip';
  53. IDFZIPFileName := ExpandConstant('{app}\releases\esp-idf-' + IDFZIPFileVersion + '.zip')
  54. if not FileExists(IDFZIPFileName) then
  55. begin
  56. ForceDirectories(ExpandConstant('{app}\releases'))
  57. Log('Adding download: ' + Url + ', mirror: ' + MirrorUrl + ', destination: ' + IDFZIPFileName);
  58. idpAddFile(Url, IDFZIPFileName);
  59. idpAddMirror(Url, MirrorUrl);
  60. end else begin
  61. Log(IDFZIPFileName + ' already exists')
  62. end;
  63. end;
  64. end;
  65. procedure RemoveAlternatesFile(Path: String);
  66. begin
  67. Log('Removing ' + Path);
  68. DeleteFile(Path);
  69. end;
  70. {
  71. Replacement of the '--dissociate' flag of 'git clone', to support older versions of Git.
  72. '--reference' is supported for submodules since git 2.12, but '--dissociate' only from 2.18.
  73. }
  74. procedure GitRepoDissociate(Path: String);
  75. var
  76. CmdLine: String;
  77. begin
  78. CmdLine := GitExecutablePath + ' -C ' + Path + ' repack -d -a'
  79. DoCmdlineInstall('Finishing ESP-IDF installation', 'Re-packing the repository', CmdLine);
  80. CmdLine := GitExecutablePath + ' -C ' + Path + ' submodule foreach git repack -d -a'
  81. DoCmdlineInstall('Finishing ESP-IDF installation', 'Re-packing the submodules', CmdLine);
  82. FindFileRecursive(Path + '\.git', 'alternates', @RemoveAlternatesFile);
  83. end;
  84. { Run git reset --hard in the repo and in the submodules, to fix the newlines. }
  85. procedure GitRepoFixNewlines(Path: String);
  86. var
  87. CmdLine: String;
  88. begin
  89. CmdLine := GitExecutablePath + ' -C ' + Path + ' reset --hard';
  90. Log('Resetting the repository: ' + CmdLine);
  91. DoCmdlineInstall('Finishing ESP-IDF installation', 'Updating newlines', CmdLine);
  92. Log('Resetting the submodules: ' + CmdLine);
  93. CmdLine := GitExecutablePath + ' -C ' + Path + ' submodule foreach git reset --hard';
  94. DoCmdlineInstall('Finishing ESP-IDF installation', 'Updating newlines in submodules', CmdLine);
  95. end;
  96. {
  97. There are 3 possible ways how an ESP-IDF copy can be obtained:
  98. - Download the .zip archive with submodules included, extract to destination directory,
  99. then do 'git reset --hard' and 'git submodule foreach git reset --hard' to correct for
  100. possibly different newlines. This is done for release versions.
  101. - Do a git clone of the Github repository into the destination directory.
  102. This is done for the master branch.
  103. - Download the .zip archive of a "close enough" release version, extract into a temporary
  104. directory. Then do a git clone of the Github repository, using the temporary directory
  105. as a '--reference'. This is done for other versions (such as release branches).
  106. }
  107. procedure IDFDownload();
  108. var
  109. CmdLine: String;
  110. IDFTempPath: String;
  111. IDFPath: String;
  112. NeedToClone: Boolean;
  113. begin
  114. IDFPath := IDFDownloadPath;
  115. { If there is a release archive to download, IDFZIPFileName and IDFZIPFileVersion will be set.
  116. See GetIDFZIPFileVersion function.
  117. }
  118. if IDFZIPFileName <> '' then
  119. begin
  120. if IDFZIPFileVersion <> IDFDownloadVersion then
  121. begin
  122. { The version of .zip file downloaded is not the same as the version the user has requested.
  123. Will use 'git clone --reference' to obtain the correct version, using the contents
  124. of the .zip file as reference.
  125. }
  126. NeedToClone := True;
  127. end;
  128. ExtractTemporaryFile('7za.exe')
  129. CmdLine := ExpandConstant('{tmp}\7za.exe x -o' + ExpandConstant('{tmp}') + ' -r -aoa "' + IDFZIPFileName + '"');
  130. IDFTempPath := ExpandConstant('{tmp}\esp-idf-') + IDFZIPFileVersion;
  131. Log('Extracting ESP-IDF reference repository: ' + CmdLine);
  132. Log('Reference repository path: ' + IDFTempPath);
  133. DoCmdlineInstall('Extracting ESP-IDF', 'Setting up reference repository', CmdLine);
  134. end else begin
  135. { IDFZIPFileName is not set, meaning that we will rely on 'git clone'. }
  136. NeedToClone := True;
  137. Log('Not .zip release archive. Will do full clone.');
  138. end;
  139. if NeedToClone then
  140. begin
  141. CmdLine := GitExecutablePath + ' clone --recursive --progress -b ' + IDFDownloadVersion;
  142. if IDFTempPath <> '' then
  143. CmdLine := CmdLine + ' --reference ' + IDFTempPath;
  144. CmdLine := CmdLine + ' https://github.com/espressif/esp-idf.git ' + IDFPath;
  145. Log('Cloning IDF: ' + CmdLine);
  146. DoCmdlineInstall('Downloading ESP-IDF', 'Using git to clone ESP-IDF repository', CmdLine);
  147. if IDFTempPath <> '' then
  148. GitRepoDissociate(IDFPath);
  149. end else begin
  150. Log('Copying ' + IDFTempPath + ' to ' + IDFPath);
  151. if DirExists(IDFPath) then
  152. begin
  153. if not DirIsEmpty(IDFPath) then
  154. begin
  155. MsgBox('Destination directory exists and is not empty: ' + IDFPath, mbError, MB_OK);
  156. RaiseException('Failed to copy ESP-IDF')
  157. end;
  158. end;
  159. { If cmd.exe command argument starts with a quote, the first and last quote chars in the command
  160. will be removed by cmd.exe.
  161. Keys explanation: /s+/e includes all subdirectories, /i assumes that destination is a directory,
  162. /h copies hidden files, /q disables file name logging (making copying faster!)
  163. }
  164. CmdLine := ExpandConstant('cmd.exe /c ""xcopy" /s /e /i /h /q "' + IDFTempPath + '" "' + IDFPath + '""');
  165. DoCmdlineInstall('Extracting ESP-IDF', 'Copying ESP-IDF into the destination directory', CmdLine);
  166. GitRepoFixNewlines(IDFPath);
  167. DelTree(IDFTempPath, True, True, True);
  168. end;
  169. end;
  170. { ------------------------------ IDF Tools setup, Python environment setup ------------------------------ }
  171. function UseBundledIDFToolsPy(Version: String) : Boolean;
  172. begin
  173. Result := False;
  174. { Use bundled copy of idf_tools.py, as the copy shipped with these IDF versions can not work due to
  175. the --no-site-packages bug.
  176. }
  177. if (Version = 'v4.0') or (Version = 'v3.3.1') then
  178. begin
  179. Log('UseBundledIDFToolsPy: version=' + Version + ', using bundled idf_tools.py');
  180. Result := True;
  181. end;
  182. end;
  183. procedure IDFToolsSetup();
  184. var
  185. CmdLine: String;
  186. IDFPath: String;
  187. IDFToolsPyPath: String;
  188. IDFToolsPyCmd: String;
  189. BundledIDFToolsPyPath: String;
  190. JSONArg: String;
  191. begin
  192. IDFPath := GetIDFPath('');
  193. IDFToolsPyPath := IDFPath + '\tools\idf_tools.py';
  194. BundledIDFToolsPyPath := ExpandConstant('{app}\idf_tools_fallback.py');
  195. JSONArg := '';
  196. if FileExists(IDFToolsPyPath) then
  197. begin
  198. Log('idf_tools.py exists in IDF directory');
  199. if UseBundledIDFToolsPy(IDFDownloadVersion) then
  200. begin
  201. Log('Using the bundled idf_tools.py copy');
  202. IDFToolsPyCmd := BundledIDFToolsPyPath;
  203. end else begin
  204. IDFToolsPyCmd := IDFToolsPyPath;
  205. end;
  206. end else begin
  207. Log('idf_tools.py does not exist in IDF directory, using a fallback version');
  208. IDFToolsPyCmd := BundledIDFToolsPyPath;
  209. JSONArg := ExpandConstant('--tools "{app}\tools_fallback.json"');
  210. end;
  211. { IDFPath not quoted, as it can not contain spaces }
  212. IDFToolsPyCmd := PythonExecutablePath + ' "' + IDFToolsPyCmd + '" --idf-path ' + IDFPath + JSONArg;
  213. SetEnvironmentVariable('PYTHONUNBUFFERED', '1')
  214. Log('idf_tools.py command: ' + IDFToolsPyCmd);
  215. CmdLine := IDFToolsPyCmd + ' install';
  216. Log('Installing tools:' + CmdLine);
  217. DoCmdlineInstall('Installing ESP-IDF tools', '', CmdLine);
  218. CmdLine := IDFToolsPyCmd + ' install-python-env';
  219. Log('Installing Python environment:' + CmdLine);
  220. DoCmdlineInstall('Installing Python environment', '', CmdLine);
  221. end;
  222. { ------------------------------ Start menu shortcut ------------------------------ }
  223. procedure CreateIDFCommandPromptShortcut(LnkString: String);
  224. var
  225. Destination: String;
  226. Description: String;
  227. Command: String;
  228. begin
  229. ForceDirectories(ExpandConstant(LnkString));
  230. Destination := ExpandConstant(LnkString + '\{#IDFCmdExeShortcutFile}');
  231. Description := '{#IDFCmdExeShortcutDescription}';
  232. { If cmd.exe command argument starts with a quote, the first and last quote chars in the command
  233. will be removed by cmd.exe; each argument needs to be surrounded by quotes as well. }
  234. Command := ExpandConstant('/k ""{app}\idf_cmd_init.bat" "') + PythonPath + '" "' + GitPath + '""';
  235. Log('CreateShellLink Destination=' + Destination + ' Description=' + Description + ' Command=' + Command)
  236. try
  237. CreateShellLink(
  238. Destination,
  239. Description,
  240. 'cmd.exe',
  241. Command,
  242. GetIDFPath(''),
  243. '', 0, SW_SHOWNORMAL);
  244. except
  245. MsgBox('Failed to create the Start menu shortcut: ' + Destination, mbError, MB_OK);
  246. RaiseException('Failed to create the shortcut');
  247. end;
  248. end;
  249. { ------------------------------ WD exclusion registration ------------------------------ }
  250. procedure RegisterIDFToolsExecutablesInWD();
  251. var
  252. CmdLine: String;
  253. begin
  254. CmdLine := ExpandConstant('powershell -ExecutionPolicy ByPass -File "{app}\dist\tools_WD_excl.ps1" -AddExclPath "{app}\*.exe"');
  255. Log('Registering IDF Tools executables in Windows Defender: ' + CmdLine);
  256. DoCmdlineInstall('Finishing ESP-IDF installation', 'Registering IDF Tools executables in Windows Defender', CmdLine);
  257. end;
  258. <event('CurPageChanged')>
  259. procedure CheckWinDefenderAvailable(CurPageID: Integer);
  260. var
  261. bHasWD: Boolean;
  262. szHasWD: String;
  263. szWDPath: String;
  264. listPSModulePath: TStringList;
  265. x: Integer;
  266. begin
  267. if CurPageID = wpSelectTasks then
  268. begin
  269. listPSModulePath := TStringList.Create;
  270. listPSModulePath.Delimiter := ';';
  271. listPSModulePath.StrictDelimiter := True;
  272. listPSModulePath.DelimitedText := GetEnv('PsModulePath');
  273. Log('Checking PSMODULEPATH for Windows Defender module...');
  274. for x:=0 to (listPSModulePath.Count-1) do
  275. begin
  276. szWDPath := listPSModulePath[x] + '\Defender'
  277. bHasWD := DirExists(szWDPath);
  278. if bHasWD then
  279. begin
  280. szHasWD := 'YES (' + szWDPath + ')';
  281. Break;
  282. end
  283. else
  284. szHasWD := 'NO';
  285. end;
  286. Log('CheckWinDefenderAvailable: ' + szHasWD);
  287. { WD registration checkbox is identified by 'Windows Defender' substring anywhere in its caption.
  288. Please, keep this in mind when making changes }
  289. for x:=0 to (WizardForm.TasksList.Items.Count-1) do
  290. begin
  291. if Pos('Windows Defender', WizardForm.TasksList.ItemCaption[x]) > 0 then
  292. begin
  293. WizardForm.TasksList.ItemEnabled[x] := bHasWD;
  294. WizardForm.TasksList.Checked[x] := bHasWD;
  295. break;
  296. end;
  297. end;
  298. end;
  299. end;