-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflags.cppm
More file actions
153 lines (129 loc) · 5.06 KB
/
flags.cppm
File metadata and controls
153 lines (129 loc) · 5.06 KB
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
// mcpp.build.flags — shared compile/link flag computation.
//
// Extracts all flag logic from ninja_backend.cppm into a single point
// of truth so both the ninja backend and compile_commands.json emitter
// (and future backends) share identical flag sets.
//
// See .agents/docs/2026-05-12-compile-commands-design.md.
export module mcpp.build.flags;
import std;
import mcpp.build.plan;
import mcpp.toolchain.detect;
import mcpp.toolchain.registry;
export namespace mcpp::build {
struct CompileFlags {
std::string cxx; // full cxxflags string
std::string cc; // full cflags string
std::string ld; // ldflags string
std::filesystem::path cxxBinary; // g++ / clang++
std::filesystem::path ccBinary; // gcc / clang (derived)
std::filesystem::path arBinary; // ar path (may be empty → use PATH)
std::string sysroot; // --sysroot=... (for ninja ldflags)
std::string bFlag; // -B<binutils> (for ninja ldflags)
std::string toolEnv; // env prefix for private toolchain executables
bool staticStdlib = true;
std::string linkage; // "static" or ""
};
CompileFlags compute_flags(const BuildPlan& plan);
} // namespace mcpp::build
namespace mcpp::build {
namespace {
std::filesystem::path staged_std_bmi_path(const BuildPlan& plan) {
return mcpp::toolchain::staged_std_bmi_path(plan.toolchain, plan.outputDir);
}
// Escape a path for embedding in ninja rule strings.
std::string escape_path(const std::filesystem::path& p) {
auto s = p.string();
std::string out;
out.reserve(s.size());
for (char c : s) {
if (c == ' ' || c == '$' || c == ':')
out.push_back('$');
out.push_back(c);
}
return out;
}
} // namespace
CompileFlags compute_flags(const BuildPlan& plan) {
CompileFlags f;
f.cxxBinary = plan.toolchain.binaryPath;
f.ccBinary = mcpp::toolchain::derive_c_compiler(plan.toolchain);
f.toolEnv = mcpp::toolchain::compiler_env_prefix(plan.toolchain);
// PIC?
bool need_pic = false;
for (auto& lu : plan.linkUnits) {
if (lu.kind == LinkUnit::SharedLibrary) {
need_pic = true;
break;
}
}
std::string pic_flag = need_pic ? " -fPIC" : "";
// Include dirs
std::string include_flags;
for (auto& inc : plan.manifest.buildConfig.includeDirs) {
auto abs = inc.is_absolute() ? inc : (plan.projectRoot / inc);
include_flags += " -I" + escape_path(abs);
}
// Sysroot
std::string sysroot_flag;
if (!plan.toolchain.sysroot.empty()) {
sysroot_flag = " --sysroot=" + escape_path(plan.toolchain.sysroot);
f.sysroot = sysroot_flag;
}
// Binutils -B flag
bool isMuslTc = mcpp::toolchain::is_musl_target(plan.toolchain);
bool isClang = mcpp::toolchain::is_clang(plan.toolchain);
std::filesystem::path binutilsBin;
if (!isMuslTc && !isClang) {
auto ar = mcpp::toolchain::archive_tool(plan.toolchain);
if (!ar.empty())
binutilsBin = ar.parent_path();
}
std::string b_flag;
if (!binutilsBin.empty()) {
b_flag = " -B" + escape_path(binutilsBin);
f.bFlag = b_flag;
}
// AR binary
f.arBinary = mcpp::toolchain::archive_tool(plan.toolchain);
// Opt level (musl ICE workaround)
std::string opt_flag = isMuslTc ? " -Og" : " -O2";
// User flags
auto join = [](const std::vector<std::string>& v) {
std::string s;
for (auto& f : v) {
s += ' ';
s += f;
}
return s;
};
std::string user_cxxflags = join(plan.manifest.buildConfig.cxxflags);
std::string user_cflags = join(plan.manifest.buildConfig.cflags);
// C standard
std::string c_std =
plan.manifest.buildConfig.cStandard.empty() ? "c11" : plan.manifest.buildConfig.cStandard;
// Assemble
std::string module_flag = isClang ? "" : " -fmodules";
std::string std_module_flag;
if (isClang && !plan.stdBmiPath.empty()) {
std_module_flag = " -fmodule-file=std=" + escape_path(staged_std_bmi_path(plan));
}
f.cxx = std::format("-std=c++23{}{}{}{}{}{}{}{}", module_flag, std_module_flag,
opt_flag, pic_flag, sysroot_flag, b_flag, include_flags, user_cxxflags);
f.cc = std::format("-std={}{}{}{}{}{}{}", c_std, opt_flag, pic_flag, sysroot_flag, b_flag,
include_flags, user_cflags);
// Link flags
f.staticStdlib = plan.manifest.buildConfig.staticStdlib;
f.linkage = plan.manifest.buildConfig.linkage;
std::string full_static = (f.linkage == "static") ? " -static" : "";
std::string static_stdlib = (f.staticStdlib && !isClang) ? " -static-libstdc++" : "";
std::string runtime_dirs;
for (auto& dir : plan.toolchain.linkRuntimeDirs) {
runtime_dirs += " -L" + escape_path(dir);
runtime_dirs += " -Wl,-rpath," + escape_path(dir);
}
f.ld = std::format("{}{}{}{}{}", full_static, static_stdlib, sysroot_flag, b_flag,
runtime_dirs);
return f;
}
} // namespace mcpp::build