CMAKE_MINIMUM_REQUIRED(VERSION 2.6.3 FATAL_ERROR)
PROJECT(GCCXML)

# Set the version number.
SET(GCCXML_VERSION_MAJOR 0)
SET(GCCXML_VERSION_MINOR 9)
SET(GCCXML_VERSION_PATCH 0)
SET(GCCXML_VERSION "${GCCXML_VERSION_MAJOR}.${GCCXML_VERSION_MINOR}")
SET(GCCXML_VERSION_FULL "${GCCXML_VERSION}.${GCCXML_VERSION_PATCH}")

# Setup installation components.
IF(GCCXML_INSTALL_COMPONENT_NAME_RUNTIME_EXECUTABLE)
  SET(GCCXML_INSTALL_COMPONENT_RUNTIME_EXECUTABLE
    COMPONENT ${GCCXML_INSTALL_COMPONENT_NAME_RUNTIME_EXECUTABLE})
ENDIF(GCCXML_INSTALL_COMPONENT_NAME_RUNTIME_EXECUTABLE)
IF(GCCXML_INSTALL_COMPONENT_NAME_RUNTIME_LIBRARY)
  SET(GCCXML_INSTALL_COMPONENT_RUNTIME_LIBRARY
    COMPONENT ${GCCXML_INSTALL_COMPONENT_NAME_RUNTIME_LIBRARY})
ENDIF(GCCXML_INSTALL_COMPONENT_NAME_RUNTIME_LIBRARY)
IF(GCCXML_INSTALL_COMPONENT_NAME_DOCUMENTATION)
  SET(GCCXML_INSTALL_COMPONENT_DOCUMENTATION
    COMPONENT ${GCCXML_INSTALL_COMPONENT_NAME_DOCUMENTATION})
ENDIF(GCCXML_INSTALL_COMPONENT_NAME_DOCUMENTATION)

# Configure the build.
IF(EXECUTABLE_OUTPUT_PATH)
  SET(GCCXML_EXECUTABLE_DIR ${EXECUTABLE_OUTPUT_PATH})
ELSE(EXECUTABLE_OUTPUT_PATH)
  SET(EXECUTABLE_OUTPUT_PATH ${GCCXML_BINARY_DIR})
  SET(GCCXML_EXECUTABLE_DIR ${GCCXML_BINARY_DIR})
ENDIF(EXECUTABLE_OUTPUT_PATH)
CONFIGURE_FILE(${GCCXML_SOURCE_DIR}/GXFront/gxConfigure.h.in
               ${GCCXML_BINARY_DIR}/GXFront/gxConfigure.h @ONLY IMMEDIATE)
INCLUDE_DIRECTORIES(${GCCXML_BINARY_DIR}/GXFront)

# Disable bogus warnings.
IF(MSVC80)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE)
ENDIF(MSVC80)

# Check for ANSI flags on non-GNU compilers.
IF(NOT CMAKE_COMPILER_IS_GNUCXX)
  SET(CMAKE_TRY_ANSI_CXX_FLAGS "")
  IF(CMAKE_SYSTEM MATCHES "IRIX.*")
    SET(CMAKE_TRY_ANSI_CXX_FLAGS "-LANG:std")
  ENDIF(CMAKE_SYSTEM MATCHES "IRIX.*")
  IF(CMAKE_SYSTEM MATCHES "OSF.*")
    SET(CMAKE_TRY_ANSI_CXX_FLAGS "-std strict_ansi -nopure_cname")
  ENDIF(CMAKE_SYSTEM MATCHES "OSF.*")
  IF(CMAKE_CXX_ACCEPTS_FLAGS MATCHES "^CMAKE_CXX_ACCEPTS_FLAGS$")
    INCLUDE(${CMAKE_ROOT}/Modules/TestCXXAcceptsFlag.cmake)
    # if CMAKE_TRY_ANSI_CXX_FLAGS has something in it, see
    # if the compiler accepts it
    IF(CMAKE_TRY_ANSI_CXX_FLAGS MATCHES ".+")
      CHECK_CXX_ACCEPTS_FLAG("${CMAKE_TRY_ANSI_CXX_FLAGS}" CMAKE_CXX_ACCEPTS_FLAGS)
      # if the compiler liked the flag then set CMAKE_ANSI_CXXFLAGS
      # to the flag
    ELSE(CMAKE_TRY_ANSI_CXX_FLAGS MATCHES ".+")
      SET(CMAKE_CXX_ACCEPTS_FLAGS 0)
    ENDIF(CMAKE_TRY_ANSI_CXX_FLAGS MATCHES ".+")
  ENDIF(CMAKE_CXX_ACCEPTS_FLAGS MATCHES "^CMAKE_CXX_ACCEPTS_FLAGS$")
  IF(CMAKE_CXX_ACCEPTS_FLAGS)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_TRY_ANSI_CXX_FLAGS}")
  ENDIF(CMAKE_CXX_ACCEPTS_FLAGS)
ENDIF(NOT CMAKE_COMPILER_IS_GNUCXX)

# Figure out what is being used to build GCC-XML.
SET(GCCXML_CXX_COMPILER "${CMAKE_CXX_COMPILER}")
SET(GCCXML_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

IF(CMAKE_BUILD_TOOL MATCHES "nmake")
  SET(GCCXML_CXX_COMPILER "cl")
ENDIF(CMAKE_BUILD_TOOL MATCHES "nmake")

IF(CMAKE_GENERATOR MATCHES "Visual Studio 6")
  SET(GCCXML_CXX_COMPILER "msvc6")
ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio 6")

IF(CMAKE_GENERATOR MATCHES "Visual Studio 7$")
  SET(GCCXML_CXX_COMPILER "msvc7")
ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio 7$")

IF(CMAKE_GENERATOR MATCHES "Visual Studio 7 .NET 2003")
  SET(GCCXML_CXX_COMPILER "msvc71")
ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio 7 .NET 2003")

# Create the kwsys library for GCC-XML.
SET(BUILD_SHARED_LIBS 0)
SET(LIBRARY_OUTPUT_PATH ${GCCXML_BINARY_DIR}/KWSys)
SET(KWSYS_NAMESPACE gxsys)
SET(KWSYS_USE_Process 1)
SET(KWSYS_USE_RegularExpression 1)
SET(KWSYS_USE_SystemTools 1)
SET(KWSYS_HEADER_ROOT ${GCCXML_BINARY_DIR})
IF("${CMAKE_SYSTEM}" MATCHES "AIX")
  SET(KWSYS_LFS_DISABLE 1)
ENDIF("${CMAKE_SYSTEM}" MATCHES "AIX")
INCLUDE_DIRECTORIES(${GCCXML_BINARY_DIR})
# install gxsys if requested:
IF(GCCXML_INSTALL_FOR_MUMMY)
  IF(NOT KWSYS_INSTALL_INCLUDE_DIR)
    SET(KWSYS_INSTALL_INCLUDE_DIR include)
  ENDIF(NOT KWSYS_INSTALL_INCLUDE_DIR)
  IF(NOT KWSYS_INSTALL_LIB_DIR)
    SET(KWSYS_INSTALL_LIB_DIR lib)
  ENDIF(NOT KWSYS_INSTALL_LIB_DIR)
  SET(KWSYS_USE_CommandLineArguments 1)
ENDIF(GCCXML_INSTALL_FOR_MUMMY)
SUBDIRS(KWSys)

# Installation directories.
SET(GCCXML_DATA_DIR_HELP "Install location for data (relative to prefix).")
SET(GCCXML_DOC_DIR_HELP "Install location for documentation (relative to prefix).")
SET(GCCXML_MAN_DIR_HELP "Install location for man pages (relative to prefix).")
IF(NOT GCCXML_DATA_DIR)
  SET(GCCXML_DATA_DIR "share/gccxml-${GCCXML_VERSION}" CACHE STRING
    "${GCCXML_DATA_DIR_HELP}")
  MARK_AS_ADVANCED(GCCXML_DATA_DIR)
ENDIF(NOT GCCXML_DATA_DIR)
IF(NOT GCCXML_DOC_DIR)
  SET(GCCXML_DOC_DIR "share/doc/gccxml-${GCCXML_VERSION}" CACHE STRING
    "${GCCXML_DOC_DIR_HELP}")
  MARK_AS_ADVANCED(GCCXML_DOC_DIR)
ENDIF(NOT GCCXML_DOC_DIR)
IF(NOT GCCXML_MAN_DIR)
  SET(GCCXML_MAN_DIR "share/man" CACHE STRING
    "${GCCXML_MAN_DIR_HELP}")
  MARK_AS_ADVANCED(GCCXML_MAN_DIR)
ENDIF(NOT GCCXML_MAN_DIR)

# Make sure there are no leading slashes.
FOREACH(dir GCCXML_DATA_DIR GCCXML_DOC_DIR GCCXML_MAN_DIR)
  IF("${dir}" MATCHES "^/")
    STRING(REGEX REPLACE "^/" "" ${dir} "${${dir}}")
    SET(${dir} "${${dir}}" CACHE STRING "${${dir}_HELP}" FORCE)
  ENDIF("${dir}" MATCHES "^/")
ENDFOREACH(dir)

# Include compiler support directory.
SUBDIRS(Support)

IF(WIN32 AND NOT CYGWIN)
  # Include Visual Studio compiler support directory.
  SUBDIRS(VcInstall)
ENDIF(WIN32 AND NOT CYGWIN)

# Directory to build gccxml executable.
SUBDIRS(GXFront)

# Create a configuration file for use from the build directory.
CONFIGURE_FILE(${GCCXML_SOURCE_DIR}/GXFront/config_build.in
               ${GCCXML_BINARY_DIR}/Support/gccxml_config)

# Create a configuration file for use from the install directory.
CONFIGURE_FILE(${GCCXML_SOURCE_DIR}/GXFront/config_install.in
  ${GCCXML_BINARY_DIR}/GXFront/InstallOnly/gccxml_config)
INSTALL(FILES ${GCCXML_BINARY_DIR}/GXFront/InstallOnly/gccxml_config
  DESTINATION ${GCCXML_INSTALL_ROOT}${GCCXML_DATA_DIR}
  ${GCCXML_INSTALL_COMPONENT_RUNTIME_LIBRARY})

# Add some tests.
IF(GCCXML_ADD_TESTS)
  SUBDIRS(Testing)
ENDIF(GCCXML_ADD_TESTS)
