aboutsummaryrefslogtreecommitdiff
path: root/manual/builtin_variables.md
blob: 677f8227a4d54a1d4d424aee4897a02d976f541f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

Xmake provides the syntax of `$(varname)` to support the acquisition of built-in variables, for example:

```lua
add_cxflags("-I$(buildir)")
```

It will convert the built-in `buildir` variable to the actual build output directory when compiling: `-I./build`

General built-in variables can be used to quickly get and splicing variable strings when passing arguments, for example:

```lua
target("test")

    -- Add source files in the project source directory
    add_files("$(projectdir)/src/*.c")

    -- Add a header file search path under the build directory
    add_includedirs("$(buildir)/inc")
```

It can also be used in the module interface of a custom script, for example:

```lua
target("test")
    on_run(function (target)
        -- Copy the header file in the current script directory to the output directory
        os.cp("$(scriptdir)/xxx.h", "$(buildir)/inc")
    end)
```

All built-in variables can also be retrieved via the [val](#val) interface.

This way of using built-in variables makes the description writing more concise and easy to read. Here are some of the variables built into xmake that can be obtained directly:

| Interface | Description | Supported Versions |
| ----------------------------------------------- | -------------------------------------------- | -------- |
| [$(os)](#var-os) | Get the operating system of the current build platform | >= 2.0.1 |
| [$(host)](#var-host) | Get native operating system | >= 2.0.1 |
| [$(tmpdir)](#var-tmpdir) | Get Temporary Directory | >= 2.0.1 |
| [$(curdir)](#var-curdir) | Get current directory | >= 2.0.1 |
| [$(buildir)](#var-buildir) | Get the build output directory | >= 2.0.1 |
| [$(scriptdir)](#var-scriptdir) | Get Project Description Script Directory | >= 2.1.1 |
| [$(globaldir)](#var-globaldir) | Get Global Configuration Directory | >= 2.0.1 |
| [$(configdir)](#var-configdir) | Get Local Project Configuration Directory | >= 2.0.1 |
| [$(programdir)](#var-programdir) | xmake installation script directory | >= 2.1.5 |
| [$(projectdir)](#var-projectdir) | Get the project root directory | >= 2.0.1 |
| [$(shell)](#var-sheLl) | Execute external shell command | >= 2.0.1 |
| [$(env)](#var-env) | Get external environment variables | >= 2.1.5 |
| [$(reg)](#var-reg) | Get the value of the windows registry configuration item | >= 2.1.5 |

Of course, this variable mode can also be extended. By default, the `xmake f --var=val` command can be used to directly obtain the parameters. For example:

```lua
target("test")
    add_defines("-DTEST=$(var)")
```

<p class="tip">
All the parameter values ​​of the `xmake f --xxx=...` configuration can be obtained through built-in variables, for example: `xmake f --arch=x86` corresponds to `$(arch)`, others have ` $(plat)`, `$(mode)` and so on.
What are the specific parameters, you can check it out by `xmake f -h`.
</p>

Since the support is directly obtained from the configuration options, it is of course convenient to extend the custom options to get the custom variables. For details on how to customize the options, see: [option](#option)

### var.$(os)

#### Get the operating system of the current build platform

If iphoneos is currently compiled, then this value is: `ios`, and so on.

### var.$(host)

#### Get the native operating system

Refers to the host system of the current native environment, if you compile on macOS, then the system is: `macosx`

### var.$(tmpdir)

#### Getting a temporary directory

Generally used to temporarily store some non-permanent files.

### var.$(curdir)

#### Get the current directory

The default is the project root directory when the `xmake` command is executed. Of course, if the directory is changed by [os.cd](#os-cd), this value will also change.

### var.$(buildir)

#### Get the current build output directory

The default is usually the `./build` directory in the current project root directory. You can also modify the default output directory by executing the `xmake f -o /tmp/build` command.

### var.$(scriptdir)

#### Get the directory of the current project description script

That is, the directory path corresponding to `xmake.lua`.

### var.$(globaldir)

#### Global Configuration Directory

Xmake's `xmake g|global` global configuration command, directory path for data storage, where you can place some of your own plugins and platform scripts.

The default is: `~/.config`

### var.$(configdir)

#### Current project configuration directory

The current project configuration storage directory, which is the storage directory of the `xmake f|config` configuration command, defaults to: `projectdir/.config`

### var.$(programdir)

#### xmake installation script directory

That is, the directory where the `XMAKE_PROGRAM_DIR` environment variable is located. We can also modify the xmake load script by setting this environment amount to implement version switching.

### var.$(projectdir)

#### Project root directory

That is, the directory path specified in the `xmake -P xxx` command, the default is not specified is the current directory when the `xmake` command is executed, which is generally used to locate the project file.

### var.$(shell)

#### Executing external shell commands

In addition to the built-in variable handling, xmake also supports the native shell to handle some of the features that xmake does not support.

For example, there is a need now, I want to use the `pkg-config` to get the actual third-party link library name when compiling the Linux program, you can do this:

```lua
target("test")
    set_kind("binary")
    if is_plat("linux") then
        add_ldflags("$(shell pkg-config --libs sqlite3)")
    end
```

Of course, xmake has its own automated third library detection mechanism, which generally does not need such trouble, and lua's own scripting is very good. .

But this example shows that xmake can be used with some third-party tools through the native shell. .

### var.$(env)

#### Get external environment variables

For example, you can get the path in the environment variable:

```lua
target("test")
    add_includedirs("$(env PROGRAMFILES)/OpenSSL/inc")
```

### var.$(reg)

#### Get the value of the windows registry configuration item

Get the value of an item in the registry by `regpath; name`:

```lua
print("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXXX;Name)")
```