Skip to content

Commit 01d6be4

Browse files
committed
Windows MSVC port
Extend the windows port so it compiles with the toolchain from Visual Studio 2013
1 parent c1c32d6 commit 01d6be4

File tree

16 files changed

+407
-8
lines changed

16 files changed

+407
-8
lines changed

py/mpconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,6 @@ typedef double mp_float_t;
312312
#endif //INT_FMT
313313

314314
// Modifier for function which doesn't return
315+
#ifndef NORETURN
315316
#define NORETURN __attribute__((noreturn))
317+
#endif

py/mpz.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,11 @@ mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2) {
10921092
*/
10931093
mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2)
10941094
{
1095-
if (z1->len == 0 || z2->len == 0)
1096-
return mpz_zero();
1095+
// braces below are required for compilation to succeed with CL, see bug report
1096+
// https://connect.microsoft.com/VisualStudio/feedback/details/864169/compilation-error-when-braces-are-left-out-of-single-line-if-statement
1097+
if (z1->len == 0 || z2->len == 0) {
1098+
return mpz_zero();
1099+
}
10971100

10981101
mpz_t *gcd = mpz_gcd(z1, z2);
10991102
mpz_t *quo = mpz_zero();

py/objint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ STATIC mp_obj_t int_from_bytes(uint n_args, const mp_obj_t *args) {
303303

304304
// convert the bytes to an integer
305305
machine_uint_t value = 0;
306-
for (const byte* buf = bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
306+
for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
307307
value = (value << 8) | *buf;
308308
}
309309

py/runtime.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdio.h>
2828
#include <string.h>
2929
#include <assert.h>
30+
#include <alloca.h>
3031

3132
#include "mpconfig.h"
3233
#include "nlr.h"
@@ -1074,11 +1075,12 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
10741075
uint pkg_name_len;
10751076
const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
10761077

1077-
char dot_name[pkg_name_len + 1 + qstr_len(name)];
1078+
const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
1079+
char *dot_name = alloca(dot_name_len);
10781080
memcpy(dot_name, pkg_name, pkg_name_len);
10791081
dot_name[pkg_name_len] = '.';
10801082
memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
1081-
qstr dot_name_q = qstr_from_strn(dot_name, sizeof(dot_name));
1083+
qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
10821084

10831085
mp_obj_t args[5];
10841086
args[0] = MP_OBJ_NEW_QSTR(dot_name_q);

windows/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.user
2+
*.*sdf
3+
*.suo
4+
*.sln
5+
*.exe
6+
*.pdb
7+
*.ilk
8+
*.filters
9+
/build/*

windows/README

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
This is experimental, community-supported Windows port of MicroPython.
22
It is based on Unix port, and expected to remain so.
3+
The port requires additional testing, debugging, and patches. Please
4+
consider to contribute.
5+
36

47
To cross-compile under Debian/Ubuntu Linux system:
58

69
sudo apt-get install mingw32 mingw32-binutils mingw32-runtime
710
make CROSS_COMPILE=i586-mingw32msvc-
811

9-
The port requires additional testing, debugging, and patches. Please
10-
consider to contribute.
12+
13+
To compile under Cygwin:
14+
15+
Install following packages using cygwin's setup.exe: mingw-gcc-g++ make
16+
make CROSS_COMPILE=i686-pc-mingw32-
17+
18+
19+
To compile using Visual Studio 2013:
20+
21+
Open micropython.vcxproj and build
22+
23+
24+
To compile using Visual Studio 2013 commandline:
25+
26+
msbuild micropython.vcxproj

windows/init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
*/
2626

2727
#include <stdlib.h>
28+
#include <stdio.h>
2829

2930
void init() {
31+
#ifdef __MINGW32__
3032
putenv("PRINTF_EXPONENT_DIGITS=2");
33+
#else
34+
_set_output_format(_TWO_DIGIT_EXPONENT);
35+
#endif
3136
}

windows/micropython.vcxproj

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<ProjectGuid>{740F3C30-EB6C-4B59-9C50-AE4D5A4A9D12}</ProjectGuid>
23+
<RootNamespace>micropython</RootNamespace>
24+
</PropertyGroup>
25+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
26+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
27+
<ConfigurationType>Application</ConfigurationType>
28+
<UseDebugLibraries>true</UseDebugLibraries>
29+
<PlatformToolset>v120</PlatformToolset>
30+
<CharacterSet>MultiByte</CharacterSet>
31+
</PropertyGroup>
32+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
33+
<ConfigurationType>Application</ConfigurationType>
34+
<UseDebugLibraries>false</UseDebugLibraries>
35+
<PlatformToolset>v120</PlatformToolset>
36+
<WholeProgramOptimization>true</WholeProgramOptimization>
37+
<CharacterSet>MultiByte</CharacterSet>
38+
</PropertyGroup>
39+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
40+
<ConfigurationType>Application</ConfigurationType>
41+
<UseDebugLibraries>true</UseDebugLibraries>
42+
<PlatformToolset>v120</PlatformToolset>
43+
<CharacterSet>MultiByte</CharacterSet>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
46+
<ConfigurationType>Application</ConfigurationType>
47+
<UseDebugLibraries>false</UseDebugLibraries>
48+
<PlatformToolset>v120</PlatformToolset>
49+
<WholeProgramOptimization>true</WholeProgramOptimization>
50+
<CharacterSet>MultiByte</CharacterSet>
51+
</PropertyGroup>
52+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
53+
<ImportGroup Label="ExtensionSettings">
54+
</ImportGroup>
55+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
56+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
57+
<Import Project="msvc/common.props" />
58+
<Import Project="msvc/debug.props" />
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
<Import Project="msvc/common.props" />
63+
<Import Project="msvc/release.props" />
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
<Import Project="msvc/common.props" />
68+
<Import Project="msvc/debug.props" />
69+
</ImportGroup>
70+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
71+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
72+
<Import Project="msvc/common.props" />
73+
<Import Project="msvc/release.props" />
74+
</ImportGroup>
75+
<PropertyGroup Label="UserMacros" />
76+
<PropertyGroup />
77+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
78+
<ClCompile />
79+
<Link />
80+
</ItemDefinitionGroup>
81+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
82+
<ClCompile />
83+
<Link />
84+
</ItemDefinitionGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
86+
<ClCompile />
87+
<Link />
88+
</ItemDefinitionGroup>
89+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
90+
<ClCompile />
91+
<Link />
92+
</ItemDefinitionGroup>
93+
<ItemGroup>
94+
</ItemGroup>
95+
<Import Project="msvc/sources.props" />
96+
<Import Project="msvc/genhdr.targets" />
97+
<Target Name="GenHeaders" BeforeTargets="BuildGenerateSources" DependsOnTargets="GenerateHeaders">
98+
</Target>
99+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
100+
<ImportGroup Label="ExtensionTargets">
101+
</ImportGroup>
102+
</Project>

windows/mpconfigport.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define MICROPY_USE_READLINE (0)
3232
#endif
3333

34+
#define MICROPY_PATH_MAX (260) //see minwindef.h for msvc or limits.h for mingw
3435
#define MICROPY_EMIT_X64 (0)
3536
#define MICROPY_EMIT_THUMB (0)
3637
#define MICROPY_EMIT_INLINE_THUMB (0)
@@ -46,9 +47,12 @@
4647

4748
// type definitions for the specific machine
4849

49-
#ifdef __LP64__
50+
#if defined( __MINGW32__ ) && defined( __LP64__ )
5051
typedef long machine_int_t; // must be pointer size
5152
typedef unsigned long machine_uint_t; // must be pointer size
53+
#elif defined ( _MSC_VER ) && defined( _WIN64 )
54+
typedef __int64 machine_int_t;
55+
typedef unsigned __int64 machine_uint_t;
5256
#else
5357
// These are definitions for machines where sizeof(int) == sizeof(void*),
5458
// regardless for actual size.
@@ -67,3 +71,43 @@ extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
6771

6872
#include "realpath.h"
6973
#include "init.h"
74+
75+
76+
// MSVC specifics
77+
#ifdef _MSC_VER
78+
79+
// Sanity check
80+
81+
#if ( _MSC_VER < 1800 )
82+
#error Can only build with Visual Studio 2013 toolset
83+
#endif
84+
85+
86+
// CL specific overrides from mpconfig
87+
88+
#define NORETURN __declspec(noreturn)
89+
#define MICROPY_EXTRA_CONSTANTS { "dummy", 0 } //can't have zero-sized array
90+
91+
92+
// CL specific definitions
93+
94+
#define restrict
95+
#define inline __inline
96+
#define STDIN_FILENO 0
97+
#define STDOUT_FILENO 1
98+
#define STDERR_FILENO 2
99+
#define PATH_MAX MICROPY_PATH_MAX
100+
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
101+
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
102+
103+
104+
// System headers (needed e.g. for nlr.h)
105+
106+
#include <stddef.h> //for NULL
107+
#include <assert.h> //for assert
108+
109+
110+
// Functions implemented in platform code
111+
112+
int snprintf(char *dest, size_t count, const char *format, ...);
113+
#endif

windows/msvc/common.props

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ImportGroup Label="PropertySheets" />
4+
<PropertyGroup Label="UserMacros" />
5+
<PropertyGroup>
6+
<OutDir>$(ProjectDir)</OutDir>
7+
<IntDir>$(ProjectDir)build\$(Configuration)$(Platform)\</IntDir>
8+
</PropertyGroup>
9+
<ItemDefinitionGroup>
10+
<ClCompile>
11+
<AdditionalIncludeDirectories>.\;.\build;.\msvc;..\py</AdditionalIncludeDirectories>
12+
<PreprocessorDefinitions>_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
13+
<SDLCheck>false</SDLCheck>
14+
<WarningLevel>Level1</WarningLevel>
15+
<ExceptionHandling>false</ExceptionHandling>
16+
</ClCompile>
17+
<Link>
18+
<GenerateDebugInformation>true</GenerateDebugInformation>
19+
</Link>
20+
</ItemDefinitionGroup>
21+
<ItemGroup />
22+
</Project>

0 commit comments

Comments
 (0)