INCLUDE(CheckParent)
# search for CCCC binary
FIND_PROGRAM(CCCC cccc )

#
# check if the GENERATE_CCCC function has already been defined
#
get_property(_GENERATE_CCCC GLOBAL PROPERTY _GENERATE_CCCC)
IF (NOT _GENERATE_CCCC)

# set that we have defined GENERATE_CCCC
set_property(GLOBAL PROPERTY _GENERATE_CCCC "YES")


  FUNCTION(GENERATE_CCCC)
      IF(CCCC)
          CMAKE_PARSE_ARGUMENTS(ARG  "" "" "TARGETS" ${ARGN})
          get_property(_ccccfiles GLOBAL PROPERTY _ccccfiles)
          foreach(_target ${ARG_TARGETS})
            get_target_property(_sources ${_target} SOURCES)
            get_target_property(_source_dir ${_target} SOURCE_DIR)

            foreach(_source ${_sources})
              set(_fullsource "${_source_dir}/${_source}")
              list(APPEND _ccccfiles "${_fullsource}")
            endforeach()
      		endforeach()
          set_property(GLOBAL PROPERTY _ccccfiles ${_ccccfiles})
      ENDIF()
  ENDFUNCTION()

  FUNCTION(RESET_CCCC)
    set_property(GLOBAL PROPERTY _ccccfiles "")
  ENDFUNCTION()

  FUNCTION(GENERATE_CCCC_TARGET)
    IF (NOT hasParent AND CCCC)
      get_property(_targetccccfiles GLOBAL PROPERTY _ccccfiles)

      ADD_CUSTOM_TARGET(cccc
                      COMMAND ${CCCC} --outdir=cccc ${_targetccccfiles}
                        COMMENT "Generating cccc result")
    ENDIF()
  ENDFUNCTION()


ENDIF()
