Skip to content

Commit 92b9fe1

Browse files
committed
Build: Add support to switch the C++ standard
1 parent a2bcb9d commit 92b9fe1

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Each library is color-coded to signal its status:
5858
🟩 Usable (a reasonable set of useful features has been implemented)
5959

6060
<picture>
61-
<source media="(prefers-color-scheme: dark)" srcset="https://pagghiu.github.io/images/dependencies/SaneCppLibrariesDependenciesDark.svg">
6261
<img alt="Sane C++ Libraries dependencies" src="https://pagghiu.github.io/images/dependencies/SaneCppLibrariesDependencies.svg">
6362
</picture>
6463

Tools/SC-build/Build.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,48 @@ struct Warning
167167
Warning(State state, uint32_t number) : state(state), type(MSVCWarning), number(number) {}
168168
};
169169

170+
struct CppStandard
171+
{
172+
enum Type
173+
{
174+
CPP11,
175+
CPP14,
176+
CPP17,
177+
CPP20,
178+
CPP23,
179+
};
180+
181+
/// @brief Get StringView from CppStandard::Type
182+
static constexpr StringView toString(Type type)
183+
{
184+
switch (type)
185+
{
186+
case CPP11: return "c++11";
187+
case CPP14: return "c++14";
188+
case CPP17: return "c++17";
189+
case CPP20: return "c++20";
190+
case CPP23: return "c++23";
191+
}
192+
Assert::unreachable();
193+
}
194+
195+
/// @brief Get MSVC LanguageStandard value from CppStandard::Type (e.g., stdcpp14)
196+
static constexpr StringView toMSVCString(Type type)
197+
{
198+
switch (type)
199+
{
200+
case CPP11: return "stdcpp11";
201+
case CPP14: return "stdcpp14";
202+
case CPP17: return "stdcpp17";
203+
case CPP20: return "stdcpp20";
204+
case CPP23: return "stdcpp23";
205+
}
206+
Assert::unreachable();
207+
}
208+
209+
/// @brief Get Makefile -std= flag from CppStandard::Type
210+
static StringView toMakefileFlag(Type type) { return toString(type); }
211+
};
170212
/// @brief Compile flags (include paths, preprocessor defines etc.)
171213
struct CompileFlags
172214
{
@@ -182,6 +224,8 @@ struct CompileFlags
182224
Parameter<bool> enableStdCpp = false; ///< Enable and include C++ Standard Library
183225
Parameter<bool> enableCoverage = false; ///< Enables code coverage instrumentation
184226

227+
Parameter<CppStandard::Type> cppStandard = CppStandard::CPP14; ///< C++ language standard version
228+
185229
/// @brief Merges opinions about flags into target flags
186230
/// @param opinions Opinions about flags from strongest to weakest
187231
/// @param flags Output flags

Tools/SC-build/Build.inl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SC::Result SC::Build::CompileFlags::merge(Span<const CompileFlags*> opinions, Co
3535
Internal::writeStrongest(&CompileFlags::enableExceptions, opinions, flags);
3636
Internal::writeStrongest(&CompileFlags::enableStdCpp, opinions, flags);
3737
Internal::writeStrongest(&CompileFlags::enableCoverage, opinions, flags);
38+
Internal::writeStrongest(&CompileFlags::cppStandard, opinions, flags);
3839

3940
// TODO: Implement ability to "remove" paths from stronger opinions
4041
for (const CompileFlags* opinion : opinions)
@@ -167,7 +168,7 @@ bool SC::Build::Project::addPresetConfiguration(Configuration::Preset preset, co
167168

168169
SC::Build::Configuration* SC::Build::Project::getConfiguration(StringView configurationName)
169170
{
170-
size_t index;
171+
size_t index = 0;
171172
if (configurations.find([=](auto it) { return it.name == configurationName; }, &index))
172173
{
173174
return &configurations[index];

Tools/SC-build/BuildWriterMakefile.inl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ endif
573573
{
574574
SC_COMPILER_WARNING_PUSH_UNUSED_RESULT;
575575

576-
// TODO: De-hardcode -std=c++14
577-
builder.append("\n{0}_COMMON_CXXFLAGS := -std=c++14", makeTarget);
576+
builder.append("\n{0}_COMMON_CXXFLAGS := -std={1}", makeTarget,
577+
CppStandard::toString(compileFlags.cppStandard));
578578

579579
if (not compileFlags.enableRTTI)
580580
{

Tools/SC-build/BuildWriterVisualStudio.inl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ struct SC::Build::ProjectWriter::WriterVisualStudio
318318
}
319319

320320
builder.append(" <MultiProcessorCompilation>true</MultiProcessorCompilation>\n");
321+
if (compileFlags.cppStandard != CppStandard::CPP14)
322+
{
323+
builder.append(" <LanguageStandard>{}</LanguageStandard>\n",
324+
CppStandard::toMSVCString(compileFlags.cppStandard));
325+
}
321326
builder.append(" </ClCompile>\n");
322327
builder.append(" <Link>\n");
323328
switch (project.targetType)

Tools/SC-build/BuildWriterXCode.inl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,16 +619,17 @@ struct SC::Build::ProjectWriter::WriterXCode
619619
return true;
620620
}
621621

622-
[[nodiscard]] bool writeCommonOptions(StringBuilder& builder, const Project& project)
622+
[[nodiscard]] bool writeCommonOptions(StringBuilder& builder, const Project& project, CppStandard::Type cppStandard)
623623
{
624624
SC_COMPILER_WARNING_PUSH_UNUSED_RESULT;
625625
builder.append(R"delimiter(
626626
ALWAYS_SEARCH_USER_PATHS = NO;
627627
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = NO;
628628
CLANG_ANALYZER_NONNULL = YES;
629629
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
630-
CLANG_CXX_LANGUAGE_STANDARD = "c++14";
631-
CURRENT_PROJECT_VERSION = 1;)delimiter");
630+
CLANG_CXX_LANGUAGE_STANDARD = "{0}";
631+
CURRENT_PROJECT_VERSION = 1;)delimiter",
632+
CppStandard::toString(cppStandard));
632633

633634
if (project.targetType == TargetType::GUIApplication)
634635
{
@@ -741,7 +742,7 @@ struct SC::Build::ProjectWriter::WriterXCode
741742
buildSettings = {{)delimiter",
742743
xcodeObject.referenceHash.view(), xcodeObject.name.view());
743744

744-
writeCommonOptions(builder, project);
745+
writeCommonOptions(builder, project, compileFlags.cppStandard);
745746
writeDirectories(builder, configuration);
746747

747748
if (compileFlags.enableRTTI)

0 commit comments

Comments
 (0)