|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include <scratchcpp/project.h> |
| 4 | +#include <filesystem> |
| 5 | +#include <fstream> |
| 6 | +#include <sstream> |
| 7 | +#include <iostream> |
| 8 | +#include <iomanip> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include "cppcodebasegenerator.h" |
| 12 | +#include "cppcodebuilderfactory.h" |
| 13 | +#include "cppcompilercontext.h" |
| 14 | +#include "../../compiler_p.h" |
| 15 | + |
| 16 | +using namespace libscratchcpp; |
| 17 | + |
| 18 | +namespace |
| 19 | +{ |
| 20 | + |
| 21 | +// Creates C++ code builders and keeps the compiler contexts to collect the generated code |
| 22 | +class RecordingFactory : public ICodeBuilderFactory |
| 23 | +{ |
| 24 | + public: |
| 25 | + std::shared_ptr<ICodeBuilder> create(CompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType) const override |
| 26 | + { |
| 27 | + return CppCodeBuilderFactory::instance()->create(ctx, procedurePrototype, codeType); |
| 28 | + } |
| 29 | + |
| 30 | + std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target) const override |
| 31 | + { |
| 32 | + auto ctx = CppCodeBuilderFactory::instance()->createCtx(engine, target); |
| 33 | + contexts.push_back(ctx); |
| 34 | + return ctx; |
| 35 | + } |
| 36 | + |
| 37 | + mutable std::vector<std::shared_ptr<CompilerContext>> contexts; |
| 38 | +}; |
| 39 | + |
| 40 | +bool writeFile(const std::filesystem::path &path, const std::string &content) |
| 41 | +{ |
| 42 | + std::ofstream file(path, std::ios::binary); |
| 43 | + |
| 44 | + if (!file.is_open()) { |
| 45 | + std::cerr << "error: failed to write " << path.string() << std::endl; |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + file << content; |
| 50 | + return true; |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace |
| 54 | + |
| 55 | +/*! |
| 56 | + * Compiles the given project using the C++ backend and writes a CMake project to outputDir: |
| 57 | + * - project.cpp - the generated code of all scripts |
| 58 | + * - project_data.cpp - the embedded project file |
| 59 | + * - main.cpp - loads and runs the embedded project |
| 60 | + * - CMakeLists.txt |
| 61 | + */ |
| 62 | +bool CppCodebaseGenerator::generate(const std::string &projectFile, const std::string &outputDir, const std::string &projectName) |
| 63 | +{ |
| 64 | + std::ifstream input(projectFile, std::ios::binary); |
| 65 | + |
| 66 | + if (!input.is_open()) { |
| 67 | + std::cerr << "error: failed to read " << projectFile << std::endl; |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + std::stringstream data; |
| 72 | + data << input.rdbuf(); |
| 73 | + |
| 74 | + RecordingFactory factory; |
| 75 | + ICodeBuilderFactory *oldFactory = CompilerPrivate::builderFactory(); |
| 76 | + CompilerPrivate::setBuilderFactory(&factory); |
| 77 | + |
| 78 | + std::stringstream code; |
| 79 | + bool ret; |
| 80 | + |
| 81 | + { |
| 82 | + Project project(projectFile); |
| 83 | + ret = project.load(); |
| 84 | + |
| 85 | + if (ret) { |
| 86 | + code << "// Generated by libscratchcpp, do not edit\n\n" << CppCompilerContext::prelude() << "\n"; |
| 87 | + |
| 88 | + for (auto ctx : factory.contexts) |
| 89 | + code << "\n" << static_cast<CppCompilerContext *>(ctx.get())->moduleSource(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + CompilerPrivate::setBuilderFactory(oldFactory); |
| 94 | + |
| 95 | + if (!ret) |
| 96 | + return false; |
| 97 | + |
| 98 | + std::filesystem::path dir(outputDir); |
| 99 | + std::error_code err; |
| 100 | + std::filesystem::create_directories(dir, err); |
| 101 | + |
| 102 | + return writeFile(dir / "project.cpp", code.str()) && writeFile(dir / "project_data.cpp", dataSource(data.str())) && writeFile(dir / "main.cpp", mainSource()) && |
| 103 | + writeFile(dir / "CMakeLists.txt", cmakeSource(projectName)); |
| 104 | +} |
| 105 | + |
| 106 | +std::string CppCodebaseGenerator::mainSource() |
| 107 | +{ |
| 108 | + return R"(// Generated by libscratchcpp, do not edit |
| 109 | +
|
| 110 | +#include <scratchcpp/project.h> |
| 111 | +#include <filesystem> |
| 112 | +#include <fstream> |
| 113 | +#include <string> |
| 114 | +
|
| 115 | +extern const unsigned char scratchcpp_project_data[]; |
| 116 | +extern const unsigned long long scratchcpp_project_size; |
| 117 | +
|
| 118 | +extern "C" void cpp_use_cpp_backend(); |
| 119 | +
|
| 120 | +int main(int argc, char **argv) |
| 121 | +{ |
| 122 | + // libscratchcpp loads projects from files, so the embedded project is extracted first |
| 123 | + std::filesystem::path path = std::filesystem::temp_directory_path() / ("scratchcpp_project_" + std::to_string(std::hash<std::string>()(argv[0])) + ".sb3"); |
| 124 | +
|
| 125 | + { |
| 126 | + std::ofstream file(path, std::ios::binary); |
| 127 | + file.write(reinterpret_cast<const char *>(scratchcpp_project_data), scratchcpp_project_size); |
| 128 | + } |
| 129 | +
|
| 130 | + // Scripts are compiled by the C++ backend which uses the code in project.cpp |
| 131 | + cpp_use_cpp_backend(); |
| 132 | +
|
| 133 | + libscratchcpp::Project project(path.string()); |
| 134 | + bool loaded = project.load(); |
| 135 | + std::error_code err; |
| 136 | + std::filesystem::remove(path, err); |
| 137 | +
|
| 138 | + if (!loaded) |
| 139 | + return 1; |
| 140 | +
|
| 141 | + project.run(); |
| 142 | + return 0; |
| 143 | +} |
| 144 | +)"; |
| 145 | +} |
| 146 | + |
| 147 | +std::string CppCodebaseGenerator::dataSource(const std::string &data) |
| 148 | +{ |
| 149 | + std::stringstream ss; |
| 150 | + ss << "// Generated by libscratchcpp, do not edit\n\n"; |
| 151 | + ss << "extern const unsigned long long scratchcpp_project_size = " << data.size() << "ULL;\n\n"; |
| 152 | + ss << "extern const unsigned char scratchcpp_project_data[] = {"; |
| 153 | + |
| 154 | + for (size_t i = 0; i < data.size(); i++) { |
| 155 | + if (i % 16 == 0) |
| 156 | + ss << "\n "; |
| 157 | + |
| 158 | + ss << " 0x" << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)data[i] << ","; |
| 159 | + } |
| 160 | + |
| 161 | + ss << std::dec << "\n 0x00\n};\n"; |
| 162 | + return ss.str(); |
| 163 | +} |
| 164 | + |
| 165 | +std::string CppCodebaseGenerator::cmakeSource(const std::string &projectName) |
| 166 | +{ |
| 167 | + std::string ret = R"(# Generated by libscratchcpp |
| 168 | +cmake_minimum_required(VERSION 3.14) |
| 169 | +project(@NAME@ LANGUAGES CXX) |
| 170 | +
|
| 171 | +set(CMAKE_CXX_STANDARD 17) |
| 172 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 173 | +
|
| 174 | +# Use either libscratchcpp sources (LIBSCRATCHCPP_SOURCE_DIR) or a prebuilt library (LIBSCRATCHCPP_INCLUDE_DIR and LIBSCRATCHCPP_LIBRARY) |
| 175 | +set(LIBSCRATCHCPP_SOURCE_DIR "" CACHE PATH "libscratchcpp source directory") |
| 176 | +set(LIBSCRATCHCPP_INCLUDE_DIR "" CACHE PATH "libscratchcpp include directory") |
| 177 | +set(LIBSCRATCHCPP_LIBRARY "" CACHE FILEPATH "libscratchcpp library") |
| 178 | +
|
| 179 | +add_executable(@NAME@ main.cpp project.cpp project_data.cpp) |
| 180 | +
|
| 181 | +if(LIBSCRATCHCPP_SOURCE_DIR) |
| 182 | + set(LIBSCRATCHCPP_BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE) |
| 183 | + add_subdirectory(${LIBSCRATCHCPP_SOURCE_DIR} libscratchcpp) |
| 184 | + target_link_libraries(@NAME@ PRIVATE scratchcpp) |
| 185 | +elseif(LIBSCRATCHCPP_INCLUDE_DIR AND LIBSCRATCHCPP_LIBRARY) |
| 186 | + target_include_directories(@NAME@ PRIVATE ${LIBSCRATCHCPP_INCLUDE_DIR}) |
| 187 | + target_link_libraries(@NAME@ PRIVATE ${LIBSCRATCHCPP_LIBRARY}) |
| 188 | +else() |
| 189 | + message(FATAL_ERROR "Set LIBSCRATCHCPP_SOURCE_DIR or LIBSCRATCHCPP_INCLUDE_DIR and LIBSCRATCHCPP_LIBRARY") |
| 190 | +endif() |
| 191 | +)"; |
| 192 | + std::string name = projectName; |
| 193 | + |
| 194 | + for (char &c : name) { |
| 195 | + if (!std::isalnum((unsigned char)c) && c != '_' && c != '-') |
| 196 | + c = '_'; |
| 197 | + } |
| 198 | + |
| 199 | + size_t pos; |
| 200 | + |
| 201 | + while ((pos = ret.find("@NAME@")) != std::string::npos) |
| 202 | + ret.replace(pos, 6, name); |
| 203 | + |
| 204 | + return ret; |
| 205 | +} |
0 commit comments