Skip to content

Commit 86585af

Browse files
committed
Generate single epilogue file by cmake
1 parent 961bf94 commit 86585af

6 files changed

Lines changed: 47 additions & 138 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,28 @@ add_custom_command(
217217
file(GLOB rule_subdirs CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/rules/*)
218218
set(cpp_rules_ir_outputs)
219219
set(rust_rules_ir_outputs)
220+
set(_cxx_epilogue)
221+
set(_c_epilogue)
220222
foreach(_rule_dir IN LISTS rule_subdirs)
221223
file(GLOB _cpp_srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
222224
if(NOT _cpp_srcs)
223225
continue()
224226
endif()
225227
get_filename_component(_rule_name ${_rule_dir} NAME)
228+
string(APPEND _cxx_epilogue "namespace cpp2rust_rules_${_rule_name} {\n")
229+
if(EXISTS ${_rule_dir}/src.cpp)
230+
string(APPEND _cxx_epilogue "#include \"${_rule_dir}/src.cpp\"\n")
231+
endif()
232+
if(EXISTS ${_rule_dir}/src.c)
233+
string(APPEND _cxx_epilogue "#include \"${_rule_dir}/src.c\"\n")
234+
string(APPEND _c_epilogue
235+
"#define CPP2RUST_EXPR_RULE(n) cpp2rust_rules_${_rule_name}_f##n\n"
236+
"#define CPP2RUST_TYPE_RULE(n) cpp2rust_rules_${_rule_name}_t##n\n"
237+
"#include \"${_rule_dir}/src.c\"\n"
238+
"#undef CPP2RUST_EXPR_RULE\n"
239+
"#undef CPP2RUST_TYPE_RULE\n")
240+
endif()
241+
string(APPEND _cxx_epilogue "}\n")
226242
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
227243
set(_cpp_out ${_out_dir}/ir_src.json)
228244
set(_cxxflags)
@@ -271,6 +287,15 @@ foreach(_rule_dir IN LISTS rule_subdirs)
271287
list(APPEND rust_rules_ir_outputs ${_rust_out})
272288
endforeach()
273289

290+
file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/cpp2rust_rules_epilogue.h CONTENT
291+
"#pragma GCC system_header
292+
#ifdef __cplusplus
293+
#define CPP2RUST_EXPR_RULE(n) f##n
294+
#define CPP2RUST_TYPE_RULE(n) t##n
295+
${_cxx_epilogue}#else
296+
${_c_epilogue}#endif
297+
")
298+
274299
add_custom_target("preprocess-cpp-rules" ALL DEPENDS ${cpp_rules_ir_outputs})
275300

276301
add_custom_target("preprocess-rust-rules" ALL

‎cpp2rust/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_C_COMPILER=\"${CMAKE_C_
1717
target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_CXX_COMPILER=\"${CMAKE_CXX_COMPILER}\"")
1818
target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_RESOURCE_DIR=\"${LLVM_LIBRARY_DIR}/clang/${LLVM_VERSION_MAJOR}\"")
1919
target_compile_definitions(cpp2rust_core PUBLIC "-DCOMPAT_INCLUDE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/compat\"")
20-
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_SOURCE_DIR=\"${PROJECT_SOURCE_DIR}/rules\"")
20+
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_EPILOGUE_PATH=\"${CMAKE_BINARY_DIR}/cpp2rust_rules_epilogue.h\"")
2121
target_compile_definitions(cpp2rust_core PUBLIC "-DRUST_STABLE_VERSION=\"${RUST_STABLE_VERSION}\"")
2222

2323
if (APPLE)

‎cpp2rust/converter/rules/rules_epilogue.cpp‎

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

‎cpp2rust/converter/rules/rules_epilogue.h‎

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

‎cpp2rust/cpp2rust_lib.cpp‎

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77
#include <clang/Tooling/CompilationDatabase.h>
88
#include <clang/Tooling/Tooling.h>
99

10-
#include <deque>
1110
#include <filesystem>
12-
#include <format>
13-
#include <fstream>
14-
#include <iterator>
1511

1612
#include "compat/platform_flags.h"
1713
#include "converter/converter.h"
1814
#include "converter/models/converter_refcount.h"
19-
#include "converter/rules/rules_epilogue.h"
2015
#include "frontend_action.h"
2116

2217
namespace cpp2rust {
@@ -31,18 +26,12 @@ std::string TranspileSrc(std::string_view cc_code, Model model,
3126
auto end_flags = getPlatformClangEndFlags();
3227
tool_args.insert(tool_args.end(), end_flags.begin(), end_flags.end());
3328

34-
auto language = GetRulesLanguage(filename);
35-
auto code = std::format("{}\n#include \"{}\"\n", cc_code,
36-
GetRulesEpiloguePath(language));
37-
3829
std::string rs_code;
3930
clang::tooling::runToolOnCodeWithArgs(
4031
std::make_unique<FrontendAction>(rs_code, model, /*first=*/true,
4132
rules_dir),
42-
code, tool_args, std::filesystem::path(filename).filename().string(),
43-
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER,
44-
std::make_shared<clang::PCHContainerOperations>(),
45-
{{GetRulesEpiloguePath(language), BuildRulesEpilogue(language)}});
33+
cc_code, tool_args, std::filesystem::path(filename).filename().string(),
34+
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER);
4635
Converter::EmitOpaqueRecords(rs_code);
4736
Converter::EmitVirtualMethods(rs_code);
4837
if (model == Model::kRefCount) {
@@ -86,26 +75,6 @@ std::string TranspileDir(std::string_view build_dir, Model model,
8675
return result;
8776
});
8877

89-
std::deque<std::string> mapped;
90-
for (auto language : {RulesLanguage::kC, RulesLanguage::kCxx}) {
91-
Tool.mapVirtualFile(mapped.emplace_back(GetRulesEpiloguePath(language)),
92-
mapped.emplace_back(BuildRulesEpilogue(language)));
93-
}
94-
for (const auto &compile_command : compile_dbase->getAllCompileCommands()) {
95-
std::filesystem::path file_path(compile_command.Filename);
96-
if (file_path.is_relative()) {
97-
file_path = std::filesystem::path(compile_command.Directory) / file_path;
98-
}
99-
std::ifstream file(file_path);
100-
std::string code{std::istreambuf_iterator<char>(file),
101-
std::istreambuf_iterator<char>()};
102-
Tool.mapVirtualFile(
103-
mapped.emplace_back(file_path.string()),
104-
mapped.emplace_back(std::format(
105-
"{}\n#include \"{}\"\n", code,
106-
GetRulesEpiloguePath(GetRulesLanguage(file_path.string())))));
107-
}
108-
10978
std::string rs_code;
11079
FrontendActionFactory factory(rs_code, model, rules_dir);
11180
Tool.run(&factory);

‎cpp2rust/frontend_action.h‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include <clang/Frontend/FrontendAction.h>
99
#include <clang/Lex/PPCallbacks.h>
1010
#include <clang/Lex/Preprocessor.h>
11+
#include <clang/Lex/PreprocessorOptions.h>
1112
#include <clang/Tooling/Tooling.h>
13+
#include <llvm/Support/MemoryBuffer.h>
1214

1315
#include <memory>
1416
#include <string>
1517

1618
#include "ast_consumer.h"
1719
#include "converter/factory.h"
18-
#include "converter/rules/rules_epilogue.h"
1920

2021
namespace cpp2rust {
2122
class SilenceRulesDiagnostics : public clang::PPCallbacks {
@@ -29,13 +30,13 @@ class SilenceRulesDiagnostics : public clang::PPCallbacks {
2930
auto &src_mgr = CI_.getSourceManager();
3031
if (Reason == LexedFileChangeReason::EnterFile) {
3132
if (auto file = src_mgr.getFileEntryRefForID(FID);
32-
file && IsRulesEpilogue(file->getName())) {
33+
file && file->getName() == RULES_EPILOGUE_PATH) {
3334
CI_.getDiagnostics().setSuppressAllDiagnostics(true);
3435
}
3536
}
3637
if (Reason == LexedFileChangeReason::ExitFile) {
3738
if (auto file = src_mgr.getFileEntryRefForID(PrevFID);
38-
file && IsRulesEpilogue(file->getName())) {
39+
file && file->getName() == RULES_EPILOGUE_PATH) {
3940
CI_.getDiagnostics().setSuppressAllDiagnostics(false);
4041
}
4142
}
@@ -59,6 +60,21 @@ class FrontendAction : public clang::ASTFrontendAction {
5960
rules_dir_);
6061
}
6162

63+
bool BeginInvocation(clang::CompilerInstance &CI) override {
64+
for (const auto &input : CI.getFrontendOpts().Inputs) {
65+
auto path = input.getFile();
66+
auto buffer = CI.getFileManager().getBufferForFile(path);
67+
if (!buffer) {
68+
continue;
69+
}
70+
auto code = (*buffer)->getBuffer().str() + "\n#include \"" +
71+
RULES_EPILOGUE_PATH + "\"\n";
72+
CI.getPreprocessorOpts().addRemappedFile(
73+
path, llvm::MemoryBuffer::getMemBufferCopy(code, path).release());
74+
}
75+
return true;
76+
}
77+
6278
bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
6379
CI.getPreprocessor().addPPCallbacks(
6480
std::make_unique<SilenceRulesDiagnostics>(CI));

0 commit comments

Comments
 (0)