# VxWorks 6.6 toolchain in Ubuntu environment.
#

SET (VX_TOOL_FAMILY gnu)
SET (VX_TOOL        e500v2gnu)
SET (VX_CPU_FAMILY  ppc)
SET (VX_CPU         PPC32)
SET (VX_CPU_CARD    ADS8572)
SET (VX_CPU_VARIANT _ppc85XX_e500v2)
SET (VX_VERSION     vxworks-6.6)

# ホストOSの判定
FIND_PROGRAM (UNAME uname)
MARK_AS_ADVANCED (UNAME)
EXECUTE_PROCESS (COMMAND ${UNAME} OUTPUT_VARIABLE VXWORKS_HOST_OS)

IF (VXWORKS_HOST_OS MATCHES "SunOS")
  SET (WIND_HOST_TYPE sun4-solaris2)
  SET (WIND_HOME /usr/local/vw/workbench3.0)
ELSEIF (VXWORKS_HOST_OS MATCHES "Linux")
  SET (WIND_HOST_TYPE x86-linux2)
  IF (NOT EXISTS "$ENV{WIND_HOME}")
    MESSAGE (STATUS "WIND_HOME=$ENV{WIND_HOME}")
    MESSAGE (FATAL_ERROR "Please provide valid WIND_HOME environment variable.")
  ENDIF (NOT EXISTS "$ENV{WIND_HOME}")
  SET (WIND_HOME "$ENV{WIND_HOME}")
ENDIF (VXWORKS_HOST_OS MATCHES "SunOS")

# Path Settings
SET (WIND_BASE ${WIND_HOME}/${VX_VERSION})
SET (WIND_GNU_BASE ${WIND_HOME}/gnu/4.1.2-${VX_VERSION}/${WIND_HOST_TYPE})
SET (VX_VW_BASE ${WIND_BASE}/target)

# environment variables required by compiler
SET (WIND_ENV "WIND_HOME=${WIND_HOME} WIND_HOST_TYPE=${WIND_HOST_TYPE} WIND_BASE=${WIND_BASE}")

################################################################################

SET (VXWORKS ON)
SET (VXWORKS_MAJOR 6)
SET (VXWORKS_MINOR 6)

SET (CMAKE_SYSTEM_NAME VxWorks)
SET (CMAKE_SYSTEM_VERSION ${VX_VERSION})
SET (CMAKE_SYSTEM_PROCESSOR ${VX_CPU_FAMILY})

INCLUDE (CMakeForceCompiler)
CMAKE_FORCE_C_COMPILER(${WIND_GNU_BASE}/bin/ccppc GNU)
CMAKE_FORCE_CXX_COMPILER(${WIND_GNU_BASE}/bin/c++ppc GNU)
SET (CMAKE_LINKER ${WIND_GNU_BASE}/bin/ldppc CACHE FILEPATH "vxworks linker")
SET (CMAKE_AR ${WIND_GNU_BASE}/bin/arppc CACHE FILEPATH "vxworks ar")
SET (CMAKE_NM ${WIND_GNU_BASE}/bin/nmppc CACHE FILEPATH "vxworks nm")
SET (CMAKE_STRIP ${WIND_GNU_BASE}/bin/stripppc CACHE FILEPATH "vxworks strip")
SET (CMAKE_OBJCOPY ${WIND_GNU_BASE}/bin/objcopyppc CACHE FILEPATH "vxworks objcopy")
SET (CMAKE_OBJDUMP ${WIND_GNU_BASE}/bin/objdumpppc CACHE FILEPATH "vxworks objdump")
SET (CMAKE_RANLIB ${WIND_GNU_BASE}/bin/ranlibppc CACHE FILEPATH "vxworks ranlib")
SET (CMAKE_EXE_LINKER_FLAGS_INIT -r)

FIND_PROGRAM (SH sh REQUIRED)
SET (MUNCH ${WIND_BASE}/host/${WIND_HOST_TYPE}/bin/munch CACHE FILEPATH "munch")

SET (CMAKE_C_COMPILE_OBJECT
  "${WIND_ENV} <CMAKE_C_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
#  "${WIND_ENV} <CMAKE_C_COMPILER> <DEFINES> <FLAGS> <INCLUDES> -o <OBJECT> -c <SOURCE>")
SET (CMAKE_CXX_COMPILE_OBJECT
  "${WIND_ENV} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
#  "${WIND_ENV} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> <INCLUDES> -o <OBJECT> -c <SOURCE>")

SET (CMAKE_C_LINK_EXECUTABLE
  "${CMAKE_LINKER} <LINK_FLAGS> <CMAKE_C_LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
SET (CMAKE_CXX_LINK_EXECUTABLE
  "${CMAKE_LINKER} <LINK_FLAGS> <CMAKE_CXX_LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

SET (CMAKE_C_FLAGS_DEBUG "-O -g1" CACHE STRING "")
SET (CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG} CACHE STRING "")

SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O -g1 -DNDEBUG"  CACHE STRING "")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_C_FLAGS_RELWITHDEBINFO} CACHE STRING "")

SET (CMAKE_C_FLAGS_RELEASE "-O -g1 -DNDEBUG" CACHE STRING "")
SET (CMAKE_CXX_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE} CACHE STRING "")

INCLUDE_DIRECTORIES (
  ${VX_VW_BASE}/h
  ${VX_VW_BASE}/h/wrn/coreip)

SET (VXWORKS_C_FLAGS_LIST
  -DCPU=${VX_CPU}
  -DVX_CPU_CARD=${VX_CPU_CARD}
  -DTOOL_FAMILY=${VX_TOOL_FAMILY}
  -DTOOL=${VX_TOOL}
  -D_WRS_KERNEL
  -DCPU_VARIANT=${VX_CPU_VARIANT}
  -DVXWORKS_MAJOR=${VXWORKS_MAJOR}
  -DVXWORKS_MINOR=${VXWORKS_MINOR}
  -mlongcall
  -fstrength-reduce
  -fno-builtin
  -te500v2
  -mcpu=8548
  -mfloat-gprs=double
  -mspe=yes
  -mabi=spe
  -mstrict-align
  -fno-implicit-fp
  -DVXWORKS_66
  )

ADD_DEFINITIONS (${VXWORKS_C_FLAGS_LIST})


	string(REPLACE ";" " " VXWORKS_C_FLAGS_LIST_STR "${VXWORKS_C_FLAGS_LIST}")
	SET (RUN_MUNCH_SCRIPT ${CMAKE_SOURCE_DIR}/build/cmake/run_munch.sh)
	SET (CMAKE_C_LINK_EXECUTABLE
		  "${CMAKE_LINKER}  <LINK_FLAGS> <CMAKE_CXX_LINK_FLAGS> -warn-common -S <OBJECTS> -o <TARGET>_partialImage.o  <LINK_LIBRARIES>"
#		  "${CMAKE_LINKER}  <LINK_FLAGS> <CMAKE_CXX_LINK_FLAGS> -warn-common -S <OBJECTS> -o <TARGET>_partialImage.o  <LINK_LIBRARIES>  /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/OENames_2.1.6/lib/libOEnames.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEtcp.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEmirror.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEbridge.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEudp.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEipmc.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEshrmem.a /home/openrtm/OIS/ORBexpress/RT_2.8.4_PATCH_KC1/target/ppc_8xx-vxworks_6.6/gcc_4.1.2_ExLcRi/config_rt_full/lib/libOEorb.a"
		  "sh ${RUN_MUNCH_SCRIPT} ${CMAKE_NM} <TARGET>_partialImage.o ${MUNCH} ctdt.c"
#sh -c WIND_BASE=${WIND_BASE}\; export WIND_BASE\; WIND_HOST_TYPE=${WIND_HOST_TYPE}\; export WIND_HOST_TYPE\; ${CMAKE_NM} <TARGET>_partialImage.o | ${MUNCH} > ctdt.c"
		  "${CMAKE_C_COMPILER} ${VXWORKS_C_FLAGS_LIST_STR} -o ctdt.o -c ctdt.c"
		  "${CMAKE_LINKER} <LINK_FLAGS> <CMAKE_CXX_LINK_FLAGS> -S -warn-common -o <TARGET> <TARGET>_partialImage.o ctdt.o "
		)
	SET (CMAKE_CXX_LINK_EXECUTABLE ${CMAKE_C_LINK_EXECUTABLE})

LIST (APPEND CMAKE_FIND_ROOT_PATH /usr/${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_SYSTEM})
LIST (APPEND CMAKE_PREFIX_PATH /)
SET (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

MARK_AS_ADVANCED (CMAKE_LINKER CMAKE_AR CMAKE_NM CMAKE_STRIP
  CMAKE_OBJCOPY CMAKE_OBJDUMP CMAKE_RANLIB CMAKE_C_FLAGS_DEBUG
  CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELWITHDEBINFO
  CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS_RELEASE
  CMAKE_CXX_FLAGS_RELEASE SH MUNCH)
