Skip to content

Commit 7acd50d

Browse files
committed
improve docs
1 parent b7f49ca commit 7acd50d

File tree

2 files changed

+146
-24
lines changed

2 files changed

+146
-24
lines changed

docs/guide/package-management/distribute-private-libraries.md

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
---
2+
outline: deep
3+
---
4+
15
# Distribute Private Libraries {#distribute-private-libraries}
26

37
Xmake supports not only installing packages from the official repository but also creating and distributing private libraries. This is very useful for reusing private code libraries within a company.
48

59
We can package compiled static/dynamic libraries into local packages or remote packages for distribution, or install them directly to the system directory.
610

7-
## Prepare Library Project
11+
## Create Library Project {#prepare-library-project}
812

913
First, we can use the `xmake create` command to quickly create an empty static or dynamic library project.
1014

@@ -35,6 +39,26 @@ target("test")
3539
add_files("src/main.cpp")
3640
```
3741

42+
By default, `add_headerfiles` installs header files directly to the `include` directory.
43+
44+
If you want to install header files to a subdirectory (e.g. `include/foo/foo.h`) to avoid filename conflicts, you can set `prefixdir`:
45+
46+
```lua
47+
add_headerfiles("src/foo.h", {prefixdir = "foo"})
48+
```
49+
50+
For more details, please refer to: [add_headerfiles documentation](/api/description/project-target.html#add_headerfiles).
51+
52+
In addition to header files, we can also use [`add_installfiles`](/api/description/project-target.html#add_installfiles) to install any other files, such as documentation, scripts, resource files, etc.
53+
54+
```lua
55+
-- Install readme.md to share/doc/foo directory
56+
add_installfiles("readme.md", {prefixdir = "share/doc/foo"})
57+
58+
-- Install all files under assets
59+
add_installfiles("assets/**", {prefixdir = "share/foo/assets"})
60+
```
61+
3862
## Distribute as Local Package
3963

4064
If our library is only used within the local LAN or shared with others via a network drive, and does not need to be deployed to a remote git repository, we can use local package distribution.
@@ -94,7 +118,7 @@ Each package directory contains the generated binary library files (`.a`/`.lib`/
94118

95119

96120

97-
### Integration
121+
### Consume Local Package {#local-package-integration}
98122

99123
We copy the generated `build/packages` directory to any location, or use it directly. Then configure it in the consumer project's `xmake.lua`:
100124

@@ -144,7 +168,7 @@ The `build/packages` directory generated by `xmake package` mentioned above is a
144168

145169
When executing `xmake` to build, Xmake will link the corresponding binary libraries directly from the local repository.
146170

147-
## Distribute as Remote Package
171+
## Distribute Remote Package {#distribute-as-remote-package}
148172

149173
If we need to manage versions and distribute via a git repository, we can use the remote package mode. It supports distributing both source packages and binary packages.
150174

@@ -153,7 +177,7 @@ The advantages of remote packages are:
153177
- Supports source distribution (automatic compilation) and binary distribution (direct installation)
154178
- Support for multi-version switching
155179

156-
### Generate Package Configuration
180+
### Configure Remote Package {#generate-package-configuration}
157181

158182
We run `xmake package -f remote` to generate a configuration template for the remote package.
159183

@@ -219,7 +243,7 @@ my-repo/
219243
└── xmake.lua
220244
```
221245

222-
### Integration
246+
### Consume Remote Package {#remote-package-integration}
223247

224248
In the consumer project, we need to add this private repository.
225249

@@ -239,13 +263,13 @@ target("bar")
239263

240264
Xmake will automatically pull the package description from the `my-repo` repository, and then download the `foo` source code according to `add_urls` for compilation and installation.
241265

242-
## Distribute C++ Modules {#distribute-cxx-modules}
266+
## Distribute C++ Modules Package {#distribute-cxx-modules}
243267

244268
Xmake also supports distributing C++ Modules libraries. We only need to add `{install = true}` in `add_files` to package and distribute module files (`.mpp`, `.ixx`, etc.) together.
245269

246270
Usually, we need to define the package in an independent repository.
247271

248-
### Library Project
272+
### Configure Library Project
249273

250274
In the `xmake.lua` of the library project, we need to export the module files:
251275

@@ -288,7 +312,7 @@ target("bar")
288312

289313
For more complete examples, please refer to: [C++ Modules Package Distribution Example](https://github.com/xmake-io/xmake/tree/master/tests/projects/c%2B%2B/modules/packages).
290314

291-
## Repository Management {#repository-management}
315+
## Manage Package Repositories {#repository-management}
292316

293317
Whether it is a remote package or a local package, we can flexibly choose how to manage the package repository.
294318

@@ -385,7 +409,7 @@ Since we configured the `utils.install.pkgconfig_importfiles` and `utils.install
385409

386410
In this way, other non-xmake third-party projects (such as CMake projects) can also find and integrate it via `find_package(foo)`.
387411

388-
## Use in Other Build Systems {#use-in-other-build-systems}
412+
## Integrate with Other Build Systems {#use-in-other-build-systems}
389413

390414
If we are developing a library that needs to be used by other non-xmake projects, we can integrate it in the following ways.
391415

@@ -448,10 +472,47 @@ Xmake also supports using the [XPack](/guide/basic-commands/pack-programs.html#x
448472

449473
This is very useful for distributing binary SDKs or deploying to production environments.
450474

475+
### Supported Formats
476+
477+
* **Windows**: `nsis`, `wix`, `zip`, `targz`
478+
* **Linux**: `deb`, `rpm`, `srpm`, `runself` (shell self-extracting script), `targz`, `srczip`, `appimage`
479+
* **MacOS**: `dmg`, `zip`, `targz`, `runself`
480+
481+
### Configuration Example
482+
483+
We can add an `xpack` configuration block in `xmake.lua` to define packaging rules.
484+
485+
For example, to configure generating an NSIS installer:
486+
487+
```lua
488+
-- Include xpack plugin
489+
includes("@builtin/xpack")
490+
491+
target("foo")
492+
set_kind("shared")
493+
add_files("src/*.cpp")
494+
add_headerfiles("src/*.h")
495+
496+
xpack("foo")
497+
set_formats("nsis")
498+
set_title("Foo Library")
499+
set_description("The foo library package")
500+
set_author("ruki")
501+
set_version("1.0.0")
502+
503+
-- Add targets to package
504+
add_targets("foo")
505+
506+
-- Add other files
507+
add_installfiles("doc/*.md", {prefixdir = "share/doc/foo"})
508+
```
509+
510+
Then execute the packaging command:
511+
451512
```bash
452-
$ xmake pack -f nsis
513+
$ xmake pack
453514
```
454515

455-
The generated installation package can be installed by double-clicking, and it automatically configures environment variables such as PATH, making it convenient for users to use.
516+
It will automatically download the NSIS tool and generate the installation package. The generated installation package can be installed by double-clicking, and it automatically configures environment variables such as PATH, making it convenient for users to use.
456517

457518
For more details, please see the documentation: [XPack Packaging](/guide/extensions/builtin-plugins.html#xpack).

docs/zh/guide/package-management/distribute-private-libraries.md

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
---
2+
outline: deep
3+
---
4+
15
# 分发私有库 {#distribute-private-libraries}
26

37
Xmake 不仅支持从官方仓库安装包,也支持用户创建和分发私有库。这对于公司内部的私有代码库复用非常有用。
48

59
我们可以将编译好的静态库/动态库打包成[本地包](#distribute-as-local-package)或者[远程包](#distribute-as-remote-package)进行分发,也可以直接[安装到系统目录](#install-to-system)
610

7-
## 准备库工程 {#prepare-library-project}
11+
## 创建库工程 {#prepare-library-project}
812

913
首先,我们可以使用 [`xmake create`](/zh/guide/basic-commands/create-project.html) 命令快速创建一个空的静态库或者动态库工程。
1014

@@ -35,7 +39,27 @@ target("test")
3539
add_files("src/main.cpp")
3640
```
3741

38-
## 分发为本地包 {#distribute-as-local-package}
42+
默认情况下,`add_headerfiles` 会将头文件直接安装到 `include` 目录下。
43+
44+
如果想让头文件安装到子目录(例如 `include/foo/foo.h`),以避免文件名冲突,我们可以设置 `prefixdir`
45+
46+
```lua
47+
add_headerfiles("src/foo.h", {prefixdir = "foo"})
48+
```
49+
50+
更多详细用法,请参考:[add_headerfiles 接口文档](/zh/api/description/project-target.html#add_headerfiles)
51+
52+
除了头文件,我们还可以使用 [`add_installfiles`](/zh/api/description/project-target.html#add_installfiles) 安装其他任意文件,比如文档、脚本、资源文件等。
53+
54+
```lua
55+
-- 安装 readme.md 到 share/doc/foo 目录
56+
add_installfiles("readme.md", {prefixdir = "share/doc/foo"})
57+
58+
-- 安装 assets 下的所有文件
59+
add_installfiles("assets/**", {prefixdir = "share/foo/assets"})
60+
```
61+
62+
## 分发本地包 (Local Package) {#distribute-as-local-package}
3963

4064
如果我们的库仅仅在本地局域网,或者通过网盘共享给其他人使用,不需要部署到远程 git 仓库,可以使用本地包分发。
4165

@@ -44,7 +68,7 @@ target("test")
4468
- 编译好的二进制直接分发,集成速度快
4569
- 支持多平台、多架构、多编译模式(Debug/Release)的二进制包
4670

47-
### 打包 {#local-package-packaging}
71+
### 打包生成 {#local-package-packaging}
4872

4973
在库工程根目录下,执行 [`xmake package`](/zh/guide/basic-commands/pack-programs.html) 命令(或者完整命令 `xmake package -f local`)即可打包。
5074

@@ -92,7 +116,7 @@ build/packages/
92116

93117
每个包目录下都包含了生成的二进制库文件 (`.a`/`.lib`/`.so`/`.dll`) 和头文件。
94118

95-
### 集成使用 {#local-package-integration}
119+
### 使用本地包 {#local-package-integration}
96120

97121
我们将生成的 `build/packages` 目录复制到任意位置,或者直接使用它。然后在消费端的工程 `xmake.lua` 中配置:
98122

@@ -142,7 +166,7 @@ repository/
142166

143167
执行 `xmake` 编译时,Xmake 会直接从本地仓库中链接对应的二进制库。
144168

145-
## 分发为远程包 {#distribute-as-remote-package}
169+
## 分发远程包 (Remote Package) {#distribute-as-remote-package}
146170

147171
如果我们需要通过 git 仓库进行版本管理和分发,可以使用远程包模式。它既支持分发源码包,也支持分发二进制包。
148172

@@ -151,7 +175,7 @@ repository/
151175
- 支持源码分发(自动编译)和二进制分发(直接安装)
152176
- 支持多版本切换
153177

154-
### 生成包配置 {#generate-package-configuration}
178+
### 配置远程包 {#generate-package-configuration}
155179

156180
我们运行 [`xmake package -f remote`](/zh/guide/basic-commands/pack-programs.html#remote-package) 来生成远程包的配置模板。
157181

@@ -217,7 +241,7 @@ my-repo/
217241
└── xmake.lua
218242
```
219243

220-
### 集成使用 {#remote-package-integration}
244+
### 使用远程包 {#remote-package-integration}
221245

222246
在消费端的工程中,我们需要添加这个私有仓库。
223247

@@ -255,7 +279,7 @@ target("foo")
255279
add_files("src/*.mpp", {install = true})
256280
```
257281

258-
### 包仓库
282+
### 配置包仓库
259283

260284
在私有包仓库(例如 `my-repo`)中,添加包描述文件 `packages/f/foo/xmake.lua`
261285

@@ -268,7 +292,7 @@ package("foo")
268292
end)
269293
```
270294

271-
### 集成使用
295+
### 使用模块包
272296

273297
消费端集成时,只需要引入仓库,并开启 `build.c++.modules` 策略:
274298

@@ -286,7 +310,7 @@ target("bar")
286310

287311
更多完整示例,可以参考:[C++ Modules 包分发例子](https://github.com/xmake-io/xmake/tree/master/tests/projects/c%2B%2B/modules/packages)
288312

289-
## 仓库管理方式 {#repository-management}
313+
## 管理包仓库 {#repository-management}
290314

291315
不管是远程包还是本地包,我们都可以灵活选择包仓库的管理方式。
292316

@@ -383,7 +407,7 @@ $ xmake install -o /tmp/output
383407

384408
这样,其他非 xmake 的第三方项目(例如 CMake 项目)也可以通过 `find_package(foo)` 找到并集成它。
385409

386-
## 在其他构建系统中使用 {#use-in-other-build-systems}
410+
## 集成到第三方构建系统 {#use-in-other-build-systems}
387411

388412
如果我们开发的是一个库,最终需要给其他非 xmake 项目使用,我们可以通过以下几种方式进行集成。
389413

@@ -446,10 +470,47 @@ Xmake 还支持使用 [XPack](/zh/guide/basic-commands/pack-programs.html#xpack)
446470

447471
这对于分发二进制 SDK 或者部署到生产环境非常有用。
448472

473+
### 支持格式
474+
475+
* **Windows**: `nsis`, `wix`, `zip`, `targz`
476+
* **Linux**: `deb`, `rpm`, `srpm`, `runself` (shell 自解压脚本), `targz`, `srczip`, `appimage`
477+
* **MacOS**: `dmg`, `zip`, `targz`, `runself`
478+
479+
### 配置示例
480+
481+
我们可以在 `xmake.lua` 中添加 `xpack` 配置域来定义打包规则。
482+
483+
例如,配置生成一个 NSIS 安装包:
484+
485+
```lua
486+
-- 引入 xpack 插件
487+
includes("@builtin/xpack")
488+
489+
target("foo")
490+
set_kind("shared")
491+
add_files("src/*.cpp")
492+
add_headerfiles("src/*.h")
493+
494+
xpack("foo")
495+
set_formats("nsis")
496+
set_title("Foo Library")
497+
set_description("The foo library package")
498+
set_author("ruki")
499+
set_version("1.0.0")
500+
501+
-- 添加需要打包的目标
502+
add_targets("foo")
503+
504+
-- 添加其他文件
505+
add_installfiles("doc/*.md", {prefixdir = "share/doc/foo"})
506+
```
507+
508+
然后执行打包命令:
509+
449510
```bash
450-
$ xmake pack -f nsis
511+
$ xmake pack
451512
```
452513

453-
生成的安装包可以双击安装,自动配置 PATH 等环境变量,方便用户使用。
514+
它会自动下载 NSIS 工具并生成安装包。生成的安装包可以双击安装,自动配置 PATH 等环境变量,方便用户使用。
454515

455516
更多详情,请查看文档:[XPack 打包](/zh/guide/extensions/builtin-plugins.html#xpack)

0 commit comments

Comments
 (0)