Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: FindSFML for CMAKE  (Read 44272 times)

0 Members and 1 Guest are viewing this topic.

Arbaal

  • Newbie
  • *
  • Posts: 1
    • View Profile
FindSFML for CMAKE
« on: November 21, 2008, 10:31:33 am »
I want to add a module to cmake for searching SFML. Before I will try to get it into the official CMAKE sources I ask here for tester.

I appended a simple program to test the module on your system and for showing how to bind it into your own program (if you are allready using cmake).

With this module it is possible to simply find SFML by using the following command:
Code: [Select]
Find_Package(SFML REQUIRED)

The module then set's following variables:
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

Asking for a specific version or a specific component is not yet possible.

Would be nice to get some feedback.

http://www.sendspace.com/file/341ed2

geenux

  • Newbie
  • *
  • Posts: 1
    • View Profile
FindSFML for CMAKE
« Reply #1 on: January 28, 2009, 10:12:16 pm »
Yeah, great work, it's just working like a charm under Debian GNU/Linux.
Very good, I can now use CMake with SFML, thanks.

SamuraiCrow

  • Newbie
  • *
  • Posts: 40
    • Yahoo Instant Messenger - samuraileumas
    • View Profile
FindSFML for CMAKE
« Reply #2 on: January 28, 2009, 10:38:08 pm »
It worked fine under XCode 3.1.2 on MacOSX 10.5.6.

Emeric

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
FindSFML for CMAKE
« Reply #3 on: June 19, 2009, 07:24:40 pm »
The findsfml.cmake file is not available anymore on sendspace, is it still possible to get it ?

I'm a hudge fan of cmake and it would be great to easily use it with sfml.

SamuraiCrow

  • Newbie
  • *
  • Posts: 40
    • Yahoo Instant Messenger - samuraileumas
    • View Profile
FindSFML for CMAKE
« Reply #4 on: June 27, 2009, 01:41:33 am »
Here it is:

Code: [Select]

# 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).

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
)

SET(SFML_LIBRARY_SEARCH_DIR
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
)

FOREACH(COMPONENT ${SFML_COMPONENTS})
    STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
    STRING(TOLOWER ${COMPONENT} LOWERCOMPONENT)
    FIND_LIBRARY(SFML_${UPPERCOMPONENT}_LIBRARY
        NAMES sfml-${LOWERCOMPONENT}
        HINTS
        $ENV{SFMLDIR}
        PATH_SUFFIXES lib64 lib
        PATHS ${SFML_LIBRARY_SEARCH_DIR}
    )
    FIND_PATH(SFML_${UPPERCOMPONENT}_INCLUDE_DIR ${COMPONENT}.hpp
        HINTS
        $ENV{SFMLDIR}
        PATH_SUFFIXES include
        PATHS ${SFML_INCLUDE_SEARCH_DIR}
    )
    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)

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)

Emeric

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
thanks
« Reply #5 on: June 30, 2009, 06:35:50 pm »
Thanks !
Working fine with Arch Linux.

xarxer

  • Newbie
  • *
  • Posts: 35
    • View Profile
FindSFML for CMAKE
« Reply #6 on: December 18, 2009, 03:11:43 pm »
Hey uhm..

I copy pasted what SamuraiCrow pasted, and put it in FindSFML.cmake and placed it in /usr/share/apps/cmake/modules/
but CMake says "Could not find module FindSFML.cmake or a configuration file for package SFML."

Did I place it wrong?
Ideas?

Thanks / Xarxer  :D

xarxer

  • Newbie
  • *
  • Posts: 35
    • View Profile
FindSFML for CMAKE
« Reply #7 on: December 20, 2009, 10:05:12 am »
Nevermind my previous post, I solved it..
It should be placed in /usr/share/cmake-2.8/Modules/..

Now I've run into another problem! :)

while setting
Code: [Select]
find_package(SFML REQUIRED) in the CMakeLists.txt, compiler says undefined reference to pretty much everything that has anything to do with SFML; sf::VideoMode::VideoMode(), sf::RenderWindow::RenderWindow() etc. etc.


Tried to add "SFML/Graphics.hpp" to the ADD_EXECUTABLE(), but this generates a lot of other errors..

Does anyone know what I'm doing wrong?

EDIT:

Well it seems I solved this one as well..
I had missed target_link_libraries()

 :)

Osbios

  • Newbie
  • *
  • Posts: 23
    • View Profile
visual studio 2008
« Reply #8 on: April 04, 2010, 04:45:30 pm »
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"
Code: [Select]
# 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)

Osbios

  • Newbie
  • *
  • Posts: 23
    • View Profile
FindSFML for CMAKE
« Reply #9 on: April 09, 2010, 11:18:17 pm »
Here is an updated cmake find script with component testing and the possibility to chose betwin the debug/nondebug static/shared versions of SFML.

I made some big changes. So please test it!
And tell me if you did have any problems or if it works with your configuration and system.

Ohh... and please tell me what a configuration and system you use, too! ;)

Code: [Select]
# Locate SFML library
# This module defines
# SFML_FOUND, if false, do not try to link to SFML
# SFML_LIBRARY, the name of the librarys to link against
# SFML_INCLUDE_DIR, where to find SFML headers
#
# By default this script will link to the shared-nondebug version of SFML
# You can change this by define this variables befor calling FIND_PACKAGE
#
# SFML_DEBUG - If defined it will link to the debug version of SFML
# SFML_STATIC - If defined it will link to the static version of SFML
#
# For example:
# SET(SFML_STATIC true)
# FIND_PACKAGE(SFML REQUIRED COMPONENTS System Window)
#
# 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 visual studio 2008 (9.0)
# 2010-04-09 - Add support for visual studio 2005 (8.0)
#            - Now the test for the requested components is also implemented.
#            - It also will only link to the requested components
#            - You can chose wich debug/nondebug static/shared versions of the librarys you want to link to

SET(SFML_LIBRARY "")
SET(SFML_INCLUDE_DIR "")

SET( SFMLDIR $ENV{SFMLDIR} )
IF(WIN32 AND NOT(CYGWIN))
# Convert backslashes to slashes
    STRING(REGEX REPLACE "\\\\" "/" SFMLDIR "${SFMLDIR}")
ENDIF(WIN32 AND NOT(CYGWIN))

SET(SFML_COMPONENTS
    System
    Audio
    Graphics
    Network
    Window
)

SET(SFML_MODE
_SHARED_NONDEBUG
_SHARED_DEBUG
_STATIC_NONDEBUG
_STATIC_DEBUG
)

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}
    ${SFMLDIR}/include
)

SET(SFML_LIBRARY_SEARCH_DIR
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
    ${SFMLDIR}
    ${SFMLDIR}/lib/vc2008
    ${SFMLDIR}/lib/vc2005
)

#looking for the include files
FOREACH(COMPONENT ${SFML_COMPONENTS})
STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
STRING(TOLOWER ${COMPONENT} LOWERCOMPONENT)

FIND_PATH(SFML_${UPPERCOMPONENT}_INCLUDE_DIR
${COMPONENT}.hpp
PATH_SUFFIXES include SFML
PATHS ${SFML_INCLUDE_SEARCH_DIR}
)

IF(SFML_${UPPERCOMPONENT}_INCLUDE_DIR)
IF(WIN32)
# 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(WIN32)

LIST(APPEND SFML_INCLUDE_DIR ${SFML_${UPPERCOMPONENT}_INCLUDE_DIR})
LIST(REMOVE_DUPLICATES SFML_INCLUDE_DIR)
ENDIF(SFML_${UPPERCOMPONENT}_INCLUDE_DIR)
ENDFOREACH(COMPONENT)

#looking for the librarys
FOREACH(MODE ${SFML_MODE})
string(COMPARE EQUAL ${MODE} "_SHARED_NONDEBUG" string_equal_result)
IF(string_equal_result)
SET(_STA "")
SET(_DBG "")
ENDIF(string_equal_result)

string(COMPARE EQUAL ${MODE} "_SHARED_DEBUG" string_equal_result)
IF(string_equal_result)
SET(_STA "")
SET(_DBG "-d")
ENDIF(string_equal_result)

string(COMPARE EQUAL ${MODE} "_STATIC_NONDEBUG" string_equal_result)
IF(string_equal_result)
SET(_STA "-s")
SET(_DBG "")
ENDIF(string_equal_result)

string(COMPARE EQUAL ${MODE} "_STATIC_DEBUG" string_equal_result)
IF(string_equal_result)
SET(_STA "-s")
SET(_DBG "-d")
ENDIF(string_equal_result)

FOREACH(COMPONENT ${SFML_COMPONENTS})
STRING(TOUPPER ${COMPONENT} UPPERCOMPONENT)
STRING(TOLOWER ${COMPONENT} LOWERCOMPONENT)
FIND_LIBRARY(SFML_${UPPERCOMPONENT}_LIBRARY${MODE}
NAMES sfml-${LOWERCOMPONENT}${_STA}${_DBG}
PATH_SUFFIXES lib64 lib
PATHS ${SFML_LIBRARY_SEARCH_DIR}
)
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${MODE}
NAMES sfml-main${_DBG}
PATH_SUFFIXES lib64 lib
PATHS ${SFML_LIBRARY_SEARCH_DIR}
)
ENDIF(WIN32)
ENDFOREACH(MODE)


#Test if we have the include directory, the system lib and other needed components
#We also fill SFML_LIBRARY here with all the files we like to link to

IF(NOT(SFML_DEBUG) AND NOT(SFML_STATIC))
SET(MODE_LABEL "_SHARED_NONDEBUG")
ENDIF(NOT(SFML_DEBUG) AND NOT(SFML_STATIC))

IF(SFML_DEBUG AND NOT(SFML_STATIC))
SET(MODE_LABEL "_SHARED_DEBUG")
ENDIF(SFML_DEBUG AND NOT(SFML_STATIC))

IF(NOT(SFML_DEBUG) AND SFML_STATIC)
SET(MODE_LABEL "_STATIC_NONDEBUG")
ENDIF(NOT(SFML_DEBUG) AND SFML_STATIC)

IF(SFML_DEBUG AND SFML_STATIC)
SET(MODE_LABEL "_STATIC_DEBUG")
ENDIF(SFML_DEBUG AND SFML_STATIC)

LIST(APPEND SFML_LIBRARY ${SFML_MAIN_LIBRARY${MODE_LABEL}})

LIST(APPEND SFML_FIND_COMPONENTS "System") #We allways need at last the System component
LIST(REMOVE_DUPLICATES SFML_FIND_COMPONENTS)

SET(SFML_FOUND "YES")
FOREACH(COMPONENT ${SFML_FIND_COMPONENTS})
SET( MODUL_NAME SFML_${COMPONENT}_LIBRARY${MODE_LABEL} )
STRING(TOUPPER ${MODUL_NAME} MODUL_NAME)

IF(NOT ${MODUL_NAME})
SET(SFML_FOUND "NO")
MESSAGE("-- SFML: Could not locate : ${MODUL_NAME}")
ELSE(NOT ${MODUL_NAME})
LIST(APPEND SFML_LIBRARY ${${MODUL_NAME}})
ENDIF(NOT ${MODUL_NAME})
ENDFOREACH(COMPONENT)

LIST(REMOVE_DUPLICATES SFML_LIBRARY)

IF(NOT SFML_INCLUDE_DIR)
SET(SFML_FOUND "NO")
MESSAGE("-- SFML: Could not locate include directory")
ENDIF(NOT SFML_INCLUDE_DIR)

IF(NOT SFML_FOUND)
MESSAGE(FATAL_ERROR "Components of SFML are missing!")
ENDIF(NOT SFML_FOUND)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SFML DEFAULT_MSG SFML_LIBRARY SFML_INCLUDE_DIR)

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
FindSFML for CMAKE
« Reply #10 on: April 11, 2010, 07:08:04 am »
Awesome work. Can you try and bug CMake to put it into their official releases? CMake 3.0 is coming and it would be neat to have this .cmake script in there.

Osbios

  • Newbie
  • *
  • Posts: 23
    • View Profile
FindSFML for CMAKE
« Reply #11 on: April 11, 2010, 03:10:03 pm »
I like to get some feedback befor I try to get it in any official package.

If you find it so awesome did you use it? What System  was it? I guess it did run just fine?

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
FindSFML for CMAKE
« Reply #12 on: April 11, 2010, 08:38:47 pm »
I did work quite well in two of my projects, both Arch Linux x86_64 using my AUR SFML package.

mongrol

  • Newbie
  • *
  • Posts: 29
    • View Profile
FindSFML for CMAKE
« Reply #13 on: April 14, 2010, 02:50:33 am »
Osbios,
Also worked great here. Also on Archlinux i686 using Svenstaro's SFML package from AUR. Much better than the previous script too as I can now do a proper find package for individual components. Well done.

Kolja

  • Newbie
  • *
  • Posts: 22
    • View Profile
FindSFML for CMAKE
« Reply #14 on: September 28, 2010, 02:25:05 pm »
Doesn't work for me.

The problem is the linking, SFML_LIBRARY is set to
Code: [Select]
/Library/Frameworks/sfml-system.framework

I'm on OS X (10.6) obviously. /Library/Frameworks contains every sfml-<module>.framework, as well as SFML.framework.

An earlier version I tried (the first one posted inline here I believe) worked fine.

Can't see the fix from a first glance though, I'd expect the foreach loop to fill SFML_LIBRARY a bit more. But I only know cmake since yesterday, so no surprise there.

[edit]
http://www.sfml-dev.org/forum/viewtopic.php?p=15578#15578 still works fine. So it broke in the last version.