MACRO(ADD_SUBDIRS)
  # 引数を指定すると、そのディレクトリを add_subdirectory から除外する
  FILE(GLOB sub_dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
  SET(exclude_list "${ARGN}")
  list(LENGTH exclude_list exclude_num)
  FOREACH(dir IN LISTS sub_dirs)
    IF (${exclude_num} GREATER 0)
      IF (dir IN_LIST exclude_list)
        continue()
      ENDIF()
    ENDIF()
    IF (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${dir})
      IF (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/CMakeLists.txt)
        add_subdirectory(${dir})
      ENDIF()
    ENDIF()
  ENDFOREACH()
ENDMACRO()

MACRO(ADD_SNIPETS)
  if (NOT "${module_name}")
    get_filename_component(module_name ${CMAKE_CURRENT_SOURCE_DIR} NAME_WE)
  endif()
  # CREATE_HALIDE_IMPL()
  FILE(GLOB file_list "${CMAKE_CURRENT_SOURCE_DIR}/[a-z]*.cpp")
  FOREACH(filename IN LISTS file_list)
    get_filename_component(basename ${filename} NAME_WE)
    add_executable(${module_name}_${basename} ${filename})
    target_link_libraries(${module_name}_${basename} ${LIBRARIES} ${EXTERNAL_LIBS})
  ENDFOREACH()
ENDMACRO()

MACRO(ADD_LIB)
  if (NOT "${module_name}")
    get_filename_component(module_name ${CMAKE_CURRENT_SOURCE_DIR} NAME_WE)
  endif()
  # CREATE_HALIDE_LIB(${module_name})
  FILE(GLOB file_list "${CMAKE_CURRENT_SOURCE_DIR}/[a-z]*.cpp")
  if (file_list)
    add_library(${module_name} ${file_list})
    set(LIBRARIES ${LIBRARIES} ${module_name} CACHE INTERNAL "")
    # message("${module_name} : ${file_list}")
  endif()
ENDMACRO()

MACRO(ADD_GTEST)
  if (NOT "${module_name}")
    get_filename_component(module_name ${CMAKE_CURRENT_SOURCE_DIR} NAME_WE)
  endif()

  file(GLOB file_list "${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp")
  foreach (fname IN LISTS file_list)
    get_filename_component(bname ${fname} NAME_WE)
    set(test_name "gtest_${module_name}_${bname}")
    add_executable(${test_name} ${fname})
    target_link_libraries(${test_name} ${LIBRARIES} ${EXTERNAL_LIBS})
    add_test(NAME ${test_name}
      COMMAND "$<TARGET_FILE:${test_name}>")
  endforeach()
ENDMACRO()
