########################################################
# Files

file(GLOB massxpert_SRCS *.cpp)
file(GLOB massxpert_UIS ui/*.ui)
file(GLOB Resources *.qrc)

set(CMAKE_INCLUDE_CURRENT_DIR ON)


#############################################################
# application icon
IF (WIN32 AND NOT MSVC)
  # Icon for windows
  # Note: can't include .rc directly to source files
  # as it's ignored when used MinGW
  IF (NOT WINDRES)
    FIND_PROGRAM (WINDRES windres)
    IF (NOT WINDRES)
      MESSAGE (FATAL_ERROR "windres not found - aborting")
    ENDIF (NOT WINDRES)
  ENDIF (NOT WINDRES)
  
  IF (MINGW)
    # resource compilation for MinGW
    ADD_CUSTOM_COMMAND (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/icon.obj
      COMMAND ${WINDRES} 
      -I${CMAKE_CURRENT_SOURCE_DIR} -i${CMAKE_CURRENT_SOURCE_DIR}/massxpert.rc
      -o ${CMAKE_CURRENT_BINARY_DIR}/icon.obj
      )
    SET (massxpert_SRCS ${massxpert_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/icon.obj)
  ELSE (MINGW)
    SET (massxpert_SRCS ${massxpert_SRCS} 
      ${CMAKE_CURRENT_SOURCE_DIR}/massxpert.rc)
  ENDIF (MINGW)
  
ENDIF (WIN32 AND NOT MSVC)


########################################################
# Build

IF(CMAKE_BUILD_TYPE STREQUAL "debug")
  ADD_DEFINITIONS (-g -O0)
ENDIF()

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

QT5_WRAP_UI(massxpert_UIS_H ${massxpert_UIS})

QT5_ADD_RESOURCES(massxpertQRC_CPP application.qrc)

IF (APPLE)

ADD_EXECUTABLE (${PROGNAME}  MACOSX_BUNDLE 
  ${massxpert_SRCS}
  ${massxpert_UIS_H} 
  ${massxpertQRC_CPP})

# Apple bundle configuration

  SET (MACOSX_BUNDLE_INFO_STRING "massXpert - version ${VERSION}")
  SET (MACOSX_BUNDLE_BUNDLE_VERSION ${VERSION})
  # Change following line to point to actual icns file in bundle.
  SET (MACOSX_BUNDLE_ICON_FILE "massXpert.icns")
  # means bundle is set to be org.massxpert
  SET (MACOSX_BUNDLE_GUI_IDENTIFIER "org.massxpert")
  SET (MACOSX_BUNDLE_BUNDLE_NAME "massXpert")

  EXEC_PROGRAM ("mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/massXpert.app/Contents/Resources")
  
EXEC_PROGRAM ("cp ${CMAKE_CURRENT_SOURCE_DIR}/images/massXpert.icns ${CMAKE_CURRENT_BINARY_DIR}/massXpert.app/Contents/Resources")
  
# We want to build universal binaries 
SET (CMAKE_OSX_ARCHITECTURES "ppc;i386" 
  CACHE STRING "Build architectures for OSX" FORCE) 

# That can be deployed on older system 
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.3" 
  CACHE STRING "Flags used by the compiler during all build types." FORCE)

SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden" 
  CACHE STRING "Flags used by the compiler during all build types." FORCE)

SET (CMAKE_OSX_SYSROOT "/Developer/SDKs/MacOSX10.4u.sdk" 
  CACHE STRING "MacOSX10.4u.sdk." FORCE)

SET_SOURCE_FILES_PROPERTIES (application.qrc ${TARGET}_fr.qm 
  PROPERTIES MACOSX_BUNDLE_LOCATION Resources)

ELSE (APPLE)

ADD_EXECUTABLE(${PROGNAME}
  ${massxpert_SRCS} 
  ${massxpert_UIS_H} 
  ${massxpertQRC_CPP})

ENDIF (APPLE)

INCLUDE_DIRECTORIES(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_BINARY_DIR} 
  ${CMAKE_BINARY_DIR} # for the config.h file generated from config.h.in
  ../lib)

# Make sure all the symbols from libmasslib.a are in the executable binary!
IF (APPLE)
  SET(MASSLIB -Wl,-all_load ${massxpert_BINARY_DIR}/lib/libmasslib.a -Wl,-noall_load -Wl,-framework,CoreFoundation)
ELSE (APPLE)
SET(MASSLIB -Wl,-whole-archive ${massxpert_BINARY_DIR}/lib/libmasslib.a -Wl,-no-whole-archive)
ENDIF (APPLE)

# Use the Widgets module from Qt 5.
TARGET_LINK_LIBRARIES(${PROGNAME}
  ${MASSLIB}
  Qt5::Widgets
  Qt5::Xml
  Qt5::Svg)

IF (APPLE)
  # Copy the massXpert.icns icon file in the bundle
  INSTALL (FILES images/massXpert.icns
    DESTINATION ${BUNDLE_DIR}/Contents/Resources)
  
  # Copy the Info.plist file in the bundle
  #INSTALL (FILES ${CMAKE_CURRENT_BINARY_DIR}/massXpert.app/Contents/Info.plist
  #  DESTINATION ${BUNDLE_DIR}/Contents)

ENDIF (APPLE)

IF (NOT APPLE)

  # When building a Mac bundle, we do not install the target
  # because the bundle is being fabricated already.
INSTALL(TARGETS ${PROGNAME} RUNTIME DESTINATION ${MASSXPERT_BIN_DIR})

ENDIF (NOT APPLE)
