Skip to content

Commit e8967a3

Browse files
committed
Fix tests that need buildenv
1 parent 9678e58 commit e8967a3

File tree

2 files changed

+77
-67
lines changed

2 files changed

+77
-67
lines changed

relenv/buildenv.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,42 @@ def buildenv(
6969
raise RelenvException("buildenv is only supported on Linux")
7070

7171
triplet = get_triplet()
72+
sysroot = f"{toolchain}/{triplet}/sysroot"
7273
env = {
7374
"RELENV_BUILDENV": "1",
7475
"TOOLCHAIN_PATH": f"{toolchain}",
7576
"TRIPLET": f"{triplet}",
7677
"RELENV_PATH": f"{relenv_path}",
7778
"CC": f"{toolchain}/bin/{triplet}-gcc",
7879
"CXX": f"{toolchain}/bin/{triplet}-g++",
79-
"CFLAGS": f"-I{relenv_path}/include -I{toolchain}/sysroot/usr/include",
80+
"CFLAGS": (
81+
f"--sysroot={sysroot} "
82+
f"-I{relenv_path}/include "
83+
f"-I{sysroot}/usr/include"
84+
),
8085
"CXXFLAGS": (
86+
f"--sysroot={sysroot} "
8187
f"-I{relenv_path}/include "
82-
f"-I{toolchain}/{triplet}/sysroot/usr/include "
83-
f"-L{relenv_path}/lib -L{toolchain}/{triplet}/sysroot/lib "
88+
f"-I{sysroot}/usr/include "
89+
f"-L{relenv_path}/lib -L{sysroot}/lib "
8490
f"-Wl,-rpath,{relenv_path}/lib"
8591
),
8692
"CPPFLAGS": (
87-
f"-I{relenv_path}/include " f"-I{toolchain}/{triplet}/sysroot/usr/include"
93+
f"--sysroot={sysroot} "
94+
f"-I{relenv_path}/include "
95+
f"-I{sysroot}/usr/include"
8896
),
8997
"CMAKE_CFLAGS": (
90-
f"-I{relenv_path}/include " f"-I{toolchain}/{triplet}/sysroot/usr/include"
98+
f"--sysroot={sysroot} "
99+
f"-I{relenv_path}/include "
100+
f"-I{sysroot}/usr/include"
91101
),
92102
"LDFLAGS": (
93-
f"-L{relenv_path}/lib -L{toolchain}/{triplet}/sysroot/lib "
103+
f"--sysroot={sysroot} "
104+
f"-L{relenv_path}/lib -L{sysroot}/lib "
94105
f"-Wl,-rpath,{relenv_path}/lib"
95106
),
107+
"CRATE_CC_NO_DEFAULTS": "1",
96108
}
97109
if sys.platform == "dawin":
98110
env["MACOS_DEVELOPMENT_TARGET"] = MACOS_DEVELOPMENT_TARGET

tests/test_verify_build.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ def _install_ppbt(pexec):
6868
assert p.returncode == 0, "Failed to extract toolchain"
6969

7070

71+
def _setup_buildenv(pyexec, env):
72+
"""
73+
Setup build environment variables for compiling C extensions.
74+
75+
On Linux, this calls 'relenv buildenv --json' to get the proper compiler
76+
flags and paths to use the relenv toolchain and bundled libraries instead
77+
of system libraries.
78+
79+
:param pyexec: Path to the relenv Python executable
80+
:param env: Environment dictionary to update with buildenv variables
81+
"""
82+
if sys.platform != "linux":
83+
return
84+
85+
p = subprocess.run(
86+
[
87+
str(pyexec),
88+
"-m",
89+
"relenv",
90+
"buildenv",
91+
"--json",
92+
],
93+
capture_output=True,
94+
)
95+
try:
96+
buildenv = json.loads(p.stdout)
97+
except json.JSONDecodeError:
98+
assert False, f"Failed to decode json: {p.stdout.decode()} {p.stderr.decode()}"
99+
100+
for k in buildenv:
101+
env[k] = buildenv[k]
102+
103+
71104
@pytest.fixture(autouse=True)
72105
def _clear_ssl_env(monkeypatch: pytest.MonkeyPatch) -> None:
73106
"""
@@ -332,27 +365,7 @@ def test_pip_install_salt_w_package_requirements(
332365

333366
_install_ppbt(pyexec)
334367
env = os.environ.copy()
335-
336-
# if sys.platform == "linux":
337-
# p = subprocess.run(
338-
# [
339-
# pyexec,
340-
# "-m",
341-
# "relenv",
342-
# "buildenv",
343-
# "--json",
344-
# ],
345-
# capture_output=True,
346-
# )
347-
# try:
348-
# buildenv = json.loads(p.stdout)
349-
# except json.JSONDecodeError:
350-
# assert (
351-
# False
352-
# ), f"Failed to decode json: {p.stdout.decode()} {p.stderr.decode()}"
353-
# for k in buildenv:
354-
# env[k] = buildenv[k]
355-
368+
_setup_buildenv(pyexec, env)
356369
env["RELENV_BUILDENV"] = "yes"
357370
env["USE_STATIC_REQUIREMENTS"] = "1"
358371
p = subprocess.run(
@@ -997,6 +1010,7 @@ def validate_shebang(path):
9971010
def test_moving_pip_installed_c_extentions(pipexec, pyexec, build, minor_version):
9981011
_install_ppbt(pyexec)
9991012
env = os.environ.copy()
1013+
_setup_buildenv(pyexec, env)
10001014
env["RELENV_DEBUG"] = "yes"
10011015
env["RELENV_BUILDENV"] = "yes"
10021016
p = subprocess.run(
@@ -1028,11 +1042,6 @@ def test_cryptography_rpath(
10281042
pyexec, pipexec, build, minor_version, cryptography_version
10291043
):
10301044
_install_ppbt(pyexec)
1031-
# log.warn("Extract ppbt")
1032-
# p = subprocess.run(
1033-
# [pyexec, "-c", "import ppbt; ppbt.extract()"],
1034-
# )
1035-
# assert p.returncode == 0
10361045

10371046
def find_library(path, search):
10381047
for root, dirs, files in os.walk(path):
@@ -1041,6 +1050,7 @@ def find_library(path, search):
10411050
return fname
10421051

10431052
env = os.environ.copy()
1053+
_setup_buildenv(pyexec, env)
10441054
env["RELENV_BUILDENV"] = "yes"
10451055
p = subprocess.run(
10461056
[
@@ -1355,6 +1365,7 @@ def test_install_python_ldap(pipexec, pyexec, build):
13551365

13561366
subprocess.run(["/usr/bin/bash", "buildscript.sh"], check=True)
13571367
env = os.environ.copy()
1368+
_setup_buildenv(pyexec, env)
13581369
env["RELENV_DEBUG"] = "yes"
13591370
env["RELENV_BUILDENV"] = "yes"
13601371

@@ -1996,45 +2007,32 @@ def rockycontainer(build):
19962007
def test_no_openssl_binary(rockycontainer, pipexec, pyexec, build):
19972008
_install_ppbt(pyexec)
19982009
env = os.environ.copy()
2010+
_setup_buildenv(pyexec, env)
19992011
env["RELENV_BUILDENV"] = "yes"
20002012
if sys.platform == "linux":
2001-
buildenv_proc = subprocess.run(
2002-
[
2003-
str(pyexec),
2004-
"-m",
2005-
"relenv",
2006-
"buildenv",
2007-
"--json",
2008-
],
2009-
capture_output=True,
2010-
env=env,
2013+
toolchain_path = pathlib.Path(env["TOOLCHAIN_PATH"])
2014+
triplet = env["TRIPLET"]
2015+
sysroot_lib = toolchain_path / triplet / "sysroot" / "lib"
2016+
sysroot_lib.mkdir(parents=True, exist_ok=True)
2017+
bz2_sources = sorted(
2018+
(pathlib.Path(build) / "lib").glob("libbz2.so*"),
2019+
key=lambda p: len(p.name),
20112020
)
2012-
if buildenv_proc.returncode == 0:
2013-
buildenv = json.loads(buildenv_proc.stdout)
2014-
env.update(buildenv)
2015-
toolchain_path = pathlib.Path(buildenv["TOOLCHAIN_PATH"])
2016-
triplet = buildenv["TRIPLET"]
2017-
sysroot_lib = toolchain_path / triplet / "sysroot" / "lib"
2018-
sysroot_lib.mkdir(parents=True, exist_ok=True)
2019-
bz2_sources = sorted(
2020-
(pathlib.Path(build) / "lib").glob("libbz2.so*"),
2021-
key=lambda p: len(p.name),
2021+
if not bz2_sources:
2022+
pytest.fail(
2023+
"libbz2.so not found in relenv build; cryptography build cannot proceed"
20222024
)
2023-
if not bz2_sources:
2024-
pytest.fail(
2025-
"libbz2.so not found in relenv build; cryptography build cannot proceed"
2026-
)
2027-
for bz2_source in bz2_sources:
2028-
target = sysroot_lib / bz2_source.name
2029-
if target.exists() or target.is_symlink():
2030-
if target.is_symlink():
2031-
try:
2032-
if target.readlink() == bz2_source:
2033-
continue
2034-
except OSError:
2035-
pass
2036-
target.unlink()
2037-
target.symlink_to(bz2_source)
2025+
for bz2_source in bz2_sources:
2026+
target = sysroot_lib / bz2_source.name
2027+
if target.exists() or target.is_symlink():
2028+
if target.is_symlink():
2029+
try:
2030+
if target.readlink() == bz2_source:
2031+
continue
2032+
except OSError:
2033+
pass
2034+
target.unlink()
2035+
target.symlink_to(bz2_source)
20382036
proc = subprocess.run(
20392037
[
20402038
str(pipexec),

0 commit comments

Comments
 (0)