[CMake] Custom object files for intermediate build products

Michael Hertling mhertling at online.de
Thu Jun 24 05:40:54 EDT 2010


On 06/24/2010 08:29 AM, Tom Birch wrote:
> Hi,
> 
> I'm trying to invoke the C compiler in cmake as would happen with an add_excecutable command, but I don't want to generate an object file. Basically I have a script which generates a c file, I want to compile that file in the environment I've set up, while passing the -S flag. I then want to run that .s file through another script to generate a header which will then be a dependency of my main executable. Doing the scripts is easy with add_custom_target, and I'm sure I could write all the magic to generate the full compile command line myself, but isn't there some elegant way to generate the a single compiler invocation that doesn't result in a dylib or a binary, that works in a similar way to add_excecutable?

If you are using a Makefile generator I could offer this approach:

Place the following file "cps" as executable in CMAKE_SOURCE_DIR:

#!/bin/sh
d=$1; shift
while [ "$1" != "--" ]; do
    cp $1 $d/$(basename $1 .o).s; shift
done

Now, look at the following CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CPS C)
FILE(WRITE f1.c "void f1(void){}\n")
FILE(WRITE f2.c "void f2(void){}\n")
FILE(WRITE f3.c "void f3(void){}\n")
ADD_EXECUTABLE(f f1.c f2.c f3.c)
SET_TARGET_PROPERTIES(f PROPERTIES
    COMPILE_FLAGS "-S"
    RULE_LAUNCH_LINK
    "${CMAKE_SOURCE_DIR}/cps ${CMAKE_BINARY_DIR} <OBJECTS> --"
)

This uses the above-mentioned "cps" as a launch script for linking the
target "f", but "cps" just copies the object files - delimited by "--"
- to the directory specified as the first argument, and of course, the
objects are in fact the assembler code files generated from the sources
by the "-S" flag which is imposed on the same target. Effectively, this
means you have a target producing ".s" files instead of an executable,
and everything else should be business as usual.

'hope that helps.

Regards,

Michael

P.S.: The source file property GENERATED is useful in this context.


More information about the CMake mailing list