# Generate examples.
#
# This is boilerplate code for generating a set of executables, one per
# .cpp file in an "examples" subdirectory. These are intended to
# be installed with the library but we don't handle installation
# here. Unlike the similar boilerplate code in the "tests"
# directory (but like the "tests/adhoc" boilerplate), this does
# not generate CMake ADD_TEST commands so these will never run
# automatically.
#
# For IDEs that can deal with PROJECT_LABEL properties (at least
# Visual Studio) the projects for building each of these adhoc
# executables will be labeled "Example - TheExampleName" if a file
# TheExampleName.cpp is found in this directory.
#
# We check the BUILD_TESTING_{SHARED,STATIC} variables to determine
# whether to build dynamically linked, statically linked, or both
# versions of the executable.

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(SimTKExamples)

SET(BUILD_TESTING_SHARED TRUE CACHE BOOL 
		"Build examples using dynamic SimTK libraries.")
SET(BUILD_TESTING_STATIC FALSE CACHE BOOL
		"Build examples using static SimTK libraries.")

SET(SimTK_SHARED_LIBS 
		optimized SimTKmolmodel
		optimized SimTKsimbody 
		optimized SimTKmath
		optimized SimTKcommon)
SET(SimTK_SHARED_LIBS_D
		debug SimTKmolmodel_d 
		debug SimTKsimbody_d 
		debug SimTKmath_d
		debug SimTKcommon_d)

SET(SimTK_STATIC_LIBS 
		optimized SimTKmolmodel_static 
		optimized SimTKsimbody_static 
		optimized SimTKmath_static
		optimized SimTKcommon_static)
SET(SimTK_STATIC_LIBS_D
		debug SimTKmolmodel_static_d 
		debug SimTKsimbody_static_d 
		debug SimTKmath_static_d
		debug SimTKcommon_static_d)


# These extra libraries are only available as a shared, optimized.
IF(WIN32)
	SET(SimTK_GENERAL_LIBS SimTKlapack pthreadVC2)
ELSEIF(APPLE)
ELSE() #Linux
	SET(SimTK_GENERAL_LIBS dl rt)
ENDIF()

SET(SimTK $ENV{SimTK_INSTALL_DIR} CACHE PATH
		"Directory where SimTK is installed, e.g. /usr/local/SimTK.")

IF(NOT SimTK)
	MESSAGE(FATAL_ERROR 
"Expected SimTK_INSTALL_DIR environment var to be set, or set SimTK in CMake")
ENDIF(NOT SimTK)

SET(LIB64) # suffix will be set only for 64 bit gcc builds not on APPLE
IF(${CMAKE_C_COMPILER} MATCHES "gcc") # Linux, Mac, Cygwin
    IF(${CMAKE_SIZEOF_VOID_P} EQUAL 8 AND NOT APPLE)
        SET( LIB64 64 )
    ENDIF()
ENDIF()

INCLUDE_DIRECTORIES(${SimTK}/include)
LINK_DIRECTORIES(${SimTK}/lib${LIB64})

# On Mac, build 32 bit binaries.
IF(APPLE)
    SET(CMAKE_OSX_ARCHITECTURES "i386" CACHE STRING "The processor architectures to build for" FORCE)
ENDIF(APPLE)

FILE(GLOB EXAMPLES "*.cpp")
FOREACH(EX_PROG ${EXAMPLES})
    GET_FILENAME_COMPONENT(EX_ROOT ${EX_PROG} NAME_WE)

    IF (BUILD_TESTING_SHARED)
        # Link with shared library
        ADD_EXECUTABLE(${EX_ROOT} ${EX_PROG})
        SET_TARGET_PROPERTIES(${EX_ROOT}
		PROPERTIES
	      PROJECT_LABEL "Example - ${EX_ROOT}")
        TARGET_LINK_LIBRARIES(${EX_ROOT} 
				${SimTK_SHARED_LIBS_D}
				${SimTK_SHARED_LIBS}
				${SimTK_GENERAL_LIBS})
    ENDIF (BUILD_TESTING_SHARED)

    IF (BUILD_TESTING_STATIC)
        # Link with static library
        SET(EX_STATIC ${EX_ROOT}Static)
        ADD_EXECUTABLE(${EX_STATIC} ${EX_PROG})
        SET_TARGET_PROPERTIES(${EX_STATIC}
		PROPERTIES
		COMPILE_FLAGS "-DSimTK_USE_STATIC_LIBRARIES"
		PROJECT_LABEL "Example - ${EX_STATIC}")
        TARGET_LINK_LIBRARIES(${EX_STATIC}
				${SimTK_STATIC_LIBS_D}
				${SimTK_STATIC_LIBS}
				${SimTK_GENERAL_LIBS})
    ENDIF (BUILD_TESTING_STATIC)

ENDFOREACH(EX_PROG ${EXAMPLES})

# If there are any .obj (geometry) or .pdb (protein data bank)
# files, copy those to the binary directory which will likely be
# the current working directory for the examples when run.
FILE(GLOB EXAMPLE_OBJS "*.obj" "*.pdb")
FOREACH(EX_OBJ ${EXAMPLE_OBJS})
    GET_FILENAME_COMPONENT(OBJ_ROOT ${EX_OBJ} NAME)
    CONFIGURE_FILE(${EX_OBJ} 
			${CMAKE_CURRENT_BINARY_DIR}/${OBJ_ROOT} COPYONLY)
ENDFOREACH(EX_OBJ ${EXAMPLE_OBJS})

