CMake and Pyproj's

Jan 16, 2012 at 10:40 PM

Hi,

I am trying to automate the process for my python projects, but I am running into some problems. Since CMake has no support for these types of projects I am trying to execute custom commands to create the python project and then load the appropriate files, but I cannot find any listing of commands in order to do this. Basically, is what I'm trying to do possible, and if so, how?

Thank you

Coordinator
Jan 16, 2012 at 11:19 PM

Could you be more specific about what you're trying to automate?  Are you trying to integrate them into your existing build process so the .py files get deployed, or do you need to automate creating new Python projects, or open the projects within Visual Studio, or something else?

The project files themselves are all msbuild projects so in general you'll want to map anything you want to do into terms of msbuild.  For example if you wanted to copy the files you could have CMake run msbuild.exe /t:SomeTargetName yourPythonProject.pyproj and then you could include a custom target in your .pyproj file such as:

<Target Name="SomeTargetName">
        <Copy
            SourceFiles="@(Content)"
            DestinationFolder="c:\MyProject\Destination"
        />
    </Target>

That'll copy all of the .py files listed in <Content> tags to the destination folder.

Note PTVS doesn't ever really do a "build" of your project so from inside of VS doing a build may not ever be too useful for the Python projects.  But presumably you already have some way to kick off CMake that I would think would be independent from VS. 

 

Jan 16, 2012 at 11:49 PM

I am trying to create the python project from the python files within a given folder. Basically, I have a src folder with all of the python files, and I want to just run cmake to make the pyproj file in a build folder, link it to the solution for all of the projects, and add all of the python files to that project. I am currently able to make a vcxproj with all of the python files included using add_custom_target and specifying the linker_language, but the project still isn't usable from that state.

Coordinator
Jan 18, 2012 at 8:30 PM

Sounds like the "New Project from Existing Code" wizard is what you're looking for. It appears under the File->New menu in Visual Studio: point it at the root of your Python project and it will find all the files and add them into a new .pyproj file.

Jan 18, 2012 at 8:48 PM

I'm sure that will work, but I'm trying to automate the entire process. I want cmake to create the pyproj and add it to the solution so the user simply needs to open the solution, and everything is already configured.

Coordinator
Jan 18, 2012 at 8:55 PM

In that case, you may need to petition cmake to add support for .pyproj files, since it appears to be their code generating it rather than anything in VS. The whole point of inventing a new project file type is because it's so different from the existing ones that they can't be substituted without manually modifying them.

Jan 18, 2012 at 9:07 PM

Exactly, I'm asking here because I was hoping there were some commands I could call from the command prompt to utilize the python tools plugin to make the python project because CMake can execute custom commands like that.

Coordinator
Jan 18, 2012 at 9:24 PM

The simplest approach here (besides including a ready-made .pyproj file) would be to write a script to create it. While it's possible to do it using the cmd.exe tools, a Python script is probably easier---presumably you require some version of Python to be installed already. All of PTVS is designed to interact with VS, so it uses their interfaces. You'd need a .NET or COM-based application to directly use PTVS, and there's no way I would endorse that.

If you start from a template (create an empty project and copy the text out of that), you only really need to specify a new GUID for ProjectGuid and add Folder and Compile tags for each directory and .py file. The InterpreterId and InterpreterVersion tags can be omitted to use the user's default interpreter, unless you want to require a particular interpreter (I'll need Dino to confirm, but I think CPython and IronPython will always have the same GUID regardless of version).

It's entirely possible that one of the other guys has a script floating around to do this already. If I had one I'd share it, but the reason I pushed for the New Project from Existing Code was to avoid writing the script.

Coordinator
Jan 18, 2012 at 9:30 PM

You're righton the GUIDs - there's 2 for CPython and 2 for IronPython, depending on x86 vs x64.  They are:

CPython x86 {2AF0F10D-7135-4994-9156-5D01C9C11B7E}

CPython x64 {9A7A9026-48C1-4688-9D5D-E5699D47D074}

IronPython x86 {80659AB7-4D53-4E0C-8588-A766116CBD46}

IronPython x64 {FCC291AA-427C-498C-A4D7-4502D6449B8C}

If you've setup a custom interpreter those will each have their own guids as well. 

Jan 18, 2012 at 9:45 PM

Thanks for all the help. The more I had looked into it, the more I figured I'd have to write a script, but I wanted to ask in case there was something I was missing.

May 8, 2012 at 3:56 AM

Have you managed to write that script?

I find myself in a similar if not the same situation, I'm starting a python project which I don't want to tie to VS thus .sln and .pyproj files are not versioned. I want developers to be able to use PTVS to aid developing on windows. I'm not sure if it's better to write a .bat or a CMakeLists to generate a .pyproj.

I'd appreciate if you'd share your solution.

May 8, 2012 at 6:05 PM

I did end up having to write a python script. CMake doesn't understand pyproj, as you'd expect, and the problem with CMake is that it doesn't generate the vcxproj until the very end, and the number of CMake layers in this project is getting a bit high. So, I made a python script that runs the CMake, and then another that modifies the new solution and adds the pyproj. After it makes the complete project as though it's making a standard solution, the script reads in the vcxproj for the python project, and replaces that with a pyproj file information. Then it goes into the solution and modifies it to update the project information for the python project. The only issue is that when it loads you have to unload and reload the project in the solution because there's a bunch of minor modifications that occur when the solution updates from a pure C++ solution into a mixed solution.