[CMake] Re: Simple dependency scanner, aka GET_SOURCE_FILE_PROPERTY +INCLUDE_DIRECTORIES

Mathieu Malaterre mathieu.malaterre at gmail.com
Wed May 30 04:27:26 EDT 2007


After a night of sleep I decided not to use
GET_SOURCE_FILE_PROPERTY+LOCATION, but instead simply loop over all
INCLUDE_DIRECTORIES. The dep seems to be ok now.


MACRO(SWIG_GET_DEP SWIG_FILE DEP_LIST)
  # Read the swig file:
  FILE(READ ${SWIG_FILE} swig_file)
  # STRING MATCHALL does not work, thus do a line by line STRING MATCH
  STRING(REGEX REPLACE "\r?\n" ";" ENT "${swig_file}")

  SET( dep_list )
  FOREACH(line ${ENT})
    STRING(REGEX MATCH "^[ ]*[%|#]include.*$" out1 ${line})
    # REGEX REPLACE is ALWAYS called, thus producing garbage most of
the time. Need to couple it with
    # REPLACE MATCH
    STRING(REGEX REPLACE "^[ ]*[%|#]include[ \"]*([^\"]+)[ \"]*$"
"\\1" out ${line})
    IF( out1 )
      # discard any `%include swig_stuff.i` by only looking at header files:
      IF( out MATCHES ".*\\.h$" )
        GET_DIRECTORY_PROPERTY(cmake_include_directories INCLUDE_DIRECTORIES)
        FOREACH(dir ${cmake_include_directories})
          # append to the list any file that can be found in the
INCLUDE_DIRECTORIES
          IF(EXISTS ${dir}/${out})
            LIST(APPEND dep_list ${dir}/${out})
          ENDIF(EXISTS ${dir}/${out})
        ENDFOREACH(dir ${cmake_include_directories})
      ENDIF( out MATCHES ".*\\.h$" )
    ENDIF( out1 )
  ENDFOREACH(line)

  LIST(SORT dep_list)
  # I need `uniq`...
  SET(${DEP_LIST} ${dep_list})
ENDMACRO(SWIG_GET_DEP)

On 5/29/07, Mathieu Malaterre <mathieu.malaterre at gmail.com> wrote:
> Hello,
>
>   I have written a very simple dependencie scanner (*). It works for
> the limited usage that I have, except for one case. Let say my swig
> file is:
>
> %module FooBar
> %{
> #include "MyModule1.h"
> #include "MyModule2.h"
> #include "Python.h"
> %}
>
> Assuming the files MyModule1.h and MyModule2.h can be found by
> GET_SOURCE_FILE_PROPERTY, but Python.h cannot. Is there a way to tell
> GET_SOURCE_FILE_PROPERTY to use
> GET_DIRECTORY_PROPERTY#INCLUDE_DIRECTORIES to figure out where
> Python.h is ?
>
> Thanks
> -Mathieu
> Ps: No I cannot use swig -MM option
>
> (*)
> MACRO(SWIG_GET_DEP SWIG_FILE)
>   # Read the swig file:
>   FILE(READ ${SWIG_FILE} swig_file)
>   # STRING MATCHALL does not work, thus do a line by line STRING MATCH
>   STRING(REGEX REPLACE "\r?\n" ";" ENT "${swig_file}")
>
>   FOREACH(line ${ENT})
>     STRING(REGEX MATCH "^[ ]*[%|#]include.*$" out1 ${line})
>     # REGEX REPLACE is ALWAYS called, thus producing garbage most of
> the time. Need to couple it with
>     # REPLACE MATCH
>     STRING(REGEX REPLACE "^[ ]*[%|#]include[ \"]*([^\"]+)[ \"]*$"
> "\\1" out ${line})
>     IF( out1 )
>       IF( ${out} MATCHES ".*\\.h$" )
>         GET_SOURCE_FILE_PROPERTY(var ${out} LOCATION)
>         IF(EXISTS ${var} )
>           MESSAGE( "DEP: ${out}" )
>         ENDIF(EXISTS ${var} )
>       ENDIF( ${out} MATCHES ".*\\.h$" )
>     ENDIF( out1 )
>   ENDFOREACH(line)
> ENDMACRO(SWIG_GET_DEP)
>


-- 
Mathieu


More information about the CMake mailing list