| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
"""Graphviz CI script for compilation on Windows""" |
| 4 |
|
| 5 |
import argparse |
| 6 |
import io |
| 7 |
import itertools |
| 8 |
import os |
| 9 |
import shlex |
| 10 |
import shutil |
| 11 |
import subprocess |
| 12 |
import sys |
| 13 |
import textwrap |
| 14 |
from pathlib import Path |
| 15 |
from typing import Optional, TextIO, Union |
| 16 |
|
| 17 |
|
| 18 |
def run( |
| 19 |
args: list[Union[str, Path]], |
| 20 |
cwd: Path, |
| 21 |
env: dict[str, str], |
| 22 |
out: Optional[TextIO] = None, |
| 23 |
) -> None: |
| 24 |
"""run a command, echoing it beforehand""" |
| 25 |
|
| 26 |
print(f"+ {shlex.join(str(x) for x in args)}", flush=True) |
| 27 |
kwargs = {} |
| 28 |
if out is not None: |
| 29 |
kwargs["stdout"] = subprocess.PIPE |
| 30 |
kwargs["stderr"] = subprocess.STDOUT |
| 31 |
p = subprocess.run(args, cwd=cwd, check=False, text=True, env=env, **kwargs) |
| 32 |
if out is not None: |
| 33 |
sys.stderr.write(p.stdout) |
| 34 |
out.write(p.stdout) |
| 35 |
p.check_returncode() |
| 36 |
|
| 37 |
|
| 38 |
def require(program: str, fallback: Path, env: dict[str, str], log: TextIO) -> None: |
| 39 |
""" |
| 40 |
detect if a given program is missing from the default $Path and needs a fallback |
| 41 |
|
| 42 |
Args: |
| 43 |
program: The name of a program to look for, without its “.exe” suffix. |
| 44 |
fallback: A path at which to find a version of that program. This alternate |
| 45 |
version will be used if the sought program is not found in $Path. |
| 46 |
env: An environment to use when searching for the program. If the program needs |
| 47 |
a fallback, the $Path variable in this environment will be adjusted. |
| 48 |
log: Sink to write informational messages to. |
| 49 |
""" |
| 50 |
assert ( |
| 51 |
fallback / f"{program}.exe" |
| 52 |
).exists(), "{fallback} is not a valid fallback path for {program}" |
| 53 |
which = shutil.which(f"{program}.exe", path=env["PATH"]) |
| 54 |
if which is None: |
| 55 |
log.write( |
| 56 |
textwrap.dedent( |
| 57 |
f"""\ |
| 58 |
{program} not found |
| 59 |
Fallback needed for: {program} |
| 60 |
Setting up fallback path for: {program} to {fallback} |
| 61 |
""" |
| 62 |
) |
| 63 |
) |
| 64 |
env["PATH"] = f"{fallback}{os.pathsep}{env['PATH']}" |
| 65 |
else: |
| 66 |
log.write(f"Found {program} at {which}\n") |
| 67 |
|
| 68 |
|
| 69 |
def main(args: list[str]) -> int: |
| 70 |
"""entry point""" |
| 71 |
|
| 72 |
parser = argparse.ArgumentParser(description=__doc__) |
| 73 |
parser.add_argument( |
| 74 |
"--build-shared-libs", |
| 75 |
choices=("ON", "OFF"), |
| 76 |
default="ON", |
| 77 |
help="control shared libraries selection", |
| 78 |
) |
| 79 |
parser.add_argument( |
| 80 |
"--configuration", |
| 81 |
choices=("Debug", "Release"), |
| 82 |
required=True, |
| 83 |
help="build configuration to select", |
| 84 |
) |
| 85 |
parser.add_argument( |
| 86 |
"--platform", choices=("Win32", "x64"), required=True, help="target platform" |
| 87 |
) |
| 88 |
options = parser.parse_args(args[1:]) |
| 89 |
|
| 90 |
# find the repository root directory |
| 91 |
root = Path(__file__).resolve().parent.parent |
| 92 |
|
| 93 |
# install Python dependencies |
| 94 |
run([sys.executable, "-m", "pip", "install", "uv"], root, None) |
| 95 |
run( |
| 96 |
[ |
| 97 |
sys.executable, |
| 98 |
"-m", |
| 99 |
"uv", |
| 100 |
"pip", |
| 101 |
"install", |
| 102 |
"--requirement", |
| 103 |
"requirements.txt", |
| 104 |
], |
| 105 |
root, |
| 106 |
None, |
| 107 |
) |
| 108 |
|
| 109 |
# retrieve submodules, dependencies are stored there |
| 110 |
run(["git", "submodule", "update", "--init", "--depth=1"], root, None) |
| 111 |
|
| 112 |
# an environment we will use during configuration/compilation |
| 113 |
build_env = os.environ.copy() |
| 114 |
|
| 115 |
# buffer for output so we can report warning count later |
| 116 |
log = io.StringIO() |
| 117 |
|
| 118 |
# find some external build dependencies |
| 119 |
utilities = root / "windows/dependencies/graphviz-build-utilities" |
| 120 |
require("win_bison", utilities / "winflexbison", build_env, log) |
| 121 |
require("win_flex", utilities / "winflexbison", build_env, log) |
| 122 |
require("makensis", utilities / "NSIS/Bin", build_env, log) |
| 123 |
|
| 124 |
build = root / "build" |
| 125 |
if build.exists(): |
| 126 |
shutil.rmtree(build) |
| 127 |
build.mkdir(parents=True) |
| 128 |
run(["cmake", "--version"], build, None, log) |
| 129 |
run( |
| 130 |
[ |
| 131 |
"cmake", |
| 132 |
"--log-level=VERBOSE", |
| 133 |
"-G", |
| 134 |
"Visual Studio 17 2022", |
| 135 |
"-A", |
| 136 |
options.platform, |
| 137 |
f"-DBUILD_SHARED_LIBS={options.build_shared_libs}", |
| 138 |
"-Dwith_cxx_api=ON", |
| 139 |
"-DENABLE_LTDL=ON", |
| 140 |
"-DWITH_EXPAT=ON", |
| 141 |
"-DWITH_GVEDIT=OFF", |
| 142 |
"-DWITH_ZLIB=ON", |
| 143 |
"--warn-uninitialized", |
| 144 |
"-Werror=dev", |
| 145 |
"..", |
| 146 |
], |
| 147 |
build, |
| 148 |
build_env, |
| 149 |
log, |
| 150 |
) |
| 151 |
run( |
| 152 |
["cmake", "--build", ".", "--config", options.configuration], |
| 153 |
build, |
| 154 |
build_env, |
| 155 |
log, |
| 156 |
) |
| 157 |
run(["cpack", "-C", options.configuration], build, build_env, log) |
| 158 |
|
| 159 |
# report warning count |
| 160 |
warning_count = log.getvalue().count(" warning ") |
| 161 |
metrics = Path("metrics.txt") |
| 162 |
summary = f"{os.environ['CI_JOB_NAME']}-warnings {warning_count}" |
| 163 |
print(summary, flush=True) |
| 164 |
metrics.write_text(f"{summary}\n", encoding="utf-8") |
| 165 |
|
| 166 |
# derive Graphviz version |
| 167 |
version_buffer = io.StringIO() |
| 168 |
run([sys.executable, "gen_version.py"], root, None, version_buffer) |
| 169 |
gv_version = version_buffer.getvalue().strip() |
| 170 |
|
| 171 |
# find the installer |
| 172 |
installers = list(build.glob("Graphviz*.exe")) |
| 173 |
assert len(installers) == 1, "failed to find Graphviz installer" |
| 174 |
installer = build / installers[0] |
| 175 |
|
| 176 |
# install Graphviz |
| 177 |
install_dir = "C:\\Graphviz" |
| 178 |
run([installer, "/S", f"/D={install_dir}"], root, None) |
| 179 |
|
| 180 |
# which Windows interface are we targeting? |
| 181 |
if options.platform == "x64": |
| 182 |
api = "win64" |
| 183 |
else: |
| 184 |
api = "win32" |
| 185 |
|
| 186 |
# move the installer to the location expected by CI archiving steps |
| 187 |
dst = build / f"graphviz-install-{gv_version}-{api}.exe" |
| 188 |
shutil.move(installer, dst) |
| 189 |
|
| 190 |
# an environment we will use during testing |
| 191 |
test_env = os.environ.copy() |
| 192 |
|
| 193 |
test_env["PATH"] = f"{install_dir}\\bin;{test_env.get('PATH', '')}" |
| 194 |
test_env["CFLAGS"] = f"{test_env.get('CFLAGS', '')} -I{install_dir}\\include" |
| 195 |
test_env["LIB"] = f"{test_env.get('LIB', '')};{install_dir}\\lib" |
| 196 |
test_env["graphviz_ROOT"] = install_dir |
| 197 |
|
| 198 |
# run the test suite |
| 199 |
run( |
| 200 |
[ |
| 201 |
sys.executable, |
| 202 |
"-m", |
| 203 |
"pytest", |
| 204 |
"-m", |
| 205 |
"not slow", |
| 206 |
"-n", |
| 207 |
"auto", |
| 208 |
"--junit-xml=report.xml", |
| 209 |
"ci/tests.py", |
| 210 |
"tests", |
| 211 |
], |
| 212 |
root, |
| 213 |
test_env, |
| 214 |
) |
| 215 |
|
| 216 |
# create artifacts to archive |
| 217 |
prefix = f"windows_10_cmake_{os.environ['configuration']}_" |
| 218 |
packages = root / "Packages" / os.environ["CI_JOB_NAME"] |
| 219 |
packages.mkdir(parents=True) |
| 220 |
for src in itertools.chain(build.glob("*.exe"), build.glob("*.zip")): |
| 221 |
dst = packages / f"{prefix}{src.name}" |
| 222 |
print(f"+ mv {shlex.join(str(a) for a in [src, dst])}", flush=True) |
| 223 |
shutil.move(src, dst) |
| 224 |
|
| 225 |
return 0 |
| 226 |
|
| 227 |
|
| 228 |
if __name__ == "__main__": |
| 229 |
sys.exit(main(sys.argv)) |