Skip to content

Commit c5845aa

Browse files
Adds a tested compilation database generator (#2636)
1 parent f593d2f commit c5845aa

File tree

19 files changed

+1039
-37
lines changed

19 files changed

+1039
-37
lines changed

.github/ISSUE_TEMPLATE/report-a-bug.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Describe any steps you've taken to try to solve or workaround the bug.
2121
**How can we reproduce this?**
2222
Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) we can use to replicate the problem in our own development environments. Use [code blocks](https://help.github.com/en/articles/creating-and-highlighting-code-blocks) to format code and console output nicely.
2323

24+
- [ ] Visual Studio 2026 (vs2026)
2425
- [ ] Visual Studio 2022 (vs2022)
2526
- [ ] Visual Studio 2019 (vs2019)
2627
- [ ] Visual Studio 2017 (vs2017)
@@ -33,6 +34,7 @@ Please provide a [minimal, reproducible example](https://stackoverflow.com/help/
3334
- [ ] GNU Makefile Legacy (gmakelegacy)
3435
- [ ] XCode (xcode)
3536
- [ ] Codelite
37+
- [ ] Compile Commands
3638
- [ ] Other (Please list below)
3739

3840
**What version of Premake are you using?**

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Makefile
4242
*.ninja_deps
4343
*.ninja_log
4444
.vscode/
45+
*compile_commands.json
46+
.cache/
4547

4648
*.bbprojectsettings
4749
Scratchpad.txt

modules/codelite/tests/test_codelite_config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
prepare()
101101
codelite.project.compiler(cfg)
102102
test.capture [[
103-
<Compiler Options="-isystem sysdir;-isystem sysdir2" C_Options="-isystem sysdir;-isystem sysdir2" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="1">
103+
<Compiler Options="-isystemsysdir;-isystemsysdir2" C_Options="-isystemsysdir;-isystemsysdir2" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="1">
104104
</Compiler>
105105
]]
106106
end
@@ -110,7 +110,7 @@
110110
prepare()
111111
codelite.project.compiler(cfg)
112112
test.capture [[
113-
<Compiler Options="-idirafter sysdir;-idirafter sysdir2" C_Options="-idirafter sysdir;-idirafter sysdir2" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="1">
113+
<Compiler Options="-idiraftersysdir;-idiraftersysdir2" C_Options="-idiraftersysdir;-idiraftersysdir2" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="1">
114114
</Compiler>
115115
]]
116116
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- _manifest.lua
3+
-- Define the compilecommands manifest
4+
-- Author: Nick Clark
5+
-- Copyright (c) 2026 Jess Perkins and the Premake project
6+
--
7+
8+
9+
return {
10+
"_preload.lua",
11+
"compilecommands.lua",
12+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
--
2+
-- _preload.lua
3+
-- Define the compilecommands actions
4+
-- Author: Nick Clark
5+
-- Copyright (c) 2026 Jess Perkins and the Premake project
6+
--
7+
8+
local p = premake
9+
10+
newoption {
11+
category = "Compilation Database",
12+
13+
trigger = "cc-config",
14+
description = "Specify the configuration to generate compile_commands.json for",
15+
}
16+
17+
newoption {
18+
category = "Compilation Database",
19+
20+
trigger = "cc-platform",
21+
description = "Specify the platform to generate compile_commands.json for",
22+
}
23+
24+
newoption {
25+
category = "Compilation Database",
26+
27+
trigger = "cc-output",
28+
description = "Specify the output file path for compile_commands.json",
29+
}
30+
31+
newaction {
32+
trigger = "compilecommands",
33+
shortname = "compilecommands",
34+
description = "Generate compile_commands.json files",
35+
36+
-- Action Capabilities
37+
valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib" },
38+
valid_languages = { "C", "C++" },
39+
valid_tools = {
40+
cc = { "clang", "cosmocc", "emcc", "gcc" },
41+
},
42+
43+
toolset = "clang",
44+
45+
onInitialize = function()
46+
require("compilecommands")
47+
end,
48+
49+
configurable = true,
50+
51+
execute = function()
52+
local all_commands = {}
53+
54+
for wks in p.global.eachWorkspace() do
55+
-- Platform is set in the following order of precedence
56+
-- 1. Command line option
57+
-- 2. Workspace default platform
58+
-- 3. Empty string
59+
local platform = _OPTIONS["cc-platform"] or wks.defaultplatform or ""
60+
61+
-- Configuration is set in the following order of precedence
62+
-- 1. Command line option
63+
-- 2. First build configuration of the workspace
64+
-- 3. Empty string
65+
local buildcfg = _OPTIONS["cc-config"] or wks.configurations[1] or ""
66+
67+
local compile_commands = p.modules.compilecommands.generate(wks, platform, buildcfg)
68+
all_commands = table.join(all_commands, compile_commands)
69+
end
70+
71+
local results, err = json.encode(all_commands)
72+
if not results then
73+
p.error("Failed to encode compile commands to JSON: %s", err)
74+
return
75+
end
76+
77+
local output_path = _OPTIONS["cc-output"] or "compile_commands.json"
78+
79+
local dir = path.getdirectory(output_path)
80+
local ok, err = os.mkdir(dir)
81+
if not ok then
82+
error(err, 0)
83+
end
84+
85+
local f, err = os.writefile_ifnotequal(results, output_path)
86+
if err then
87+
p.error("Failed to write compile_commands.json: %s", err)
88+
elseif f ~= 0 then
89+
printf("Generated %s...", path.getrelative(os.getcwd(), output_path))
90+
end
91+
end
92+
}
93+
94+
return function(cfg)
95+
return (_ACTION == "compilecommands")
96+
end

0 commit comments

Comments
 (0)