file (WRITE  "lcmtypes.h" "/* AUTOGENERATED CMAKE FILE - DO NOT EDIT BY HAND!! */\n")
file (APPEND "lcmtypes.h" "#ifndef __LCMTYPES_H__\n")
file (APPEND "lcmtypes.h" "#define __LCMTYPES_H__\n")
file (APPEND "lcmtypes.h" "\n")

# emit c/c++ lcmtypes
set (LCM_SRC "")
foreach (PACKAGE ${LCM_PACKAGES})

  # round up .lcm definition files
  file (GLOB LCM_DEFS "${LCM_DEFS_DIR}/${PACKAGE}/*.lcm")

  foreach (LCM_DEF ${LCM_DEFS})

    string (REPLACE ".lcm" "" LCM_TYPE ${LCM_DEF})
    string (REPLACE "${LCM_DEFS_DIR}/${PACKAGE}/" "" LCM_TYPE ${LCM_TYPE})

    set (LCM_TYPE_C "${CMAKE_CURRENT_SOURCE_DIR}/${LCM_TYPE}.c")
    set (LCM_TYPE_H "${CMAKE_CURRENT_SOURCE_DIR}/${LCM_TYPE}.h")

    file (APPEND "lcmtypes.h" "#include \"${LCM_TYPE}.h\"\n")
    
    add_custom_command (
      OUTPUT ${LCM_TYPE_C} ${LCM_TYPE_H}
      COMMAND ${LCM_GEN} ${LCM_DEF} --lazy -c
      DEPENDS ${LCM_DEF}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      )

    set (LCM_SRC ${LCM_SRC} ${LCM_TYPE_C} ${LCM_TYPE_H})
    
  endforeach ()
endforeach () #foreach (PACKAGE ${LCM_PACKAGES})

file (APPEND "lcmtypes.h" "\n")
file (APPEND "lcmtypes.h" "#endif //__LCMTYPES_H__\n")
file (GLOB CLEAN "*.[ch]")
set (CLEAN ${CLEAN} ${CMAKE_CURRENT_SOURCE_DIR}/lcmtypes.h)
set_directory_properties (PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN}")

# build liblcmtypes
add_library (lcmtypes ${LCM_SRC})
target_link_libraries (lcmtypes ${LIB_LCM})
