Skip to content

Commit 87fc856

Browse files
committed
makeOptions: Fix CMake build for stable/21.x rebranch
Adjust the code to work with both stable/20240723 and stable/21.x after llvm/llvm-project#119198. This saves us from having to cut and qualify a rebranch branch for swift-driver.
1 parent 69a7a93 commit 87fc856

File tree

2 files changed

+79
-33
lines changed

2 files changed

+79
-33
lines changed

Sources/makeOptions/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ add_executable(makeOptions
1616
set_target_properties(makeOptions PROPERTIES
1717
CXX_STANDARD 17)
1818
target_compile_definitions(makeOptions PRIVATE
19-
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1)
19+
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1
20+
LLVM_VERSION_MAJOR=${LLVM_VERSION_MAJOR})
2021
target_include_directories(makeOptions PRIVATE
2122
${SWIFT_INCLUDE_DIRS}
2223
${LLVM_BUILD_BINARY_DIR}/include

Sources/makeOptions/makeOptions.cpp

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
#include <vector>
1919

2020
#if __has_include("swift/Option/Options.inc")
21-
#if __has_include("llvm/ADT/ArrayRef.h")
22-
#if __has_include("llvm/ADT/StringRef.h")
21+
#if __has_include("llvm/Option/OptTable.h")
22+
#if __has_include("llvm/Option/Option.h")
2323

24-
#include "llvm/ADT/ArrayRef.h"
25-
#include "llvm/ADT/StringRef.h"
24+
#include "llvm/Option/OptTable.h"
25+
#include "llvm/Option/Option.h"
2626

2727
enum class OptionKind {
2828
Group = 0,
@@ -37,12 +37,6 @@ enum class OptionKind {
3737
MultiArg,
3838
};
3939

40-
#define LLVM_MAKE_OPT_ID_WITH_ID_PREFIX(ID_PREFIX, PREFIX, NAME, ID, KIND, \
41-
GROUP, ALIAS, ALIASARGS, FLAGS, \
42-
VISIBILITY, PARAM, HELPTEXT, \
43-
HELPTEXTFORVARIANTS, METAVAR, VALUES) \
44-
ID_PREFIX##ID
45-
4640
//. The IDs of each option
4741
enum class OptionID {
4842
Opt_INVALID = 0,
@@ -106,7 +100,7 @@ static std::string swiftify(const std::string &name) {
106100
/// Raw option from the TableGen'd output of the Swift options.
107101
struct RawOption {
108102
OptionID id;
109-
const llvm::ArrayRef<llvm::StringLiteral> prefixes;
103+
std::vector<llvm::StringRef> prefixes;
110104
const char *spelling;
111105
std::string idName;
112106
OptionKind kind;
@@ -130,16 +124,65 @@ struct RawOption {
130124
}
131125
};
132126

127+
#if defined(LLVM_VERSION_MAJOR) && LLVM_VERSION_MAJOR == 21
128+
129+
#define OPTTABLE_PREFIXES_TABLE_CODE
130+
#include "swift/Option/Options.inc"
131+
#undef OPTTABLE_PREFIXES_TABLE_CODE
132+
133+
#define OPTTABLE_STR_TABLE_CODE
134+
#include "swift/Option/Options.inc"
135+
#undef OPTTABLE_STR_TABLE_CODE
136+
137+
static std::vector<llvm::StringRef> getPrefixes(unsigned prefixesOffset) {
138+
unsigned numPrefixes = OptionPrefixesTable[prefixesOffset].value();
139+
140+
std::vector<llvm::StringRef> prefixes;
141+
142+
const unsigned firstOffsetIndex = prefixesOffset + 1;
143+
for (unsigned i = firstOffsetIndex, e = i + numPrefixes; i < e; ++i) {
144+
prefixes.push_back(OptionStrTable[OptionPrefixesTable[i]]);
145+
}
146+
147+
return prefixes;
148+
}
149+
150+
static const char *getPrefixedName(unsigned prefixedNameOffset) {
151+
return OptionStrTable[prefixedNameOffset].data();
152+
}
153+
154+
#else // #if defined(LLVM_VERSION_MAJOR) && LLVM_VERSION_MAJOR == 21
155+
133156
#define PREFIX(NAME, VALUE) static constexpr llvm::StringLiteral NAME[] = VALUE;
134157
#include "swift/Option/Options.inc"
135158
#undef PREFIX
136159

160+
static std::vector<llvm::StringRef>
161+
getPrefixes(llvm::ArrayRef<llvm::StringLiteral> prefixes) {
162+
return std::vector<llvm::StringRef>(prefixes.begin(), prefixes.end());
163+
}
164+
165+
static const char *getPrefixedName(const char *prefixedName) {
166+
return prefixedName;
167+
}
168+
169+
#endif // #if defined(LLVM_VERSION_MAJOR) && LLVM_VERSION_MAJOR == 21
170+
137171
static const RawOption rawOptions[] = {
138-
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
139-
VISIBILITY, PARAM, HELPTEXT, HELPTEXTFORVARIANTS, METAVAR, \
140-
VALUES) \
141-
{ OptionID::Opt_##ID, PREFIX, NAME, swiftify(#ID), OptionKind::KIND, \
142-
OptionID::Opt_##GROUP, OptionID::Opt_##ALIAS, FLAGS, HELPTEXT, METAVAR, PARAM },
172+
#define OPTION(PREFIXES_OFFSET, PREFIXED_NAME_OFFSET, ID, KIND, GROUP, ALIAS, \
173+
ALIASARGS, FLAGS, VISIBILITY, PARAM, HELPTEXT, \
174+
HELPTEXTFORVARIANTS, METAVAR, VALUES) \
175+
{OptionID::Opt_##ID, \
176+
getPrefixes(PREFIXES_OFFSET), \
177+
getPrefixedName(PREFIXED_NAME_OFFSET), \
178+
swiftify(#ID), \
179+
OptionKind::KIND, \
180+
OptionID::Opt_##GROUP, \
181+
OptionID::Opt_##ALIAS, \
182+
FLAGS, \
183+
HELPTEXT, \
184+
METAVAR, \
185+
PARAM},
143186
#include "swift/Option/Options.inc"
144187
#undef OPTION
145188
};
@@ -183,9 +226,13 @@ void forEachOption(std::function<void(const RawOption &)> fn) {
183226
}
184227
}
185228

186-
void forEachSpelling(const llvm::ArrayRef<llvm::StringLiteral> prefixes, const std::string &spelling,
187-
std::function<void(const std::string &spelling,
188-
bool isAlternateSpelling)> fn) {
229+
void forEachSpelling(
230+
const RawOption &option,
231+
std::function<void(const std::string &spelling, bool isAlternateSpelling)>
232+
fn) {
233+
std::string spelling(option.spelling);
234+
auto &prefixes = option.prefixes;
235+
189236
fn(spelling, /*isAlternateSpelling=*/false);
190237

191238
if (prefixes.empty())
@@ -253,9 +300,8 @@ int makeOptions_main() {
253300
out << "extension Option {\n";
254301
forEachOption([&](const RawOption &option) {
255302
// Look through each spelling of the option.
256-
forEachSpelling(option.prefixes, option.spelling,
257-
[&](const std::string &spelling,
258-
bool isAlternateSpelling) {
303+
forEachSpelling(option, [&](const std::string &spelling,
304+
bool isAlternateSpelling) {
259305
out << " public static let " << option.idName;
260306

261307
// Add a '_' suffix if this is an alternate spelling.
@@ -372,14 +418,13 @@ int makeOptions_main() {
372418
<< " return [\n";
373419
forEachOption([&](const RawOption &option) {
374420
// Look through each spelling of the option.
375-
forEachSpelling(option.prefixes, option.spelling,
376-
[&](const std::string &spelling,
377-
bool isAlternateSpelling) {
378-
out << " Option." << option.idName;
379-
if (isAlternateSpelling)
380-
out << "_";
381-
out << ",\n";
382-
});
421+
forEachSpelling(
422+
option, [&](const std::string &spelling, bool isAlternateSpelling) {
423+
out << " Option." << option.idName;
424+
if (isAlternateSpelling)
425+
out << "_";
426+
out << ",\n";
427+
});
383428
});
384429
out << " ]\n";
385430
out << " }\n";
@@ -424,10 +469,10 @@ int makeOptions_main() {
424469
return 0;
425470
}
426471
#else
427-
static_assert(false, "Failed to locate/include StringRef.h");
472+
static_assert(false, "Failed to locate/include llvm/Option/Option.h");
428473
#endif
429474
#else
430-
static_assert(false, "Failed to locate/include ArrayRef.h");
475+
static_assert(false, "Failed to locate/include llvm/Option/OptTable.h");
431476
#endif
432477
#else
433478
#warning "Unable to include 'swift/Option/Options.inc', `makeOptions` will not be usable"

0 commit comments

Comments
 (0)