|
| 1 | +""" |
| 2 | +Action to build a specific Makefile target |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from aws_lambda_builders.actions import BaseAction, Purpose, ActionFailedError |
| 9 | +from .exceptions import MakeFileNotFoundError |
| 10 | +from .make import MakeExecutionError |
| 11 | + |
| 12 | +LOG = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class CustomMakeAction(BaseAction): |
| 16 | + |
| 17 | + """ |
| 18 | + A Lambda Builder Action that builds and packages a provided runtime project using Make. |
| 19 | + """ |
| 20 | + |
| 21 | + NAME = "MakeBuild" |
| 22 | + DESCRIPTION = "Running build target on Makefile" |
| 23 | + PURPOSE = Purpose.COMPILE_SOURCE |
| 24 | + |
| 25 | + def __init__(self, artifacts_dir, scratch_dir, manifest_path, osutils, subprocess_make, build_logical_id): |
| 26 | + """ |
| 27 | + :type artifacts_dir: str |
| 28 | + :param artifacts_dir: directory where artifacts needs to be stored. |
| 29 | +
|
| 30 | + :type scratch_dir: str |
| 31 | + :param scratch_dir: an existing (writable) directory for temporary files |
| 32 | +
|
| 33 | + :type manifest_path: str |
| 34 | + :param manifest_path: path to Makefile of an Make project with the source in same folder. |
| 35 | +
|
| 36 | + :type osutils: aws_lambda_builders.workflows.custom_make.utils.OSUtils |
| 37 | + :param osutils: An instance of OS Utilities for file manipulation |
| 38 | +
|
| 39 | + :type subprocess_make aws_lambda_builders.workflows.custom_make.make.SubprocessMake |
| 40 | + :param subprocess_make: An instance of the Make process wrapper |
| 41 | + """ |
| 42 | + super(CustomMakeAction, self).__init__() |
| 43 | + self.artifacts_dir = artifacts_dir |
| 44 | + self.scratch_dir = scratch_dir |
| 45 | + self.manifest_path = manifest_path |
| 46 | + self.osutils = osutils |
| 47 | + self.subprocess_make = subprocess_make |
| 48 | + self.build_logical_id = build_logical_id |
| 49 | + |
| 50 | + @property |
| 51 | + def artifact_dir_path(self): |
| 52 | + # This is required when running on windows to determine if we are running in linux |
| 53 | + # subsystem or on native cmd or powershell. |
| 54 | + if self.osutils.is_windows(): |
| 55 | + return Path(self.artifacts_dir).as_posix() if self.osutils.which("sh") else self.artifacts_dir |
| 56 | + else: |
| 57 | + return self.artifacts_dir |
| 58 | + |
| 59 | + def manifest_check(self): |
| 60 | + # Check for manifest file presence and if not present raise MakefileNotFoundError |
| 61 | + if not self.osutils.exists(self.manifest_path): |
| 62 | + raise MakeFileNotFoundError("Makefile not found at {}".format(self.manifest_path)) |
| 63 | + |
| 64 | + def execute(self): |
| 65 | + """ |
| 66 | + Runs the action. |
| 67 | +
|
| 68 | + :raises lambda_builders.actions.ActionFailedError: when Make Build fails. |
| 69 | + """ |
| 70 | + |
| 71 | + # Check for manifest file |
| 72 | + try: |
| 73 | + self.manifest_check() |
| 74 | + except MakeFileNotFoundError as ex: |
| 75 | + raise ActionFailedError(str(ex)) |
| 76 | + |
| 77 | + # Create the Artifacts Directory if it doesnt exist. |
| 78 | + if not self.osutils.exists(self.artifacts_dir): |
| 79 | + self.osutils.makedirs(self.artifacts_dir) |
| 80 | + |
| 81 | + try: |
| 82 | + current_env = self.osutils.environ() |
| 83 | + LOG.info("Current Artifacts Directory : %s", self.artifact_dir_path) |
| 84 | + current_env.update({"ARTIFACTS_DIR": self.artifact_dir_path}) |
| 85 | + # Export environmental variables that might be needed by other binaries used |
| 86 | + # within the Makefile and also specify the makefile to be used as well. |
| 87 | + self.subprocess_make.run( |
| 88 | + [ |
| 89 | + "--makefile", |
| 90 | + "{}".format(self.manifest_path), |
| 91 | + "build-{logical_id}".format(logical_id=self.build_logical_id), |
| 92 | + ], |
| 93 | + env=current_env, |
| 94 | + cwd=self.scratch_dir, |
| 95 | + ) |
| 96 | + except MakeExecutionError as ex: |
| 97 | + raise ActionFailedError(str(ex)) |
0 commit comments