[CMake] How to configure target or command to preprocess C file?

Petr Kmoch petr.kmoch at gmail.com
Mon Jul 9 03:17:33 EDT 2012


Hi Mateusz,

I am not aware of direct cmake support for preprocessing a file, so I
will only comment on the add_custom_*() part.

You have to create a custom command (using the OUTPUT signature) to
actually do the preprocessing. If you're targetting gcc and Visual
Studio, the command could look something like this:

add_custom_command(
  OUTPUT myfile.sql
  COMMAND "${CMAKE_C_COMPILER}" -E myfile.sql.in -I ../common
  MAIN_DEPENDENCY myfile.sql.in
  COMMENT "Preprocessing myfile.sql.in"
  VERBATIM
)

(myfile.sql and myfile.sql.in may need to be specified with paths)

Now you need something to "drive" this command. If the resulting
myfile.sql is used in another build step (such as given as a source
file to add_library/add_executable), you just need to specify it
there, cmake will find the dependency and run the custom command as
required.

On the other hand, if myfile.sql is a final output and processed no
further, you need to add a custom target to drive the command, like
this:

add_custom_target(
  ProcessSQL ALL
  DEPENDS myfile.sql
  COMMENT "Preprocessing SQL files"
  VERBATIM
)

This will add a makefile target/VS project into your buildsystem, the
build of which will trigger the custom command added earlier. If you
have more than one sql file to preprocess, add a custom command for
each and then list all the resulting files in the DEPENDS section of
the one custom target.

Please check the docs on add_custom_*() to make sure all the options
suit you needs.

Hope this helps,

Petr

On Sun, Jul 8, 2012 at 2:34 PM, Mateusz Loskot <mateusz at loskot.net> wrote:
> Hi,
>
> I'm porting build configuration based on GNU Autotools to CMake
> and I have to deal with C preprocessing to generate a file.
>
> The input for preprocessor is SQL file with C preprocessor directives
> used, like #include "another.sql", etc.
>
> Currently, Makefile uses the following rule to generate plain SQL file
> as output:
>
> myfile.sql: myfile.sql.in.c
>     cpp -I../common $< | grep -v '^#' > $@
>
> So, the myfile.sql is meant to be one of products of the build process,
> similar to share libraries or executables.
>
> What CMake tools should I use to achieve the same effect?
>
> It's unclear to me if I should use add_custom_command,
> add_custom_target or combine both.
>
> Obviously, I'm looking for a portable solution that would work at least
> with GNU GCC and Visual Studio toolsets. I presume I will have to define
> platform-specific custom commands, one for cpp preprocessor, one for cl.exe.
> Or, does CMake provide some kind of abstraction for C-preprocessing?
>
> I scanned the archives, but I only found preprocessing of fortran files
> or solutions based on make capabilities (make myfile.i).
> So, it's not quite what I'm looking for.
>
> Could anyone help me with this?
>
> Best regards,
> --
> Mateusz Loskot, http://mateusz.loskot.net
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list