
function(lazy_compile filename direct)
    get_filename_component(base ${filename} NAME_WE)
    set(base_abs ${CMAKE_BINARY_DIR}/${direct}/${base})
    set(output ${base_abs}.cpp ${base_abs}.h)
    # message("${output} output")
    add_custom_command(
        OUTPUT ${output}
        COMMAND ${LZZ_COMMAND} -sl -hl -hd -sd -o ${CMAKE_BINARY_DIR}/${direct} ${filename}
        DEPENDS ${filename})
      # message("${LZZ_COMMAND} -sl -hl -hd -sd -o ${CMAKE_BINARY_DIR}/${direct} ${filename}")
    set_source_files_properties(${output} PROPERTIES GENERATED TRUE)
endfunction()

SET(BUILD_SHARED_LIBS ON)

FILE(GLOB common_lzz_sources "common/*.lzz")
#message("${common_lzz_sources} src")
set(common_sources "")
foreach(f ${common_lzz_sources})
    get_filename_component(bas ${f} NAME_WE)
    set(bas_abs ${CMAKE_BINARY_DIR}/src/common/${bas})
    list(APPEND common_sources ${bas_abs}.cpp)
    #    list(APPEND common_sources ${bas_abs}.h)
    lazy_compile(${f} "src/common")
    #message("lazy compile ${f} src")
endforeach(f)
#message("${common_sources} src")
FILE(GLOB system_lzz_sources "system/*.lzz")
set(system_sources "")
foreach(f ${system_lzz_sources})
#     message("${f} source")
    get_filename_component(bas ${f} NAME_WE)
    set(bas_abs ${CMAKE_BINARY_DIR}/src/system/${bas})
    list(APPEND system_sources ${bas_abs}.cpp)
    #    list(APPEND system_sources ${bas_abs}.h)
    lazy_compile(${f} "src/system")
endforeach(f)
FILE(GLOB search_lzz_sources "search/*.lzz")
set(search_sources "")
foreach(f ${search_lzz_sources})
    get_filename_component(bas ${f} NAME_WE)
    set(bas_abs ${CMAKE_BINARY_DIR}/src/search/${bas})
    list(APPEND search_sources ${bas_abs}.cpp)
#     list(APPEND search_sources ${bas_abs}.h)
    lazy_compile(${f} "src/search")
endforeach(f)
FILE(GLOB main_lzz_sources "*.lzz")
set(main_sources "")
foreach(f ${main_lzz_sources})
    get_filename_component(bas ${f} NAME_WE)
    set(bas_abs ${CMAKE_BINARY_DIR}/src/${bas})
    list(APPEND main_sources ${bas_abs}.cpp)
#     list(APPEND main_sources ${bas_abs}.h)
    lazy_compile(${f} "src")
endforeach(f)
 
# lazy_compile("verifix.lzz" ".")

# ADD_DEFINITIONS(-iquote ${verifix_SOURCE_DIR})
#I am not sure why but add_definitions seems not to work so we specify FLAGS directly:
set(CMAKE_CXX_FLAGS "-iquote ${verifix_BINARY_DIR}/src -Wall -ggdb")
#message(STATUS ${CMAKE_C_FLAGS})

cxx_executable_with_flags(verifix "${cxx_default} -ggdb" "" ${main_sources} ${system_sources} ${common_sources} ${search_sources})


target_link_libraries (
    verifix
    ${Boost_LIBRARIES} utap udbm ${LIBXML2_LIBRARIES}
    )
if (verifix_build_tests)
  ############################################################
  # Preparing libraries to be used with tests

    cxx_library_odir(gtest ${verifix_SOURCE_DIR}/bin/tests "${cxx_strict}" tests/gtest/gtest-all.cc)
    cxx_library_odir(gtest_main ${verifix_SOURCE_DIR}/bin/tests "${cxx_strict}" tests/gtest/gtest_main.cc)
  target_link_libraries(gtest_main gtest)
  ############################################################
  # C++ tests built with standard compiler flags.

  cxx_test(systemparser_test gtest_main ${system_sources} search/transition.cpp common/message.cpp)
  target_link_libraries(tests/systemparser_test ${Boost_LIBRARIES} utap ${LIBXML2_LIBRARIES} udbm)
  cxx_test(reachability_test gtest_main ${search_sources} ${system_sources} common/option.cpp  common/message.cpp common/global_func.cpp)
  target_link_libraries(tests/reachability_test ${Boost_LIBRARIES} utap ${LIBXML2_LIBRARIES} udbm)
  cxx_test(state_test gtest_main ${system_sources} ${search_sources} ${common_sources})
  target_link_libraries(tests/state_test ${Boost_LIBRARIES} utap ${LIBXML2_LIBRARIES} udbm)
endif()

