2021-10-01 11:00:22 -07:00
|
|
|
"Build the bundled capnp distribution"
|
2014-12-11 22:04:34 -08:00
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import os
|
2019-10-15 00:42:34 -07:00
|
|
|
import shutil
|
2019-10-15 18:09:11 -07:00
|
|
|
import struct
|
2019-10-15 00:42:34 -07:00
|
|
|
import sys
|
2014-12-11 22:04:34 -08:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2020-11-20 23:38:13 -08:00
|
|
|
def build_libcapnp(bundle_dir, build_dir): # noqa: C901
|
2021-10-01 11:00:22 -07:00
|
|
|
"""
|
2019-09-26 22:18:28 -07:00
|
|
|
Build capnproto
|
2021-10-01 11:00:22 -07:00
|
|
|
"""
|
2019-09-26 22:18:28 -07:00
|
|
|
bundle_dir = os.path.abspath(bundle_dir)
|
2021-10-01 11:00:22 -07:00
|
|
|
capnp_dir = os.path.join(bundle_dir, "capnproto-c++")
|
2019-09-26 22:18:28 -07:00
|
|
|
build_dir = os.path.abspath(build_dir)
|
2021-10-01 11:00:22 -07:00
|
|
|
tmp_dir = os.path.join(capnp_dir, "build{}".format(8 * struct.calcsize("P")))
|
2019-10-15 09:05:01 -07:00
|
|
|
|
|
|
|
# Clean the tmp build directory every time
|
|
|
|
if os.path.exists(tmp_dir):
|
|
|
|
shutil.rmtree(tmp_dir)
|
|
|
|
os.mkdir(tmp_dir)
|
2014-12-11 22:04:34 -08:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
cxxflags = os.environ.get("CXXFLAGS", None)
|
|
|
|
ldflags = os.environ.get("LDFLAGS", None)
|
|
|
|
os.environ["CXXFLAGS"] = (cxxflags or "") + " -O2 -DNDEBUG"
|
|
|
|
os.environ["LDFLAGS"] = ldflags or ""
|
2014-12-11 22:04:34 -08:00
|
|
|
|
2019-10-15 00:42:34 -07:00
|
|
|
# Enable ninja for compilation if available
|
|
|
|
build_type = []
|
2023-10-23 11:30:45 +02:00
|
|
|
if shutil.which("ninja") and os.name != "nt":
|
2021-10-01 11:00:22 -07:00
|
|
|
build_type = ["-G", "Ninja"]
|
2019-10-15 00:42:34 -07:00
|
|
|
|
2019-10-17 00:45:14 -07:00
|
|
|
# Determine python shell architecture for Windows
|
2019-10-15 18:09:11 -07:00
|
|
|
python_arch = 8 * struct.calcsize("P")
|
|
|
|
build_arch = []
|
2019-10-17 00:20:03 -07:00
|
|
|
build_flags = []
|
2021-10-01 11:00:22 -07:00
|
|
|
if os.name == "nt":
|
2019-10-15 18:09:11 -07:00
|
|
|
if python_arch == 64:
|
|
|
|
build_arch_flag = "x64"
|
|
|
|
elif python_arch == 32:
|
|
|
|
build_arch_flag = "Win32"
|
|
|
|
else:
|
2021-10-01 11:00:22 -07:00
|
|
|
raise RuntimeError("Unknown windows build arch")
|
|
|
|
build_arch = ["-A", build_arch_flag]
|
|
|
|
build_flags = ["--config", "Release"]
|
|
|
|
print("Building module for {}".format(python_arch))
|
2019-10-15 00:42:34 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
if not shutil.which("cmake"):
|
|
|
|
raise RuntimeError("Could not find cmake in your path!")
|
2019-10-17 00:20:03 -07:00
|
|
|
|
2019-10-15 00:42:34 -07:00
|
|
|
args = [
|
2021-10-01 11:00:22 -07:00
|
|
|
"cmake",
|
|
|
|
"-DCMAKE_POSITION_INDEPENDENT_CODE=1",
|
|
|
|
"-DBUILD_TESTING=OFF",
|
|
|
|
"-DBUILD_SHARED_LIBS=OFF",
|
|
|
|
"-DCMAKE_INSTALL_PREFIX:PATH={}".format(build_dir),
|
2019-10-15 00:42:34 -07:00
|
|
|
capnp_dir,
|
|
|
|
]
|
|
|
|
args.extend(build_type)
|
2019-10-15 18:09:11 -07:00
|
|
|
args.extend(build_arch)
|
2019-10-15 00:42:34 -07:00
|
|
|
conf = subprocess.Popen(args, cwd=tmp_dir, stdout=sys.stdout)
|
|
|
|
returncode = conf.wait()
|
|
|
|
if returncode != 0:
|
2021-10-01 11:00:22 -07:00
|
|
|
raise RuntimeError("CMake failed {}".format(returncode))
|
2019-10-15 00:42:34 -07:00
|
|
|
|
|
|
|
# Run build through cmake
|
2019-10-17 00:20:03 -07:00
|
|
|
args = [
|
2021-10-01 11:00:22 -07:00
|
|
|
"cmake",
|
|
|
|
"--build",
|
|
|
|
".",
|
|
|
|
"--target",
|
|
|
|
"install",
|
2019-10-17 00:20:03 -07:00
|
|
|
]
|
|
|
|
args.extend(build_flags)
|
|
|
|
build = subprocess.Popen(args, cwd=tmp_dir, stdout=sys.stdout)
|
2019-10-15 00:42:34 -07:00
|
|
|
returncode = build.wait()
|
|
|
|
if cxxflags is None:
|
2021-10-01 11:00:22 -07:00
|
|
|
del os.environ["CXXFLAGS"]
|
2019-10-15 00:42:34 -07:00
|
|
|
else:
|
2021-10-01 11:00:22 -07:00
|
|
|
os.environ["CXXFLAGS"] = cxxflags
|
2020-06-03 22:49:52 -07:00
|
|
|
if ldflags is None:
|
2021-10-01 11:00:22 -07:00
|
|
|
del os.environ["LDFLAGS"]
|
2020-06-03 22:49:52 -07:00
|
|
|
else:
|
2021-10-01 11:00:22 -07:00
|
|
|
os.environ["LDFLAGS"] = ldflags
|
2019-10-15 00:42:34 -07:00
|
|
|
if returncode != 0:
|
2021-10-01 11:00:22 -07:00
|
|
|
raise RuntimeError("capnproto compilation failed: {}".format(returncode))
|