0

I'm trying to use subprocess.run to build my CMake project but the code finishing without errors but its not working.

the code is:

import subprocess

git_repo_remvoe_destination = '/home/yaodav/Desktop/'
git_repo_clone_destination = git_repo_remvoe_destination + 'test_clone_git'
test_path = git_repo_clone_destination + "/test/"
cmake_debug_path = test_path + "cmake-debug-build"
cmake_build_command = " cmake -Bcmake-build-debug -H. -DCMAKE_BUILD_TYPE=debug -DCMAKE_C_COMPILER=/usr/local/bin/gcc " \
                      "-DCMAKE_CXX_COMPILER=/usr/local/bin/c++ -Bcmake-build-debug -H. " \
                      "-DSYTEM_ARCH=x86_64-pc-linux-gnu-gcc-7.4.0"
cmake_link_command = "cmake --build cmake-build-debug --target all"

cmake_command = ['cd '+test_path, cmake_build_command, cmake_link_command]

out = subprocess.run(cmake_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

I tried this answer but it didn't work

how to I do that?

yaodav
  • 1,126
  • 12
  • 34

1 Answers1

1

2 Issues.

First you should call subprocess.run() once for each command instead of putting three different commands in a list.

Second: The cd ... command just changes the present working directory in one sub process and the consecutive command will not be any more in the same directory.

However there is a simple solution to it.

subprocess.run has a cwd parameter ( https://docs.python.org/2/library/subprocess.html#popen-constructor ) that allows you to specify the directory in which a subprocess should be executed.

So Following should do:

out = subprocess.run(cmake_build_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=test_path, shell=True)
out += subprocess.run(cmake_link_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=test_path, shell=True)
gelonida
  • 5,327
  • 2
  • 23
  • 41