Skip to content

Commit

Permalink
pio extra script: registering my custom option in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
MacDada committed Jun 20, 2023
1 parent f2539f1 commit 29d08ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
33 changes: 28 additions & 5 deletions scripts/pio_extra_script_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from platformio.compat import MISSING
from platformio.platform.base import PlatformBase
from platformio.project.config import ProjectConfigBase
from platformio.project.config import ProjectOptions
from platformio.project.options import ConfigEnvOption
from typing import Callable
from utils import partition


Expand Down Expand Up @@ -33,15 +36,35 @@ def __init__(self, script_globals: dict):
self.env,
]

def register_custom_config_option(
self,
name: str,
description: str,
default=None,
multiple: bool = False,
type: Callable = str,
processor: Callable = None
) -> None:
"""
https://docs.platformio.org/en/latest/scripting/examples/platformio_ini_custom_options.html
https://community.platformio.org/t/custom-platformio-ini-options-as-list-str-not-str/34380/6
"""

option = ConfigEnvOption(
group='custom',
name=name,
description=description,
type=type,
multiple=multiple,
default=default,
validate=processor,
)
ProjectOptions['%s.%s' % (option.scope, option.name)] = option

def get_config_value_as_list(self, name: str) -> list[str]:
"""
https://docs.platformio.org/en/latest/scripting/examples/platformio_ini_custom_options.html
https://community.platformio.org/t/custom-platformio-ini-options-as-list-str-not-str/34380/2
todo: why `env.GetProjectOption()` sometimes returns `str` instead of `list[str]`?
for example `build_src_flags` returns a list,
while my `custom_system_packages` is a string…
https://community.platformio.org/t/custom-platformio-ini-options-as-list-str-not-str/34380
"""

return self.get_project_config().parse_multi_values(
Expand Down
13 changes: 12 additions & 1 deletion scripts/pio_post_extra_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# https://docs.platformio.org/en/latest/projectconf/sections/env/options/advanced/extra_scripts.html
# https://docs.platformio.org/en/latest/scripting/actions.html

import click
from pio_extra_script_helper import Helper

helper = Helper(globals())
Expand Down Expand Up @@ -43,6 +44,16 @@
# https://github.com/platformio/platformio-core/issues/1728#issuecomment-403297776
helper.env.Append(CXXFLAGS=['-Wno-volatile'])

helper.register_custom_config_option(
name='custom_system_packages',
description=(
'Adds `-isystem` flag for the specified packages. '
'As the result, warnings for those packages will be silenced.'
),
multiple=True,
type=click.Choice(helper.list_available_packages()),
)

helper.mark_packages_as_system(
helper.get_config_value_as_list('custom_system_packages')
helper.get_config_value('custom_system_packages')
)

0 comments on commit 29d08ce

Please sign in to comment.