[CMake] add_custom_target: Compiling a Matlab function into a binary with mcc

Nils Gladitz nilsgladitz at gmail.com
Sat Jul 26 08:06:19 EDT 2014


On 26.07.2014 12:18, Ramón Casero Cañas wrote:
>
> Dear all,
>
> I have a Matlab function vmu2png.m (this is the source code), and I'd 
> like to compile it into an executable/binary using a CMake 
> project (cmake 2.8.11.2 on linux).
>
> To compile the function, I need to run Matlab's mcc program, e.g. "mcc 
> -m vmu2png.m" from CMake. This will produce two files: run_vmu2png.sh 
> and vmu2png.
>
> So far, I have managed to do it with add_custom_command() and 
> add_custom_target(), but this makes the function compile every time I 
> use "make", regardless of whether vmu2png.m has been modified.
>
> My code is
>
>   # vmu2png
>   add_custom_command(
>     OUTPUT  run_vmu2png.sh vmu2png
>     COMMAND ${MATLAB_ROOT}/bin/mcc
>     ARGS    -m vmu2png.m
>     DEPENDS vmu2png.m
>     COMMENT "Compiling vmu2png.m into a binary"
>     )
>   add_custom_target(do_vmu2png ALL
>     DEPENDS run_vmu2png.sh vmu2png
>     SOURCES vmu2png.m
>     )
>
> If I remove ALL from add_custom_target(), then this compilation never 
> happens.
>
> Is there a way to integrate this kind of compilation nicely with 
> CMake? That is, I'd like it to happen only when run_vmu2png.sh or 
> vmu2png are missing, or when the source file has been modified.
>

Try using full paths for the OUTPUTs in the custom command and the 
DEPENDS in your custom target (e.g. 
"${CMAKE_CURRENT_BINARY_DIR}/run_vmu2png.sh").
The custom commands will keep rerunning when the actual outputs do not 
coincide with the declared outputs.

Nils


More information about the CMake mailing list