I already played around with sfml and cmake on a ubuntu systems. I was supprised by the fact, that the original cmake on windows did not come with a sfml script.
So I ended up here...
...still there was no support for windows. So I add support for visual studo 2008.
You just have to set the the enviroment variable SFMLDIR to the SFML directory.
And make sure to have a SET(CMAKE_BUILD_TYPE Release) in your cmake project!!!
Because the vs compiler uses the debug option by default. You would end up with the release SFML components in a debug executable. That can cause unexpected behavior and crashes of you application!
I do not have that much experienc with cmake so this file is still limited to create release versions only. Maybe I add support for the debug files later.
I created working nmake files and vs2008 projects with this:
Save this file as "FindSFML.cmake"
# Locate SFML library
# This module defines
# SFML_LIBRARY, the name of the library to link against
# SFML_FOUND, if false, do not try to link to SFML
# SFML_INCLUDE_DIR, where to find SFML headers
#
# Created by Nils Hasenbanck. Based on the FindSDL_*.cmake modules,
# created by Eric Wing, which were influenced by the FindSDL.cmake
# module, but with modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
#
# Changelog:
# 2010-04-04 Add support for wxWIN with visual studio 2008 (9.0)
SET( SFMLDIR $ENV{SFMLDIR} )
IF(MSVC)
# Convert backslashes to slashes
STRING(REGEX REPLACE "\\\\" "/" SFMLDIR "${SFMLDIR}")
ENDIF(MSVC)
SET(SFML_COMPONENTS
System
Audio
Graphics
Network
Window
)
SET(SFML_INCLUDE_SEARCH_DIR
~/Library/Frameworks
/Library/Frameworks
/usr/local/include/SFML
/usr/include/SFML
/usr/local/include
/usr/include
/sw/include/SFML # Fink
/sw/include
/opt/local/include/SFML # DarwinPorts
/opt/local/include
/opt/csw/include/SFML # Blastwave
/opt/csw/include
/opt/include/SFML
/opt/include
${SFMLDIR}/include #for wxWIN vc2008(9.0)
)
SET(SFML_LIBRARY_SEARCH_DIR
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
${SFMLDIR}/lib/vc2008 #for wxWIN vc2008(9.0)
)
FOREACH(COMPONENT ${SFML_COMPONENTS})
STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
STRING(TOLOWER ${COMPONENT} LOWERCOMPONENT)
FIND_LIBRARY(SFML_${UPPERCOMPONENT}_LIBRARY
NAMES sfml-${LOWERCOMPONENT}
PATH_SUFFIXES lib64 lib
PATHS ${SFML_LIBRARY_SEARCH_DIR}
)
FIND_PATH(SFML_${UPPERCOMPONENT}_INCLUDE_DIR ${COMPONENT}.hpp
PATH_SUFFIXES include SFML
PATHS ${SFML_INCLUDE_SEARCH_DIR}
)
IF(MSVC)
# In wxWIN we need the root include directory without the "/SFML" at the end... so we have to remove it.
# This is a oversized "remove 5 chars at the right end of the string" function:
string(LENGTH ${SFML_${UPPERCOMPONENT}_INCLUDE_DIR} STRING_SIZE)
math(EXPR STRING_SIZE ${STRING_SIZE}-5)
string(SUBSTRING "${SFML_${UPPERCOMPONENT}_INCLUDE_DIR}" 0 ${STRING_SIZE} SFML_${UPPERCOMPONENT}_INCLUDE_DIR)
ENDIF(MSVC)
IF(SFML_${UPPERCOMPONENT}_INCLUDE_DIR AND SFML_${UPPERCOMPONENT}_LIBRARY)
LIST(APPEND SFML_LIBRARY ${SFML_${UPPERCOMPONENT}_LIBRARY})
LIST(APPEND SFML_INCLUDE_DIR ${SFML_${UPPERCOMPONENT}_INCLUDE_DIR})
LIST(REMOVE_DUPLICATES SFML_LIBRARY)
LIST(REMOVE_DUPLICATES SFML_INCLUDE_DIR)
ENDIF(SFML_${UPPERCOMPONENT}_INCLUDE_DIR AND SFML_${UPPERCOMPONENT}_LIBRARY)
ENDFOREACH(COMPONENT)
IF(WIN32)
#Now we are looking for "sfml-main.lib".
#Because we need it if we give ADD_EXECUTABLE the WIN32 switch to creat a GUI application (that one without a cmd promt)
FIND_LIBRARY( SFML_MAIN_LIBRARY
NAMES sfml-main
PATH_SUFFIXES lib64 lib
PATHS ${SFML_LIBRARY_SEARCH_DIR}
)
LIST(APPEND SFML_LIBRARY ${SFML_MAIN_LIBRARY})
ENDIF(WIN32)
SET(SFML_FOUND "NO")
IF(SFML_SYSTEM_LIBRARY AND SFML_INCLUDE_DIR)
SET(SFML_FOUND "YES")
ENDIF(SFML_SYSTEM_LIBRARY AND SFML_INCLUDE_DIR)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SFML DEFAULT_MSG SFML_LIBRARY SFML_INCLUDE_DIR)
And before you all copy FindSFML.cmake in your cmake module directory here is a much more elegant way:
Create a directory like "cmake_modules" in you project directory and move the "FindSFML.cmake" there.
Then add the line set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules") to your "CMakeLists.txt".
Now you can use it with the regular Find_Package(SFML REQUIRED COMPONENTS System).
This is very useful because people that compile your project dont have to copy around the "FindSFML.cmake" script.
EDIT: I added the SFML_main lib so you dont get linking errors if you create windows GUI applications (the one without the cmd promt)