[CMake] Executing python though CMake and linking libraries

Matthew Woehlke mwoehlke.floss at gmail.com
Mon Nov 28 12:27:38 EST 2016


On 2016-11-25 04:04, Kit Chambers wrote:
> I have a Cmake custom target which runs a python script:
> 
> add_custom_target(run
>                  COMMAND python myscript.py
>                   ) 
> 
> ...
> 
> To me it looks like my LD_LIBRARY_PATH and DYLD_LIBRARY_PATH are not being passed through CMake to Python so it cannot find all the necessary sub libraries. However, i cannot work out how to pass this information through.

As written, your custom target will be run with whatever environment
your build tool decides to use. *Probably* that will be the environment
when you actually run the build tool (make, ninja, etc.), but it's
possible some build too might monkey with the environment in which it
runs build commands. Also, requiring people building your software to
modify their local environment is probably not ideal.

You probably want to wrap your custom target's command with
`${CMAKE_COMMAND} -E env ...` to ensure it gets the necessary
environment. (Also, you should probably use `${PYTHON_EXECUTABLE}`
instead of `python`, and, similarly, note `${CMAKE_COMMAND}` instead of
`cmake`.)

(Personally, I wouldn't mind seeing CMake learn an `ENVIRONMENT` option
to add_custom_command / add_custom_target that would set up this
wrapping automatically.)

-- 
Matthew



More information about the CMake mailing list