FILE(GLOB lzz_sources "*.lzz")
set(ccarve_sources "")

foreach(f ${lzz_sources})
    get_filename_component(bas ${f} NAME_WE)
    set(bas_abs ${CMAKE_CURRENT_SOURCE_DIR}/generated/${bas})
    list(APPEND ccarve_sources ${bas_abs}.cpp)
    #    list(APPEND ccarve_sources ${bas_abs}.h)
endforeach(f)

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

foreach(f ${lzz_sources})
    lazy_compile(${f})
endforeach(f)

#if (CCMAKE_COMPILER_IS_GNUCXX)
#    set_source_files_properties(${ccarve_sources} PROPERTIES COMPILE_FLAGS "-Werror -Wall -pedantic")
#endif ()

if (ccarve_debug)
    set_source_files_properties(${ccarve_sources} PROPERTIES COMPILE_FLAGS "-DDEBUG")
endif ()
cxx_executable_with_flags(ccarve "${cxx_default}" "" ${ccarve_sources})
target_link_libraries(ccarve
     ${Boost_PROGRAM_OPTIONS_LIBRARY}
     ${Boost_SYSTEM_LIBRARY}
     ${Boost_FILESYSTEM_LIBRARY}
    )

if (ccarve_build_tests)

  ############################################################
  # Preparing libraries to be used with tests

    cxx_library_odir(gtest ${ccarve_SOURCE_DIR}/bin/tests "${cxx_strict}" tests/gtest/gtest-all.cc)
    cxx_library_odir(gtest_main ${ccarve_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(option_test gtest_main generated/option.cpp)
  target_link_libraries(tests/option_test ${Boost_LIBRARIES})
  cxx_test(project_test gtest_main generated/project.cpp generated/option.cpp generated/carve.cpp)
  target_link_libraries(tests/project_test ${Boost_LIBRARIES})
  cxx_test(carve_test gtest_main generated/carve.cpp generated/option.cpp
    generated/project.cpp)
  target_link_libraries(tests/carve_test ${Boost_LIBRARIES})
  cxx_test(main_test gtest_main generated/carve.cpp generated/option.cpp 
    generated/project.cpp)
  target_link_libraries(tests/main_test ${Boost_LIBRARIES})
  #  cxx_executable_with_flags(option_test "${cxx_default}" gtest_main tests/option_test.cc)
  #ADD_TEST(option_test ${CMAKE_CURRENT_BINARY_DIR}/option_test hello)


  ############################################################
  # C++ tests built with non-standard compiler flags.

     #cxx_library(gtest_main_no_exception "${cxx_no_exception}"
     #  src/gtest-all.cc src/gtest_main.cc)
     #cxx_test_with_flags(gtest-death-test_ex_catch_test
     #  "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
     #  gtest test/gtest-death-test_ex_test.cc)
     #cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
     #  gtest_dll test/gtest_all_test.cc)
     #set_target_properties(gtest_dll_test_
     #                      PROPERTIES
     #                      COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
endif()

install(TARGETS ccarve 
  DESTINATION bin )

