[CMake] Re: Sharing code between projects

kitts kitts.mailinglists at gmail.com
Fri Aug 24 14:13:12 EDT 2007


On Friday 24 Aug 2007 9:41:26 pm James Bigler wrote:
> >> Code is organized as;
> >> src/common
> >> src/project1
> >> src/project2
> >>
> >> The CMakeLists.tst files are located inside for each project which sets
> >> the right compiler and sources to be built. Now i want common to be
> >> included in each of these proejects and build separately for each by
> >> inheriting all the settings for that project.
> >>
> >> What is the best way to achieve this? I cant use ADD_SUBDIRECTORY() as
> >> it is really a sibling folder (cmake complains if I use "../common").
> >
> > Does anybody have a solution around this? The only way i could think of
> > is to copy the common folder during configure time and run cmake again
> > inside it by using the current cache. This is not a very efficient way as
> > not all of the common folder code is necessary in every project. Also
> > code changes in the common directory will not result in an update in the
> > project.
>
> Have a look at LOAD_CACHE in the documentation.  This will allow you to
> load in cache variables from another cmake build.
>
> Also look at EXPORT_LIBRARY_DEPENDENCIES if you need to get out library
> dependencies of your project.  With that file you can simply IMPORT()
> said file in your new project and life gets happy again.  I've done this
> with projects that build static libraries, and I need to tell an
> external project the library dependencies.

I am now trying something like this:

if(NOT IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/common)
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/common)
    execute_process(COMMAND cmake ${CMAKE_SOURCE_DIR}/../common
                    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/common
                    RESULT_VARIABLE _ERROR
    )
    if(_ERROR)
        file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/common)
        message(FATAL_ERROR "Failed to configure shared code")
    endif(_ERROR)
endif(NOT IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/common)

I call make inside with common as the working directory with 
add_custom_command (PRE_BUILD).

As with the the LOAD_CACHE, is there such an option that can be passed when 
calling the cmake executable?

The other problem with the above approach is if there is a change in the 
project variables, the common code is not updated. I would want to merge 
common into the project in a way as though it were a subdirectory. Is this 
possible?
-- 
Cheers!
kitts


More information about the CMake mailing list