0

I want to populate a variable, MY_VARIABLE, in cmake with a list of all the /foo subdirectories containing a Makefile. The problem is, all the /foo subdirectories are of different depth from where my CMakeLists is located at, and I don't want any subdirectories not containing a Makefile. I also just want the name of the directory.

For example, if $(BUILD_DIR)/A/B/foo/ contained a makefile:

$(BUILD_DIR)/A/B/foo/ would be good

$(BUILD_DIR)/A/B/foo/Makefile would be bad

Tinkering around, I know I can do something like this with the UNIX shell:

find $(BUILD_DIR) -name 'Makefile' | grep 'foo/' | sed 's/\/Makefile//'

But I don't know how to invoke that in CMake, or if there's another way. I tried

execute_process(COMMAND find $(BUILD_DIR) -name 'Makefile' | grep 'foo/' | sed 's/\/Makefile\//' 
                OUTPUT_VARIABLE MY_VARIABLE
               )

only to be met with parsing errors.

Any help or advice would be appreciated.

Alter R.
  • 3
  • 2
  • you can do [string manipulation in CMake too](https://cmake.org/cmake/help/v3.0/command/string.html), no need to invoke grep, sed nor anything externally – brunocodutra Mar 01 '16 at 19:05

1 Answers1

0

As @brunocodutra mentioned, no need to call the external find()/grep()/sed() commands:

cmake_minimum_required(VERSION 2.8)

project(SearchFooMakefile CXX)

file(
    GLOB_RECURSE _makefileDirs 
    RELATIVE     "${CMAKE_CURRENT_SOURCE_DIR}"
    "Makefile"
)

foreach(_makefileDir IN LISTS _makefileDirs)
    get_filename_component(_dir "${_makefileDir}" PATH)
    get_filename_component(_last_dir "${_dir}" NAME)
    if ("${_last_dir}" STREQUAL "foo")
        list(APPEND MY_VARIABLE "${_dir}")
    endif()
endforeach()

message("${MY_VARIABLE}")

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149