# CMakeLists.txt
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

# We should use CPack to help generate a .msi using WIX.

set(WIX_GENERATED_FILES
	${CMAKE_CURRENT_BINARY_DIR}/DependentComponents.wxs
	${CMAKE_CURRENT_BINARY_DIR}/QtDependentComponents.wxs
)
set(WIX_GENERATED_FILES ${WIX_GENERATED_FILES} PARENT_SCOPE)

set(WIX_SOURCE_FILES
	${CMAKE_SOURCE_DIR}/packaging/wix/Wireshark.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/WiresharkCertificationDlg.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/WiresharkDonateDlg.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/WiresharkOptionsDlg.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/WiresharkWixUI.wxs
	${WIX_GENERATED_FILES}
)
set(WIX_SOURCE_FILES ${WIX_SOURCE_FILES} PARENT_SCOPE)

set(WIX_FILES
	${CMAKE_CURRENT_SOURCE_DIR}/ComponentGroups.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/COPYING.rtf
	${CMAKE_CURRENT_SOURCE_DIR}/DirectoryStructure.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/Features.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/InputPaths.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/Plugins.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/Prerequisites.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/UserInterface.wxi
	${WIX_SOURCE_FILES}
	PARENT_SCOPE
)

# Variables required for Wireshark.wxs
set(PROGRAM_NAME ${CMAKE_PROJECT_NAME})
file(TO_NATIVE_PATH "${CMAKE_SOURCE_DIR}" TOP_SRC_DIR)
# STAGING_DIR depends on the build configuration so we pass it
# on the command line below.
file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/doc" DOC_DIR)

# To do:
# - Sync the various version names between CMake and Wix.
# - Set CMakeLists.txt version strings in tools/make-version.py
# - Add a VERSION_EXTRA cmake option
set (VERSION "${PROJECT_VERSION}")
set(PRODUCT_VERSION ${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}.${PROJECT_PATCH_VERSION}.${PROJECT_BUILD_VERSION})

# DependentComponents.wxi. Can be created at configure time.
set(_all_manifest_wix "${CMAKE_CURRENT_BINARY_DIR}/DependentComponents.wxs")
file(WRITE "${_all_manifest_wix}" "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file(APPEND "${_all_manifest_wix}" "<Wix xmlns=\"http://wixtoolset.org/schemas/v4/wxs\">\n\n")
file(APPEND "${_all_manifest_wix}" "  <!-- Files required for all sections. Generated by CMake. -->\n")
file(APPEND "${_all_manifest_wix}"  "<?include InputPaths.wxi ?>\n")
file(APPEND "${_all_manifest_wix}" "  <Fragment>\n")
file(APPEND "${_all_manifest_wix}" "    <DirectoryRef Id=\"INSTALLFOLDER\">\n")
# Third party DLLs registered with ws_add_runtime_dlls(). DLLs whose names
# differ between Release and Debug builds go in a BUNDLE_DEBUG_DLLS section.
ws_get_runtime_dll_names(_common_dlls _release_dlls _debug_dlls FOR wireshark)

# Append a Component, or with REF a ComponentRef, for each DLL.
function(append_wix_dll_components file indent ref) # ...DLL names
	foreach(_dll IN LISTS ARGN)
		string(REGEX REPLACE "[-|\\.]" "_" _wix_name ${_dll})
		if(ref)
			file(APPEND "${file}" "${indent}<ComponentRef Id=\"cmp${_wix_name}\" />\n")
		else()
			file(APPEND "${file}" "${indent}<Component Id=\"cmp${_wix_name}\">\n")
			file(APPEND "${file}" "${indent}  <File Id=\"fil${_wix_name}\" KeyPath=\"yes\" Source=\"$(var.Staging.Dir)\\${_dll}\" />\n")
			file(APPEND "${file}" "${indent}</Component>\n")
		endif()
	endforeach()
endfunction()

function(append_wix_dlls file ref)
	if(_release_dlls OR _debug_dlls)
		file(APPEND "${file}" "      <?ifdef BUNDLE_DEBUG_DLLS ?>\n")
		append_wix_dll_components("${file}" "        " ${ref} ${_debug_dlls})
		file(APPEND "${file}" "      <?else?>\n")
		append_wix_dll_components("${file}" "        " ${ref} ${_release_dlls})
		file(APPEND "${file}" "      <?endif?>\n")
	endif()
	append_wix_dll_components("${file}" "      " ${ref} ${_common_dlls})
endfunction()

append_wix_dlls("${_all_manifest_wix}" FALSE)
file(APPEND "${_all_manifest_wix}" "    </DirectoryRef>\n")
file(APPEND "${_all_manifest_wix}" "  </Fragment>\n")
file(APPEND "${_all_manifest_wix}" "  <Fragment>\n")
file(APPEND "${_all_manifest_wix}" "    <ComponentGroup Id=\"CG.RequiredDependencies\">\n")
append_wix_dlls("${_all_manifest_wix}" TRUE)
file(APPEND "${_all_manifest_wix}" "    </ComponentGroup>\n")
file(APPEND "${_all_manifest_wix}" "  </Fragment>\n")
file(APPEND "${_all_manifest_wix}" "\n</Wix>\n")

if(SMI_DIR)
	set(d_smi_dir -d SMI_DIR)
endif()

if (MAXMINDDB_FOUND)
	set(d_mmdbresolve_exe -d MMDBRESOLVE_EXE)
endif()

# The Visual C++ Redistributable installed on the target machine must be at
# least as new as the one that shipped with the toolset used to build
# Wireshark. VCToolsVersion is set by the Visual Studio developer
# environment, e.g. "14.38.33135".
set(d_vcredist_min_version)
if(DEFINED ENV{VCToolsVersion} AND "$ENV{VCToolsVersion}" MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+$")
	set(d_vcredist_min_version -d VCRedistMinVersion=$ENV{VCToolsVersion})
endif()

# Flags for "wix build", which replaces candle and light.
set(WIX_BUILD_FLAGS
	-arch ${WIRESHARK_TARGET_PLATFORM}
	-d Platform=${WIRESHARK_TARGET_PLATFORM}
	-d WiresharkName=${CMAKE_PROJECT_NAME}
	-d WiresharkVersion=${PRODUCT_VERSION}
	-d WiresharkMajorVersion=${PROJECT_MAJOR_VERSION}
	-d WiresharkMinorVersion=${PROJECT_MINOR_VERSION}
	-d AssetDir=${CMAKE_SOURCE_DIR}/packaging/wix
	-d BuildOutputDir=${DATAFILE_DIR}
	-d StagingDatafileDir=${WIRESHARK_DATAFILE_DIR}
	-d DiameterDir=${WIRESHARK_DATAFILE_DIR}/diameter
	-d QualcommDir=${WIRESHARK_DATAFILE_DIR}/qualcomm
	-d IconDir=${CMAKE_SOURCE_DIR}/resources/icons
	-d QtTranslationDir=${DATAFILE_DIR}/translations
	-d RadiusDictDir=${WIRESHARK_DATAFILE_DIR}/radius
	-d SnmpMibDir=${DATAFILE_DIR}/snmp/mibs
	-d UsersGuideDir=${CMAKE_BINARY_DIR}/doc/wsug_html_chunked
	${d_smi_dir}
	${d_mmdbresolve_exe}
	${d_vcredist_min_version}
	-ext WixToolset.UI.wixext
	-i ${CMAKE_SOURCE_DIR}/packaging/wix
	-loc ${CMAKE_CURRENT_SOURCE_DIR}/StringOverrides.wxl
	# The NSIS CMakeFile sets the program name + version slightly differently.
	-o ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}-${WIRESHARK_TARGET_PLATFORM}.msi
	PARENT_SCOPE
)

macro( ADD_WIX_PACKAGE_TARGET )
	#
	# XXX - if we're not building Wireshark, we can't build
	# QtDependentComponents.wxs.  On the other hand, if we're
	# not building Wireshark, we have no need to include Qt
	# in the installer, so it's not clear we need that file.
	#
	# This should probably be fixed, so that people can produce
	# command-line-only installer packages.
	if(BUILD_wireshark)
		set (_wix_source_dir ${CMAKE_SOURCE_DIR}/packaging/wix )
		set (_wix_binary_dir ${CMAKE_BINARY_DIR}/packaging/wix )

		# QtDependentComponents.wxs. Created using Wireshark.exe.
		add_custom_command(OUTPUT ${_wix_binary_dir}/QtDependentComponents.wxs
			COMMAND set "PATH=${QT_BIN_PATH};%PATH%"
			COMMAND ${POWERSHELL_COMMAND} "${_wix_source_dir}/windeployqt-to-wix.ps1"
				-Executable $<TARGET_FILE:wireshark>
				-FilePath ${_wix_binary_dir}/QtDependentComponents.wxs
			DEPENDS "${_wix_source_dir}/windeployqt-to-wix.ps1"
		)

		# Build WiX package dependencies. We build the package in
		# two stages so that wireshark_wix below doesn't trigger any
		# dependencies that might clobber any signed executables.
		# XXX Rename this to wireshark_wix_prep
		add_custom_target(wireshark_wix_prep
			DEPENDS
				${WIX_FILES}
				copy_data_files
				user_guide_html
				release_notes_html
		)
		set_target_properties(wireshark_wix_prep PROPERTIES
			FOLDER "Packaging"
			EXCLUDE_FROM_DEFAULT_BUILD True
		)

		# Dump the installer into
		# ${CMAKE_CURRENT_SOURCE_DIR}/packaging/wix
		# Note that executables and DLLs *must* be built separately
		add_custom_target(wireshark_wix
			COMMAND ${WIX_EXECUTABLE} build ${WIX_BUILD_FLAGS}
				"$<$<CONFIG:Debug>:-d BUNDLE_DEBUG_DLLS>"
				${WIX_SOURCE_FILES}
			WORKING_DIRECTORY ${_wix_source_dir}
		)
		set_target_properties(wireshark_wix PROPERTIES
			FOLDER "Packaging"
			EXCLUDE_FROM_DEFAULT_BUILD True
		)
	else()
		message(WARNING "The WiX installer cannot be built if the Wireshark program isn't built.")
	endif()
endmacro( ADD_WIX_PACKAGE_TARGET )

set(CLEAN_FILES
	DependentComponents.wxs
	QtDependentComponents.wxs
	wireshark-${VERSION}-${WIRESHARK_TARGET_PLATFORM}.msi
)
