built-in variables

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

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:

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:

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 interface.

This way of using built-in variables makes the description writing more concise and easy to read.

It is possible to add custom, project-specific, variables. By default, the xmake f --var=val command can be used to directly obtain the parameters. For example:

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

ℹ️ 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.

Since the support is directly obtained from the configuration options, it is convenient to extend the custom options to get the custom variables. For details on how to customize the options, see: option.

edit

$(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.

Introduced in version 2.0.1

See also

config.buildir

edit

$(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

Introduced in version 2.0.1

See also

os.curdir

edit

$(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, this value will also change.

Introduced in version 2.0.1

See also

os.curdir

edit

$(env)

Get external environment variables

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

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

Introduced in version 2.1.5

See also

os.getenvs

edit

$(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

Introduced in version 2.0.1

edit

$(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

Introduced in version 2.0.1

See also

is_host

edit

$(os)

Get the operating system of the current build platform

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

Introduced in version 2.0.1

See also

is_os

edit

$(programdir)

Get 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.

Introduced in version 2.1.5

See also

os.programdir

edit

$(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.

Introduced in version 2.0.1

See also

os.projectdir

edit

$(reg)

Get the value of the windows registry configuration item

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

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

Introduced in version 2.1.5

See also

winos.registry_query

edit

$(scriptdir)

Get the directory of the current project description script

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

Introduced in version 2.1.1

See also

os.scriptdir

edit

$(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:

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

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.

Introduced in version 2.0.1

See also

os.shell

edit

$(tmpdir)

Getting a temporary directory

Generally used to temporarily store some non-permanent files.

Introduced in version 2.0.1

See also

os.tmpdir

edit
Interfaces
$(buildir)
$(configdir)
$(curdir)
$(env)
$(globaldir)
$(host)
$(os)
$(programdir)
$(projectdir)
$(reg)
$(scriptdir)
$(shell)
$(tmpdir)