Skip to content

Commit 43a0bc3

Browse files
committed
manage version through xmake, see more at DavidingPlus/nvmixfs#14
1 parent 4dd5896 commit 43a0bc3

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/config.h.in

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @file config.h.in
3+
* @author DavidingPlus (davidingplus@qq.com)
4+
* @brief 生成配置文件 config.h 的模板文件。请勿手动修改或格式化!!!
5+
* @details 配置文件 config.h 通过 XMake 构建系统动态生成,对本文件进行任何修改或手动格式化操作都可能导致:
6+
* - ${VERSION} 等占位符失效。
7+
* - 版本号宏定义结构破坏。
8+
*
9+
* Copyright (c) 2025 电子科技大学 刘治学
10+
*
11+
*/
12+
13+
#ifndef _LINUX_KERNEL_MODULE_CONFIG_H_
14+
#define _LINUX_KERNEL_MODULE_CONFIG_H_
15+
16+
17+
/**
18+
* @brief 完整版本号(X.Y.Z)。
19+
*/
20+
#define LINUX_KERNEL_MODULE_CONFIG_VERSION "${VERSION}"
21+
22+
/**
23+
* @brief 主版本号。
24+
*/
25+
#define LINUX_KERNEL_MODULE_CONFIG_VERSION_MAJOR ${VERSION_MAJOR}
26+
27+
/**
28+
* @brief 次版本号。
29+
*/
30+
#define LINUX_KERNEL_MODULE_CONFIG_VERSION_MINOR ${VERSION_MINOR}
31+
32+
/**
33+
* @brief 修订版本号。
34+
* @details patch 和 alter 均可。Linux 内核的习惯是 patch,这里顺从 XMake 内置变量命名规则,即 alter。
35+
*/
36+
#define LINUX_KERNEL_MODULE_CONFIG_VERSION_ALTER ${VERSION_ALTER}
37+
38+
39+
#endif

src/main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
/**
2+
* @file main.c
3+
* @author DavidingPlus (davidingplus@qq.com)
4+
* @brief 内核模块主程序入口文件。
5+
*
6+
* Copyright (c) 2025 电子科技大学 刘治学
7+
*
8+
*/
9+
110
#include <linux/init.h>
211
#include <linux/module.h>
312

13+
#include "config.h"
14+
415

5-
MODULE_VERSION("1.0.0");
16+
MODULE_VERSION(LINUX_KERNEL_MODULE_CONFIG_VERSION);
617
MODULE_LICENSE("Dual BSD/GPL");
718
MODULE_AUTHOR("DavidingPlus");
819
MODULE_DESCRIPTION("A Linux Kernel Module");

test/version-test.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <gtest/gtest.h>
2+
#include <string>
3+
4+
#include "config.h"
5+
6+
7+
TEST(VersionTest, Test1)
8+
{
9+
std::cout << LINUX_KERNEL_MODULE_CONFIG_VERSION << std::endl;
10+
11+
std::cout << LINUX_KERNEL_MODULE_CONFIG_VERSION_MAJOR << std::endl;
12+
13+
std::cout << LINUX_KERNEL_MODULE_CONFIG_VERSION_MINOR << std::endl;
14+
15+
std::cout << LINUX_KERNEL_MODULE_CONFIG_VERSION_ALTER << std::endl;
16+
}
17+
18+
TEST(VersionTest, Test2)
19+
{
20+
unsigned char major = '0' + LINUX_KERNEL_MODULE_CONFIG_VERSION_MAJOR;
21+
unsigned char minor = '0' + LINUX_KERNEL_MODULE_CONFIG_VERSION_MINOR;
22+
unsigned char alter = '0' + LINUX_KERNEL_MODULE_CONFIG_VERSION_ALTER;
23+
24+
std::string version;
25+
26+
version += major;
27+
version += '.';
28+
version += minor;
29+
version += '.';
30+
version += alter;
31+
32+
33+
EXPECT_EQ(LINUX_KERNEL_MODULE_CONFIG_VERSION, version);
34+
}

xmake.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
set_project ("linux-kernel-module")
2+
set_version ("1.1.0")
3+
4+
15
option ("linux-headers", {showmenu = true, description = "Set linux-headers path."})
26
option ("with-gtest", {showmenu = true, description = "Whether to enable unit test by GTest.", default = false})
37

48

9+
set_configdir ("$(buildir)/config")
10+
add_configfiles ("src/config.h.in")
11+
12+
add_includedirs ("$(buildir)/config/")
513
add_includedirs ("src/")
614

15+
716
target ("linux-kernel-module")
817
add_rules ("platform.linux.module")
918
add_files ("src/*.c")

0 commit comments

Comments
 (0)