Search code examples
installationnsis

Use macro more than once in a section


I am calling a macro many times within a section. The macro checks whether a directory exists, if it doesn't it creates that directory.

My Problem: I get an error because I am calling this macro more than once from inside a section. How can I fix my compile error?

Error: "Error: label "CreateDirThenInstall:" already declared in section"

Can you tell me how I can use this macro more than once in a section?

Section "Install Plugin Files" MainSetup
  !insertmacro ValidateDir "c:/blah"
  setOutPath "c:/blah"
  file "C:/blah/a.txt"
  file "C:/blah/b.txt"

  !insertmacro ValidateDir "c:/other"
  setOutPath "c:/other"
  file "c:/other/a.txt"
  file "c:/other/b.txt" 
sectionend

!macro ValidateDir dir
   IfFileExists "$dir" ExitMacro CreateDirThenInstall
   CreateDirThenInstall:   # Error here: Error: label "CreateDirThenInstall:" already declared in section
      createDirectory "${dir}"   
   ExitMacro: 
!macroend

Solution

  • The problem is with label, rather than with the macro. You have used exactly same label twice in the section, which is not possible.

    You can make the label in the macro unique (even if macro inserted more than once). Compile time command ${__LINE__} can be used for that. You can then write something like this:

    !macro ValidateDir dir
      !define UniqueId1 ${__LINE__}
      !define UniqueId2 ${__LINE__}
      IfFileExists "${dir}" Exit_${UniqueId1} CreateDir_${UniqueId2}
      CreateDir_${UniqueId2}:
          createDirectory "${dir}"   
      Exit_${UniqueId1}: 
      !undef UniqueId1
      !undef UniqueId2
    !macroend
    

    But in your case I think the above is not necessary. SetOutPath instruction creates directory if necessary for you. From doc:

    Sets the output path ($OUTDIR) and creates it (recursively if necessary), if it does not exist.

    So if you don't need to know about every directory created (and write it somewhere, e.g. to use it during uninstall), you don't have to do it at all.