]> git.lyx.org Git - features.git/blob - development/Win32/packaging/installer/include/LaTeX.nsh
Win installer: respect also 64bit MiKTeX
[features.git] / development / Win32 / packaging / installer / include / LaTeX.nsh
1 /*
2 LaTeX.nsh
3
4 Handling of LaTeX distributions
5 */
6
7 # This script contains the following functions:
8 #
9 # - LaTeXActions (checks if MiKTeX or TeXLive is installed)
10 #
11 # - InstallMiKTeX (installs MiKTeX if not already installed),
12 #   only for bunlde installer, uses:
13 #    LaTeXCheck # function from LyXUtils.nsh
14 #
15 # - ConfigureMiKTeX
16 #   (installs the LaTeX class files that are delivered with LyX,
17 #    a Perl interpreter for splitindex and pdfcrop
18 #    and enable MiKTeX's automatic package installation)
19 #
20 # - ConfigureTeXLive
21 #   (installs the LaTeX class files that are delivered with LyX)
22 #
23 # - UpdateMiKTeX (asks to update MiKTeX)
24
25 # ---------------------------------------
26
27 Function LaTeXActions
28  # checks if MiKTeX or TeXLive is installed
29
30   StrCpy $Is64bit "false"
31   
32   ${if} ${RunningX64}
33    SetRegView 64 # the PATH is in the 64bit registry section
34   ${endif}
35   # test if MiKTeX is installed
36   # reads the PATH variable via the registry because NSIS' "$%Path%" variable is not updated when the PATH changes
37   ReadRegStr $String HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path"
38   StrCpy $Search "miktex"
39   Call LaTeXCheck # sets the path to the latex.exe to $PathLaTeX # Function from LyXUtils.nsh
40   
41   StrCpy $String $PathLaTeX
42   StrCpy $Search "x64" # search if it is 64bit MiKTeX
43   Call StrPoint # Function from LyXUtils.nsh
44   ${if} $Pointer != "-1" # if something was found
45    StrCpy $Is64bit "true"
46   ${endif}
47   
48   # check for 32bit MiKTeX
49   ${if} $PathLaTeX != ""
50   ${andif} $Is64bit != "true"
51    ${if} ${RunningX64}
52     SetRegView 32
53    ${endif}
54    # check if MiKTeX 2.8 or newer is installed
55    StrCpy $0 0
56    loop32:
57     EnumRegKey $1 HKLM "SOFTWARE\MiKTeX.org\MiKTeX" $0 # check the last subkey
58     StrCmp $1 "" done32
59     StrCpy $String $1
60     IntOp $0 $0 + 1
61     Goto loop32
62    done32:
63    ${if} $String == "2.8"
64     StrCpy $MiKTeXVersion "2.8"
65     StrCpy $LaTeXName "MiKTeX 2.8"
66    ${endif}
67    ${if} $String == "2.9"
68     StrCpy $MiKTeXVersion "2.9"
69     StrCpy $LaTeXName "MiKTeX 2.9"
70    ${endif}
71   ${endif}
72   
73   # check for 64bit MiKTeX
74   ${if} $LaTeXName == ""
75    ${if} ${RunningX64}
76     SetRegView 64
77    ${endif}
78    # check if MiKTeX 2.8 or newer is installed
79    StrCpy $0 0
80    loop64:
81     EnumRegKey $1 HKLM "SOFTWARE\MiKTeX.org\MiKTeX" $0 # check the last subkey
82     StrCmp $1 "" done64
83     StrCpy $String $1
84     IntOp $0 $0 + 1
85     Goto loop64
86    done64:
87    ${if} $String == "2.8"
88     StrCpy $MiKTeXVersion "2.8"
89     StrCpy $LaTeXName "MiKTeX 2.8"
90    ${endif}
91    ${if} $String == "2.9"
92     StrCpy $MiKTeXVersion "2.9"
93     StrCpy $LaTeXName "MiKTeX 2.9"
94    ${endif}
95   ${endif}
96   
97   ${if} $PathLaTeX != ""
98    StrCpy $MiKTeXUser "HKLM" # needed later to configure MiKTeX
99   ${else} # check if MiKTeX is installed only for the current user
100    ${if} ${RunningX64}
101     SetRegView 64 # the PATH is in the 64bit registry section
102    ${endif}
103    ReadRegStr $String HKCU "Environment" "Path"
104    StrCpy $Search "miktex"
105    Call LaTeXCheck # function from LyXUtils.nsh
106    ${if} $PathLaTeX != ""
107     StrCpy $MiKTeXUser "HKCU"
108    ${endif}
109   ${endif}
110   ${if} $LaTeXName == "" # check for the MiKTeX version
111    StrCpy $0 0
112    loopB:
113     EnumRegKey $1 HKCU "SOFTWARE\MiKTeX.org\MiKTeX" $0 # check the last subkey
114     StrCmp $1 "" doneB
115     StrCpy $String $1
116     IntOp $0 $0 + 1
117     Goto loopB
118    doneB:
119    ${if} $String == "2.8"
120     StrCpy $MiKTeXVersion "2.8"
121     StrCpy $LaTeXName "MiKTeX 2.8"
122    ${endif}
123    ${if} $String == "2.9"
124     StrCpy $MiKTeXVersion "2.9"
125     StrCpy $LaTeXName "MiKTeX 2.9"
126    ${endif}
127   ${endif}
128   
129   ${if} $PathLaTeX != ""
130    StrCpy $String $PathLaTeX
131    StrCpy $Search "x64" # search if it is 64bit MiKTeX
132    Call StrPoint # Function from LyXUtils.nsh
133    ${if} $Pointer != "-1" # if something was found
134     StrCpy $Is64bit "true"
135    ${endif}
136    StrCpy $LaTeXInstalled "MiKTeX"
137    # on some installations the path ends with a "\" on some not
138    # therefore assure that we remove it if it exists
139    StrCpy $0 $PathLaTeX "" -1
140    ${if} $0 == "\"
141     StrCpy $PathLaTeX "$PathLaTeX" -1 # delete "\"
142    ${endif}
143   ${endif}
144   
145   # test if TeXLive is installed
146   # TeXLive can be installed so that it appears in the PATH variable and/or only as current user.
147   # The safest method is to first check for the PATH because this is independent of the TeXLive version.
148   ${if} ${RunningX64}
149    SetRegView 64 # the PATH is in the 64bit registry section
150   ${endif}
151   ${if} $PathLaTeX == ""
152    ReadRegStr $String HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path"
153    StrCpy $Search "TeXLive"
154    Call LaTeXCheck # function from LyXUtils.nsh
155   ${endif}
156   # check for the current user Path variable
157   ${if} $PathLaTeX == ""
158    ReadRegStr $String HKCU "Environment" "Path"
159    StrCpy $Search "texlive"
160    StrCpy $2 "TeXLive"
161    Call LaTeXCheck # function from LyXUtils.nsh
162   ${endif}
163   # check if it was installed to the system
164   ${if} ${RunningX64}
165    SetRegView 32 # TeXLive is a 32bit application
166   ${endif}
167   ${if} $PathLaTeX == ""
168    ReadRegStr $String HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TeXLive2015" "UninstallString"
169    ${if} $String == ""
170     ReadRegStr $String HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\TeXLive2015" "UninstallString"
171    ${endif}
172    ${if} $String != ""
173     StrCpy $String $String -28 # remove '\tlpkg\installer\uninst.bat"'
174     StrCpy $String $String "" 1 # remove the leading quote
175    ${endif}
176    StrCpy $PathLaTeX "$String\bin\win32"
177    # check if the latex.exe exists in the $PathLaTeX folder
178    !insertmacro FileCheck $5 "latex.exe" "$PathLaTeX" # macro from LyXUtils.nsh
179    ${if} $5 == "False"
180     StrCpy $PathLaTeX ""
181    ${endif}
182   ${endif}
183   # finally set the name
184   ${if} $PathLaTeX != ""
185   ${andif} $LaTeXName != "MiKTeX 2.8"
186   ${andif} $LaTeXName != "MiKTeX 2.9"
187    StrCpy $LaTeXInstalled "TeXLive"
188    ReadRegStr $String HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TeXLive2015" "DisplayVersion"
189    ${if} $String == ""
190     ReadRegStr $String HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\TeXLive2015" "DisplayVersion"
191    ${endif}
192    ${if} $String == ""
193     ReadRegStr $String HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TeXLive2014" "DisplayVersion"
194    ${endif}
195    ${if} $String == ""
196     ReadRegStr $String HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\TeXLive2014" "DisplayVersion"
197    ${endif}
198    ${if} $String != ""
199     StrCpy $LaTeXName "TeXLive $String"
200    ${else}
201     StrCpy $LaTeXName "TeXLive"
202    ${endif}
203   ${endif}
204
205 FunctionEnd
206
207 # -------------------------------------------
208
209 !if ${SETUPTYPE} == BUNDLE
210
211  Function InstallMiKTeX
212   # installs MiKTeX if not already installed
213   
214   ${if} $PathLaTeX == ""
215    # launch MiKTeX's installer
216    MessageBox MB_OK|MB_ICONINFORMATION "$(LatexInfo)" /SD IDOK
217    ${if} $MultiUser.Privileges != "Admin"
218    ${andif} $MultiUser.Privileges != "Power"
219     # call the non-admin version
220     ExecWait ${MiKTeXInstall}
221    ${else}
222     ExecWait "${MiKTeXInstall} --shared"
223    ${endif}
224    # test if MiKTeX is installed
225    Call LaTeXActions
226    ${if} ${RunningX64}
227     SetRegView 32 # we install the 32bit version of MiKTeX
228    ${endif}
229    ${if} $PathLaTeX != ""
230     # special entry that it was installed together with LyX
231     # so that we can later uninstall it together with LyX
232     ${if} $MiKTeXUser == "HKCU"
233      WriteRegStr HKCU "SOFTWARE\MiKTeX.org\MiKTeX" "OnlyWithLyX" "Yes${APP_SERIES_KEY}"
234     ${else}
235      WriteRegStr HKLM "SOFTWARE\MiKTeX.org\MiKTeX" "OnlyWithLyX" "Yes${APP_SERIES_KEY}"
236     ${endif}
237     StrCpy $LaTeXInstalled "MiKTeX"
238     StrCpy $MiKTeXVersion ${MiKTeXDeliveredVersion}
239    ${else}
240     MessageBox MB_OK|MB_ICONSTOP "$(LatexError1)" /SD IDOK
241     SetOutPath $TEMP # to be able to delete the $INSTDIR
242     RMDir /r $INSTDIR
243     Abort
244    ${endif} # endif $PathLaTeX != ""
245   ${endif}
246
247   FunctionEnd
248
249 !endif # endif ${SETUPTYPE} == BUNDLE
250
251 # ------------------------------
252
253 Function ConfigureMiKTeX
254  # installs the LaTeX class files that are delivered with LyX,
255  # a Perl interpreter for splitindex and pdfcrop
256  # and enable MiKTeX's automatic package installation
257  
258  # install LyX's LaTeX class and style files and a Perl interpreter
259  ${if} $PathLaTeX != ""
260   ${if} $MultiUser.Privileges == "Admin"
261   ${orif} $MultiUser.Privileges == "Power"
262    ${if} $Is64bit == "true"
263     StrCpy $PathLaTeXLocal "$PathLaTeX" -15 # delete "\miktex\bin\x64"
264    ${else}
265     StrCpy $PathLaTeXLocal "$PathLaTeX" -11 # delete "\miktex\bin"
266    ${endif}
267   ${else}
268    StrCpy $PathLaTeXLocal "$APPDATA\MiKTeX\$MiKTeXVersion"
269   ${endif}
270
271   # only install the LyX packages if they are not already installed
272   ${ifnot} ${FileExists} "$PathLaTeXLocal\tex\latex\lyx\broadway.cls"
273    # dvipost
274    SetOutPath "$PathLaTeXLocal\tex\latex\dvipost"
275    File "${FILES_DVIPOST_PKG}\dvipost.sty"
276    # files in Resources\tex
277    SetOutPath "$PathLaTeXLocal\tex\latex\lyx"
278    CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" "$PathLaTeXLocal\tex\latex\lyx"
279   ${endif}
280   
281   # install a Perl interpreter for splitindex and pdfcrop
282   SetOutPath "$INSTDIR"
283   # recursively copy all files under Perl
284   File /r "${FILES_PERL}"
285   
286   # refresh MiKTeX's file name database (do this always to assure everything is in place)
287   ${if} $MultiUser.Privileges != "Admin"
288   ${andif} $MultiUser.Privileges != "Power"
289    # call the non-admin version
290    nsExec::ExecToLog "$PathLaTeX\initexmf --update-fndb"
291   ${else}
292    ${if} $MiKTeXUser != "HKCU" # call the admin version
293     nsExec::ExecToLog "$PathLaTeX\initexmf --admin --update-fndb"
294    ${else}
295     nsExec::ExecToLog "$PathLaTeX\initexmf --update-fndb"
296    ${endif}
297   ${endif}
298   Pop $UpdateFNDBReturn # Return value
299   
300  ${endif} # end if $PathLaTeX != ""
301   
302   # enable package installation without asking (1 = Yes, 0 = No, 2 = Ask me first) and
303   # if there is not package repository (MiKTeX's primary package repository) then set it
304   ${if} ${RunningX64}
305   ${andif} $Is64bit == "true"
306    SetRegView 64
307   ${else}
308    SetRegView 32
309   ${endif}
310   ${if} $MiKTeXUser == "HKCU" # if only for current user
311    # if AutoInstall is set to "0" we can assume that this was set purposly since the default is "1"
312    ReadRegStr $2 HKCU "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "AutoInstall"
313    ${if} $2 == "0"
314     Goto NoAutoInstall
315    ${endif}
316    WriteRegStr HKCU "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "AutoInstall" "1"
317    ReadRegStr $1 HKCU "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "RemoteRepository"
318    ${if} $1 == ""
319     WriteRegStr HKCU "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "RemoteRepository" "${MiKTeXRepo}" 
320     WriteRegStr HKCU "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "RepositoryType" "remote"
321    ${endif}
322   ${else}
323    # if AutoInstall is set to "0" we can assume that this was set purposly since the default is "1"
324    ReadRegStr $2 HKLM "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "AutoInstall"
325    ${if} $2 == "0"
326     Goto NoAutoInstall
327    ${endif}
328    WriteRegStr HKLM "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "AutoInstall" "1"
329    ReadRegStr $1 HKLM "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "RemoteRepository"
330    ${if} $1 == ""
331     WriteRegStr HKLM "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "RemoteRepository" "${MiKTeXRepo}"
332     WriteRegStr HKLM "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "RepositoryType" "remote"
333    ${endif}
334    # we need to state that missing packages should be installed for all users too
335    WriteRegStr HKCU "SOFTWARE\MiKTeX.org\MiKTeX\$MiKTeXVersion\MPM" "AutoAdmin" "t"
336   ${endif}
337   NoAutoInstall:
338   
339   # update MiKTeX's package file list
340   ${if} $MultiUser.Privileges != "Admin"
341   ${andif} $MultiUser.Privileges != "Power"
342    # call the non-admin version
343    nsExec::ExecToLog "$PathLaTeX\mpm.exe --update-fndb"
344   ${else}
345    ${if} $MiKTeXUser != "HKCU" # call the admin version
346     nsExec::ExecToLog "$PathLaTeX\mpm.exe --admin --update-fndb"
347    ${else}
348     nsExec::ExecToLog "$PathLaTeX\mpm.exe --update-fndb"
349    ${endif}
350   ${endif}
351   Pop $UpdateFNDBReturn # Return value
352   
353   # we must return to 32bit because LyX is a 32bit application
354   SetRegView 32
355   
356 FunctionEnd
357
358 # ------------------------------
359
360 Function ConfigureTeXLive
361  # installs the LaTeX class files that are delivered with LyX
362  # (TeXLive comes already with a Perl interpreter.)
363  
364  ${if} $PathLaTeX != ""
365   StrCpy $PathLaTeXLocal "$PathLaTeX" -10 # delete "\bin\win32"
366   
367   # only install the LyX packages if they are not already installed
368   ${ifnot} ${FileExists} "$PathLaTeXLocal\texmf-dist\tex\latex\lyx\broadway.cls"
369    # dvipost
370    SetOutPath "$PathLaTeXLocal\texmf-dist\tex\latex\dvipost"
371    File "${FILES_DVIPOST_PKG}\dvipost.sty"
372    # files in Resources\tex
373    SetOutPath "$PathLaTeXLocal\texmf-dist\tex\latex\lyx"
374    CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" "$PathLaTeXLocal\texmf-dist\tex\latex\lyx"
375   ${endif}
376  ${endif}
377  
378  # update TeXLive's package file list
379  ExecWait '$PathLaTeX\texhash'
380  
381  # update TeXLive
382  ExecWait '$PathLaTeX\tlmgr update --all'
383  
384 FunctionEnd
385
386 # ------------------------------
387
388 Function UpdateMiKTeX
389  # asks to update MiKTeX
390
391   # only for the 2.3.0 installer: force a silent update of MiKTeX then restore
392   # MiKTeX's inernal links
393   # The reason is that MikTeX uses a new package handling system LyX must use
394   # Due to a bug in the old MikTeX package handling the update to the new package
395   # handling might fail and users cannot use LaTeX at all afterwards - they then
396   # would have no other choice than to reinstall MiKTeX
397   # This case is fixed by forcing the restoration of the internal links
398   # There is another issue: the MiKTeX update program needs to be replaced by
399   # the new MiKTeX console. This is a 3-step process.
400   #MessageBox MB_YESNO|MB_ICONINFORMATION "$(MiKTeXInfo)" #/SD IDNO IDYES UpdateNow IDNO UpdateLater
401   #UpdateNow:
402   # graphical update:
403   #MessageBox MB_OK|MB_ICONINFORMATION 'To assure that LyX can create PDF files the MiKTeX update program must be run two times.$\r$\n\
404   # Please click in the MiKTeX update program only on the "Next" button.$\r$\n\
405   # If "Next" is disabled, click on "Cancel" or "Finish".'
406   #${if} $MultiUser.Privileges != "Admin"
407   #${andif} $MultiUser.Privileges != "Power"
408   # # call the non-admin version
409   # nsExec::ExecToLog '"$PathLaTeX\internal\miktex-update.exe"'
410   #${else}
411   # ${if} $MiKTeXUser != "HKCU" # call the admin version
412   #  nsExec::ExecToLog '"$PathLaTeX\internal\miktex-update_admin.exe"'
413   # ${else}
414   #   nsExec::ExecToLog '"$PathLaTeX\internal\miktex-update.exe"'
415   # ${endif}
416   #${endif}
417   # silent update:
418   MessageBox MB_OK|MB_ICONINFORMATION "MiKTeX must be updated to assure that LyX can create PDF files.$\r$\n\
419    This update can take several minutes, depending on your Internet speed.$\r$\n\
420    Please do not close the LyX installer until it is finished!" /SD IDOK
421   ${if} $MultiUser.Privileges != "Admin"
422   ${andif} $MultiUser.Privileges != "Power"
423    # call the non-admin version
424    # the order of the different commands is important!
425    ${if} $Is64bit == "true"
426     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--update=miktex-bin-x64-2.9"'
427     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--install=miktex-console-bin-x64-2.9"'
428     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--uninstall=miktex-mpm-bin-x64-2.9"'
429    ${else}
430     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--update=miktex-bin-2.9"'
431     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--install=miktex-console-bin-2.9"'
432     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--uninstall=miktex-mpm-bin-2.9"'
433    ${endif}
434    nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--update"'
435   ${else}
436    ${if} $MiKTeXUser != "HKCU" # call the admin version
437     # the order of the different commands is important!
438     ${if} $Is64bit == "true"
439      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--update=miktex-bin-x64-2.9"'
440      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--install=miktex-console-bin-x64-2.9"'
441      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--uninstall=miktex-mpm-bin-x64-2.9"'
442     ${else}
443      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--update=miktex-bin-2.9"'
444      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--install=miktex-console-bin-2.9"'
445      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--uninstall=miktex-mpm-bin-2.9"'
446     ${endif}
447     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--admin" "--update"'
448    ${else}
449     ${if} $Is64bit == "true"
450      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--update=miktex-bin-x64-2.9"'
451      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--install=miktex-console-bin-x64-2.9"'
452      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--uninstall=miktex-mpm-bin-x64-2.9"'
453     ${else}
454      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--update=miktex-bin-2.9"'
455      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--install=miktex-console-bin-2.9"'
456      nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--uninstall=miktex-mpm-bin-2.9"'
457     ${endif}
458     nsExec::ExecToLog '"$PathLaTeX\mpm.exe" "--update"'
459    ${endif}
460   ${endif}
461   # restore possibly broken internal MiKTeX links after the update
462   # suggested by the MikTeX maintainer: https://github.com/MiKTeX/miktex/issues/82
463   ${if} $MultiUser.Privileges != "Admin"
464   ${andif} $MultiUser.Privileges != "Power"
465    # call the non-admin version
466    nsExec::ExecToLog '"$PathLaTeX\initexmf.exe" "--mklinks" "--force"'
467   ${else}
468    ${if} $MiKTeXUser != "HKCU" # call the admin version
469     nsExec::ExecToLog '"$PathLaTeX\initexmf.exe" "--admin" "--mklinks" "--force"'
470    ${else}
471      nsExec::ExecToLog '"$PathLaTeX\initexmf.exe" "--mklinks" "--force"'
472    ${endif}
473   ${endif}
474   #UpdateLater:
475
476 FunctionEnd