How to configure target or cmake command for C prefile?

I originally asked this question on the CMake mailing list: How do I configure target or command for a preliminary C file?

I am migrating the GNU Autotools- based build configuration to CMake, and I have to deal with C preprocessing to create the file.

The preprocessor input is an SQL file with preprocessor C directives used , such as #include "another.sql"etc.

Currently Makefileuses the following rule to create a simple file SQLas output:

myfile.sql: myfile.sql.in.c
    cpp -I../common $< | grep -v '^#' > $@

Thus, it myfile.sqlmeans one of the products of the assembly process, similar to shared libraries or executable files.

What CMake tools should be used to achieve the same effect?

I wonder if I should use add_custom_command, add_custom_targetor combine both.

Obviously, I'm looking for a portable solution that will work, at least with the GNU GCC and Visual Studio tools. I assume that I will have to define special commands for a particular platform, one for the preprocessor cpp, one for cl.exe /P.

Or, does CMake do any abstraction for the C preprocessor?

I looked through the archives, but I found only the preprocessing of fortran files or solutions based on the capabilities of make: make myfile.i So this is not quite what I am looking for.

UPDATE: , Petr Kmoch CMake .

+5
2

CMake 'make myfile.i, :

add_library(sql_cpp_target EXCLUDE_FROM_ALL myfile.sql.in.c)

make myfile.sql.in.c.i , CMAKE_C_FLAGS. , dir .

, make invocations add_custom_target(ALL ...), CMake .

CMAKE_MAKE_PROGRAM .

, cmake . ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target targetname .


add_custom_command(), . , .

+2

, Petr Kmoch .

, add_custom_command ( OUTPUT), .

:

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) , add_library add_executable, . CMake , .

  • , , add_custom_target

:

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

: Petr Kmoch

+7