########################################
# BEGIN_COPYRIGHT
#
# Copyright (C) 2008-2019 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_target(iquery_parser parser.yy ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
flex_target(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
#
#    Rather than this hack, adding '%option never-interactive' to scanner.ll results in a
#    generated cpp file that does not have the 'extern "C" int isatty (int );'
#    signature. The scanner is never used interactively.
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\;/' ${FLEX_iquery_scanner_OUTPUTS} > ${iquery_scanner_fixed_src}
		DEPENDS ${FLEX_iquery_scanner_OUTPUTS}
		COMMENT "Fixing generated FLEX source. During building file '${iquery_scanner_fixed_src}' will be used instead '${FLEX_iquery_scanner_OUTPUTS}'!"
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/
	)
else()
	set(iquery_scanner_fixed_src ${FLEX_iquery_scanner_OUTPUTS})
endif()

#
# HACKS ENDS HERE
#

set (iquery_src
    iquery.cpp
    ${BISON_iquery_parser_OUTPUTS}
    ${iquery_scanner_fixed_src}  #  ${FLEX_iquery_scanner_OUTPUTS} (when hack is removed)
    iquery_parser.cpp
    keywords.cpp
    ../../src/system/Config.cpp
)

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(${BISON_iquery_parser_OUTPUTS}
                            ${iquery_scanner_fixed_src}  #  ${FLEX_iquery_scanner_OUTPUTS} (when hack is removed)
  PROPERTIES
  COMPILE_FLAGS "-Wno-parentheses -Wno-conversion"
  )
add_scidb_executable(iquery ${iquery_src})

set_target_properties(iquery PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY ${GENERAL_OUTPUT_DIRECTORY}
  )
target_link_libraries(iquery
  PUBLIC scidbclient
	 Boost::filesystem
         Boost::program_options
         Boost::regex
         Boost::serialization
         Boost::system
	 ${LIBEDIT_LIBRARIES}
         ${CURSES_LIBRARIES}
	 ${LIBRT_LIBRARIES}
	 Threads::Threads
  )
target_compile_definitions(iquery PRIVATE "SCIDB_CLIENT")
