From 1be660ff64f2cb69cac0376ae57f65908b34238b Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Sun, 12 Apr 2020 15:11:54 +0200 Subject: cmake: Capture stdout with UNIX pipes --- mesonbuild/cmake/data/run_ctgt.py | 26 +++++++++++++- .../subprojects/cmMod/CMakeLists.txt | 3 ++ .../8 custom command/subprojects/cmMod/genMain.cpp | 40 ++++++++++++++++++++++ .../8 custom command/subprojects/cmMod/main.cpp | 30 ---------------- 4 files changed, 68 insertions(+), 31 deletions(-) create mode 100644 test cases/cmake/8 custom command/subprojects/cmMod/genMain.cpp delete mode 100644 test cases/cmake/8 custom command/subprojects/cmMod/main.cpp diff --git a/mesonbuild/cmake/data/run_ctgt.py b/mesonbuild/cmake/data/run_ctgt.py index 1897a1b25..9d5d43704 100755 --- a/mesonbuild/cmake/data/run_ctgt.py +++ b/mesonbuild/cmake/data/run_ctgt.py @@ -5,6 +5,7 @@ import subprocess import shutil import os import sys +from pathlib import Path commands = [[]] SEPARATOR = ';;;' @@ -40,9 +41,32 @@ for i in commands: if not i: continue + cmd = [] + stdout = None + stderr = None + capture_file = '' + + for j in i: + if j in ['>', '>>']: + stdout = subprocess.PIPE + continue + elif j in ['&>', '&>>']: + stdout = subprocess.PIPE + stderr = subprocess.STDOUT + continue + + if stdout is not None or stderr is not None: + capture_file += j + else: + cmd += [j] + try: os.makedirs(args.directory, exist_ok=True) - subprocess.run(i, cwd=args.directory, check=True) + + res = subprocess.run(cmd, stdout=stdout, stderr=stderr, cwd=args.directory, check=True) + if capture_file: + out_file = Path(args.directory) / capture_file + out_file.write_bytes(res.stdout) except subprocess.CalledProcessError: exit(1) diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt index 5c75e65b3..1498c3607 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt +++ b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt @@ -7,6 +7,9 @@ set (CMAKE_CXX_STANDARD_REQUIRED ON) include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_definitions("-DDO_NOTHING_JUST_A_FLAG=1") +add_executable(genMain genMain.cpp) +add_custom_command(OUTPUT main.cpp COMMAND genMain > main.cpp) + add_executable(gen main.cpp) add_executable(mycpy cp.cpp) diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/genMain.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/genMain.cpp new file mode 100644 index 000000000..33f020159 --- /dev/null +++ b/test cases/cmake/8 custom command/subprojects/cmMod/genMain.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() { + cout << R"asd( +#include +#include + +using namespace std; + +int main(int argc, const char *argv[]) { + if(argc < 2) { + cerr << argv[0] << " requires an output file!" << endl; + return 1; + } + ofstream out1(string(argv[1]) + ".hpp"); + ofstream out2(string(argv[1]) + ".cpp"); + out1 << R"( +#pragma once + +#include + +std::string getStr(); +)"; + + out2 << R"( +#include ")" << argv[1] << R"(.hpp" + +std::string getStr() { + return "Hello World"; +} +)"; + + return 0; +} +)asd"; + + return 0; +} diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/main.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/main.cpp deleted file mode 100644 index 9fade211d..000000000 --- a/test cases/cmake/8 custom command/subprojects/cmMod/main.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -using namespace std; - -int main(int argc, const char *argv[]) { - if(argc < 2) { - cerr << argv[0] << " requires an output file!" << endl; - return 1; - } - ofstream out1(string(argv[1]) + ".hpp"); - ofstream out2(string(argv[1]) + ".cpp"); - out1 << R"( -#pragma once - -#include - -std::string getStr(); -)"; - - out2 << R"( -#include ")" << argv[1] << R"(.hpp" - -std::string getStr() { - return "Hello World"; -} -)"; - - return 0; -} -- cgit v1.2.3