首页 > 其他分享 >Using kconfig for own projects

Using kconfig for own projects

时间:2023-06-06 20:01:04浏览次数:60  
标签:own KConfig demo DESTINATION SELECT file Using config projects

2023-06-06    https://www.cnblogs.com/NJ-Leon/

 

Intro

Every Linux professional write scripts. Someеimes light, linear. Sometimes complex script with functions and libs(yes, you can write your bash-library for use in other scripts).

But some of the scripts need a configuration file to work. For instance, I wrote a script that builds the ubuntu image for pxe, and I need to change the build process without build-script changes. The best way to resolve this task is to add configuration files.

Writing this config by hands is not a great idea:

  • every user should know all available options
  • hight risk of incorrect data entry
  • uncomfortable
  • maybe something else

Linux kernel gave us a great solution — KConfig. This system resolves all these troubles:

  • New config generated automatically
  • You can set the default value
  • The previous version of config saved automatically
  • text/graphical interface available

These are not all the benefits. You can see more here.

How it works

Simplified schema:

  • All configuration parameters described in KConfig-format file (including available ones)
  • Interpreter (mconf, gconf, qconf, others) reads this file, current config (if it exists), and shows the menu
  • The user selects needed options and selects the SAVE option
  • KConfig moves old config to .config.old and creates new config (default name .config)

After that, you get a ready-made config that can be used in scripts(or for project build).

Example project

Now, I'll show you the demo-project. I placed it on github.

overview

It's a simple script that shows the information about the last logins. Available options:

  • Select destination (file or console)
  • If the selected destination is a file, the user can set the filename
  • Shows information about both all and only current users

Just clone this repo and go to its directory:

boozlachu@debian:~$ git clone https://github.com/skif-web/demo-kbuild.git
Cloning into 'demo-kbuild'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 8 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
boozlachu@debian:~$ cd demo-kbuild/
boozlachu@debian:~/demo-kbuild$ ls
getLast.sh  KConfig  README.md

KConfig interpreter notes

KConfig is just a language. You have to use the interpreter to show the menu. In the Linux kernel, the interpreter is built when you run the "make menuconfig" command. But I'm a lazy potato and will use the kconfig-frontends package. It is available in Debian/Ubuntu, and you can also build it from the source. Or add an interpreter build from source to your project.

KConfig file

The main file here is KConfig. KConfig supports a lot of operators, but I'll use only some of them.

Various types of variables are available there. The most useful are:

  • string to save string data
  • bool to save the bool value (y or commented string in config)
  • tristate (*/m/commented)
boozlachu@debian:~/demo-kbuild$ cat KConfig 
choice SELECT_DESTINATION
    prompt "Select data destination"
    default DESTINATION_TTY

config SELECT_DESTINATION_TTY
    bool "show data in console"
config SELECT_DESTINATION_FILE
    bool "save data to file"
endchoice

config SELECT_DESTINATION_FILE_FILENAME
    string "destination file"
    depends on SELECT_DESTINATION_FILE
    default "last.log"
    help
      Write destination file with relative or full path

config SHOW_ONLY_CURRENT_USER
    bool "show data only for current user"
    default y
    help
      script will get data only for current user

Let's see it. Choice-endchoice pair allows creating SELECT of variants. Inside these keywords, I set definition (prompt), default value (default), and added available variants (SELECT_DESTINATION_TTY, SELECT_DESTINATION_FILE).

The next option (SELECT_DESTINATION_FILE_FILENAME) will only be shown if the destination is the file. The keyword "depends on" allows us to do this. Also, the available keyword "selects" that works in the opposite direction.

Option SHOW_ONLY_CURRENT_USER will always be shown.

Run kconfig in text mode:

boozlachu@debian:~/demo-kbuild$ kconfig-mconf KConfig

 And with qt:

boozlachu@debian:~/demo-kbuild$ kconfig-qconf KConfig

Test run

After configuring it, we get .config file with options:

boozlachu@debian:~/demo-kbuild$ cat .config 
#
# Automatically generated file; DO NOT EDIT.
# Configuration
#
CONFIG_SELECT_DESTINATION_TTY=y
# CONFIG_SELECT_DESTINATION_FILE is not set
CONFIG_SHOW_ONLY_CURRENT_USER=y

As you can see, non-selected options are commented on. It's useful for my script.

I run it with this config:

boozlachu@debian:~/demo-kbuild$ ./getLast.sh 
boozlach pts/0        192.168.2.4      Sun Jul 19 06:00   still logged in
boozlach pts/0        192.168.2.4      Sun Jul 19 05:58 - 05:58  (00:00)

wtmp begins Thu Apr 16 05:48:31 2020

I changed the parameters via KConfig and got another result. Now the date was saved to a file, instead of being shown in the console:

boozlachu@debian:~/demo-kbuild$ cat .config
#
# Automatically generated file; DO NOT EDIT.
# Configuration
#
# CONFIG_SELECT_DESTINATION_TTY is not set
CONFIG_SELECT_DESTINATION_FILE=y
CONFIG_SELECT_DESTINATION_FILE_FILENAME="last.log"
CONFIG_SHOW_ONLY_CURRENT_USER=y
boozlachu@debian:~/demo-kbuild$ ./getLast.sh 
boozlachu@debian:~/demo-kbuild$ cat last.log 
boozlach pts/0        192.168.2.4      Sun Jul 19 06:00   still logged in
boozlach pts/0        192.168.2.4      Sun Jul 19 05:58 - 05:58  (00:00)

Conclusion

As you can see, KConfig is very powerful and friendly util for configuring. It can be used for scripts, package building, and other variants.

If you want to see an improved variant of KConfig using a makefile in the real project — write in comments.

Originally published at https://habr.com.    

标签:own,KConfig,demo,DESTINATION,SELECT,file,Using,config,projects
From: https://www.cnblogs.com/NJ-Leon/p/17461529.html

相关文章

  • Markdown学习
    Markdown学习标题+空格一级标题+空格二级标题+空格三级标题...注:最多有六级标题字体粗体斜体粗体+斜体删除线引用引用就是前面加>分割线三个-三个*图片![]![截图](C:\Users\那片海\Pictures\CameraRoll\微信图片_20220624223010.jpg)超链接点击跳转......
  • P3087 [USACO13NOV]Farmer John has no Large Brown Cow S
    正解像是康托展开之类的?但是蒟蒻不会,所以用了一堆STL。对于每一列的字符串,按照字典序给它们编号。这样每一行的形容词串就变成了一堆数字。设共有\(s\)列,第\(i\)列共有\(b_i\)个不同的形容词,那么实际上每一行就是一个“第\(i\)位是\(b_i\)进制”的数。设第\(j\)行......
  • Markdown学习 typora
    Markdown学习#标题##三级标题###四级标题#### 字体Hello,World!Hello,World**Hello,World~引用 选择狂绅说java,走向人生巅> 分割线--- 图片![文本](图片路径)  超链接文本点击跳转狂绅博客 列表-ABC ABC 表格|..|........
  • Hibernate处理Oracle的分页,是用rownum。
    代码如下:Java代码publicStringgetLimitString(Stringsql,booleanhasOffset);{StringBufferpagingSelect=newStringBuffer(sql.length();+100);;if(hasOffset);{pagingSelect.append("select*from(selectrow_.*,rown......
  • markdown使用技巧 - 处理表格
    怎样制作方便的合并表格?1.Googlesheet中制作表格合并的单元格在Excel/WPS直接复制过去即可,或者手动选择多个单元格后按这个按钮。文件-导出为html3.Emeditor正则处理/* 用于谷歌表格合并单元格后导出到vscodemd预览用 html只保留rowspancolspan其他额外属......
  • VSCode 中 Markdown 设置
       1.导出html带侧边栏  2.支持markdown 3.支持导出PDF,HTML ......
  • vscode中markdown文件中怎么直接粘贴复制的图片?
    在vscode中显示图片用如下命令:![图片描述](图片URL)但是需要先把图片保存成文件,不太方便.安装vscode的PasteImage插件即可.这样,先截屏,然后按ctrl+alt+v键,就可以自动插入图片了,形如下面的样式:![](2023-06-02-17-41-00.png)另外,再说一个截屏的小技巧,如果是......
  • Markdown语法学习
    Markdown学习二级标题字体HelloWorldhelloworld210=1024helloworldH2Ois是液体。helloworldhelloworld引用每一个不曾起舞的日子,都是对生命的辜负分割线图片本地图片网络图片超链接点击跳转到csdn列表ABCDABC计划1计划2......
  • Using Spring for Apache Kafka
    UsingSpringforApacheKafkaSendingMessagesKafkaTemplateThe KafkaTemplate wrapsaproducerandprovidesconveniencemethodstosenddatatokafkatopics.Bothasynchronousandsynchronousmethodsareprovided,withtheasyncmethodsreturninga ......
  • Markdown的学习
    Markdown学习标题使用#号,最好再后面再加一个空格,几级标题相对应几个#号三级标题四级标题字体使内容加粗可在内容两边各添加**使内容倾斜可在内容两边各添加*同理,想要让内容倾斜并加粗两边同时添加即可***使内容中间添加一条横线,在左右两边添加~~引用在内容......