• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Provides .props file.
3"""
4
5import os
6
7from .constants import *
8
9__all__ = ["get_props_layout"]
10
11PYTHON_PROPS_NAME = "python.props"
12
13PROPS_DATA = {
14    "PYTHON_TAG": VER_DOT,
15    "PYTHON_VERSION": os.getenv("PYTHON_NUSPEC_VERSION"),
16    "PYTHON_PLATFORM": os.getenv("PYTHON_PROPS_PLATFORM"),
17    "PYTHON_TARGET": "",
18}
19
20if not PROPS_DATA["PYTHON_VERSION"]:
21    PROPS_DATA["PYTHON_VERSION"] = "{}.{}{}{}".format(
22        VER_DOT, VER_MICRO, "-" if VER_SUFFIX else "", VER_SUFFIX
23    )
24
25PROPS_DATA["PYTHON_TARGET"] = "_GetPythonRuntimeFilesDependsOn{}{}_{}".format(
26    VER_MAJOR, VER_MINOR, PROPS_DATA["PYTHON_PLATFORM"]
27)
28
29PROPS_TEMPLATE = r"""<?xml version="1.0" encoding="utf-8"?>
30<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
31  <PropertyGroup Condition="$(Platform) == '{PYTHON_PLATFORM}'">
32    <PythonHome Condition="$(PythonHome) == ''">$([System.IO.Path]::GetFullPath("$(MSBuildThisFileDirectory)\..\..\tools"))</PythonHome>
33    <PythonInclude>$(PythonHome)\include</PythonInclude>
34    <PythonLibs>$(PythonHome)\libs</PythonLibs>
35    <PythonTag>{PYTHON_TAG}</PythonTag>
36    <PythonVersion>{PYTHON_VERSION}</PythonVersion>
37
38    <IncludePythonExe Condition="$(IncludePythonExe) == ''">true</IncludePythonExe>
39    <IncludeDistutils Condition="$(IncludeDistutils) == ''">false</IncludeDistutils>
40    <IncludeLib2To3 Condition="$(IncludeLib2To3) == ''">false</IncludeLib2To3>
41    <IncludeVEnv Condition="$(IncludeVEnv) == ''">false</IncludeVEnv>
42
43    <GetPythonRuntimeFilesDependsOn>{PYTHON_TARGET};$(GetPythonRuntimeFilesDependsOn)</GetPythonRuntimeFilesDependsOn>
44  </PropertyGroup>
45
46  <ItemDefinitionGroup Condition="$(Platform) == '{PYTHON_PLATFORM}'">
47    <ClCompile>
48      <AdditionalIncludeDirectories>$(PythonInclude);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
49      <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
50    </ClCompile>
51    <Link>
52      <AdditionalLibraryDirectories>$(PythonLibs);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
53    </Link>
54  </ItemDefinitionGroup>
55
56  <Target Name="GetPythonRuntimeFiles" Returns="@(PythonRuntime)" DependsOnTargets="$(GetPythonRuntimeFilesDependsOn)" />
57
58  <Target Name="{PYTHON_TARGET}" Returns="@(PythonRuntime)">
59    <ItemGroup>
60      <_PythonRuntimeExe Include="$(PythonHome)\python*.dll" />
61      <_PythonRuntimeExe Include="$(PythonHome)\python*.exe" Condition="$(IncludePythonExe) == 'true'" />
62      <_PythonRuntimeExe>
63        <Link>%(Filename)%(Extension)</Link>
64      </_PythonRuntimeExe>
65      <_PythonRuntimeDlls Include="$(PythonHome)\DLLs\*.pyd" />
66      <_PythonRuntimeDlls Include="$(PythonHome)\DLLs\*.dll" />
67      <_PythonRuntimeDlls>
68        <Link>DLLs\%(Filename)%(Extension)</Link>
69      </_PythonRuntimeDlls>
70      <_PythonRuntimeLib Include="$(PythonHome)\Lib\**\*" Exclude="$(PythonHome)\Lib\**\*.pyc;$(PythonHome)\Lib\site-packages\**\*" />
71      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\distutils\**\*" Condition="$(IncludeDistutils) != 'true'" />
72      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\lib2to3\**\*" Condition="$(IncludeLib2To3) != 'true'" />
73      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\ensurepip\**\*" Condition="$(IncludeVEnv) != 'true'" />
74      <_PythonRuntimeLib Remove="$(PythonHome)\Lib\venv\**\*" Condition="$(IncludeVEnv) != 'true'" />
75      <_PythonRuntimeLib>
76        <Link>Lib\%(RecursiveDir)%(Filename)%(Extension)</Link>
77      </_PythonRuntimeLib>
78      <PythonRuntime Include="@(_PythonRuntimeExe);@(_PythonRuntimeDlls);@(_PythonRuntimeLib)" />
79    </ItemGroup>
80
81    <Message Importance="low" Text="Collected Python runtime from $(PythonHome):%0D%0A@(PythonRuntime->'  %(Link)','%0D%0A')" />
82  </Target>
83</Project>
84"""
85
86
87def get_props_layout(ns):
88    if ns.include_all or ns.include_props:
89        # TODO: Filter contents of props file according to included/excluded items
90        d = dict(PROPS_DATA)
91        if not d.get("PYTHON_PLATFORM"):
92            d["PYTHON_PLATFORM"] = {
93                "win32": "Win32",
94                "amd64": "X64",
95                "arm32": "ARM",
96                "arm64": "ARM64",
97            }[ns.arch]
98        props = PROPS_TEMPLATE.format_map(d)
99        yield "python.props", ("python.props", props.encode("utf-8"))
100