I use CMake to create build scripts (Makefiles + VS solutions) for my projects. As best practice I create the build scripts in a separate folder (out of source). I build the projects in the same folder.
This works fine for compiled programs but I can’t find an adequate solution for my Python scripts as these have no build step that would copy (build) them to the build folder.
Looking for creative solutions….
Requirements:
- All executables should be available in the build folder post build (I consider *.py files to be executable
- Python scripts should be easily managed using an IDE (spyder, eclipse, etc)
- Source folder with python scripts is in Git repository. Build folder is not.
- C++ compiled python modules should reside next to relevant python scripts
So far I considered two options:
- Copy scripts to build folder when running CMake – Need to run CMake for every change in python files (IDE unfriendly). Can cause confusion: which copy of the sources to edit?
- Create links to source folder in build folder – Multi platform mess. Problem deploying compiled c++ python modules next to the scripts without polluting source folder.
I hope this is clear enough.
Eventually I found a solution which involves creating symbolic links to the python sources and other related files that are not compiled but are necessary in the build environment. To allow mixing built modules with the symbolic links I used real folders instead of symbolic links.
This way:
Utility function:
Usage for a python module (inside CMakeLists.txt):
Usage:
IMPORTANT:
cmakemklinksupport only some Windows versionsmklinkcan be run as Administrator only. A workaround can be found here.create_symlinksas this is how the directory structure is reconstructed in the binary folder.