Skip to content

Commit 074d713

Browse files
dhylandsdpgeorge
authored andcommitted
lib/memzip: Factor out memzip from teensy/ into lib/memzip.
1 parent a9f3030 commit 074d713

File tree

7 files changed

+126
-13
lines changed

7 files changed

+126
-13
lines changed

lib/memzip/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
MEMZIP - a simple readonly file system
2+
3+
memzip takes a zip file which is comprised of uncompressed files and
4+
and presents it as a filesystem, allowing Python files to be imported.
5+
6+
The script make-memzip.py takes a directory name and will create a zip file
7+
containing uncompressed files found in the directory. It will then generate
8+
a C file which contains the data from the zip file.
9+
10+
A typical addition to a makefile would look like:
11+
```
12+
SRC_C += \
13+
lib/memzip/import.c \
14+
lib/memzip/lexermemzip.c \
15+
lib/memzip/memzip.c \
16+
17+
OBJ += $(BUILD)/memzip-files.o
18+
19+
MAKE_MEMZIP = ../lib/memzip/make-memzip.py
20+
21+
$(BUILD)/memzip-files.o: $(BUILD)/memzip-files.c
22+
$(call compile_c)
23+
24+
$(BUILD)/memzip-files.c: $(shell find ${MEMZIP_DIR} -type f)
25+
@$(ECHO) "Creating $@"
26+
$(Q)$(PYTHON) $(MAKE_MEMZIP) --zip-file $(BUILD)/memzip-files.zip --c-file $@ $(MEMZIP_DIR)
27+
```
28+
File renamed without changes.
File renamed without changes.

lib/memzip/make-memzip.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
#
3+
# Takes a directory of files and zips them up (as uncompressed files).
4+
# This then gets converted into a C data structure which can be read
5+
# like a filesystem at runtime.
6+
#
7+
# This is somewhat like frozen modules in python, but allows arbitrary files
8+
# to be used.
9+
10+
from __future__ import print_function
11+
12+
import argparse
13+
import os
14+
import subprocess
15+
import sys
16+
import types
17+
18+
def create_zip(zip_filename, zip_dir):
19+
abs_zip_filename = os.path.abspath(zip_filename)
20+
save_cwd = os.getcwd()
21+
os.chdir(zip_dir)
22+
if os.path.exists(abs_zip_filename):
23+
os.remove(abs_zip_filename)
24+
subprocess.check_call(['zip', '-0', '-r', '-D', abs_zip_filename, '.'])
25+
os.chdir(save_cwd)
26+
27+
def create_c_from_file(c_filename, zip_filename):
28+
with open(zip_filename, 'rb') as zip_file:
29+
with open(c_filename, 'wb') as c_file:
30+
print('#include <stdint.h>', file=c_file)
31+
print('', file=c_file)
32+
print('const uint8_t memzip_data[] = {', file=c_file)
33+
while True:
34+
buf = zip_file.read(16)
35+
if not buf:
36+
break
37+
print(' ', end='', file=c_file)
38+
for byte in buf:
39+
if type(byte) is types.StringType:
40+
print(' 0x{:02x},'.format(ord(byte)), end='', file=c_file)
41+
else:
42+
print(' 0x{:02x},'.format(byte), end='', file=c_file)
43+
print('', file=c_file)
44+
print('};', file=c_file)
45+
46+
def main():
47+
parser = argparse.ArgumentParser(
48+
prog='make-memzip.py',
49+
usage='%(prog)s [options] [command]',
50+
description='Generates a C source memzip file.'
51+
)
52+
parser.add_argument(
53+
'-z', '--zip-file',
54+
dest='zip_filename',
55+
help='Specifies the name of the created zip file.',
56+
default='memzip_files.zip'
57+
)
58+
parser.add_argument(
59+
'-c', '--c-file',
60+
dest='c_filename',
61+
help='Specifies the name of the created C source file.',
62+
default='memzip_files.c'
63+
)
64+
parser.add_argument(
65+
dest='source_dir',
66+
default='memzip_files'
67+
)
68+
args = parser.parse_args(sys.argv[1:])
69+
70+
print('args.zip_filename =', args.zip_filename)
71+
print('args.c_filename =', args.c_filename)
72+
print('args.source_dir =', args.source_dir)
73+
74+
create_zip(args.zip_filename, args.source_dir)
75+
create_c_from_file(args.c_filename, args.zip_filename)
76+
77+
if __name__ == "__main__":
78+
main()
79+

teensy/memzip.c renamed to lib/memzip/memzip.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#include "py/misc.h"
66
#include "memzip.h"
77

8-
extern uint8_t _staticfs[];
8+
extern uint8_t memzip_data[];
99

1010
const MEMZIP_FILE_HDR *memzip_find_file_header(const char *filename) {
1111

12-
const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)_staticfs;
12+
const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)memzip_data;
1313
uint8_t *mem_data;
1414

1515
/* Zip file filenames don't have a leading /, so we strip it off */
@@ -33,7 +33,7 @@ const MEMZIP_FILE_HDR *memzip_find_file_header(const char *filename) {
3333
}
3434

3535
bool memzip_is_dir(const char *filename) {
36-
const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)_staticfs;
36+
const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)memzip_data;
3737
uint8_t *mem_data;
3838

3939
if (strcmp(filename, "/") == 0) {
File renamed without changes.

teensy/Makefile

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ SRC_C = \
7878
hal_ftm.c \
7979
hal_gpio.c \
8080
help.c \
81-
import.c \
8281
main.c \
8382
lcd.c \
8483
led.c \
85-
lexermemzip.c \
86-
memzip.c \
84+
lib/memzip/import.c \
85+
lib/memzip/lexermemzip.c \
86+
lib/memzip/memzip.c \
8787
modpyb.c \
8888
pin_defs_teensy.c \
8989
reg.c \
@@ -92,6 +92,7 @@ SRC_C = \
9292
uart.c \
9393
usb.c \
9494

95+
9596
STM_SRC_C = $(addprefix stmhal/,\
9697
gccollect.c \
9798
input.c \
@@ -126,12 +127,13 @@ SRC_TEENSY = $(addprefix core/,\
126127
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(STM_SRC_S:.s=.o) $(SRC_TEENSY:.c=.o))
127128
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
128129
OBJ += $(BUILD)/pins_gen.o
130+
OBJ += $(BUILD)/memzip-files.o
129131

130132
all: hex
131-
hex: $(BUILD)/micropython-mz.hex
133+
hex: $(BUILD)/micropython.hex
132134

133135
ifeq ($(ARDUINO),)
134-
post_compile: $(BUILD)/micropython-mz.hex
136+
post_compile: $(BUILD)/micropython.hex
135137
$(ECHO) "Please define ARDUINO (where TeensyDuino is installed)"
136138
exit 1
137139

@@ -142,7 +144,7 @@ reboot:
142144
else
143145
TOOLS_PATH = $(ARDUINO)/hardware/tools
144146

145-
post_compile: $(BUILD)/micropython-mz.hex
147+
post_compile: $(BUILD)/micropython.hex
146148
$(ECHO) "Preparing $@ for upload"
147149
$(Q)$(TOOLS_PATH)/teensy_post_compile -file="$(basename $(<F))" -path="$(abspath $(<D))" -tools="$(TOOLS_PATH)"
148150

@@ -163,15 +165,12 @@ ifeq ($(MEMZIP_DIR),)
163165
MEMZIP_DIR = memzip_files
164166
endif
165167

166-
$(BUILD)/micropython-mz.hex: $(BUILD)/micropython.hex $(shell find ${MEMZIP_DIR} -type f)
167-
@$(ECHO) "Creating $@"
168-
$(Q)./add-memzip.sh $< $@ ${MEMZIP_DIR}
169-
170168
$(BUILD)/%.hex: $(BUILD)/%.elf
171169
$(ECHO) "HEX $<"
172170
$(Q)$(OBJCOPY) -O ihex -R .eeprom "$<" "$@"
173171

174172
MAKE_PINS = make-pins.py
173+
MAKE_MEMZIP = ../lib/memzip/make-memzip.py
175174
BOARD_PINS = teensy_pins.csv
176175
AF_FILE = mk20dx256_af.csv
177176
PREFIX_FILE = mk20dx256_prefix.c
@@ -197,6 +196,13 @@ $(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qst
197196
$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c
198197
$(call compile_c)
199198

199+
$(BUILD)/memzip-files.o: $(BUILD)/memzip-files.c
200+
$(call compile_c)
201+
202+
$(BUILD)/memzip-files.c: $(shell find ${MEMZIP_DIR} -type f)
203+
@$(ECHO) "Creating $@"
204+
$(Q)$(PYTHON) $(MAKE_MEMZIP) --zip-file $(BUILD)/memzip-files.zip --c-file $@ $(MEMZIP_DIR)
205+
200206
$(BUILD)/%.pp: $(BUILD)/%.c
201207
$(ECHO) "PreProcess $<"
202208
$(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $<

0 commit comments

Comments
 (0)