
FetchContent_Declare(
  pybind11
  GIT_REPOSITORY https://github.com/pybind/pybind11
  GIT_TAG        v2.5.0
  )
set(FETCHCONTENT_QUIET OFF)
FetchContent_MakeAvailable(pybind11)

find_package (Python COMPONENTS Interpreter Development)
include_directories(${Python_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
find_program(CastXML_EXECUTABLE NAMES castxml)
if(NOT CastXML_EXECUTABLE)
  message(FATAL_ERROR
    "Required program castxml not found set CastXML_EXECUTABLE")
endif()

option(DEFINE_TEST_PARAM "Adds a define parameter to swap definitions of the 'summer' function" OFF)
if (DEFINE_TEST_PARAM)
  set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-DTESTFLAG")
 endif()

string(REPLACE "/" "-" cxx_flags "${CMAKE_CXX_FLAGS}")
string(REPLACE "-GR" "" cxx_flags "${cxx_flags}")
string(REPLACE "-EHsc" "" cxx_flags "${cxx_flags}")
string(REPLACE "-W3" "" cxx_flags "${cxx_flags}")
message("${cxx_flags}")

# First gather the list of files to be created
execute_process(COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/generator.py -n
                                                                              --module_name "example"
                                                                              -j ${CMAKE_CURRENT_SOURCE_DIR}/wrapper_input.json
                                                                              "-o"  ${CMAKE_CURRENT_BINARY_DIR}
                OUTPUT_VARIABLE generator_return OUTPUT_STRIP_TRAILING_WHITESPACE)
# Add custom command to output wrapper file for default code
add_custom_command(OUTPUT wrapper.cpp COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/generator.py -j ${CMAKE_CURRENT_SOURCE_DIR}/wrapper_input.json
                                                                              --module_name "example"
                                                                              "-g"  ${CastXML_EXECUTABLE}
                                                                              "-cf" \"${cxx_flags}\"
                                                                              "-o"  ${CMAKE_CURRENT_BINARY_DIR}
                                                                              WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
# Create default library
add_library(wrapper_example SHARED wrapper.cpp test.hpp test_2.hpp)
target_include_directories(wrapper_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# For each generated file, add the same custom command to generate the necessary file
message(${generator_return})
# Get name of file which should correspond directly to name of python module created in it
# The first name in the list should be the overall module
list(GET generator_return 0 file)
get_filename_component(tgt_helper_name ${file} NAME_WE)

# Add custom target to generate the code
# Add depends on wrapper_input.json and header files?
message(${file})
add_custom_command(OUTPUT ${generator_return} COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/generator.py -j ${CMAKE_CURRENT_SOURCE_DIR}/wrapper_input.json
                                                                          "-g"  ${CastXML_EXECUTABLE}
                                                                          "-cf" \"${cxx_flags}\"
                                                                          "-o"  ${CMAKE_CURRENT_BINARY_DIR}
                                                                          DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/wrapper_input.json
                                                                          WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
# Create the module with that file as it's only source
pybind11_add_module(${tgt_helper_name} ${generator_return})

# Create default tests, importing the module
set(py_command "from example import ${tgt_helper_name}")
# test_py has an un-handled dependency on test_base_py, be sure to import that first
if(${tgt_helper_name} STREQUAL "test_py")
set(py_command "from example import test_base_py\n\rfrom example import ${tgt_helper_name}")
endif()
add_test(NAME test_import_${tgt_helper_name} COMMAND ${Python_EXECUTABLE} -c ${py_command} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME WG_Linter COMMAND pycodestyle ${CMAKE_SOURCE_DIR}/generator.py WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
