Skip to content

Commit 7d75109

Browse files
authored
Set Make's default CXX (#1002)
fixes #924, closes #942
1 parent b05bf18 commit 7d75109

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/BuildConfig.cc

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "BuildConfig.hpp"
22

33
#include "Algos.hpp"
4+
#include "Command.hpp"
45
#include "Exception.hpp"
56
#include "Git2.hpp"
67
#include "Logger.hpp"
@@ -109,15 +110,36 @@ struct BuildConfig {
109110
std::optional<std::unordered_set<std::string>> all;
110111

111112
std::string OUT_DIR;
112-
std::string CXX = "clang++";
113+
std::string CXX;
113114
std::vector<std::string> CXXFLAGS;
114115
std::vector<std::string> DEFINES;
115116
std::vector<std::string> INCLUDES = { "-I../../include" };
116117
std::vector<std::string> LIBS;
117118

118-
BuildConfig() = default;
119119
explicit BuildConfig(const std::string& packageName)
120-
: packageName{ packageName }, buildOutDir{ packageName + ".d" } {}
120+
: packageName{ packageName }, buildOutDir{ packageName + ".d" } {
121+
if (const char* cxx = std::getenv("CXX")) {
122+
CXX = cxx;
123+
} else {
124+
std::string output = Command("make")
125+
.addArg("--print-data-base")
126+
.addArg("--question")
127+
.addArg("-f")
128+
.addArg("/dev/null")
129+
.output()
130+
.output;
131+
std::istringstream iss(output);
132+
std::string line;
133+
134+
while (std::getline(iss, line)) {
135+
if (line.starts_with("CXX = ")) {
136+
CXX = line.substr(6);
137+
return;
138+
}
139+
}
140+
throw PoacError("failed to get CXX from make");
141+
}
142+
}
121143

122144
void setOutDir(const bool isDebug) {
123145
if (isDebug) {
@@ -593,7 +615,7 @@ BuildConfig::addDefine(
593615

594616
void
595617
BuildConfig::setVariables(const bool isDebug) {
596-
this->defineCondVar("CXX", CXX);
618+
this->defineSimpleVar("CXX", CXX);
597619

598620
CXXFLAGS.push_back("-std=c++" + getPackageEdition().getString());
599621
if (shouldColor()) {
@@ -816,9 +838,6 @@ configureBuild(BuildConfig& config, const bool isDebug) {
816838
if (!fs::exists(outDir)) {
817839
fs::create_directories(outDir);
818840
}
819-
if (const char* cxx = std::getenv("CXX")) {
820-
config.CXX = cxx;
821-
}
822841

823842
config.setVariables(isDebug);
824843

@@ -999,7 +1018,7 @@ namespace tests {
9991018

10001019
void
10011020
testCycleVars() {
1002-
BuildConfig config;
1021+
BuildConfig config("test");
10031022
config.defineSimpleVar("a", "b", { "b" });
10041023
config.defineSimpleVar("b", "c", { "c" });
10051024
config.defineSimpleVar("c", "a", { "a" });
@@ -1017,7 +1036,7 @@ testCycleVars() {
10171036

10181037
void
10191038
testSimpleVars() {
1020-
BuildConfig config;
1039+
BuildConfig config("test");
10211040
config.defineSimpleVar("c", "3", { "b" });
10221041
config.defineSimpleVar("b", "2", { "a" });
10231042
config.defineSimpleVar("a", "1");
@@ -1036,7 +1055,7 @@ testSimpleVars() {
10361055

10371056
void
10381057
testDependOnUnregisteredVar() {
1039-
BuildConfig config;
1058+
BuildConfig config("test");
10401059
config.defineSimpleVar("a", "1", { "b" });
10411060

10421061
std::ostringstream oss;
@@ -1049,7 +1068,7 @@ testDependOnUnregisteredVar() {
10491068

10501069
void
10511070
testCycleTargets() {
1052-
BuildConfig config;
1071+
BuildConfig config("test");
10531072
config.defineTarget("a", { "echo a" }, { "b" });
10541073
config.defineTarget("b", { "echo b" }, { "c" });
10551074
config.defineTarget("c", { "echo c" }, { "a" });
@@ -1067,7 +1086,7 @@ testCycleTargets() {
10671086

10681087
void
10691088
testSimpleTargets() {
1070-
BuildConfig config;
1089+
BuildConfig config("test");
10711090
config.defineTarget("a", { "echo a" });
10721091
config.defineTarget("b", { "echo b" }, { "a" });
10731092
config.defineTarget("c", { "echo c" }, { "b" });
@@ -1092,7 +1111,7 @@ testSimpleTargets() {
10921111

10931112
void
10941113
testDependOnUnregisteredTarget() {
1095-
BuildConfig config;
1114+
BuildConfig config("test");
10961115
config.defineTarget("a", { "echo a" }, { "b" });
10971116

10981117
std::ostringstream oss;

0 commit comments

Comments
 (0)