Move the definition of what to build in a table.

This provide a better visual definition (and potential better explicit)
of the previous code base on `if/else`.
This commit is contained in:
Matthieu Gautier 2023-05-11 16:12:53 +02:00
parent 7f7156ece5
commit fd46c52473
3 changed files with 108 additions and 46 deletions

105
.github/scripts/build_definition.py vendored Normal file
View File

@ -0,0 +1,105 @@
from typing import NamedTuple
import csv, io, re
# Definition of what to build.
# Array is read line by line.
# Empty cells under (OS_NAME, DESKTOP, PLATFORM_TARGET) mean "always match" (catch all, or `.*` regex)
# Once a cell doesn't match, skip to the next line.
# Once a line matches, other lines are not read, so put more specific combination first.
# Lines composed of `-` , or `=`, or starting by `#` are ignored.
BUILD_DEF = """
| OS_NAME | DESKTOP | PLATFORM_TARGET | libzim | libkiwix | zim-tools | kiwix-tools | kiwix-desktop |
=======================================================================================================
# Bionic is a special case as we need to compile libzim on old arch for python
| bionic | | | x | | | | |
-------------------------------------------------------------------------------------------------------
# Osx builds, build binaries on native_dyn and native_static. On any other things, build only the libraries
| osx | | native_dyn | | | x | x | |
| osx | | native_static | | | x | x | |
| osx | | | x | x | | | |
-------------------------------------------------------------------------------------------------------
# Build kiwix-desktop only on specific targets
| | eval'True | | | | | | x |
| | | flatpak | | | | | x |
-------------------------------------------------------------------------------------------------------
# Library builds, on embedded archs or on all *_mixed targets
| | | android_.* | x | x | | | |
| | | native_mixed | x | x | | | |
| | | .*_mixed | x | | | | |
| | | wasm | x | | | | |
# Build binaries on *_static targets or on all others "non mixed" targets (where we have already build libs)
| | | native_.* | | | x | x | |
| | | .*_static | | | x | x | |
| | | armv[68]_.* | | | x | x | |
| | | aarch64_.* | | | x | x | |
# Else, let's build everything.
| | | | x | x | x | x | |
"""
class TableDialect(csv.Dialect):
delimiter = "|"
quoting = csv.QUOTE_NONE
lineterminator = "\n"
def strip_array(array_str):
"""Return a iterable of lines, skiping "decorative lines" and with all values in the line's cells stripped"""
for line in array_str.splitlines():
line = line.strip()
line_set = set(line)
if (
not line
or line.startswith("#")
or (len(line_set) == 1 and line_set.pop() in "-=")
):
continue
yield "|".join(c.strip() for c in line.split("|"))
def selector_match(selector, value):
if not selector:
return True
if selector.startswith("eval'"):
selector = eval(selector[5:])
return selector == value
return re.fullmatch(selector, value) is not None
class Context(NamedTuple):
OS_NAME: str
DESKTOP: bool
PLATFORM_TARGET: str
def match(self, row):
for key in ["OS_NAME", "DESKTOP", "PLATFORM_TARGET"]:
context_value = getattr(self, key)
selector = row[key]
if not selector_match(selector, context_value):
return False
return True
def select_build_targets():
from common import PLATFORM_TARGET, DESKTOP, OS_NAME
context = Context(PLATFORM_TARGET=PLATFORM_TARGET, DESKTOP=DESKTOP, OS_NAME=OS_NAME)
reader = csv.DictReader(strip_array(BUILD_DEF), dialect=TableDialect())
for row in reader:
if context.match(row):
build_order = [
k
for k in (
"libzim",
"libkiwix",
"zim-tools",
"kiwix-tools",
"kiwix-desktop",
)
if row[k] == "x"
]
print(build_order)
return build_order
raise "No definition match with current context."

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from build_definition import select_build_targets
from common import ( from common import (
run_kiwix_build, run_kiwix_build,
make_archive, make_archive,
@ -12,50 +12,7 @@ from common import (
DEV_BRANCH, DEV_BRANCH,
) )
for target in select_build_targets():
def select_build_target():
from common import (
PLATFORM_TARGET,
DESKTOP,
OS_NAME
)
if OS_NAME == "bionic" and PLATFORM_TARGET.endswith("_mixed"):
return ("libzim", )
elif (PLATFORM_TARGET.startswith("android_")
or PLATFORM_TARGET.startswith("iOS")):
return ("libzim", "libkiwix")
elif PLATFORM_TARGET.startswith("macOS"):
if PLATFORM_TARGET.endswith("_mixed"):
return ("libzim", "libkiwix")
elif PLATFORM_TARGET.endswith("_dyn"):
return ("zim-tools", "kiwix-tools")
else:
return []
elif PLATFORM_TARGET.startswith("native_"):
if OS_NAME == "osx":
if PLATFORM_TARGET.endswith("_mixed"):
return ("libzim", "libkiwix")
else:
return ("zim-tools", "kiwix-tools")
else:
if DESKTOP:
return ("kiwix-desktop",)
elif PLATFORM_TARGET == "native_mixed":
return ("libzim", "libkiwix")
else:
return ("zim-tools", "kiwix-tools")
elif PLATFORM_TARGET in ("win32_static", "armv6_static", "armv6_dyn", "armv8_static", "armv8_dyn", "aarch64_static", "aarch64_dyn", "i586_static"):
return ("zim-tools", "kiwix-tools")
elif PLATFORM_TARGET == "flatpak":
return ("kiwix-desktop",)
elif PLATFORM_TARGET in ("wasm", "armv6_mixed", "armv8_mixed", "aarch64_mixed"):
return ("libzim", )
else:
return ("libzim", "zim-tools", "libkiwix", "kiwix-tools")
TARGETS = select_build_target()
for target in TARGETS:
run_kiwix_build(target, platform=PLATFORM_TARGET) run_kiwix_build(target, platform=PLATFORM_TARGET)
if target == "kiwix-desktop": if target == "kiwix-desktop":
archive = create_desktop_image(make_release=False) archive = create_desktop_image(make_release=False)

View File

@ -21,7 +21,7 @@ from common import (
notarize_macos_build, notarize_macos_build,
) )
from build_projects import select_build_target from build_definition import select_build_targets
TARGETS = select_build_target() TARGETS = select_build_target()