Skip to content

Commit ebf0fe0

Browse files
committed
mpy-cross: Make it easier to build mpy-cross static targets
1 parent c2fd303 commit ebf0fe0

File tree

8 files changed

+183
-83
lines changed

8 files changed

+183
-83
lines changed

mpy-cross/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
mpy-cross
1+
mpy-cross{,.exe}
2+
mpy-cross.static{,.exe}
3+
build-*

mpy-cross/Makefile

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,84 +11,4 @@ override undefine BUILD
1111
override undefine PROG
1212
endif
1313

14-
include ../py/mkenv.mk
15-
16-
# define main target
17-
18-
ifeq ($(OS),Windows_NT)
19-
# Detect a MINGW32 build, and change the name of the final executable.
20-
PROG = mpy-cross.exe
21-
else
22-
PROG = mpy-cross
23-
endif
24-
25-
# qstr definitions (must come before including py.mk)
26-
QSTR_DEFS = qstrdefsport.h
27-
28-
# OS name, for simple autoconfig
29-
UNAME_S := $(shell uname -s)
30-
31-
# include py core make definitions
32-
include $(TOP)/py/py.mk
33-
34-
INC += -I.
35-
INC += -I$(TOP)
36-
INC += -I$(BUILD)
37-
38-
# compiler settings
39-
CWARN = -Wall -Werror
40-
CWARN += -Wpointer-arith -Wuninitialized
41-
CFLAGS = $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
42-
CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables
43-
44-
# Build a static executable.
45-
# Useful for Windows builds, etc., that must run on multiple operating system versions.
46-
ifdef STATIC_BUILD
47-
CFLAGS += -static -static-libgcc -static-libstdc++
48-
endif
49-
50-
51-
# Debugging/Optimization
52-
ifdef DEBUG
53-
CFLAGS += -g
54-
COPT = -O0
55-
else
56-
COPT = -Os #-DNDEBUG
57-
endif
58-
59-
# On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
60-
# The unix port of MicroPython on OSX must be compiled with clang,
61-
# while cross-compile ports require gcc, so we test here for OSX and
62-
# if necessary override the value of 'CC' set in py/mkenv.mk
63-
ifeq ($(UNAME_S),Darwin)
64-
CC = clang
65-
# Use clang syntax for map file
66-
LDFLAGS_ARCH = -Wl,-map,$@.map -Wl,-dead_strip
67-
else
68-
# Use gcc syntax for map file
69-
LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections
70-
endif
71-
LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
72-
73-
ifdef STATIC_BUILD
74-
LDFLAGS += -static -static-libgcc -static-libstdc++
75-
endif
76-
77-
# source files
78-
SRC_C = \
79-
main.c \
80-
gccollect.c \
81-
supervisor/stub/safe_mode.c \
82-
supervisor/stub/stack.c \
83-
supervisor/shared/translate.c
84-
85-
# Add fmode when compiling with mingw gcc
86-
COMPILER_TARGET := $(shell $(CC) -dumpmachine)
87-
ifneq (,$(findstring mingw,$(COMPILER_TARGET)))
88-
SRC_C += ports/windows/fmode.c
89-
endif
90-
91-
OBJ = $(PY_O)
92-
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
93-
94-
include $(TOP)/py/mkrules.mk
14+
include mpy-cross.mk

mpy-cross/Makefile.static

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PROG=mpy-cross.static
2+
BUILD=build-static
3+
STATIC_BUILD=1
4+
5+
include mpy-cross.mk

mpy-cross/Makefile.static-mingw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PROG=mpy-cross.static.exe
2+
CROSS_COMPILE = x86_64-w64-mingw32-
3+
BUILD=build-static-mingw
4+
STATIC_BUILD=1
5+
6+
include mpy-cross.mk

mpy-cross/fmode.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "fmode.h"
28+
#include "py/mpconfig.h"
29+
#include <fcntl.h>
30+
#include <stdlib.h>
31+
32+
// Workaround for setting file translation mode: we must distinguish toolsets
33+
// since mingw has no _set_fmode, and altering msvc's _fmode directly has no effect
34+
STATIC int set_fmode_impl(int mode) {
35+
#ifndef _MSC_VER
36+
_fmode = mode;
37+
return 0;
38+
#else
39+
return _set_fmode(mode);
40+
#endif
41+
}
42+
43+
void set_fmode_binary(void) {
44+
set_fmode_impl(O_BINARY);
45+
}
46+
47+
void set_fmode_text(void) {
48+
set_fmode_impl(O_TEXT);
49+
}

mpy-cross/fmode.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_WINDOWS_FMODE_H
27+
#define MICROPY_INCLUDED_WINDOWS_FMODE_H
28+
29+
// Treat files opened by open() as binary. No line ending translation is done.
30+
void set_fmode_binary(void);
31+
32+
// Treat files opened by open() as text.
33+
// When reading from the file \r\n will be converted to \n.
34+
// When writing to the file \n will be converted into \r\n.
35+
void set_fmode_text(void);
36+
37+
#endif // MICROPY_INCLUDED_WINDOWS_FMODE_H

mpy-cross/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "py/gc.h"
3636
#include "py/stackctrl.h"
3737
#ifdef _WIN32
38-
#include "ports/windows/fmode.h"
38+
#include "fmode.h"
3939
#endif
4040

4141
// Command line options, with their defaults

mpy-cross/mpy-cross.mk

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
include ../py/mkenv.mk
2+
3+
# define main target
4+
5+
ifeq ($(OS),Windows_NT)
6+
# Detect a MINGW32 build, and change the name of the final executable.
7+
PROG ?= mpy-cross.exe
8+
else
9+
PROG ?= mpy-cross
10+
endif
11+
12+
# qstr definitions (must come before including py.mk)
13+
QSTR_DEFS = qstrdefsport.h
14+
15+
# OS name, for simple autoconfig
16+
UNAME_S := $(shell uname -s)
17+
18+
# include py core make definitions
19+
include $(TOP)/py/py.mk
20+
21+
INC += -I.
22+
INC += -I$(TOP)
23+
INC += -I$(BUILD)
24+
25+
# compiler settings
26+
CWARN = -Wall -Werror
27+
CWARN += -Wpointer-arith -Wuninitialized
28+
CFLAGS = $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
29+
CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables
30+
31+
# Build a static executable.
32+
# Useful for Windows builds, etc., that must run on multiple operating system versions.
33+
ifdef STATIC_BUILD
34+
CFLAGS += -static -static-libgcc -static-libstdc++
35+
endif
36+
37+
38+
# Debugging/Optimization
39+
ifdef DEBUG
40+
CFLAGS += -g
41+
COPT = -O0
42+
else
43+
COPT = -Os #-DNDEBUG
44+
endif
45+
46+
# On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
47+
# The unix port of MicroPython on OSX must be compiled with clang,
48+
# while cross-compile ports require gcc, so we test here for OSX and
49+
# if necessary override the value of 'CC' set in py/mkenv.mk
50+
ifeq ($(UNAME_S),Darwin)
51+
CC = clang
52+
# Use clang syntax for map file
53+
LDFLAGS_ARCH = -Wl,-map,$@.map -Wl,-dead_strip
54+
else
55+
# Use gcc syntax for map file
56+
LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections
57+
endif
58+
LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
59+
60+
ifdef STATIC_BUILD
61+
LDFLAGS += -static -static-libgcc -static-libstdc++
62+
endif
63+
64+
# source files
65+
SRC_C = \
66+
main.c \
67+
gccollect.c \
68+
supervisor/stub/safe_mode.c \
69+
supervisor/stub/stack.c \
70+
supervisor/shared/translate.c
71+
72+
# Add fmode when compiling with mingw gcc
73+
COMPILER_TARGET := $(shell $(CC) -dumpmachine)
74+
ifneq (,$(findstring mingw,$(COMPILER_TARGET)))
75+
SRC_C += fmode.c
76+
endif
77+
78+
OBJ = $(PY_O)
79+
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
80+
81+
include $(TOP)/py/mkrules.mk

0 commit comments

Comments
 (0)