cmake: Capture stdout with UNIX pipes

0.54
Daniel Mensinger 5 years ago committed by Nirbheek Chauhan
parent b3832e632d
commit eea52a367a
  1. 26
      mesonbuild/cmake/data/run_ctgt.py
  2. 3
      test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt
  3. 10
      test cases/cmake/8 custom command/subprojects/cmMod/genMain.cpp

@ -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)

@ -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)

@ -1,4 +1,10 @@
#include <iostream>
using namespace std;
int main() {
cout << R"asd(
#include <iostream>
#include <fstream>
using namespace std;
@ -28,3 +34,7 @@ std::string getStr() {
return 0;
}
)asd";
return 0;
}
Loading…
Cancel
Save