---
url: /api/scripts/extension-modules/core/tool/compiler.md
---
# core.tool.compiler

Compiler related operations, often used for plugin development.

## compiler.compile

* Perform compilation

#### Function Prototype

::: tip API

```lua
compiler.compile(sourcefile: <string>, objectfile: <string>, depfile: <string>, opt: <table>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| sourcefile | Required. Source file path |
| objectfile | Required. Target file path |
| depfile | Optional. Dependency file path |
| opt | Optional. Option parameters, supports `target` |

#### Return Value

| Type | Description |
|------|-------------|
| boolean | Returns true on success, false on failure |

#### Usage

For the target, link the specified object file list to generate the corresponding target file, for example:

```lua
compiler.compile("xxx.c", "xxx.o", "xxx.h.d", {target = target})
```

Where [target](/api/description/project-target) is the project target, here is the specific compile option that is mainly used to get the target.
For the project target object, see: [core.project.project](/api/scripts/extension-modules/core/project/project)

The `xxx.h.d` file is used to store the header file dependency file list for this source file. Finally, these two parameters are optional.
You can not pass them when compiling:

```lua
compiler.compile("xxx.c", "xxx.o")
```

To simply compile a source file.

## compiler.compcmd

* Get the compile command line

#### Function Prototype

::: tip API

```lua
compiler.compcmd(sourcefile: <string>, objectfile: <string>, opt: <table>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| sourcefile | Required. Source file path |
| objectfile | Required. Target file path |
| opt | Optional. Option parameters, supports `target` and `configs` |

#### Return Value

| Type | Description |
|------|-------------|
| string | Returns compile command string |

#### Usage

Get the command line string executed directly in [compiler.compile](#compiler-compile), which is equivalent to:

```lua
local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {target = target})
```

Note: The extension part of \`\`target = target}\` is optional. If the target object is passed, the generated compile command will add the link option corresponding to this target configuration.

And you can also pass various configurations yourself, for example:

```lua
local cmdstr = compiler.compcmd("xxx.c", "xxx.o", {configs = {includedirs = "/usr/include", defines = "DEBUG"}})
```

With target, we can export all source file compilation commands for the specified target:

```lua
import("core.project.project")

for _, target in pairs(project.targets()) do
    for sourcekind, sourcebatch in pairs(target:sourcebatches()) do
        for index, objectfile in ipairs(sourcebatch.objectfiles) do
            local cmdstr = compiler.compcmd(sourcebatch.sourcefiles[index], objectfile, {target = target})
        end
    end
end
```

## compiler.compargv

* Get compiled command line list

#### Function Prototype

::: tip API

```lua
compiler.compargv(sourcefile: <string>, objectfile: <string>, opt: <table>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| sourcefile | Required. Source file path |
| objectfile | Required. Target file path |
| opt | Optional. Option parameters |

#### Return Values

| Type | Description |
|------|-------------|
| string | Compiler program path |
| table | Compile arguments list |

#### Usage

A little different from [compiler.compcmd](#compiler-compcmd) is that this interface returns a list of parameters, table representation, more convenient to operate:

```lua
local program, argv = compiler.compargv("xxx.c", "xxx.o")
```

## compiler.compflags

* Get compilation options

#### Function Prototype

::: tip API

```lua
compiler.compflags(sourcefile: <string>, opt: <table>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| sourcefile | Required. Source file path |
| opt | Optional. Option parameters, supports `target` |

#### Return Value

| Type | Description |
|------|-------------|
| table | Returns compilation options list array |

#### Usage

Get the compile option string part of [compiler.compcmd](#compiler-compcmd) without shellname and files, for example:

```lua
local flags = compiler.compflags(sourcefile, {target = target})
for _, flag in ipairs(flags) do
    print(flag)
end
```

The returned array of flags is an array.

## compiler.has\_flags

* Determine if the specified compilation option is supported

#### Function Prototype

::: tip API

```lua
compiler.has_flags(sourcekind: <string>, flag: <string>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| sourcekind | Required. Source file type, e.g., "c", "cxx" |
| flag | Required. Compilation option to check |

#### Return Value

| Type | Description |
|------|-------------|
| boolean | Returns true if supported, false otherwise |

#### Usage

Although it can be judged by [lib.detect.has\_flags](/api/scripts/extension-modules/lib/detect#detect-has_flags), but the interface is more low-level, you need to specify the compiler name.
This interface only needs to specify the language type, it will automatically switch to select the currently supported compiler.

```lua
-- Determine if the c language compiler supports the option: -g
if compiler.has_flags("c", "-g") then
    -- ok
end

-- Determine if the C++ language compiler supports the option: -g
if compiler.has_flags("cxx", "-g") then
    -- ok
end
```

## compiler.features

* Get all compiler features

#### Function Prototype

::: tip API

```lua
compiler.features(sourcekind: <string>, opt: <table>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| sourcekind | Required. Source file type, e.g., "c", "cxx" |
| opt | Optional. Option parameters, supports `target` and `configs` |

#### Return Value

| Type | Description |
|------|-------------|
| table | Returns feature list array |

#### Usage

Although it can be obtained by [lib.detect.features](/api/scripts/extension-modules/lib/detect#detect-features), but the interface is more low-level, you need to specify the compiler name.
This interface only needs to specify the language type, it will automatically switch to select the currently supported compiler, and then get the current list of compiler features.

```lua
-- Get all the features of the current c compiler
local features = compiler.features("c")

-- Get all the features of the current C++ language compiler, enable the C++11 standard, otherwise you will not get the new standard features.
local features = compiler.features("cxx", {configs = {cxxflags = "-std=c++11"}})

-- Get all the features of the current C++ language compiler, pass all configuration information of the project target
local features = compiler.features("cxx", {target = target, configs = {defines = "..", includedirs = ".."}})
```

A list of all c compiler features:

| Feature Name |
| --------------------- |
| c\_static\_assert |
| c\_restrict |
| c\_variadic\_macros |
| c\_function\_prototypes |

A list of all C++ compiler features:

| Feature Name |
| ------------------------------------ |
| cxx\_variable\_templates |
| cxx\_relaxed\_constexpr |
| cxx\_aggregate\_default\_initializers |
| cxx\_contextual\_conversions |
| cxx\_attribute\_deprecated |
| cxx\_decltype\_auto |
| cxx\_digit\_separators |
| cxx\_generic\_lambdas |
| cxx\_lambda\_init\_captures |
| cxx\_binary\_literals |
| cxx\_return\_type\_deduction |
| cxx\_decltype\_incomplete\_return\_types |
| cxx\_reference\_qualified\_functions |
| cxx\_alignof |
| cxx\_attributes |
| cxx\_inheriting\_constructors |
| cxx\_thread\_local |
| cxx\_alias\_templates |
| cxx\_delegating\_constructors |
| cxx\_extended\_friend\_declarations |
| cxx\_final |
| cxx\_nonstatic\_member\_init |
| cxx\_override |
| cxx\_user\_literals |
| cxx\_constexpr |
| cxx\_defaulted\_move\_initializers |
| cxx\_enum\_forward\_declarations |
| cxx\_noexcept |
| cxx\_nullptr |
| cxx\_range\_for |
| cxx\_unrestricted\_unions |
| cxx\_explicit\_conversions |
| cxx\_lambdas |
| cxx\_local\_type\_template\_args |
| cxx\_raw\_string\_literals |
| cxx\_auto\_type |
| cxx\_defaulted\_functions |
| cxx\_deleted\_functions |
| cxx\_generalized\_initializers |
| cxx\_inline\_namespaces |
| cxx\_sizeof\_member |
| cxx\_strong\_enums |
| cxx\_trailing\_return\_types |
| cxx\_unicode\_literals |
| cxx\_uniform\_initialization |
| cxx\_variadic\_templates |
| cxx\_decltype |
| cxx\_default\_function\_template\_args |
| cxx\_long\_long\_type |
| cxx\_right\_angle\_brackets |
| cxx\_rvalue\_references |
| cxx\_static\_assert |
| cxx\_extern\_templates |
| cxx\_func\_identifier |
| cxx\_variadic\_macros |
| cxx\_template\_template\_parameters |

## compiler.has\_features

* Determine if the specified compiler feature is supported

#### Function Prototype

::: tip API

```lua
compiler.has_features(features: <string|table>, opt: <table>)
```

:::

#### Parameter Description

| Parameter | Description |
|-----------|-------------|
| features | Required. Feature name or feature name list |
| opt | Optional. Option parameters, supports `languages`, `target`, `configs` |

#### Return Value

| Type | Description |
|------|-------------|
| boolean | Returns true if supported, false otherwise |

#### Usage

Although it can be obtained by [lib.detect.has\_features](/api/scripts/extension-modules/lib/detect#detect-has_features), but the interface is more low-level, you need to specify the compiler name.
And this interface only needs to specify the special name list that needs to be detected, it can automatically switch to select the currently supported compiler, and then determine whether the specified feature is supported in the current compiler.

```lua
if compiler.has_features("c_static_assert") then
    -- ok
end

if compiler.has_features({"c_static_assert", "cxx_constexpr"}, {languages = "cxx11"}) then
    -- ok
end

if compiler.has_features("cxx_constexpr", {target = target, defines = "..", includedirs = ".."}) then
    -- ok
end
```

For specific feature names, refer to [compiler.features](#compiler-features).
