Multiple directories under CMake

Viewed 43718

I'm currently using recursive make and autotools and am looking to migrate to CMake for a project that looks something like this:

lx/ (project root)
    src/
        lx.c (contains main method)
        conf.c
        util/
            str.c
            str.h
            etc.c
            etc.h
        server/
            server.c
            server.h
            request.c
            request.h
        js/
            js.c
            js.h
            interp.c
            interp.h
    bin/
        lx (executable)

How should I go about this?

2 Answers

Steinberg VST3 library has a reusable method which recursively loops through subdirectories and adds them if they include a CMakeLists.txt file:

# add every sub directory of the current source dir if it contains a CMakeLists.txt
function(smtg_add_subdirectories)
    file(GLOB subDirectories RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
    foreach(dir ${subDirectories})
        if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
            if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}/CMakeLists.txt")
                add_subdirectory(${dir})
            endif()
        endif()
    endforeach(dir)
endfunction()

https://github.com/steinbergmedia/vst3_cmake/blob/master/modules/SMTG_AddSubDirectories.cmake

Related