[CMake] shared directory with subdirs and custom command

Doug Reiland dreiland at gmail.com
Wed Jun 2 10:23:19 EDT 2010


I am porting a library over to cmake.
This library is built both shared and static AND has several composite
objects that get linked in.

For example,

subdir-a had makefile that compiled and linked a1.c a2.c into ../a.o

top directory linked in a.o into it's libs (shared and static)

I have converted most of this over to cmake by:

subdir-a's CMakeList.txt
    -  make a shared lib and top directory shared lib link in via
target_link_libraries
    -  add it's list of source files to a global property. top
directory CMakeList.txt looks for that property and adds those files
to its list for it's static library

This is cumbersome, but it is working ok.
However, I am now running to other stuff the subdirs are doing, like
custom compile flags and dependencies.

Question 1) Is the best method to handle these to get add more unique
(by source file name) properties in the global scope and have top
level CMakeList.txt look for them?

Question 2) How to handle custom targets at the subdir-level for the
static library? For example, I have:
a1.c which includes aa.c and ab.c
aa.c and ab.c are generated by a perl script

In subdir's CMakeList.txt I have, note add_sources, and
add_file_dependencies are not included here, but add the absolute
filenames to the global property for top-level library and
add_file_source_properties(). I have already managed to extended
add_file_dependencies() to add per-file global property where
top-level Makefile to look to see if it needs to set
source_file_properties, but I have no idea how to handle this
custom_command. The shared library build works fine.

set(name my-subdir-lib)
set(build-dir ${CMAKE_CURRENT_BINARY_DIR})
set(source-dir ${CMAKE_CURRENT_SOURCE_DIR})
set(generated-files aa.c ab.c)

foreach (g ${generated-files})
        add_custom_command(
                OUTPUT  ${build-dir}/${g}
                DEPENDS ${source-dir}/my_perl_script.pl
                        ${source-dir}/some_header_file.h
                COMMAND ${source-dir}/my_perl_script.pl
                ARGS    -i ${source-dir}/some_header_file.h -o
                        ${build-dir}/${g}
        )
        set(generated-results ${generated-results} ${build-dir}/${g})
endforeach()


set(sources
        a1.c a2.c
        a3.c
)

add_file_dependencies(a3.c ${generated-results})
add_sources(super ${sources})

include_directories(
        ${source-dir}
        ${build-dir}
)

add_library(${name} SHARED ${sources})


More information about the CMake mailing list