Skip to content

Commit 07449f6

Browse files
aparcarynezz
authored andcommitted
build: refactor JSON info files to profiles.json
JSON info files contain machine readable information of built profiles and resulting images. These files were added in commit 881ed09 ("build: create JSON files containing image info"). They are useful for firmware wizards and script checking for reproducibility. Currently all JSON files are stored next to the built images, resulting in up to 168 individual files for the ath79/generic target. This patch refactors the JSON creation to store individual per image (not per profile) files in $(BUILD_DIR)/json_info_files and create an single overview file called `profiles.json` in the target directory. Storing per image files and not per profile solves the problem of parallel file writes. If a profiles sysupgrade and factory image are finished at the same time both processes would write to the same JSON file, resulting in randomly broken outputs. Some target like x86/64 do not use the image code yet, resulting in missing JSON files. If no JSON info files were created, no `profiles.json` files is created as it would be empty anyway. As before, this creation is enabled by default only if `BUILDBOT` is set. Tested via buildroot & ImageBuilder on ath79/generic, imx6 and x86/64. Signed-off-by: Paul Spooren <mail@aparcar.org> [json_info_files dir handling in Make, if case refactoring] Signed-off-by: Petr Štetiar <ynezz@true.cz>
1 parent fcd1401 commit 07449f6

File tree

6 files changed

+136
-66
lines changed

6 files changed

+136
-66
lines changed

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ prereq: $(target/stamp-prereq) tmp/.prereq_packages
8888
exit 1; \
8989
fi
9090

91+
$(BIN_DIR)/profiles.json: FORCE
92+
$(if $(CONFIG_JSON_OVERVIEW_IMAGE_INFO), \
93+
WORK_DIR=$(BUILD_DIR)/json_info_files \
94+
$(SCRIPT_DIR)/json_overview_image_info.py $@ \
95+
)
96+
97+
json_overview_image_info: $(BIN_DIR)/profiles.json
98+
9199
checksum: FORCE
92100
$(call sha256sums,$(BIN_DIR),$(CONFIG_BUILDBOT))
93101

@@ -109,6 +117,7 @@ prepare: .config $(tools/stamp-compile) $(toolchain/stamp-compile)
109117

110118
world: prepare $(target/stamp-compile) $(package/stamp-compile) $(package/stamp-install) $(target/stamp-install) FORCE
111119
$(_SINGLE)$(SUBMAKE) -r package/index
120+
$(_SINGLE)$(SUBMAKE) -r json_overview_image_info
112121
$(_SINGLE)$(SUBMAKE) -r checksum
113122

114123
.PHONY: clean dirclean prereq prepare world package/symlinks package/symlinks-install package/symlinks-clean

config/Config-build.in

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
menu "Global build settings"
99

10-
config JSON_ADD_IMAGE_INFO
11-
bool "Create JSON info files per build image"
10+
config JSON_OVERVIEW_IMAGE_INFO
11+
bool "Create JSON info file overview per target"
1212
default BUILDBOT
1313
help
14-
The JSON info files contain information about the device and
15-
build images, stored next to the firmware images.
14+
Create a JSON info file called profiles.json in the target
15+
directory containing machine readable list of built profiles
16+
and resulting images.
1617

1718
config ALL_NONSHARED
1819
bool "Select all target specific packages by default"

include/image.mk

+32-29
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,11 @@ endef
545545

546546
define Device/Build/image
547547
GZ_SUFFIX := $(if $(filter %dtb %gz,$(2)),,$(if $(and $(findstring ext4,$(1)),$(CONFIG_TARGET_IMAGES_GZIP)),.gz))
548-
$$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))$$(GZ_SUFFIX)
548+
$$(_TARGET): $(if $(CONFIG_JSON_OVERVIEW_IMAGE_INFO), \
549+
$(BUILD_DIR)/json_info_files/$(call IMAGE_NAME,$(1),$(2)).json, \
550+
$(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))$$(GZ_SUFFIX))
549551
$(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
552+
550553
ROOTFS/$(1)/$(3) := \
551554
$(KDIR)/root.$(1)$$(strip \
552555
$$(if $$(FS_OPTIONS/$(1)),+fs=$$(call param_mangle,$$(FS_OPTIONS/$(1)))) \
@@ -568,32 +571,33 @@ define Device/Build/image
568571

569572
$(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
570573
cp $$^ $$@
571-
$(if $(CONFIG_JSON_ADD_IMAGE_INFO), \
572-
DEVICE_ID="$(DEVICE_NAME)" \
573-
BIN_DIR="$(BIN_DIR)" \
574-
IMAGE_NAME="$(IMAGE_NAME)" \
575-
IMAGE_TYPE=$(word 1,$(subst ., ,$(2))) \
576-
IMAGE_PREFIX="$(IMAGE_PREFIX)" \
577-
DEVICE_VENDOR="$(DEVICE_VENDOR)" \
578-
DEVICE_MODEL="$(DEVICE_MODEL)" \
579-
DEVICE_VARIANT="$(DEVICE_VARIANT)" \
580-
DEVICE_ALT0_VENDOR="$(DEVICE_ALT0_VENDOR)" \
581-
DEVICE_ALT0_MODEL="$(DEVICE_ALT0_MODEL)" \
582-
DEVICE_ALT0_VARIANT="$(DEVICE_ALT0_VARIANT)" \
583-
DEVICE_ALT1_VENDOR="$(DEVICE_ALT1_VENDOR)" \
584-
DEVICE_ALT1_MODEL="$(DEVICE_ALT1_MODEL)" \
585-
DEVICE_ALT1_VARIANT="$(DEVICE_ALT1_VARIANT)" \
586-
DEVICE_ALT2_VENDOR="$(DEVICE_ALT2_VENDOR)" \
587-
DEVICE_ALT2_MODEL="$(DEVICE_ALT2_MODEL)" \
588-
DEVICE_ALT2_VARIANT="$(DEVICE_ALT2_VARIANT)" \
589-
DEVICE_TITLE="$(DEVICE_TITLE)" \
590-
TARGET="$(BOARD)" \
591-
SUBTARGET="$(if $(SUBTARGET),$(SUBTARGET),generic)" \
592-
VERSION_NUMBER="$(VERSION_NUMBER)" \
593-
VERSION_CODE="$(VERSION_CODE)" \
594-
SUPPORTED_DEVICES="$(SUPPORTED_DEVICES)" \
595-
$(TOPDIR)/scripts/json_add_image_info.py \
596-
)
574+
575+
$(BUILD_DIR)/json_info_files/$(call IMAGE_NAME,$(1),$(2)).json: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))$$(GZ_SUFFIX)
576+
@mkdir -p $$(shell dirname $$@)
577+
DEVICE_ID="$(DEVICE_NAME)" \
578+
BIN_DIR="$(BIN_DIR)" \
579+
IMAGE_NAME="$(IMAGE_NAME)" \
580+
IMAGE_TYPE=$(word 1,$(subst ., ,$(2))) \
581+
IMAGE_PREFIX="$(IMAGE_PREFIX)" \
582+
DEVICE_VENDOR="$(DEVICE_VENDOR)" \
583+
DEVICE_MODEL="$(DEVICE_MODEL)" \
584+
DEVICE_VARIANT="$(DEVICE_VARIANT)" \
585+
DEVICE_ALT0_VENDOR="$(DEVICE_ALT0_VENDOR)" \
586+
DEVICE_ALT0_MODEL="$(DEVICE_ALT0_MODEL)" \
587+
DEVICE_ALT0_VARIANT="$(DEVICE_ALT0_VARIANT)" \
588+
DEVICE_ALT1_VENDOR="$(DEVICE_ALT1_VENDOR)" \
589+
DEVICE_ALT1_MODEL="$(DEVICE_ALT1_MODEL)" \
590+
DEVICE_ALT1_VARIANT="$(DEVICE_ALT1_VARIANT)" \
591+
DEVICE_ALT2_VENDOR="$(DEVICE_ALT2_VENDOR)" \
592+
DEVICE_ALT2_MODEL="$(DEVICE_ALT2_MODEL)" \
593+
DEVICE_ALT2_VARIANT="$(DEVICE_ALT2_VARIANT)" \
594+
DEVICE_TITLE="$(DEVICE_TITLE)" \
595+
TARGET="$(BOARD)" \
596+
SUBTARGET="$(if $(SUBTARGET),$(SUBTARGET),generic)" \
597+
VERSION_NUMBER="$(VERSION_NUMBER)" \
598+
VERSION_CODE="$(VERSION_CODE)" \
599+
SUPPORTED_DEVICES="$(SUPPORTED_DEVICES)" \
600+
$(TOPDIR)/scripts/json_add_image_info.py $$@
597601

598602
endef
599603

@@ -612,8 +616,6 @@ define Device/Build/artifact
612616
endef
613617

614618
define Device/Build
615-
$(shell rm -f $(BIN_DIR)/$(IMG_PREFIX)-$(1).json)
616-
617619
$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
618620
$(call Device/Build/kernel,$(1))
619621

@@ -699,6 +701,7 @@ define BuildImage
699701

700702
image_prepare: compile
701703
mkdir -p $(BIN_DIR) $(KDIR)/tmp
704+
rm -rf $(BUILD_DIR)/json_info_files
702705
$(call Image/Prepare)
703706

704707
legacy-images-prepare-make: image_prepare

scripts/json_add_image_info.py

+41-33
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,63 @@
11
#!/usr/bin/env python3
22

3-
import json
4-
import os
3+
from os import getenv
4+
from pathlib import Path
5+
from sys import argv
56
import hashlib
7+
import json
68

9+
if len(argv) != 2:
10+
print("ERROR: JSON info script requires output arg")
11+
exit(1)
712

8-
def e(variable, default=None):
9-
return os.environ.get(variable, default)
10-
11-
12-
json_path = "{}{}{}.json".format(e("BIN_DIR"), os.sep, e("IMAGE_PREFIX"))
13+
json_path = Path(argv[1])
14+
bin_dir = Path(getenv("BIN_DIR"))
15+
image_file = bin_dir / getenv("IMAGE_NAME")
1316

14-
with open(os.path.join(e("BIN_DIR"), e("IMAGE_NAME")), "rb") as image_file:
15-
image_hash = hashlib.sha256(image_file.read()).hexdigest()
17+
if not image_file.is_file():
18+
print("Skip JSON creation for non existing image ", image_file)
19+
exit(0)
1620

1721

1822
def get_titles():
1923
titles = []
2024
for prefix in ["", "ALT0_", "ALT1_", "ALT2_"]:
2125
title = {}
2226
for var in ["vendor", "model", "variant"]:
23-
if e("DEVICE_{}{}".format(prefix, var.upper())):
24-
title[var] = e("DEVICE_{}{}".format(prefix, var.upper()))
27+
if getenv("DEVICE_{}{}".format(prefix, var.upper())):
28+
title[var] = getenv("DEVICE_{}{}".format(prefix, var.upper()))
2529

2630
if title:
2731
titles.append(title)
2832

2933
if not titles:
30-
titles.append({"title": e("DEVICE_TITLE")})
34+
titles.append({"title": getenv("DEVICE_TITLE")})
3135

3236
return titles
3337

3438

35-
if not os.path.exists(json_path):
36-
device_info = {
37-
"id": e("DEVICE_ID"),
38-
"image_prefix": e("IMAGE_PREFIX"),
39-
"images": [],
40-
"metadata_version": 1,
41-
"supported_devices": e("SUPPORTED_DEVICES").split(),
42-
"target": "{}/{}".format(e("TARGET"), e("SUBTARGET", "generic")),
43-
"titles": get_titles(),
44-
"version_commit": e("VERSION_CODE"),
45-
"version_number": e("VERSION_NUMBER"),
46-
}
47-
else:
48-
with open(json_path, "r") as json_file:
49-
device_info = json.load(json_file)
50-
51-
image_info = {"type": e("IMAGE_TYPE"), "name": e("IMAGE_NAME"), "sha256": image_hash}
52-
device_info["images"].append(image_info)
53-
54-
with open(json_path, "w") as json_file:
55-
json.dump(device_info, json_file, sort_keys=True, indent=" ")
39+
device_id = getenv("DEVICE_ID")
40+
image_hash = hashlib.sha256(image_file.read_bytes()).hexdigest()
41+
42+
image_info = {
43+
"metadata_version": 1,
44+
"target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
45+
"version_code": getenv("VERSION_CODE"),
46+
"version_number": getenv("VERSION_NUMBER"),
47+
"profiles": {
48+
device_id: {
49+
"image_prefix": getenv("IMAGE_PREFIX"),
50+
"images": [
51+
{
52+
"type": getenv("IMAGE_TYPE"),
53+
"name": getenv("IMAGE_NAME"),
54+
"sha256": image_hash,
55+
}
56+
],
57+
"supported_devices": getenv("SUPPORTED_DEVICES").split(),
58+
"titles": get_titles(),
59+
}
60+
},
61+
}
62+
63+
json_path.write_text(json.dumps(image_info, separators=(",", ":")))

scripts/json_overview_image_info.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
import json
4+
from pathlib import Path
5+
from os import getenv
6+
from sys import argv
7+
8+
if len(argv) != 2:
9+
print("JSON info files script requires ouput file as argument")
10+
exit(1)
11+
12+
output_path = Path(argv[1])
13+
14+
assert getenv("WORK_DIR"), "$WORK_DIR required"
15+
16+
work_dir = Path(getenv("WORK_DIR"))
17+
18+
assert work_dir.is_dir(), "$WORK_DIR not a directory"
19+
20+
output = {}
21+
22+
for json_file in work_dir.glob("*.json"):
23+
image_info = json.loads(json_file.read_text())
24+
if not output:
25+
output.update(image_info)
26+
else:
27+
# get first (and only) profile in json file
28+
device_id = next(iter(image_info["profiles"].keys()))
29+
if device_id not in output["profiles"]:
30+
output["profiles"].update(image_info["profiles"])
31+
else:
32+
output["profiles"][device_id]["images"].append(
33+
image_info["profiles"][device_id]["images"][0]
34+
)
35+
36+
if output:
37+
output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":")))
38+
else:
39+
print("JSON info file script could not find any JSON files for target")

target/imagebuilder/files/Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ _call_image: staging_dir/host/.prereq-build
118118
$(MAKE) package_install
119119
$(MAKE) -s prepare_rootfs
120120
$(MAKE) -s build_image
121+
$(MAKE) -s json_overview_image_info
121122
$(MAKE) -s checksum
122123

123124
_call_manifest: FORCE
@@ -163,12 +164,21 @@ prepare_rootfs: FORCE
163164
$(CP) $(TARGET_DIR) $(TARGET_DIR_ORIG)
164165
$(call prepare_rootfs,$(TARGET_DIR),$(USER_FILES),$(DISABLED_SERVICES))
165166

167+
166168
build_image: FORCE
167169
@echo
168170
@echo Building images...
169171
$(NO_TRACE_MAKE) -C target/linux/$(BOARD)/image install TARGET_BUILD=1 IB=1 EXTRA_IMAGE_NAME="$(EXTRA_IMAGE_NAME)" \
170172
$(if $(USER_PROFILE),PROFILE="$(USER_PROFILE)")
171173

174+
$(BIN_DIR)/profiles.json: FORCE
175+
$(if $(CONFIG_JSON_OVERVIEW_IMAGE_INFO), \
176+
WORK_DIR=$(BUILD_DIR)/json_info_files \
177+
$(SCRIPT_DIR)/json_overview_image_info.py $@ \
178+
)
179+
180+
json_overview_image_info: $(BIN_DIR)/profiles.json
181+
172182
checksum: FORCE
173183
@echo
174184
@echo Calculating checksums...

0 commit comments

Comments
 (0)