[CMake] static library from several subdirectories

Verweij, Arjen VerweijA at tass-safe.com
Fri Mar 19 08:58:00 EDT 2010


Hi,

>-----Original Message-----
>From: Michael Wild [mailto:themiwi at gmail.com]


>I'd recommend to change the add_sources function to do all the
>preprocessing and then only add the products (.f, .f90, .c etc.) files
>to the list. You can do the preprocessing like this (of course, you'll
>have to change things for your setup, perhaps even do different things
>depending on filename extension etc.)
>
>find_program(FPP_EXECUTABLE fpp)
>if(NOT FPP_EXECUTABLE)
>  message(SEND_ERROR "Failed to find fpp")
>endif()
>
>set(SRCS)
>foreach(src IN LISTS ARGN)
>  get_filename_component(abs_src "${src}" ABSOLUTE)
>  file(RELATIVE_PATH rel_src "${CMAKE_CURRENT_SOURCE_DIR}" "${abs_src}")
>  set(pre "${CMAKE_CURRENT_BINARY_DIR}/${rel_src}")
>  add_custom_command(OUTPUT "${pre}"
>    COMMAND ${FPP_EXECUTABLE} "${abs_src}" -o "${pre}"
>    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
>    COMMENT "Creating ${pre}"
>    VERBATIM
>    )
>  list(APPEND SRCS "${pre}")
>endforeach()
>set_property(GLOBAL APPEND PROPERTY "${target}_SRCS" "${SRCS}")

OK, so I finally found some time to implement AND email the results. I tried it a little differently, then I tried Michael's approach. Both seem to fail, since at the moment I call add_library() my source file is unavailable. I hoped it would resolve the source file to a dependency through the custom command, but it doesn't and I can't get cmake to execute the conversion inline.

What am I missing? I'm trying again with my simple a/liba.c and b/b/libb.c setup, but this time I start out with liba.cr and libb.cr that need to be converted to liba.c and libb.c.

function(add_sources target)
  # define the <target>_SRCS properties if necessary
  get_property(prop_defined GLOBAL PROPERTY ${target}_SRCS DEFINED)
  if(NOT prop_defined)
    define_property(GLOBAL PROPERTY ${target}_SRCS
      BRIEF_DOCS "Sources for the ${target} target"
      FULL_DOCS "List of source files for the ${target} target")
  endif()
  # create list of sources (absolute paths)
  set(SRCS)
  foreach(src IN LISTS ARGN)
    string (REGEX REPLACE "^(.+)\\.cr$" "\\1" BASECR ${src} )
    string (COMPARE EQUAL ${src} ${BASECR}.cr FILE_IS_C)
    if(NOT IS_ABSOLUTE "${src}")
      get_filename_component(path_to_src "${src}" ABSOLUTE )
      get_filename_component(path_to_src "${path_to_src}" PATH )
      get_filename_component(src "${src}" ABSOLUTE)
    endif()
    add_custom_command(
      OUTPUT "${path_to_src}/${BASECR}.c"
      COMMAND cat "${path_to_src}/${BASECR}.cr" > "${path_to_src}/${BASECR}.c"
      WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
      COMMENT "Creating ${path_to_src}/${BASECR}.c"
      VERBATIM
      )

    #add_custom_command (
    #  OUTPUT ${path_to_src}/${BASECR}.c
    #  COMMAND cat ${path_to_src}/${BASECR}.cr > ${path_to_src}/${BASECR}.c
    #  DEPENDS ${path_to_src}/${BASECR}.cr
    #  )
    list(APPEND SRCS "${path_to_src}/${BASECR}.c")
  endforeach()
  # append to global property
  set_property(GLOBAL APPEND PROPERTY "${target}_SRCS" "${SRCS}")
endfunction()


More information about the CMake mailing list