Skip to content

Commit f2c6a9b

Browse files
generate the list of compatibility macros in a host tool
1 parent 2862a0a commit f2c6a9b

File tree

7 files changed

+119
-57
lines changed

7 files changed

+119
-57
lines changed

lib/PrintAsClang/CMakeLists.txt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,3 @@ target_link_libraries(swiftPrintAsClang PRIVATE
1414
swiftIDE)
1515

1616
set_swift_llvm_is_available(swiftPrintAsClang)
17-
18-
# Add target to install the list of symbols added to compatibility headers
19-
set(symbols_file "${CMAKE_CURRENT_SOURCE_DIR}/compatibility_symbols")
20-
21-
add_custom_target(swift-compatibility-symbols DEPENDS ${symbols_file})
22-
23-
add_dependencies(swiftPrintAsClang swift-compatibility-symbols)
24-
25-
swift_install_in_component(
26-
FILES
27-
${symbols_file}
28-
DESTINATION
29-
"share/swift"
30-
COMPONENT
31-
compiler
32-
)

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ static void emitObjCConditional(raw_ostream &out,
5353
static void writePrologue(raw_ostream &out, ASTContext &ctx,
5454
StringRef macroGuard) {
5555

56-
// NOTE: If you add macros to this prologue, also add them to the `compatibility_symbols` file!
57-
5856
out << "// Generated by "
5957
<< version::getSwiftFullVersion(ctx.LangOpts.EffectiveLanguageVersion)
6058
<< "\n"
@@ -148,7 +146,7 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
148146
BODY "\n" \
149147
"#endif\n";
150148

151-
#include "ClangMacros.def"
149+
#include "swift/PrintAsClang/ClangMacros.def"
152150

153151
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
154152
"need to add SIMD typedefs here if max elements is increased");

lib/PrintAsClang/compatibility_symbols

Lines changed: 0 additions & 38 deletions
This file was deleted.

tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ endif()
1919

2020
add_swift_tool_subdirectory(driver)
2121
add_swift_tool_subdirectory(sil-opt)
22+
add_swift_tool_subdirectory(swift-compatibility-symbols)
2223
add_swift_tool_subdirectory(swift-dependency-tool)
2324
add_swift_tool_subdirectory(swift-demangle)
2425
add_swift_tool_subdirectory(swift-demangle-yamldump)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
add_swift_host_tool(swift-compatibility-symbols
2+
swift-compatibility-symbols.cpp
3+
LLVM_LINK_COMPONENTS support
4+
SWIFT_COMPONENT tools
5+
)
6+
7+
set(syms_file "${CMAKE_BINARY_DIR}/share/swift/compatibility-symbols")
8+
9+
add_custom_command_target(copy_compat_target
10+
OUTPUT
11+
${syms_file}
12+
COMMAND
13+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swift-compatibility-symbols"
14+
--output-filename ${syms_file}
15+
DEPENDS
16+
swift-compatibility-symbols
17+
)
18+
19+
add_dependencies(swift-frontend "${copy_compat_target}")
20+
21+
swift_install_in_component(
22+
FILES
23+
${syms_file}
24+
DESTINATION
25+
"share/swift"
26+
COMPONENT
27+
compiler
28+
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===--- swift-compatibility-symbols.cpp - Emit Clang symbol list ---------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// A simple program to dynamically generate the list of macros added to
14+
// compatibility headers.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#include "llvm/ADT/SmallVector.h"
19+
#include "llvm/ADT/StringRef.h"
20+
#include "llvm/Support/CommandLine.h"
21+
#include "llvm/Support/FileSystem.h"
22+
#include "llvm/Support/raw_ostream.h"
23+
24+
#include <algorithm>
25+
#include <cstdlib>
26+
#include <iostream>
27+
#include <system_error>
28+
29+
namespace options {
30+
31+
static llvm::cl::OptionCategory Category("swift-compatibility-symbols Options");
32+
33+
static llvm::cl::opt<std::string>
34+
OutputFilename("output-filename",
35+
llvm::cl::desc("Filename for the output file"),
36+
llvm::cl::init("-"),
37+
llvm::cl::cat(Category));
38+
39+
}
40+
41+
int main(int argc, char *argv[]) {
42+
llvm::cl::HideUnrelatedOptions(options::Category);
43+
llvm::cl::ParseCommandLineOptions(argc, argv,
44+
"Swift Compatibility Symbols listing\n");
45+
46+
std::error_code error;
47+
llvm::raw_fd_ostream OS(options::OutputFilename, error, llvm::sys::fs::CD_CreateAlways);
48+
if (OS.has_error() || error) {
49+
llvm::errs() << "Error when trying to write to output file; error code "
50+
<< error.message() << "\n";
51+
return EXIT_FAILURE;
52+
}
53+
54+
llvm::SmallVector<llvm::StringRef, 40> symbols;
55+
#define CLANG_MACRO_DEFINED(NAME) \
56+
symbols.push_back(#NAME);
57+
58+
#define CLANG_MACRO(NAME, ARGS, VALUE) \
59+
CLANG_MACRO_DEFINED(NAME)
60+
61+
#define CLANG_MACRO_BODY(NAME, BODY) \
62+
CLANG_MACRO_DEFINED(NAME)
63+
64+
#define CLANG_MACRO_ALTERNATIVE(NAME, ARGS, CONDITION, VALUE, ALTERNATIVE) \
65+
CLANG_MACRO_DEFINED(NAME)
66+
67+
#define CLANG_MACRO_CONDITIONAL(NAME, ARGS, CONDITION, VALUE) \
68+
CLANG_MACRO_DEFINED(NAME)
69+
70+
#define CLANG_MACRO_OBJC(NAME, ARGS, VALUE) \
71+
CLANG_MACRO_DEFINED(NAME)
72+
73+
#define CLANG_MACRO_CXX(NAME, ARGS, VALUE, ALTERNATIVE) \
74+
CLANG_MACRO_DEFINED(NAME)
75+
76+
#define CLANG_MACRO_CXX_BODY(NAME, BODY) \
77+
CLANG_MACRO_DEFINED(NAME)
78+
79+
#include "swift/PrintAsClang/ClangMacros.def"
80+
81+
std::sort(symbols.begin(), symbols.end());
82+
83+
for (const llvm::StringRef sym : symbols) {
84+
OS << sym << "\n";
85+
}
86+
87+
return EXIT_SUCCESS;
88+
}
89+

0 commit comments

Comments
 (0)