Skip to content

Kconfig: Support macro expansion within symbol names #9797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ulfalizer
Copy link
Collaborator

Update Kconfiglib to upstream revision 2be02fac78527 to add support for
expanding macros within symbol names, extending the existing Kconfig
preprocessor
(https://github.com/torvalds/linux/blob/master/Documentation/kbuild/
kconfig-macro-language.txt). Some minor optimizations are included as
well.

This makes it possible to add Kconfig templates to avoid code
repetition, e.g. like below:

Kconfig.log_template:

choice
	prompt "Max compiled-in log level for $(module-str)"

config $(module)_LOG_LEVEL_OFF
	bool "Off"

config $(module)_LOG_LEVEL_ERR
	bool "Error"

config $(module)_LOG_LEVEL_WRN
	bool "Warning"

config $(module)_LOG_LEVEL_INF
	bool "Info"

config $(module)_LOG_LEVEL_DBG
	bool "Debug"

endchoice

config $(module)_LOG_LEVEL
	int
	default 0 if $(module)_LOG_LEVEL_OFF
	default 1 if $(module)_LOG_LEVEL_ERR
	default 2 if $(module)_LOG_LEVEL_WRN
	default 3 if $(module)_LOG_LEVEL_INF
	default 4 if $(module)_LOG_LEVEL_DBG

Using the template:

module = FOO
module-str = foo
source "Kconfig.log_template"

...

module = BAR
module-str = bar
source "Kconfig.log_template"

This feature might create harder-to-read Kconfig files if abused, so it
should probably be used sparingly.

Fixes: #9761

Update Kconfiglib to upstream revision 2be02fac78527 to add support for
expanding macros within symbol names, extending the existing Kconfig
preprocessor
(https://github.com/torvalds/linux/blob/master/Documentation/kbuild/
kconfig-macro-language.txt). Some minor optimizations are included as
well.

This makes it possible to add Kconfig templates to avoid code
repetition, e.g. like below:

Kconfig.log_template:

    choice
    	prompt "Max compiled-in log level for $(module-str)"

    config $(module)_LOG_LEVEL_OFF
    	bool "Off"

    config $(module)_LOG_LEVEL_ERR
    	bool "Error"

    config $(module)_LOG_LEVEL_WRN
    	bool "Warning"

    config $(module)_LOG_LEVEL_INF
    	bool "Info"

    config $(module)_LOG_LEVEL_DBG
    	bool "Debug"

    endchoice

    config $(module)_LOG_LEVEL
    	int
    	default 0 if $(module)_LOG_LEVEL_OFF
    	default 1 if $(module)_LOG_LEVEL_ERR
    	default 2 if $(module)_LOG_LEVEL_WRN
    	default 3 if $(module)_LOG_LEVEL_INF
    	default 4 if $(module)_LOG_LEVEL_DBG

Using the template:

    module = FOO
    module-str = foo
    source "Kconfig.log_template"

    ...

    module = BAR
    module-str = bar
    source "Kconfig.log_template"

This feature might create harder-to-read Kconfig files if abused, so it
should probably be used sparingly.

Fixes: zephyrproject-rtos#9761

Signed-off-by: Ulf Magnusson <[email protected]>
@ulfalizer
Copy link
Collaborator Author

CC @nordic-krch @carlescufi

@codecov-io
Copy link

Codecov Report

Merging #9797 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9797      +/-   ##
==========================================
+ Coverage   52.23%   52.24%   +<.01%     
==========================================
  Files         212      212              
  Lines       25951    25951              
  Branches     5593     5593              
==========================================
+ Hits        13556    13557       +1     
  Misses      10159    10159              
+ Partials     2236     2235       -1
Impacted Files Coverage Δ
boards/posix/native_posix/timer_model.c 53.28% <0%> (+0.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 13a2103...885b192. Read the comment docs.

@nordic-krch
Copy link
Collaborator

I've tried it and works as expected. @nashif @carlescufi, could we get that in?

Copy link
Member

@carlescufi carlescufi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy with this. This is a very powerful feature and we can make good use of it.

@nashif nashif added this to the v1.14.0 milestone Sep 6, 2018
Copy link
Contributor

@mbolivar mbolivar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants