Skip to content

Commit

Permalink
Merge pull request #455 from zapta/develop
Browse files Browse the repository at this point in the history
Removed native exe mode and added filtering of env settings
  • Loading branch information
Obijuan authored Nov 12, 2024
2 parents d4fab61 + 55fad16 commit 3f6a697
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 64 deletions.
25 changes: 0 additions & 25 deletions apio/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def __init__(self, project_dir: Optional[Path]):
self.project_dir = util.get_project_dir(project_dir)
self.board: str = None
self.top_module: str = None
self.native_exe_mode: bool = None

@staticmethod
def create_ini(
Expand Down Expand Up @@ -178,8 +177,6 @@ def read(self):
self.top_module = self._parse_top_module(
config_parser, parsed_attributes
)
exe_mode = self._parse_exe_mode(config_parser, parsed_attributes)
self.native_exe_mode = {"default": False, "native": True}[exe_mode]

# Verify that the project file (api.ini) doesn't contain additional
# (illegal) keys that where not parsed
Expand Down Expand Up @@ -233,25 +230,3 @@ def _parse_top_module(
)
return DEFAULT_TOP_MODULE
return top_module

@staticmethod
def _parse_exe_mode(
config_parser: ConfigParser, parsed_attributes: set[str]
) -> str:
"""Read the configured exe mode from the
project file parser and add the keys used
to parsed_attributes.
RETURN:
* A string with "default" (default) or "native"
"""
parsed_attributes.add("exe-mode")
exe_mode = config_parser.get("env", "exe-mode", fallback="default")
if exe_mode not in {"default", "native"}:
click.secho(
f"Error: invalid {PROJECT_FILENAME} project file\n"
"Optional attribute 'exe-mode' should have"
" the value 'default' or 'native'.",
fg="red",
)
sys.exit(1)
return exe_mode
23 changes: 6 additions & 17 deletions apio/managers/scons.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,24 +966,13 @@ def _run(
# -- It is passed to scons using the flag -f default_scons_file
variables += ["-f", f"{scons_file_path}"]

# -- Verify necessary packages if needed.
# pylint: disable=fixme
# TODO: Drop the 'native' mode for simplicity? It is broken anyway.
# E.g. when referencing yosys libraries in Sconstruct.
if self.project.native_exe_mode:
# Assuming blindly that the binaries we need are on the path.
click.secho(
"Warning: native exe mode (binaries should be on path)",
fg="yellow",
)
else:
# Run on `default` config mode
# -- Check that the required packages are installed
pkg_util.check_required_packages(
required_packages_names, self.resources
)
# -- Check that the required packages are installed
pkg_util.check_required_packages(
required_packages_names, self.resources
)

pkg_util.set_env_for_packages(self.resources)
# -- Set env path and vars to use the packages.
pkg_util.set_env_for_packages(self.resources)

# -- Execute scons
return self._execute_scons(command, variables, board)
Expand Down
29 changes: 21 additions & 8 deletions apio/pkg_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,37 @@ def _apply_env_mutations(mutations: EnvMutations) -> None:
os.environ[name] = value


# -- A static flag that is used to make sure we set the env only once.
# -- This is to avoid extending the $PATH variable multiple time with the
# -- same directories.
__ENV_ALREADY_SET_FLAG = False


def set_env_for_packages(resources: Resources, verbose: bool = False) -> None:
"""Sets the environment variables for using all the that are
available for this platform, even if currently not installed.
The function sets the environment only on first call and in latter calls
skips the operation silently.
"""
# pylint: disable=global-statement
global __ENV_ALREADY_SET_FLAG

# -- Collect the env mutations for all packages.
mutations = _get_env_mutations_for_packages(resources)

if verbose:
_dump_env_mutations(mutations)
else:
# -- Be transparent to the user about setting the environment, in case
# -- they will try to run the commands from a regular shell.
click.secho("Setting the envinronment.")

# -- Apply the env mutations. These mutations are temporary and does not
# -- affect the user's shell environment.
_apply_env_mutations(mutations)

# -- If this is the first call, apply the mutations. These mutations are
# -- temporary for the lifetime of this process and does not affect the
# -- user's shell environment. The mutations are also inheritated by
# -- child processes such as the scons processes.
if not __ENV_ALREADY_SET_FLAG:
_apply_env_mutations(mutations)
__ENV_ALREADY_SET_FLAG = True
if not verbose:
click.secho("Setting the envinronment.")


def check_required_packages(
Expand Down
14 changes: 0 additions & 14 deletions test/commands/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ def test_create(clirunner, configenv, validate_cliresult):
apio_ini, {"board": "alhambra-ii", "top-module": "my_module"}
)

# -- Add to the ini file an additional var. It should disappear after
# -- the next wrire.
conf = ConfigObj(str(apio_ini))
conf["env"]["exe-mode"] = "native"
conf.write()
check_ini_file(
apio_ini,
{
"board": "alhambra-ii",
"top-module": "my_module",
"exe-mode": "native",
},
)

# -- Execute "apio create --board icezum
# -- --top-module my_module
# -- --sayyse" with 'y' input
Expand Down

0 comments on commit 3f6a697

Please sign in to comment.