###
# This file is a part of the NVDA project.
# URL: http://www.nvaccess.org/
# Copyright 2016-2024 NV Access Limited, Leonard de Ruijter
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.0, as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# This license can be found at:
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
###

import os
import glob

from SCons.Tool.MSCommon.vc import find_vc_pdir

Import(
	"env",
	"sourceDir",
	"localLib",
)


# Ignoring Flake8 F821: 'undefined name' due to nonstandard SCons import
env = env.Clone()  # noqa: F821

localWin10Lib = env.SharedLibrary(
	target="nvdaHelperLocalWin10",
	source=[
		env["projectResFile"],
		"oneCoreSpeech.cpp",
		"uwpOcr.cpp",
	],
	LIBS=[
		"WindowsApp",
		# Ignoring Flake8 F821: 'undefined name' due to nonstandard SCons import
		localLib[2],
	],  # noqa: F821
)

# UWP dlls can only be dynamically linked with the CRT,
# but some systems might not have this version of the CRT.
# Therefore, we must include it.
# VS keeps changing the path to reflect the latest major.minor.build version which we canot easily find out.
# Therefore  Search these versioned directories from newest to oldest  to collect all the files we need.
msvc = env.get("MSVC_VERSION")
vcRedistDirs = glob.glob(
	os.path.join(
		find_vc_pdir(msvc, env), rf"Redist\MSVC\{msvc[:2]}*\x86\Microsoft.VC{msvc.replace('.', '')}.CRT"
	)
)
if len(vcRedistDirs) == 0:
	raise RuntimeError(
		"Could not locate vc redistributables. Perhaps the Universal Windows Platform component in visual Studio is not installed"
	)
vcRedistDirs.sort(reverse=True)
for fn in ("msvcp140.dll", "vccorlib140.dll", "vcruntime140.dll"):
	for vcRedistDir in vcRedistDirs:
		path = os.path.join(vcRedistDir, fn)
		if os.path.isfile(path):
			env.Install(sourceDir, path)
			break
		else:
			raise RuntimeError(
				"Could not locate %s. Perhaps the Universal Windows Platform component in visual Studio is not installed"
				% fn
			)

Return(["localWin10Lib"])
