Skip to content

Commit a9f7b82

Browse files
authored
Support input files with --include-source (#165)
This PR updates the `--include-source` option to also work with input files and not just direct input.
1 parent e4d7317 commit a9f7b82

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/API/api.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "Frontend/OpenQASM3/OpenQASM3Frontend.h"
5757

5858
#include <filesystem>
59+
#include <fstream>
5960
#include <string_view>
6061
#include <utility>
6162

@@ -677,11 +678,28 @@ compile_(int argc, char const **argv, std::string *outputString,
677678

678679
if (emitAction == Action::GenQEM) {
679680

680-
if (includeSourceInPayload && directInput) {
681-
if (inputType == InputType::QASM)
682-
payload->addFile("manifest/input.qasm", inputSource + "\n");
683-
else if (inputType == InputType::MLIR)
684-
payload->addFile("manifest/input.mlir", inputSource + "\n");
681+
if (includeSourceInPayload) {
682+
if (directInput) {
683+
if (inputType == InputType::QASM)
684+
payload->addFile("manifest/input.qasm", inputSource + "\n");
685+
else if (inputType == InputType::MLIR)
686+
payload->addFile("manifest/input.mlir", inputSource + "\n");
687+
else
688+
llvm_unreachable("Unhandled input file type");
689+
} else { // just copy the input file
690+
std::ifstream fileStream(inputSource);
691+
std::stringstream fileSS;
692+
fileSS << fileStream.rdbuf();
693+
694+
if (inputType == InputType::QASM)
695+
payload->addFile("manifest/input.qasm", fileSS.str());
696+
else if (inputType == InputType::MLIR)
697+
payload->addFile("manifest/input.mlir", fileSS.str());
698+
else
699+
llvm_unreachable("Unhandled input file type");
700+
701+
fileStream.close();
702+
}
685703
}
686704

687705
if (auto err = generateQEM_(target, std::move(payload), moduleOp, ostream))

0 commit comments

Comments
 (0)