config.c

可以看到这是一个module,在init函数中在proc下建立一个config的node,其提供一个read函数可以读取kernel config中的配置
http://lxr.free-electrons.com/source/kernel/configs.c
 72 static int __init ikconfig_init(void)
 73 {
 74         struct proc_dir_entry *entry;
 75 
 76         /* create the current config file */
 77         entry = proc_create("config.gz", S_IFREG | S_IRUGO, NULL,
 78                             &ikconfig_file_ops);
 79         if (!entry)
 80                 return -ENOMEM;
 81 
 82         proc_set_size(entry, kernel_config_data_size);
 83 
 84         return 0;
 85 }
 86 
 92 module_init(ikconfig_init);


我们看看ikconfig_file_ops 的实现
 66 static const struct file_operations ikconfig_file_ops = {
 67         .owner = THIS_MODULE,
 68         .read = ikconfig_read_current,
 69         .llseek = default_llseek,
 70 };
继续看ikconfig_read_current
 57 static ssize_t
 58 ikconfig_read_current(struct file *file, char __user *buf,
 59                       size_t len, loff_t * offset)
 60 {
 61         return simple_read_from_buffer(buf, len, offset,
 62                                        kernel_config_data + MAGIC_SIZE,
 63                                        kernel_config_data_size);
 64 }
发现原来kernel config都是存在kernel_config_data 数组中,那这个数组又是在哪里赋值的呢?
答案就是在config_data.h 中定义的
在同级的目录下,可以看到config_data.gz是有KCONFIG_CONFIG 经过gzip 压缩的,而KCONFIG_CONFIG 就是kernel的配置文件.


从126行可以看到config_data.h 就是124行动态生成的。


http://lxr.free-electrons.com/source/kernel/Makefile
116 $(obj)/configs.o: $(obj)/config_data.h
117 
118 # config_data.h contains the same information as ikconfig.h but gzipped.
119 # Info from config_data can be extracted from /proc/config*
120 targets += config_data.gz
121 $(obj)/config_data.gz: $(KCONFIG_CONFIG) FORCE
122         $(call if_changed,gzip)
123 
124       filechk_ikconfiggz = (echo "static const char kernel_config_data[] __used = MAGIC_START"; cat $< | scripts/basic/bin2c; echo "MAGIC_END;")
125 targets += config_data.h
126 $(obj)/config_data.h: $(obj)/config_data.gz FORCE
127         $(call filechk,ikconfiggz)


所以就可以通过cat proc/config.gz  XXX 来查是否有enable 某个config。


要使能这个module的话,需要CONFIG_IKCONFIG_PROC 这个宏
<think>好的,用户现在询问的是关于eslintrc.config.cjs的配置。我需要先回顾之前的对话历史,看看之前讨论过什么内容。之前用户问了新旧版本ESLint配置的差异,我详细介绍了核心变化、配置步骤、对比示例、注意事项和迁移建议。现在用户的问题更具体,指向了.cjs扩展名的配置文件。 首先,我需要确定用户为什么会提到.cjs。通常,ESLint的配置文件可以是.js、.cjs、.json、.yaml等格式。当项目使用ES模块(即package.json中设置了"type": "module")时,ESLint默认会寻找.mjs或.js(作为ES模块)的配置文件。但在这种情况下,如果用户想使用CommonJS语法,就需要将配置文件命名为.cjs,这样Node.js会将其识别为CommonJS模块。 用户可能遇到了在ES模块项目中配置ESLint的问题,或者他们看到别人使用.cjs扩展名,想要了解其用途和如何配置。我需要解释.cjs的作用,与普通.js文件的区别,以及使用场景。 接下来,我需要详细说明如何创建和配置eslintrc.config.cjs。应该包括基本结构、环境配置、解析器选项、规则设置,以及注意事项,比如CommonJS的语法要求,比如必须使用module.exports而不是export default。 另外,用户可能需要知道在什么情况下应该使用.cjs而不是其他格式。例如,当项目使用ES模块但需要兼容旧的CommonJS配置时,或者在ESLint的配置中需要动态导入某些模块,而CommonJS的require更适合时。 还要注意可能的常见错误,例如在.cjs文件中错误使用ES模块语法,这会导致Node.js抛出错误。需要提醒用户检查文件扩展名和语法是否正确对应。 最后,可能需要给出一个完整的配置示例,帮助用户直观理解如何编写自己的eslintrc.config.cjs文件,并说明如何验证配置是否正确生效,比如使用eslint命令行工具进行检查。 总结,用户的需求可能是在特定模块系统下正确配置ESLint,特别是当项目中使用ES模块时,如何通过.cjs文件来保持CommonJS的配置方式。需要明确两者的区别,使用场景,以及具体的配置方法,避免用户混淆和配置错误。</think>以下是关于 `.eslintrc.config.cjs` 的详细说明: ### 一、文件作用 1. **用途**: `.eslintrc.config.cjs` 是 ESLint 的配置文件,用于替代传统的 `.eslintrc.js` 或 `.eslintrc.json`,专门用于 **CommonJS 模块系统** 的项目(如 Node.js 项目或需要兼容 CommonJS 的浏览器环境)。 2. **命名规范**: - 扩展名 `.cjs` 是 Node.js 对 CommonJS 模块的显式声明 - 与 `.mjs`(ES 模块)形成对应关系 --- ### 二、核心配置结构 #### 基础模板 ```javascript // eslintrc.config.cjs module.exports = { env: { browser: true, // 浏览器环境 node: true, // Node.js 环境 es2022: true // ES2022 语法支持 }, extends: [ 'eslint:recommended', // ESLint 推荐规则 'plugin:vue/vue3-recommended' // Vue 3 插件规则 ], parser: '@babel/eslint-parser', // Babel 解析器 parserOptions: { ecmaVersion: 2022, // ECMAScript 版本 sourceType: 'script', // 模块类型(script/module) requireConfigFile: false // 禁用 Babel 配置文件 }, rules: { 'no-unused-vars': 'warn', // 未使用变量警告 'semi': ['error', 'always'] // 必须分号 }, overrides: [{ files: ['*.test.cjs'], // 针对测试文件 env: { jest: true } // 启用 Jest 环境 }] }; ``` --- ### 三、关键配置项解析 #### 1. 模块系统声明 ```javascript // 必须显式声明 CommonJS parserOptions: { sourceType: 'script' // 默认值,对应 CommonJS } ``` > **注意**:若设置为 `'module'` 需配合 `.mjs` 扩展名使用 #### 2. 与 `.eslintrc.js` 的区别 | 特性 | `.eslintrc.js` | `.eslintrc.config.cjs` | |--------------------|-----------------------|------------------------| | 模块系统 | 根据项目类型自动判断 | 强制 CommonJS | | 文件扩展名要求 | `.js` | `.cjs` | | 导出语法 | `export default` | `module.exports` | | 适用场景 | 混合模块项目 | 纯 CommonJS 项目 | --- ### 四、典型使用场景 #### 场景 1:Node.js 项目 ```javascript module.exports = { env: { node: true }, extends: ['plugin:node/recommended'], rules: { 'node/no-unpublished-require': 'off' } }; ``` #### 场景 2:Vue 3 + CommonJS ```javascript module.exports = { extends: ['plugin:vue/vue3-essential'], rules: { 'vue/multi-word-component-names': 'off' } }; ``` --- ### 五、配置验证方法 1. 命令行验证: ```bash npx eslint --print-config index.js > effective-config.json ``` 2. 运行时检查: ```javascript console.log(require('./.eslintrc.config.cjs')); ``` --- ### 六、注意事项 1. **语法强制要求**: - 必须使用 `module.exports` 导出配置 - 禁止使用 `import/export` 语法 2. **版本兼容性**: - ESLint v7.0+ 完整支持 `.cjs` 配置 - 旧版本需通过 `--config` 参数指定文件: ```bash eslint --config .eslintrc.config.cjs src/ ``` 3. **与 `package.json` 的关联**: 若 `package.json` 中包含 `"type": "module"`,必须使用 `.cjs` 扩展名明确声明 CommonJS 配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值