Cristopher Sosa
Cristopher Sosa

Reputation: 167

CMake exclude files from a given pattern after file(GLOB_RECURSE)

I have an little library which has an file structure like this:

CMakeLists.txt
LibFoo/
  Foo.h
  FooWin.cpp
  FooWin.inl
  FooPosix.cpp
  FooPosix.inl

And when i have to build the library in a specific OS (for example Windows) they should contain in the list before using file(GLOB_RECURSE) using a macro:

macro( add_recursive dir retVal)
file( GLOB_RECURSE ${retVal} ${dir}/*.h ${dir}/*.cpp ${dir}/*.c ${dir}/*.inl )
endmacro()

and my excluding pattern is like this (when Windows is the build host): *Posix.* but doesn't work, i tried with this approach:

macro( add_recursive dir retVal pattern)
file( GLOB_RECURSE ${retVal} ${dir}/*.h ${dir}/*.cpp ${dir}/*.c ${dir}/*.inl EXCLUDE PATTERN "${pattern}")
endmacro()

but the POSIX files still here anyways, CMake doesn't report an error or anything suggested about that.

Upvotes: 3

Views: 10001

Answers (3)

user20039963
user20039963

Reputation: 21

This piece of code almost worked for me. cmake nags about the set not having enough arguments and discontinues the macro. This snippet also makes the extention and exclude filter variable

macro( add_recursive retVal dir ext excl)
    # Store unfiltered list of files into temporary list
    file( GLOB_RECURSE _tmp_list ${dir}/${ext})
    # Resulted list is initially empty. Copy all needed elements into it.
    set(${retval} "")
    foreach(f ${_tmp_list})
        if(NOT f MATCHES ${excl})
            list(APPEND ${retVal} ${f})
        endif()
    endforeach()
endmacro( add_recursive )

#example of usage
add_recursive(inc "./" "*.h" "/exclude_folder/")

Upvotes: 2

Zitrax
Zitrax

Reputation: 20255

You can use list filtering to remove items after the GLOB_RECURSE:

list(FILTER ${retVal} EXCLUDE REGEX "Posix.")

Upvotes: 9

Tsyvarev
Tsyvarev

Reputation: 65928

There is no EXCLUDE option for file(GLOB_RECURSE ...) command flow. You probably take this option from file(COPY|INSTALL ...), which is a different command flow.

You may iterate over list, obtained from file(GLOB_RECURSE) and exclude needed files manually:

macro( add_recursive dir retVal)
    # Store unfiltered list of files into temporary list
    file( GLOB_RECURSE _tmp_list ${dir}/*.h ${dir}/*.cpp ${dir}/*.c ${dir}/*.inl )
    # Resulted list is initially empty. Copy all needed elements into it.
    set(${retval})
    foreach(f ${_tmp_list})
        if(NOT f MATCHES "Posix.")
            list(APPEND ${retVal} ${f})
        endif()
    endforeach()

endmacro()

Upvotes: 3

Related Questions