Skip to content

Commit 28a8fbb

Browse files
committed
pbio/image: Add dependencies for generated images.
Two changes: - make sure that dependencies are specified in the Makefile, output files need to change when one of the source media file changed, - make sure that the list of source media is checked, rebuild is needed when the list change. This fixes spurious build failures when switching branches for example.
1 parent 11f88f3 commit 28a8fbb

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

bricks/_common/common.mk

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,16 @@ else
679679
FW_SECTIONS :=
680680
endif
681681

682-
$(BUILD)/pbio_image_media.c $(BUILD)/pb_type_image_attributes.c: $(MEDIA_CONVERT)
682+
# Force rebuild if list of media sources changed.
683+
-include $(BUILD)/media_sources_gen.mk
684+
MEDIA_SOURCES := $(shell $(MEDIA_CONVERT) --list)
685+
MEDIA_FORCE := $(if $(strip $(filter-out $(MEDIA_SOURCES),$(MEDIA_SOURCES_GEN)) $(filter-out $(MEDIA_SOURCES_GEN),$(MEDIA_SOURCES))),media-force)
686+
media-force:
687+
.PHONY: media-force
688+
689+
$(BUILD)/pbio_image_media.c $(BUILD)/pbio_image_media.h $(BUILD)/pb_type_image_attributes.c: $(MEDIA_CONVERT) $(MEDIA_SOURCES) $(MEDIA_FORCE)
683690
$(ECHO) "Generating image media files"
684-
$(Q)$(PYTHON) $(MEDIA_CONVERT) $(BUILD)
691+
$(Q)$(PYTHON) $(MEDIA_CONVERT) --dest $(BUILD)
685692

686693
$(BUILD)/font_liberationsans_regular_14.c: $(PBTOP)/lib/pbio/src/image/fonts/LiberationSans-Regular.ttf $(FONT_CONVERT)
687694
$(ECHO) "GEN $@"

lib/pbio/src/image/media.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
#!/usr/bin/env python3
22
import argparse
3+
import sys
34
from pathlib import Path
45
from PIL import Image
56
import cairosvg
67

78
# Take build directory as argument to save generated C files and PNG files.
89
parser = argparse.ArgumentParser(description="Convert image files to C structs.")
9-
parser.add_argument("dest", help="Destination build folder for PNG files.")
10+
parser.add_argument(
11+
"--dest", default="build", help="Destination build folder for generated files."
12+
)
13+
parser.add_argument(
14+
"--list-sources", action="store_true", help="Only list sources files."
15+
)
1016
args = parser.parse_args()
1117

18+
media_dir = Path(__file__).parent.relative_to(Path.cwd()) / "media"
19+
20+
# Collect all SVG and image files in media_dir (svg, png, bmp, jpg), including subfolders.
21+
media_images_sources = (
22+
list(media_dir.rglob("*.png"))
23+
+ list(media_dir.rglob("*.bmp"))
24+
+ list(media_dir.rglob("*.jpg"))
25+
)
26+
media_svg_sources = list(media_dir.rglob("*.svg"))
27+
media_sources = media_images_sources + media_svg_sources
28+
media_sources.sort()
29+
30+
if args.list_sources:
31+
for m in media_sources:
32+
print(m)
33+
sys.exit(0)
34+
1235
build_dir = Path(args.dest)
1336
build_dir.mkdir(parents=True, exist_ok=True)
14-
media_dir = Path(__file__).parent / "media"
37+
38+
media_gen = []
1539

1640
# Convert all SVG files in media_dir to PNG and save in build_dir if not already present.
17-
svg_files = media_dir.rglob("*.svg")
18-
for svg in svg_files:
41+
for svg in media_svg_sources:
1942
png = svg.with_suffix(".png").name
2043
png_path = build_dir / png
44+
media_gen.append(png_path)
2145
if png_path.exists() and png_path.stat().st_mtime >= svg.stat().st_mtime:
2246
continue
2347
with open(svg, "rb") as svg_file:
2448
png_bytes = cairosvg.svg2png(file_obj=svg_file)
2549
with open(png_path, "wb") as out_png:
2650
out_png.write(png_bytes)
2751

28-
# Collect all image files in media_dir (png, bmp, jpg) and build_dir (png), including subfolders.
29-
media_images = (
30-
list(media_dir.rglob("*.png"))
31-
+ list(media_dir.rglob("*.bmp"))
32-
+ list(media_dir.rglob("*.jpg"))
33-
+ list(build_dir.rglob("*.png"))
34-
)
35-
3652

3753
# Convert rgba to monochrome, treating fully transparent pixels as white.
3854
def is_black(r, g, b, a):
@@ -60,6 +76,7 @@ def image_to_8bit_map(img):
6076

6177

6278
# Process each image.
79+
media_images = media_images_sources + media_gen
6380
results = {}
6481
for img_path in media_images:
6582
with Image.open(img_path) as img:
@@ -129,3 +146,8 @@ def image_to_8bit_map(img):
129146
f.write(
130147
"MP_DEFINE_CONST_DICT(pb_type_image_attributes_dict, pb_type_image_attributes_dict_table);"
131148
)
149+
150+
with open(build_dir / "media_sources_gen.mk", "w") as f:
151+
f.write("MEDIA_SOURCES_GEN = \\\n ")
152+
f.write(" \\\n ".join(str(m) for m in media_sources))
153+
f.write("\n")

0 commit comments

Comments
 (0)