# 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(WIRESHARK_WIX_GENERATED_FILES
	${CMAKE_CURRENT_BINARY_DIR}/WiresharkDependentComponents.wxs
	${CMAKE_CURRENT_BINARY_DIR}/WiresharkQtDependentComponents.wxs
)
set(WIRESHARK_WIX_GENERATED_FILES ${WIRESHARK_WIX_GENERATED_FILES} PARENT_SCOPE)

set(WIRESHARK_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
	${WIRESHARK_WIX_GENERATED_FILES}
)
set(WIRESHARK_WIX_SOURCE_FILES ${WIRESHARK_WIX_SOURCE_FILES} PARENT_SCOPE)

set(STRATOSHARK_WIX_GENERATED_FILES
	${CMAKE_CURRENT_BINARY_DIR}/StratosharkDependentComponents.wxs
	${CMAKE_CURRENT_BINARY_DIR}/StratosharkQtDependentComponents.wxs
)
set(STRATOSHARK_WIX_GENERATED_FILES ${STRATOSHARK_WIX_GENERATED_FILES} PARENT_SCOPE)

set(STRATOSHARK_WIX_SOURCE_FILES
	${CMAKE_SOURCE_DIR}/packaging/wix/Stratoshark.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/StratosharkDonateDlg.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/StratosharkOptionsDlg.wxs
	${CMAKE_SOURCE_DIR}/packaging/wix/StratosharkWixUI.wxs
	${STRATOSHARK_WIX_GENERATED_FILES}
)
set(STRATOSHARK_WIX_SOURCE_FILES ${STRATOSHARK_WIX_SOURCE_FILES} PARENT_SCOPE)

set(STRATOSHARK_WIX_FILES
	${CMAKE_CURRENT_SOURCE_DIR}/StratosharkComponentGroups.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/COPYING.rtf
	${CMAKE_CURRENT_SOURCE_DIR}/InputPaths.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/StratosharkDirectoryStructure.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/StratosharkFeatures.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/StratosharkPlugins.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/StratosharkPrerequisites.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/StratosharkUserInterface.wxi
	${STRATOSHARK_WIX_SOURCE_FILES}
	PARENT_SCOPE
)

set(WIRESHARK_WIX_FILES
	${CMAKE_CURRENT_SOURCE_DIR}/WiresharkComponentGroups.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/COPYING.rtf
	${CMAKE_CURRENT_SOURCE_DIR}/InputPaths.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/WiresharkDirectoryStructure.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/WiresharkFeatures.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/WiresharkPlugins.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/WiresharkPrerequisites.wxi
	${CMAKE_CURRENT_SOURCE_DIR}/WiresharkUserInterface.wxi
	${WIRESHARK_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})
set(STRATOSHARK_PRODUCT_VERSION ${STRATOSHARK_MAJOR_VERSION}.${STRATOSHARK_MINOR_VERSION}.${PROJECT_PATCH_VERSION}.${PROJECT_BUILD_VERSION})

# WiresharkDependentComponents.wxs and StratosharkDependentComponents.wxs. Can be
# created at configure time.
# 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()

function(write_wix_dependent_components _all_manifest_wix _target)
	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 ${_target})

	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")
endfunction()

write_wix_dependent_components("${CMAKE_CURRENT_BINARY_DIR}/WiresharkDependentComponents.wxs" wireshark)
if(BUILD_stratoshark)
	write_wix_dependent_components("${CMAKE_CURRENT_BINARY_DIR}/StratosharkDependentComponents.wxs" stratoshark)
endif()

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()

if(BUILD_sshdig AND LIBSSH_FOUND)
	set(d_libssh_found -d LIBSSH_FOUND)
endif()

# Flags for "wix build", which replaces candle and light.
# Shared by the Wireshark and Stratoshark packages.
set(_wix_common_flags
	-arch ${WIRESHARK_TARGET_PLATFORM}
	-d Platform=${WIRESHARK_TARGET_PLATFORM}
	# The plugin directories are named after the Wireshark version.
	-d WiresharkMajorVersion=${PROJECT_MAJOR_VERSION}
	-d WiresharkMinorVersion=${PROJECT_MINOR_VERSION}
	-d AssetDir=${CMAKE_SOURCE_DIR}/packaging/wix
	-d BuildOutputDir=${DATAFILE_DIR}
	-d IconDir=${CMAKE_SOURCE_DIR}/resources/icons
	-d QtTranslationDir=${DATAFILE_DIR}/translations
	-d SnmpMibDir=${DATAFILE_DIR}/snmp/mibs
	-d SnmpPibDir=${DATAFILE_DIR}/snmp/pibs
	-d SnmpYangDir=${DATAFILE_DIR}/snmp/yang
	${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
)

set(WIRESHARK_WIX_BUILD_FLAGS
	${_wix_common_flags}
	-d WiresharkName=${CMAKE_PROJECT_NAME}
	-d WiresharkVersion=${PRODUCT_VERSION}
	-d StagingDatafileDir=${WIRESHARK_DATAFILE_DIR}
	-d DiameterDir=${WIRESHARK_DATAFILE_DIR}/diameter
	-d QualcommDir=${WIRESHARK_DATAFILE_DIR}/qualcomm
	-d RadiusDictDir=${WIRESHARK_DATAFILE_DIR}/radius
	-d JsonDir=${WIRESHARK_DATAFILE_DIR}/json
	-d UsersGuideDir=${CMAKE_BINARY_DIR}/doc/wsug_html_chunked
	# 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
)

# Stratoshark uses its own data file directory.
set(STRATOSHARK_WIX_BUILD_FLAGS
	${_wix_common_flags}
	-d StratosharkName=${STRATOSHARK_NAME}
	-d StratosharkVersion=${STRATOSHARK_PRODUCT_VERSION}
	-d StagingDatafileDir=${STRATOSHARK_DATAFILE_DIR}
	-d DiameterDir=${STRATOSHARK_DATAFILE_DIR}/diameter
	-d RadiusDictDir=${STRATOSHARK_DATAFILE_DIR}/radius
	${d_libssh_found}
	-o ${CMAKE_CURRENT_BINARY_DIR}/${STRATOSHARK_NAME}-${STRATOSHARK_VERSION}-${WIRESHARK_TARGET_PLATFORM}.msi
	PARENT_SCOPE
)

macro( ADD_WIX_PACKAGE_TARGET )
	set (_wix_source_dir ${CMAKE_SOURCE_DIR}/packaging/wix )
	set (_wix_binary_dir ${CMAKE_BINARY_DIR}/packaging/wix )

	#
	# XXX - if we're not building Wireshark, we can't build
	# WiresharkQtDependentComponents.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)
		# WiresharkQtDependentComponents.wxs. Created using Wireshark.exe.
		add_custom_command(OUTPUT ${_wix_binary_dir}/WiresharkQtDependentComponents.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}/WiresharkQtDependentComponents.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
				${WIRESHARK_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 ${WIRESHARK_WIX_BUILD_FLAGS}
				"$<$<CONFIG:Debug>:-d BUNDLE_DEBUG_DLLS>"
				${WIRESHARK_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 Wireshark WiX installer cannot be built if the Wireshark program isn't built.")
	endif()

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

		# Build WiX package dependencies. We build the package in
		# two stages so that stratoshark_wix below doesn't trigger
		# any dependencies that might clobber any signed executables.
		# The Stratoshark package doesn't include the User's Guide.
		add_custom_target(stratoshark_wix_prep
			DEPENDS
				${STRATOSHARK_WIX_FILES}
				copy_data_files
				release_notes_html
		)
		set_target_properties(stratoshark_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(stratoshark_wix
			COMMAND ${WIX_EXECUTABLE} build ${STRATOSHARK_WIX_BUILD_FLAGS}
				"$<$<CONFIG:Debug>:-d BUNDLE_DEBUG_DLLS>"
				${STRATOSHARK_WIX_SOURCE_FILES}
			WORKING_DIRECTORY ${_wix_source_dir}
		)
		set_target_properties(stratoshark_wix PROPERTIES
			FOLDER "Packaging"
			EXCLUDE_FROM_DEFAULT_BUILD True
		)
	endif()
endmacro( ADD_WIX_PACKAGE_TARGET )

set(CLEAN_FILES
	WiresharkDependentComponents.wxs
	WiresharkQtDependentComponents.wxs
	wireshark-${VERSION}-${WIRESHARK_TARGET_PLATFORM}.msi
	StratosharkDependentComponents.wxs
	StratosharkQtDependentComponents.wxs
	stratoshark-${STRATOSHARK_VERSION}-${WIRESHARK_TARGET_PLATFORM}.msi
)
