macro(ob_define VAR DEFAULT)
  if (NOT DEFINED ${VAR})
    set(${VAR} ${DEFAULT})
  endif()
endmacro()

function(ob_replace_in_file INFILE OUTFILE MATCH-STRING REPLACE-STRING)
  file(READ ${INFILE} CONTENT)
  string(REPLACE ${MATCH-STRING} ${REPLACE-STRING} NEW-CONTENT ${CONTENT})
  file(WRITE ${OUTFILE} ${NEW-CONTENT})
endfunction()

function(ob_set_subtarget target group)
  list(APPEND "${target}_cache_objects_" ${ARGN})
  set("${target}_cache_objects_" ${${target}_cache_objects_} PARENT_SCOPE)
  if (OB_ENALBE_UNITY)
    set(i 0)
    set(group_id 0)
    set(ob_sub_objects "")
    FOREACH(item ${ARGN})
      math(EXPR i "(${i} + 1) % ${OB_MAX_UNITY_BATCH_SIZE}")
      list(APPEND ob_sub_objects ${item})
      if (${i} EQUAL 0)
        set_source_files_properties(${ob_sub_objects} PROPERTIES UNITY_GROUP "${target}_${group}/${group_id}")
        math(EXPR group_id "${group_id} + 1")
        set(ob_sub_objects "")
      endif()
    ENDFOREACH(item)
    if (${i} GREATER 0)
      set_source_files_properties(${ob_sub_objects} PROPERTIES UNITY_GROUP "${target}_${group}/${group_id}")
    endif()
  endif()
endfunction()

set(unity_after [[
#ifdef USING_LOG_PREFIX
#undef USING_LOG_PREFIX
#endif
]])

function(ob_add_target target)
  if(ARGC EQUAL 1)
    add_library(${target} OBJECT "${${target}_cache_objects_}")
    if (OB_ENALBE_UNITY)
      set_target_properties(${target} PROPERTIES UNITY_BUILD ON)
      set_target_properties(${target} PROPERTIES UNITY_BUILD_CODE_AFTER_INCLUDE "${unity_after}")
      set_target_properties(${target} PROPERTIES UNITY_BUILD_MODE GROUP)
    endif()
  else()
    add_library(${target} OBJECT ${ARGN})
  endif()
endfunction()

function(disable_pch target)
  set_target_properties(${target} PROPERTIES DISABLE_PRECOMPILE_HEADERS ON)
endfunction()
