0

I have the following Python script.

Code

#!/usr/bin/env python3

import os

def Build(cmake_args):
    cmake cmake_args  # How to call cmake here by passing in the arguments?
    make install  # How to call make install here from within Python?

os.mkdir('build')
os.chdir('build')

# Call method Build by passing in all correct arguments
Build('-DCMAKE_INSTALL_PREFIX="./install" -DCMAKE_PREFIX_PATH="$PWD/../packages" ../SourceCodeFolder')

Question:
How do I call cmake by passing in all Cmake arguments that my CMakeLists.txt understands? And then how do I call make install?

Environment:
I am running Python 3.9.0 on macOS Catalina.

PS:
I have ensured that mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX="./install" -DCMAKE_PREFIX_PATH="$PWD/../packages" ../SourceCodeFolder succeeds when run from the bash shell. So, its only a question of how to call cmake and make from my Python script.

Edit:
I want to run my script on Macos as well as Windows. This is the reason for running cmake and make from a Python script.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • I believe you could explain what you are trying to do with cmake, I think you're trying to automate some compiling tasks by using python, and I think that using python is not the most efficient solution – Be Chiller Too May 26 '21 at 16:12

1 Answers1

1

You can use subprocess.run(["command", "-arg"])

(from https://docs.python.org/3.8/library/subprocess.html#subprocess.run)

Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42