##############################
##############################
##############################
#JSCORE
##############################
##############################
##############################

SET (CREATE_HASH_TABLE ${CMAKE_CURRENT_SOURCE_DIR}/JavaScriptCore/kjs/create_hash_table )

MACRO (CREATE_LUT _srcs_LIST _in_FILE _out_FILE _dep_FILE)
   GET_FILENAME_COMPONENT (_baseName ${_out_FILE} PATH)
   ADD_CUSTOM_COMMAND (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_out_FILE}
	COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${_baseName}
      COMMAND ${PERL_EXECUTABLE} ${CREATE_HASH_TABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${_in_FILE} -i > ${CMAKE_CURRENT_BINARY_DIR}/${_out_FILE}
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_in_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/${_dep_FILE} )
   SET ( ${_srcs_LIST}  ${${_srcs_LIST}} ${CMAKE_CURRENT_BINARY_DIR}/${_out_FILE})
ENDMACRO (CREATE_LUT)

#ADD_EXECUTABLE (dftables JavaScriptCore/pcre/dftables.c)
ADD_CUSTOM_COMMAND (OUTPUT ${dftables_executable}
   COMMAND gcc -o ${dftables_executable} -I./astronav/JavaScriptCore ./astronav/JavaScriptCore/pcre/dftables.c)

IF (CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 5)
	SET (CMAKE_CROSSCOMPILING OFF CACHE BOOL "Cross compile")
ENDIF (CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 5)
#IF(CMAKE_CROSSCOMPILING)
#	SET (dftables_executable "${CMAKE_CURRENT_BINARY_DIR}/../build/dftables" CACHE FILEPATH "Path to dftables")
#ELSE (CMAKE_CROSSCOMPILING)
#	GET_TARGET_PROPERTY (dftables_executable dftables LOCATION)
#ENDIF(CMAKE_CROSSCOMPILING)

ADD_CUSTOM_COMMAND (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs/chartables.c
   COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/generated_sources
   COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs
   COMMAND ${dftables_executable} ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs/chartables.c
   DEPENDS ${dftables_executable} )

IF (WEBKIT_USE_JS)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/array_object.cpp generated_sources/kjs/array_object.lut.h JavaScriptCore/kjs/array_object.cpp)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/date_object.cpp generated_sources/kjs/date_object.lut.h JavaScriptCore/kjs/date_object.cpp)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/keywords.table generated_sources/kjs/lexer.lut.h JavaScriptCore/kjs/lexer.cpp)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/math_object.cpp generated_sources/kjs/math_object.lut.h JavaScriptCore/kjs/math_object.cpp)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/number_object.cpp generated_sources/kjs/number_object.lut.h JavaScriptCore/kjs/number_object.cpp)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/regexp_object.cpp generated_sources/kjs/regexp_object.lut.h JavaScriptCore/kjs/regexp_object.cpp)
	CREATE_LUT (jscore_SRC JavaScriptCore/kjs/string_object.cpp generated_sources/kjs/string_object.lut.h JavaScriptCore/kjs/string_object.cpp)
ENDIF (WEBKIT_USE_JS)

ADD_CUSTOM_COMMAND (
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs/grammar.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs/grammar.h
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}

  COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/generated_sources
  COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs
  COMMAND ${BISON_EXECUTABLE} -d -p kjsyy ${CMAKE_CURRENT_SOURCE_DIR}/JavaScriptCore/kjs/grammar.y -o grammar.tab.c
  COMMAND mv grammar.tab.c ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs/grammar.cpp
  COMMAND mv grammar.tab.h ${CMAKE_CURRENT_BINARY_DIR}/generated_sources/kjs/grammar.h
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/JavaScriptCore/kjs/grammar.y
)

INCLUDE (JavaScriptCore/JscoreCMakeLists.txt)


