[CMake] CPack custom page

Bo Thorsen bo at askmonty.org
Fri Jul 2 04:38:01 EDT 2010


Den 28-06-2010 10:42, Bo Thorsen skrev:
> Hi people,
>
> I'm trying to build a custom NSIS page that's shown at the end of the
>  installer, after all files have been installed. The purpose is to
> set up a Windows service.
>
> I can do the code for installing the service, but I'm having some
> problems adding the custom page for it. And searching the net haven't
>  given me any decent hits. Some pages say I should just hack the
> template file, but that doesn't give any clue what to put in the
> file.
>
> Can one of you point to example code that adds a custom page,
> please?

No takers, so I figured it out myself.

Here is the patch necessary for my CMakeLists.txt:

+# Use our own NSIS template
+set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/win/cmake" 
${CMAKE_MODULE_PATH})

I have copied the NSIS.template.in file to the directory win/cmake.

The patch below adds a page to the installer just before the files are 
copied. The page asks the user if he wants to install MariaDB as a 
service. After the files are installed, the installer code sets up the 
service, if the user checked the checkbox on the page. The patch is 
against the NSIS.template.in from cmake 2.8.0.

--- \Program Files (x86)\CMake 
2.8\share\cmake-2.8\Modules\NSIS.template.in	2010-03-16 
18:52:34.000000000 +0100
+++ win\cmake\NSIS.template.in	2010-06-30 11:55:34.936755500 +0200
@@ -19,6 +19,10 @@
    Var ADD_TO_PATH_CURRENT_USER
    Var INSTALL_DESKTOP
    Var IS_DEFAULT_INSTALLDIR
+
+  Var INSTALL_AS_SERVICE
+  Var INSTALL_AS_SERVICE_CHECKBOX
+
  ;--------------------------------
  ;Include Modern UI

@@ -27,6 +31,10 @@
    ;Default installation folder
    InstallDir "$PROGRAMFILES\@CPACK_PACKAGE_INSTALL_DIRECTORY@"

+;Include the nsDialogs package
+  !include nsDialogs.nsh
+  !include LogicLib.nsh
+
  ;--------------------------------
  ;General

@@ -383,7 +391,35 @@
      Pop $1
      Pop $0
  FunctionEnd
-
+
+Function InstallServicePage
+    !insertmacro MUI_HEADER_TEXT "Service Setup" "Run MariaDB 
automatically."
+
+    nsDialogs::Create /NOUNLOAD 1018
+    Pop $0
+
+    ${If} $0 == error
+        Abort
+    ${EndIf}
+
+    ${NSD_CreateLabel} 0u 0u 100% 9u "Should MariaDB be installed as a 
service?"
+    Pop $0
+
+    ${NSD_CreateCheckBox} 0u 14u 100% 16u "&Yes, please install the 
service."
+    Pop $INSTALL_AS_SERVICE_CHECKBOX
+    GetFunctionAddress $0 ServiceCheckBoxClicked
+    nsDialogs::OnClick /NOUNLOAD $INSTALL_AS_SERVICE_CHECKBOX $0
+
+    ${NSD_CreateLabel} 0u 30u 100% 35u "If this option is checked, this 
installer will add MariaDB to the list of services and start it."
+	Pop $0
+
+    nsDialogs::Show
+FunctionEnd
+
+Function ServiceCheckBoxClicked
+    ${NSD_GetState} $INSTALL_AS_SERVICE_CHECKBOX $INSTALL_AS_SERVICE
+FunctionEnd
+
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; Uninstall sutff
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -557,6 +593,8 @@

    @CPACK_NSIS_PAGE_COMPONENTS@

+  Page custom InstallServicePage
+
    !insertmacro MUI_PAGE_INSTFILES
    !insertmacro MUI_PAGE_FINISH

@@ -718,6 +756,12 @@

  @CPACK_NSIS_EXTRA_INSTALL_COMMANDS@

+  ${If} $INSTALL_AS_SERVICE == ${BST_CHECKED}
+      ;MessageBox MB_OK "Install the service"
+      nsExec::Exec '"$INSTDIR\bin\mysqld.exe" --install 
"@CPACK_PACKAGE_NAME at -@CPACK_PACKAGE_VERSION@"'
+      nsExec::Exec 'net start 
"@CPACK_PACKAGE_NAME at -@CPACK_PACKAGE_VERSION@"'
+  ${EndIf}
+
  SectionEnd

  Section "-Add to path"
@@ -818,6 +862,9 @@

  @CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS@

+  nsExec::Exec 'net stop "@CPACK_PACKAGE_NAME at -@CPACK_PACKAGE_VERSION@"'
+  nsExec::Exec '"$INSTDIR\bin\mysqld.exe" --remove 
"@CPACK_PACKAGE_NAME at -@CPACK_PACKAGE_VERSION@"'
+
    ;Remove files we installed.
    ;Keep the list of directories here in sync with the File commands above.
  @CPACK_NSIS_DELETE_FILES@

I hope this is helpful for others.

Bo Thorsen.
Monty Program AB.

-- 

MariaDB: MySQL replacement
Community developed. Feature enhanced. Backward compatible.


More information about the CMake mailing list