[CMake] Removing C-style comments from fortran include file?

Michael Hertling mhertling at online.de
Wed Oct 5 13:15:33 EDT 2011


On 10/03/2011 07:39 PM, John R. Cary wrote:
> I am using the cmakedefine mechanism for a fortran include file.
> 
> If the variable is not defined, rather than
> 
> /* #undef HAVE_PLASMASTATE */
> 
> 
> I need
> 
> ! #undef HAVE_PLASMASTATE
> 
> Are there existing solutions?

You might postprocess the already configured file:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(FORTRANIZE NONE)
SET(CMAKE_VERBOSE_MAKEFILE ON)

FUNCTION(FORTRANIZE file)
    UNSET(result)
    FILE(STRINGS "${file}" lines)
    FOREACH(i IN LISTS lines)
        STRING(REGEX REPLACE "^/\\* (#undef .*) \\*/$" "! \\1" i "${i}")
        SET(result "${result}${i}\n")
    ENDFOREACH()
    FILE(WRITE "${file}" "${result}")
ENDFUNCTION()

FILE(WRITE ${CMAKE_BINARY_DIR}/file.f.in
"#cmakedefine VAR1\n#cmakedefine VAR2
/* #undef DONTTOUCH */ \n")
SET(VAR1 1)
SET(VAR2 0)
CONFIGURE_FILE(${CMAKE_BINARY_DIR}/file.f.in ${CMAKE_BINARY_DIR}/file.f)
FORTRANIZE(${CMAKE_BINARY_DIR}/file.f)

However, this is possibly not totally bullet-proof when a line

/* #undef ... */

appears intentionally and shouldn't be touched. Since CONFIGURE_FILE()
throws away the rest of a #cmakedefine line if the variable evaluates
to 0, i.e. "#cmakedefine VAR /* TAG */" becomes "/* #undef VAR */",
the lines to be postprocessed can't be specially tagged. Instead,
the intentional lines could be silently tagged with a trailing
whitespace like in the above-noted example.

'hope that helps.

Regards,

Michael


More information about the CMake mailing list