Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call CMake from python script results in "Could not create named generator"

Tags:

python

cmake

I'm building a project that should be able to move freely between machines. In order to not have hundreds of MB of libs I'm writing a series of python scripts that download and build the dependencies as well as my own project. I'm using CMake to generate VS projects.

To call CMake I build a command line and I use python subprocess.check_call as below

cmakeCmd = ["cmake.exe", '-G "Visual Studio 11 Win64"', build_path]
retCode = subprocess.check_call(cmakeCmd, stderr=subprocess.STDOUT, shell=True)

The problem is that if I use -G option from CMake I get the following error regardless of the generator I choose:

CMake Error: Could not create named generator  "Visual Studio 11 Win64"

I was thinking that should be some enviroment variable missing but the python path is complete with all my systems' variables.

What is odd is that if I don't set the generator and let CMake choose the default one the script runs fine. I have no ideia why.

like image 687
McLeary Avatar asked Apr 18 '13 01:04

McLeary


2 Answers

You need to split the -G flag from its value in the command. Python appears to be effectively invoking

cmake "-G Visual Studio 11 Win64" <build_path>

What you want is more like:

cmakeCmd = ["cmake.exe", "-G", "Visual Studio 11 Win64", build_path]
like image 179
Fraser Avatar answered Nov 16 '22 05:11

Fraser


Should be:

cmakeCmd = ['cmake', '-G', 'Visual Studio 11 Win64', build_path]

For the sake of cross-platformness, I recommend to do the following:

if sys.platform.startswith('win'):
    shell = True
else:
    shell = False

retCode = subprocess.check_call(cmakeCmd, stderr=subprocess.STDOUT, shell=shell)

if you plan to work with the project on several platforms (including Unix OS family). By the way, since you are using shell=True on Windows, you can omit .exe suffix in the cmakeCmd (which is good if this script is used on Unix OS family too as there is no such thing as .exe).

like image 1
Alexander Shukaev Avatar answered Nov 16 '22 06:11

Alexander Shukaev