Skip to content

Commit 8e44be7

Browse files
committed
add add_embeddirs
1 parent e9fd678 commit 8e44be7

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

docs/api/description/builtin-rules.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,10 @@ cat build/.gens/test/macosx/x86_64/release/rules/c++/bin2c/image.png.h
612612
0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x78, 0x6D, 0x61, 0x6B, 0x65, 0x21, 0x0A, 0x00
613613
```
614614

615+
:::tip TIP
616+
If you are using a compiler that supports the C23 `#embed` feature (such as clang or gcc), you can also use the `#embed` directive directly to embed binary files. You need to set the C23 language standard first via `set_languages("c23")`, and then use [add_embeddirs](project-target.md#add_embeddirs) to set the search path. This approach is more aligned with the C23 standard and does not require generating additional header files.
617+
:::
618+
615619
## utils.glsl2spv
616620

617621
This rule can be used in v2.6.1 and above. Import glsl shader files such as `*.vert/*.frag` into the project, and then realize automatic compilation to generate `*.spv` files.

docs/api/description/project-target.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,6 +3271,70 @@ In the case of the msvc compiler, it will be:
32713271
In addition, the dependency package introduced with `add_requires()` will also use `-isystem` as the external system header file by default.
32723272
:::
32733273

3274+
## add_embeddirs
3275+
3276+
### Add #embed search directory
3277+
3278+
#### Function Prototype
3279+
3280+
::: tip API
3281+
```lua
3282+
add_embeddirs(embeddirs: <string|array>, ..., {
3283+
public|interface|private = <boolean>
3284+
})
3285+
```
3286+
:::
3287+
3288+
3289+
#### Parameter Description
3290+
3291+
| Parameter | Description |
3292+
|-----------|-------------|
3293+
| embeddirs | #embed search directory string or array, supports wildcard matching patterns |
3294+
| ... | Variable parameters, can pass multiple #embed search directory strings |
3295+
| public\|interface\|private | Visibility setting, see [Visibility Settings](#visibility) for details |
3296+
3297+
#### Usage
3298+
3299+
Set the search directory for the C23 `#embed` preprocessor directive, similar to how `add_includedirs` works. This interface is used to configure the directory paths that the compiler searches when looking for files referenced by `#embed` directives.
3300+
3301+
Both clang and gcc support the C23 `#embed` feature and provide the `--embed-dir=` argument to set the search path.
3302+
3303+
```lua
3304+
target("test")
3305+
set_kind("binary")
3306+
set_languages("c23")
3307+
add_embeddirs("./my_dir")
3308+
add_files("src/*.c")
3309+
```
3310+
3311+
You can also set different search paths based on platform or configuration conditions:
3312+
3313+
```lua
3314+
target("test")
3315+
set_kind("binary")
3316+
set_languages("c23")
3317+
add_embeddirs("./my_dir")
3318+
on_config("linux", function(target)
3319+
target:add("embeddirs", "./linux/embeds")
3320+
end)
3321+
add_files("src/*.c")
3322+
```
3323+
3324+
The generated compilation options are as follows:
3325+
3326+
```sh
3327+
--embed-dir=./my_dir --embed-dir=./linux/embeds
3328+
```
3329+
3330+
:::tip NOTE
3331+
Using the `#embed` feature requires setting the C23 language standard, which can be enabled via `set_languages("c23")`. If you don't want it to be fixed in the project, you can also set it through `cxflags`/`cxxflags`, but you need to provide different argument formats for different compilers. Using `add_embeddirs` can automatically adapt to different compilers.
3332+
:::
3333+
3334+
:::tip TIP
3335+
For embedding binary files using `#embed`, you can also refer to the [utils.bin2c](builtin-rules.md#utilsbin2c) rule, which provides another way to embed binary files into code.
3336+
:::
3337+
32743338
## add_defines
32753339

32763340
### Add macro definition

docs/zh/api/description/builtin-rules.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ cat build/.gens/test/macosx/x86_64/release/rules/c++/bin2c/image.png.h
601601
0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x78, 0x6D, 0x61, 0x6B, 0x65, 0x21, 0x0A, 0x00
602602
```
603603

604+
:::tip 提示
605+
如果你使用支持 C23 `#embed` 特性的编译器(如 clang 或 gcc),也可以直接使用 `#embed` 指令来嵌入二进制文件。需要先通过 `set_languages("c23")` 设置 C23 语言标准,然后使用 [add_embeddirs](project-target.md#add_embeddirs) 来设置搜索路径。这种方式更符合 C23 标准,无需生成额外的头文件。
606+
:::
607+
604608
## utils.glsl2spv
605609

606610
v2.6.1 以上版本可以使用此规则,在项目中引入 `*.vert/*.frag` 等 glsl shader 文件,然后实现自动编译生成 `*.spv` 文件。

docs/zh/api/description/project-target.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,6 +3335,70 @@ target("test")
33353335
另外,使用 `add_requires()` 引入的依赖包,默认也会使用 `-isystem` 作为外部系统头文件。
33363336
:::
33373337

3338+
## add_embeddirs
3339+
3340+
### 添加 #embed 搜索目录
3341+
3342+
#### 函数原型
3343+
3344+
::: tip API
3345+
```lua
3346+
add_embeddirs(embeddirs: <string|array>, ..., {
3347+
public|interface|private = <boolean>
3348+
})
3349+
```
3350+
:::
3351+
3352+
3353+
#### 参数说明
3354+
3355+
| 参数 | 描述 |
3356+
|------|------|
3357+
| embeddirs | #embed 搜索目录字符串或数组,支持通配符匹配模式 |
3358+
| ... | 可变参数,可传入多个 #embed 搜索目录字符串 |
3359+
| public\|interface\|private | 可见性设置,详见[可见性设置](#visibility) |
3360+
3361+
#### 用法说明
3362+
3363+
设置 C23 `#embed` 预处理器的搜索目录,类似于 `add_includedirs` 的工作方式。此接口用于配置编译器在查找 `#embed` 指令引用的文件时搜索的目录路径。
3364+
3365+
clang 和 gcc 都支持 C23 `#embed` 特性,并且都提供了 `--embed-dir=` 参数来设置搜索路径。
3366+
3367+
```lua
3368+
target("test")
3369+
set_kind("binary")
3370+
set_languages("c23")
3371+
add_embeddirs("./my_dir")
3372+
add_files("src/*.c")
3373+
```
3374+
3375+
也可以根据平台或配置条件设置不同的搜索路径:
3376+
3377+
```lua
3378+
target("test")
3379+
set_kind("binary")
3380+
set_languages("c23")
3381+
add_embeddirs("./my_dir")
3382+
on_config("linux", function(target)
3383+
target:add("embeddirs", "./linux/embeds")
3384+
end)
3385+
add_files("src/*.c")
3386+
```
3387+
3388+
生成的编译选项如下:
3389+
3390+
```sh
3391+
--embed-dir=./my_dir --embed-dir=./linux/embeds
3392+
```
3393+
3394+
:::tip 注意
3395+
使用 `#embed` 特性需要设置 C23 语言标准,通过 `set_languages("c23")` 来启用。如果不想在工程中写死,也可以通过 `cxflags`/`cxxflags` 来设置,但需要根据不同编译器提供不同的参数格式,使用 `add_embeddirs` 可以自动适配不同编译器。
3396+
:::
3397+
3398+
:::tip 提示
3399+
关于使用 `#embed` 嵌入二进制文件,也可以参考 [utils.bin2c](builtin-rules.md#utilsbin2c) 规则,它提供了另一种将二进制文件嵌入到代码中的方式。
3400+
:::
3401+
33383402
## add_defines
33393403

33403404
### 添加宏定义

0 commit comments

Comments
 (0)