Skip to content

Commit

Permalink
environment: add build-on and build-for architectures (#532)
Browse files Browse the repository at this point in the history
Existing `CRAFT_TARGET_ARCH` and `CRAFT_ARCH_TRIPLET` refer to the
architecture we're building for. To align with the build-on/build-for
naming convention and to give users access to both architectures
when defining override scripts and environments, set new environment
variables as follows:
```
New environment variable 	Deprecated name
------------------------        ------------------
CRAFT_ARCH_BUILD_ON 	        n/a
CRAFT_ARCH_BUILD_FOR            CRAFT_TARGET_ARCH
CRAFT_ARCH_TRIPLET_BUILD_ON     n/a
CRAFT_ARCH_TRIPLET_BUILD_FOR    CRAFT_ARCH_TRIPLET
```
Signed-off-by: Claudio Matsuoka <claudio.matsuoka@canonical.com>
  • Loading branch information
cmatsuoka committed Aug 24, 2023
1 parent fd11ddd commit 9f8e969
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
21 changes: 19 additions & 2 deletions craft_parts/executor/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,14 @@ def _get_global_environment(info: ProjectInfo) -> Dict[str, str]:
:return: A dictionary containing environment variables and values.
"""
global_environment = {
# deprecated, use CRAFT_ARCH_TRIPLET_BUILD_{ON|FOR}
"CRAFT_ARCH_TRIPLET": info.arch_triplet,
# deprecated, use CRAFT_ARCH_BUILD_FOR
"CRAFT_TARGET_ARCH": info.target_arch,
"CRAFT_ARCH_BUILD_ON": info.arch_build_on,
"CRAFT_ARCH_BUILD_FOR": info.arch_build_for,
"CRAFT_ARCH_TRIPLET_BUILD_ON": info.arch_triplet_build_on,
"CRAFT_ARCH_TRIPLET_BUILD_FOR": info.arch_triplet_build_for,
"CRAFT_PARALLEL_BUILD_COUNT": str(info.parallel_build_count),
"CRAFT_PROJECT_DIR": str(info.project_dir),
}
Expand Down Expand Up @@ -248,8 +254,10 @@ def _replace_attr(
) -> Union[List[str], Dict[str, str], str]:
"""Replace environment variables according to the replacements map."""
if isinstance(attr, str):
for replacement, value in replacements.items():
attr = attr.replace(replacement, str(value))
for key, value in replacements.items():
if key in attr:
_warn_if_deprecated_key(key)
attr = attr.replace(key, str(value))
return attr

if isinstance(attr, (list, tuple)):
Expand All @@ -265,3 +273,12 @@ def _replace_attr(
return result

return attr


def _warn_if_deprecated_key(key: str) -> None:
if key in ("$CRAFT_TARGET_ARCH", "${CRAFT_TARGET_ARCH}"):
logger.info("CRAFT_TARGET_ARCH is deprecated, use CRAFT_ARCH_BUILD_FOR")
elif key in ("$CRAFT_ARCH_TRIPLET", "${CRAFT_ARCH_TRIPLET}"):
logger.info(
"CRAFT_ARCH_TRIPLET is deprecated, use CRAFT_ARCH_TRIPLET_BUILD_{ON|FOR}"
)
22 changes: 21 additions & 1 deletion craft_parts/infos.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ProjectVar(YamlModel):
updated: bool = False


# pylint: disable-next=too-many-instance-attributes
# pylint: disable-next=too-many-instance-attributes,too-many-public-methods
class ProjectInfo:
"""Project-level information containing project-specific fields.
Expand Down Expand Up @@ -132,6 +132,26 @@ def cache_dir(self) -> Path:
"""Return the directory used to store cached files."""
return self._cache_dir

@property
def arch_build_on(self) -> str:
"""The architecture we are building on."""
return self._host_machine["deb"]

@property
def arch_build_for(self) -> str:
"""The architecture we are building for."""
return self._machine["deb"]

@property
def arch_triplet_build_on(self) -> str:
"""The machine-vendor-os triplet for the platform we are building on."""
return self._host_machine["triplet"]

@property
def arch_triplet_build_for(self) -> str:
"""The machine-vendor-os triplet for the platform we are building for."""
return self._machine["triplet"]

@property
def arch_triplet(self) -> str:
"""Return the machine-vendor-os platform triplet definition."""
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/executor/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ def test_step_callback(new_dir, mocker, capfd, step):
assert out == (
textwrap.dedent(
f"""\
CRAFT_ARCH_BUILD_FOR=arm64
CRAFT_ARCH_BUILD_ON=arm64
CRAFT_ARCH_TRIPLET=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_FOR=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_ON=aarch64-linux-gnu
CRAFT_PARALLEL_BUILD_COUNT=1
CRAFT_PART_BUILD={new_dir}/parts/foo/build
CRAFT_PART_BUILD_WORK={new_dir}/parts/foo/build
Expand Down Expand Up @@ -126,7 +130,11 @@ def test_prologue_callback(new_dir, capfd, mocker):
assert out == (
textwrap.dedent(
f"""\
CRAFT_ARCH_BUILD_FOR=arm64
CRAFT_ARCH_BUILD_ON=arm64
CRAFT_ARCH_TRIPLET=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_FOR=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_ON=aarch64-linux-gnu
CRAFT_PARALLEL_BUILD_COUNT=1
CRAFT_PART_BUILD={new_dir}/parts/foo/build
CRAFT_PART_BUILD_WORK={new_dir}/parts/foo/build
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/executor/test_environment_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def test_step_callback(new_dir, mocker, capfd, step):
assert out == (
textwrap.dedent(
f"""\
CRAFT_ARCH_BUILD_FOR=arm64
CRAFT_ARCH_BUILD_ON=arm64
CRAFT_ARCH_TRIPLET=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_FOR=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_ON=aarch64-linux-gnu
CRAFT_OVERLAY={new_dir}/overlay/overlay
CRAFT_PARALLEL_BUILD_COUNT=1
CRAFT_PART_BUILD={new_dir}/parts/foo/build
Expand Down Expand Up @@ -146,7 +150,11 @@ def test_prologue_callback(new_dir, capfd, mocker):
assert out == (
textwrap.dedent(
f"""\
CRAFT_ARCH_BUILD_FOR=arm64
CRAFT_ARCH_BUILD_ON=arm64
CRAFT_ARCH_TRIPLET=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_FOR=aarch64-linux-gnu
CRAFT_ARCH_TRIPLET_BUILD_ON=aarch64-linux-gnu
CRAFT_OVERLAY={new_dir}/overlay/overlay
CRAFT_PARALLEL_BUILD_COUNT=1
CRAFT_PART_BUILD={new_dir}/parts/foo/build
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/executor/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging
import textwrap
from pathlib import Path
from typing import Dict, List, Set
Expand Down Expand Up @@ -102,6 +103,10 @@ def test_generate_step_environment_build(new_dir):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_STAGE="{new_dir}/stage"
Expand Down Expand Up @@ -150,6 +155,10 @@ def test_generate_step_environment_no_project_name(new_dir):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_STAGE="{new_dir}/stage"
Expand Down Expand Up @@ -200,6 +209,10 @@ def test_generate_step_environment_no_build(new_dir, step):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_STAGE="{new_dir}/stage"
Expand Down Expand Up @@ -249,6 +262,10 @@ def test_generate_step_environment_no_user_env(new_dir):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_STAGE="{new_dir}/stage"
Expand Down Expand Up @@ -324,6 +341,34 @@ def test_expand_variables_skip(new_dir, partitions):
}


@pytest.mark.parametrize(
"name,value",
[
("$CRAFT_TARGET_ARCH", "arm64"),
("${CRAFT_TARGET_ARCH}", "arm64"),
("$CRAFT_ARCH_TRIPLET", "aarch64-linux-gnu"),
("${CRAFT_ARCH_TRIPLET}", "aarch64-linux-gnu"),
],
)
def test_expand_variables_deprecated(new_dir, name, value, caplog):
info = ProjectInfo(
project_dirs=ProjectDirs(work_dir="/work"),
arch="aarch64",
application_name="xyz",
cache_dir=new_dir,
project_name="test-project",
work_dir="/work",
)

data = {"foo": name}
varname = name.strip("${}")

with caplog.at_level(logging.DEBUG):
environment.expand_environment(data, info=info)
assert data == {"foo": value} # the variable is still expanded
assert f"{varname} is deprecated, use" in caplog.text # but a warning is issued


@pytest.mark.parametrize(
"invalid_vars",
[
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/executor/test_step_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def test_run_builtin_build(self, new_dir, partitions, mocker):
"## Part environment",
f'export CRAFT_ARCH_TRIPLET="{triplet}"',
f'export CRAFT_TARGET_ARCH="{deb}"',
f'export CRAFT_ARCH_BUILD_ON="{deb}"',
f'export CRAFT_ARCH_BUILD_FOR="{deb}"',
f'export CRAFT_ARCH_TRIPLET_BUILD_ON="{triplet}"',
f'export CRAFT_ARCH_TRIPLET_BUILD_FOR="{triplet}"',
'export CRAFT_PARALLEL_BUILD_COUNT="1"',
f'export CRAFT_PROJECT_DIR="{new_dir}"',
*partition_script_lines,
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/features/overlay/test_executor_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def test_generate_step_environment_build(new_dir, partitions):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_OVERLAY="{new_dir}/overlay/overlay"
Expand Down Expand Up @@ -161,6 +165,10 @@ def test_generate_step_environment_no_project_name(new_dir, partitions):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_OVERLAY="{new_dir}/overlay/overlay"
Expand Down Expand Up @@ -216,6 +224,10 @@ def test_generate_step_environment_no_build(new_dir, partitions, step):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_OVERLAY="{new_dir}/overlay/overlay"
Expand Down Expand Up @@ -267,6 +279,10 @@ def test_generate_step_environment_no_user_env(new_dir, partitions):
## Part environment
export CRAFT_ARCH_TRIPLET="aarch64-linux-gnu"
export CRAFT_TARGET_ARCH="arm64"
export CRAFT_ARCH_BUILD_ON="amd64"
export CRAFT_ARCH_BUILD_FOR="arm64"
export CRAFT_ARCH_TRIPLET_BUILD_ON="x86_64-linux-gnu"
export CRAFT_ARCH_TRIPLET_BUILD_FOR="aarch64-linux-gnu"
export CRAFT_PARALLEL_BUILD_COUNT="1"
export CRAFT_PROJECT_DIR="{new_dir}"
export CRAFT_OVERLAY="{new_dir}/overlay/overlay"
Expand Down

0 comments on commit 9f8e969

Please sign in to comment.