# Checks Fortran and C/C++ compilers compatibility, relying on the
# FortranCInterface CMake module.
# The FCMangle.hpp header will be generated. This header contains macros
# to perform the correct name mangling of Fortran/C interfacing
# subroutines/functions.

# 12.5.0 broken (LAB), above should trap pre-Mavericks
set(BROKEN_MACOSX FALSE)
if((${CMAKE_SYSTEM_NAME} MATCHES "Darwin") AND (CMAKE_SYSTEM_VERSION VERSION_LESS 13))
    set(BROKEN_MACOSX TRUE)
endif()

if(BROKEN_MACOSX)
    set(CMAKE_C_FLAGS_HOLD ${CMAKE_C_FLAGS})
    set(CMAKE_CXX_FLAGS_HOLD ${CMAKE_CXX_FLAGS})
    message(STATUS "Trying Fortran name mangling appending -m32 flag")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
endif()

include(FortranCInterface)
FortranCInterface_VERIFY(CXX)
# Generates with name-mangling helper macros and post-process it to add the copyright notice
FortranCInterface_HEADER(${PROJECT_BINARY_DIR}/include/FCMangle.hpp)
file(STRINGS ${PROJECT_BINARY_DIR}/include/FCMangle.hpp contents NEWLINE_CONSUME)
file(WRITE ${PROJECT_BINARY_DIR}/include/FCMangle.hpp
    "/* pcmsolver_copyright_start */\n/* pcmsolver_copyright_end */\n\n")
file(APPEND ${PROJECT_BINARY_DIR}/include/FCMangle.hpp "// Header file automagically generated by CMake. DO NOT TOUCH!\n")
file(APPEND ${PROJECT_BINARY_DIR}/include/FCMangle.hpp ${contents})
install(FILES ${PROJECT_BINARY_DIR}/include/FCMangle.hpp DESTINATION include)

if(BROKEN_MACOSX)
    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_HOLD})
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_HOLD})
endif()
