aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake/config.lua
blob: b4b2bf71347ba642348e797796bceca6a052afa1 (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
---@class CMakeConfig
---@field cmake CMakeConfigCMake Configuration for `cmake` command itself
---@field save_before_build boolean Save all unsaved files before running `cmake`
---@field generate_after_save boolean Generate after saving `CMakeLists.txt` file
---@field cmake_terminal CMakeConfigCMakeTerminal Settings for terminal where cmake will be executed
---@field target_terminal CMakeConfigTargetTerminal Settings for terminal where executable targets will be executed
---@field variants {[string]:CMakeVariant} Default variants. Parameters defined in variants have more priority than defined in `cmake = {...}` ones

---@class CMakeConfigCMake
---@field cmake_path string Path to `cmake` executable
---@field ctest_path string Path to `ctest` executable
---@field cpack_path string Path to `cpack` executable
---@field build_args string[] An array of additional arguments to pass to `cmake --build`
---@field build_tool_args string[] An array of additional arguments to pass to the underlying build tool
---@field generator? string Set to a string to override CMake Tools’ preferred generator logic. If this is set, CMake will unconditionally use it as the -G CMake generator command line argument
---@field parallel_jobs? number By specifying a number, you can define how many jobs are run in parallel during the build
---@field build_directory string Build directory template
---@field environment {[string]:string} Environment for all `cmake` commands
---@field configure_environment {[string]:string} Environment for all `cmake -B ...` commands
---@field build_environment {[string]:string} Environment for all `cmake --build ...` commands

---@class CMakeVariant
---@field default string Default choice
---@field description string Description for variant option
---@field choices {[string]:CMakeVariantChoice} Choices for variant option

---@class CMakeVariantChoice
---@field short string Short description for choice
---@field long? string Short description for choice
---@field buildType? string Value for `CMAKE_BUILD_TYPE` variable.
---@field generator? string Set to a string to override CMake Tools’ preferred generator logic. If this is set, CMake will unconditionally use it as the -G CMake generator command line argument
---@field buildArgs? string[] An array of additional arguments to pass to `cmake --build`
---@field buildToolArgs? string[] An array of additional arguments to pass to the underlying build tool
---@field settings? {[string]:string} Table of parameters which will be passed as `-Dkey=value` to `cmake` command
---@field env? {[string]:string} Table of parameters which will be passed as environment variables to `cmake`
---@field linkage? "static"|"shared" Linkage type

---@class CMakeConfigCMakeTerminal
---@field split "left"|"right"|"below"|"above" Split direction
---@field size number Terminal's size in lines
---@field close_on_exit "success"|"failure"|boolean When to close termilal. `"success"` - after success, `"failure"` - after failure, `true` - always, `false` - never
---@field open_on_start boolean Open terminal when `cmake` starts
---@field clear_env boolean Do not pass shell environment to cmake process
---@field enter boolean Focus on opened terminal window

---@class CMakeConfigTargetTerminal
---@field split "left"|"right"|"below"|"above" Split direction
---@field size number Terminal's size in lines
---@field enter boolean Focus on opened terminal window
---@field immediately boolean Run command immediately. If false, command will just be pasted to terminal so you can modify it

---@type CMakeConfig
local default_config = {
  cmake = {
    cmake_path = "cmake",
    ctest_path = "ctest",
    cpack_path = "cpack",
    environment = {},
    configure_environment = {},
    build_directory = "${workspaceFolder}/build-${buildType}",
    build_environment = {},
    build_args = {},
    build_tool_args = {},
    generator = nil,
    parallel_jobs = nil,
  },
  variants = {
    buildType = {
      default = "debug",
      description = "Build type",
      choices = {
        debug = { short = "Debug", buildType = "Debug" },
        release = { short = "Release", buildType = "Release" },
        relWithDebInfo = { short = "RelWithDebInfo", buildType = "RelWithDebInfo" },
        minSizeRel = { short = "MinSizeRel", buildType = "MinSizeRel" },
      },
    },
  },
  save_before_build = true,
  generate_after_save = true,
  cmake_terminal = {
    split = "below",
    size = 15,
    close_on_exit = "success",
    open_on_start = true,
    clear_env = false,
    enter = false,
  },
  target_terminal = {
    split = "below",
    size = 15,
    enter = true,
    immediately = true,
  },
  notification = {
    after = "success",
  },
  variants_display = {
    short = { sep = " × ", show = true },
    long = { sep = " ❄ ", show = false },
  },
  keybinds = {},
}

local M = vim.deepcopy(default_config)

---Setup configs
---@param opts CMakeConfig
M.setup = function(opts)
  local newconf = vim.tbl_deep_extend("force", default_config, opts or {})

  for k, v in pairs(newconf) do
    M[k] = v
  end
end

return M