[Cmake] howto add *.cpp sources

Sebastien BARRE sebastien . barre at kitware . com
Tue, 29 Jul 2003 17:43:38 -0400


At 7/29/2003 10:40 AM, Jan Woetzel wrote:

>Hi,
>
>is it possible to add all wildcard *.cpp matching files in a directory to 
>a variable?
>
>I want to add all *.cpp files to a variable SRC and all headers *.hh to 
>HEADER.

Possible with 1.6, but it is tricky, and was tested for Msdev compilers only.
I had to write 2 macros.

1) GET_SPECIAL_COMPILE_FLAGS_TO_FORCE_CPP
this macro takes one arg, a variable name, and in this arg will be written 
the flags needed for the compiler to force a source file to be compiled as 
C++. Then you can use that variable in SET_SOURCE_FILES_PROPERTIES to set 
the compile flag of a .cpp source file (see below).

2) FIX_MSDEV_EXTENSION_SUPPORT_IN_REGISTRY
Don't do this at home kids ! It takes one arg, a file extension. This is 
some heavy surgery, it actually patches your registery on Windows to have 
Msdev6 support that extension as a C++ file. Won't do anything if no 
Msdev6, or the registery has been patched already. It's kind of optional then.

Here is how to use the macros (for .cc files, but this is the same scheme 
for .cpp)

FIX_MSDEV_EXTENSION_SUPPORT_IN_REGISTRY(".cc")
GET_SPECIAL_COMPILE_FLAGS_TO_FORCE_CPP (SPECIAL_COMPILE_FLAGS)

FOREACH (sfile
   dcmhmod.cc
   dcstrmpl.cc
   )
   SET (SRCS ${SRCS}  ${sfile})
   IF (SPECIAL_COMPILE_FLAGS)
     SET_SOURCE_FILES_PROPERTIES (${sfile}
                                  COMPILE_FLAGS ${SPECIAL_COMPILE_FLAGS})
   ENDIF (SPECIAL_COMPILE_FLAGS)
ENDFOREACH (sfile)

now the macros:

# Check if we are running Microsoft compilers, in that case we will need
# to force some files (.cc, .cpp) to be compiled as C++ files using the -TP 
flag

MACRO (GET_SPECIAL_COMPILE_FLAGS_TO_FORCE_CPP VAR)
   GET_FILENAME_COMPONENT (
     CMAKE_PLATFORM_FILENAME_WE
     ${CMAKE_SYSTEM_AND_C_COMPILER_INFO_FILE}
     NAME_WE
   )
   IF (CMAKE_PLATFORM_FILENAME_WE MATCHES "Windows-cl")
     SET (${VAR} " -TP")
   ELSE (CMAKE_PLATFORM_FILENAME_WE MATCHES "Windows-cl")
     SET (${VAR} "")
   ENDIF (CMAKE_PLATFORM_FILENAME_WE MATCHES "Windows-cl")
ENDMACRO (GET_SPECIAL_COMPILE_FLAGS_TO_FORCE_CPP VAR)

# Fix the registery for MsDev6 so that .cc/.cpp files can be compiled

MACRO (FIX_MSDEV_EXTENSION_SUPPORT_IN_REGISTRY EXT)
   IF(CMAKE_GENERATOR MATCHES "Visual Studio 6")
     SET (REG_KEY 
"HKEY_CURRENT_USER\\Software\\Microsoft\\DevStudio\\6.0\\Build 
System\\Components\\Platforms\\Win32 (x86)\\Tools\\32-bit C/C++ Compiler 
for 80x86;Input_Spec")
     GET_FILENAME_COMPONENT (INPUT_SPEC "[${REG_KEY}]" NAME)
     IF ("${INPUT_SPEC}" MATCHES "^registry$")
     ELSE ("${INPUT_SPEC}" MATCHES "^registry$")
       IF ("${INPUT_SPEC}" MATCHES ";\\*\\${EXT}")
       ELSE ("${INPUT_SPEC}" MATCHES ";\\*\\${EXT}")
         MESSAGE("The registry key ${REG_KEY}\nhas been fixed so that Msdev 
can support the file extension ${EXT}")
         EXEC_PROGRAM(${CMAKE_COMMAND}
                     ARGS "-E write_regv \"${REG_KEY}\" 
\"${INPUT_SPEC};*${EXT}\""
         )
       ENDIF ("${INPUT_SPEC}" MATCHES ";\\*\\${EXT}")
     ENDIF ("${INPUT_SPEC}" MATCHES "^registry$")
   ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio 6")
ENDMACRO (FIX_MSDEV_EXTENSION_SUPPORT_IN_REGISTRY)



--
Sebastien Barre