[CMake] INCLUDE cannot find file

Eric Noulard eric.noulard at gmail.com
Tue Oct 2 07:25:36 EDT 2007


2007/10/2, Dizzy <dizzy at roedu.net>:
> On Tuesday 02 October 2007 13:18:15 Shiqing Fan wrote:
>
> > And some thing about why I use it. The project has a complicated file
> > structure. Take the above structure as an example, both sub1 ans sub2
> > have source files for generating a library on top level.
> > And on
> > different platforms, not all the files are necessary. Yes, I could use
> > SET(SOURCE ${SOURCE} file1 file2....) with relative path, but if the we
> > have 4 or 5 levels of subdirectory, this is going to be a nightmare,
> > because we have to manage all the source files on top level for
> > different platforms.
>
> But if the files are scattered randomly in various 4 level directories, some
> files that may need to be used on a platforms, others than dont, then you
> already have a nightmare and using SET() just shows the nightmare. Try to
> describe in more detail your project structure and also the simplest solution
> you can think of (even that it doesn't work directly with cmake like you
> think of it).

Having several level of directories from which you don't want to
ADD_SUBDIRECTORY
reveals a probably flawed source setup.

However you may easily do the following:

in toplevel/CMakeLists.txt

SET(LIB_SOURCES "")

INCLUDE(sub1/source_definitions.cmake)
SET(LIB_SOURCES "${LIB_SOURCES} ${SUB1_SOURCES}"

INCLUDE(sub1/sub2/source_definitions.cmake)
SET(LIB_SOURCES "${LIB_SOURCES} ${SUB1_SUB2_SOURCES}"

etc...
ADD_LIBRARY(toplib ${LIB_SOURCES})

SUBx_SUBy_SOURCES var is defined in INCLUDE(source_definitions.cmake)
as you already figured out SOURCES should be specified with as
relative to toplevel ie. subx/suby/<filename>

I think this would both clearer and fits your needs.
If you are not afraid of doing a bit of CMake MACRO
you should even be able to do MACROify with something like:

FILE(GLOB_RECURSE  TOBE_INCLUDED "source_def*.cmake")
FOREACH(SUBFILE ${TOBE_INCLUDED})
    GET_FILENAME_COMPONENT(DIRNAME ${SUBFILE} PATH)
    INCLUDE(${SUBFILE})
    /* find way to change DIRNAME=sub1/sub2/sub3 to
       SOURCE_PREFIX SUB1_SUB2_SUB3_SOURCES */
    SET(SOURCE ${SOURCE} ${${SOURCE_PREFIX}})
ENDFOREACH()


-- 
Erk


More information about the CMake mailing list