########################################
# BEGIN_COPYRIGHT
#
# Copyright (C) 2008-2018 SciDB, Inc.
# All Rights Reserved.
#
# SciDB is free software: you can redistribute it and/or modify
# it under the terms of the AFFERO GNU General Public License as published by
# the Free Software Foundation.
#
# SciDB is distributed "AS-IS" AND WITHOUT ANY WARRANTY OF ANY KIND,
# INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
# NON-INFRINGEMENT, OR FITNESS FOR A PARTICULAR PURPOSE. See
# the AFFERO GNU General Public License for the complete license terms.
#
# You should have received a copy of the AFFERO GNU General Public License
# along with SciDB.  If not, see <http://www.gnu.org/licenses/agpl-3.0.html>
#
# END_COPYRIGHT
########################################

bison(iquery_parser parser.yy ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
flex(iquery_scanner scanner.ll ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp)
add_flex_bison_dependency(iquery_scanner iquery_parser)


#
# HACKS BEGINS HERE
#

# Fixing isatty declaration in source file generated by FLEX on linux.
# Related bug: http://sourceforge.net/tracker/index.php?func=detail&aid=1984987&group_id=97492&atid=618177

set(iquery_scanner_src ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp)

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
	set(iquery_scanner_fixed_src ${CMAKE_CURRENT_BINARY_DIR}/scanner.fixed.cpp)
	add_custom_command(
		OUTPUT ${iquery_scanner_fixed_src}
		COMMAND ${SED_EXECUTABLE} 's/isatty \(int \)\;/isatty \(int \) __THROW\;/' ${iquery_scanner_src} > ${iquery_scanner_fixed_src}
		DEPENDS ${iquery_scanner_src}
		COMMENT "Fixing generated FLEX source. During building file '${iquery_scanner_fixed_src}' will be used instead '${iquery_scanner_src}'!"
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/
	)
else()
	set(iquery_scanner_fixed_src ${iquery_scanner_src})
endif()

#
# HACKS ENDS HERE
#

set (iquery_src
    iquery.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
    ${iquery_scanner_fixed_src}
    iquery_parser.cpp
    keywords.cpp
    ../../src/system/Config.cpp
)

file(GLOB iquery_inc "*.h" "*.yy" "*.ll")

set_source_files_properties(iquery_parser.cpp keywords.cpp PROPERTIES COMPILE_FLAGS "-Wno-parentheses")
# The generated flex/bison files may have conversion warning. Ignore them.
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/parser.cpp ${iquery_scanner_fixed_src} PROPERTIES COMPILE_FLAGS "-Wno-parentheses -Wno-conversion")
add_executable(iquery ${iquery_src} ${iquery_inc})
extractDebugInfo("${GENERAL_OUTPUT_DIRECTORY}" "iquery" iquery)

set_target_properties(iquery PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${GENERAL_OUTPUT_DIRECTORY})
target_link_libraries(iquery
	scidbclient
	${Boost_LIBRARIES}
	${LIBEDIT_LIBRARIES}
        ${CURSES_LIBRARIES}
	${LIBRT_LIBRARIES}
	${CMAKE_THREAD_LIBS_INIT}
)

set_target_properties(iquery
	PROPERTIES
#       BUILD_WITH_INSTALL_RPATH TRUE
#	CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE
#	INSTALL_RPATH ${DEFAULT_RPATH}
	COMPILE_FLAGS "-DSCIDB_CLIENT"
)
