Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem2/cmake/FindLua.cmake @ 2617

Last change on this file since 2617 was 2615, checked in by rgrieder, 16 years ago

Added FindLua.cmake:

  • Finds both version 5.0 and 5.1, but prefers 5.1
  • Regards version arguments (5.1.3 EXACT will only match 5.1.3, whereas 5 EXACT will match all Lua 5 versions)
  • Checks the header for the right version always
  • Also finds debug libraries

As you might know, there are already two scripts in the CMake module path: FindLua50.cmake and FindLua51.cmake. Unfortunately it would require writing a lot of hacky extra code to meet our needs with Lua versions. So I decided to write a whole new find script that should be able to all we need.

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1INCLUDE(CompareVersionStrings)
2INCLUDE(FindPackageHandleAdvancedArgs)
3INCLUDE(HandleLibraryTypes)
4
5# Macro that determines Lua version. Should work for versions 2.2 and above (current release: 5.1.4)
6FUNCTION(DETERMINE_LUA_VERSION _file _varname)
7  IF(EXISTS ${_file})
8    FILE(STRINGS ${_file} _file_content REGEX "LUA_VERSION|LUA_RELEASE")
9  ELSE(EXISTS ${_file})
10    SET(${_varname} "0" PARENT_SCOPE)
11    RETURN()
12  ENDIF(EXISTS ${_file})
13  STRING(REGEX REPLACE "^.*LUA_RELEASE[ \t]+\"Lua[ \t]+([.0-9]+)\".*$" "\\1" ${_varname} "${_file_content}")
14  IF(${_varname} STREQUAL "${_file_content}")
15    # At most version 5.1.0
16    STRING(REGEX REPLACE "^.*LUA_VERSION[ \t]+\"Lua[ \t]+([.0-9]+)\".*$" "\\1" ${_varname} "${_file_content}")
17    IF(${_varname} STREQUAL "${_file_content}")
18      MESSAGE(FATAL_ERROR "Could not determine Lua version which means this script has a bug")
19    ENDIF(${_varname} STREQUAL "${_file_content}")
20    IF(${_varname} MATCHES "^[0-9]+\\.[0-9]+$")
21      SET(${_varname} "${${_varname}}.0") # E.g. "3.2" is "3.2.0" actually
22    ENDIF(${_varname} MATCHES "^[0-9]+\\.[0-9]+$")
23  ENDIF(${_varname} STREQUAL "${_file_content}")
24  SET(${_varname} "${${_varname}}" PARENT_SCOPE)
25ENDFUNCTION(DETERMINE_LUA_VERSION)
26
27
28# Find Lua 5.1
29FIND_PATH(LUA_5.1_INCLUDE_DIR lua.h
30  PATHS $ENV{LUA_DIR}
31  PATH_SUFFIXES include/lua51 include/lua5.1 include/lua include
32)
33IF(LUA_5.1_INCLUDE_DIR)
34  DETERMINE_LUA_VERSION(${LUA_5.1_INCLUDE_DIR}/lua.h LUA_5.1_VERSION)
35  COMPARE_VERSION_STRINGS("${LUA_5.1_VERSION}" "5.1" _version_compare TRUE)
36  IF(NOT _version_compare EQUAL 0)
37    # Incorrect version found, abort search
38    SET(LUA_5.1_INCLUDE_DIR "LUA_5.1_INCLUDE_DIR-NOTFOUND" CACHE PATH "" FORCE)
39  ELSE(NOT _version_compare EQUAL 0)
40    FIND_LIBRARY(LUA_5.1_LIBRARY_OPTIMIZED
41      NAMES lua51 lua5.1 lua
42      PATHS $ENV{LUA_DIR}
43      PATH_SUFFIXES lib64 lib
44    )
45    FIND_LIBRARY(LUA_5.1_LIBRARY_DEBUG
46      NAMES lua51d lua51_d lua5.1d lua5.1_d luad lua_d
47      PATHS $ENV{LUA_DIR}
48      PATH_SUFFIXES lib64 lib
49    )
50    HANDLE_LIBRARY_TYPES(LUA_5.1)
51    SET(LUA_5.1_LIBRARIES ${LUA_5.1_LIBRARY})
52    IF(LUA_5.1_LIBRARIES)
53      SET(LUA_5.1_FOUND TRUE)
54    ENDIF(LUA_5.1_LIBRARIES)
55  ENDIF(NOT _version_compare EQUAL 0)
56ENDIF(LUA_5.1_INCLUDE_DIR)
57
58
59# Find Lua 5.0
60FIND_PATH(LUA_5.0_INCLUDE_DIR lua.h
61  PATHS $ENV{LUA_DIR}
62  PATH_SUFFIXES include/lua50 include/lua5.0 include/lua5 include/lua include
63)
64IF(LUA_5.0_INCLUDE_DIR)
65  DETERMINE_LUA_VERSION(${LUA_5.0_INCLUDE_DIR}/lua.h LUA_5.0_VERSION)
66  COMPARE_VERSION_STRINGS("${LUA_5.0_VERSION}" "5.0" _version_compare TRUE)
67  IF(NOT _version_compare EQUAL 0)
68    # Incorrect version found, abourt search
69    SET(LUA_5.0_INCLUDE_DIR "LUA_5.0_INCLUDE_DIR-NOTFOUND" CACHE PATH "" FORCE)
70  ELSE(NOT _version_compare EQUAL 0)
71    FIND_LIBRARY(LUA_5.0_LUA_LIBRARY_OPTIMIZED
72      NAMES lua50 lua5.0 lua5 lua
73      PATHS $ENV{LUA_DIR}
74      PATH_SUFFIXES lib64 lib
75    )
76    FIND_LIBRARY(LUA_5.0_LUA_LIBRARY_DEBUG
77      NAMES lua50d lua50_d lua5.0d lua5.0_d lua5d lua5_d luad lua_d
78      PATHS $ENV{LUA_DIR}
79      PATH_SUFFIXES lib64 lib
80    )
81    HANDLE_LIBRARY_TYPES(LUA_5.0_LUA)
82
83    # In an OS X framework, lualib is usually included as part of the framework
84    # (like GLU in OpenGL.framework)
85    IF(${LUA_5.0_LUA_LIBRARY} MATCHES "framework")
86      SET(LUA_5.0_LIBRARIES ${LUA_5.0_LUA_LIBRARY})
87    ELSE(${LUA_5.0_LUA_LIBRARY} MATCHES "framework")
88      FIND_LIBRARY(LUA_5.0_LUALIB_LIBRARY_OPTIMIZED
89        NAMES lualib50 lualib5.0 lualib5 lualib
90        PATHS $ENV{LUALIB_DIR} $ENV{LUA_DIR}
91        PATH_SUFFIXES lib64 lib
92      )
93      FIND_LIBRARY(LUA_5.0_LUALIB_LIBRARY_DEBUG
94        NAMES lualib50d lualib50_d lualib5.0d lualib5.0_d
95              lualib5d lualib5_d lualibd lualib_d
96        PATHS $ENV{LUALIB_DIR} $ENV{LUA_DIR}
97        PATH_SUFFIXES lib64 lib
98      )
99      HANDLE_LIBRARY_TYPES(LUA_5.0_LUALIB)
100      # Both libraries are required for Lua 5.0 to be found
101      IF(LUA_5.0_LUA_LIBRARY AND LUA_5.0_LUALIB_LIBRARY)
102        SET(LUA_5.0_LIBRARIES ${LUA_5.0_LUA_LIBRARY} ${LUA_5.0_LUALIB_LIBRARY})
103      ENDIF(LUA_5.0_LUA_LIBRARY AND LUA_5.0_LUALIB_LIBRARY)
104    ENDIF(${LUA_5.0_LUA_LIBRARY} MATCHES "framework")
105   
106    IF(LUA_5.0_LIBRARIES)
107      SET(LUA_5.0_FOUND TRUE)
108    ENDIF(LUA_5.0_LIBRARIES)
109  ENDIF(NOT _version_compare EQUAL 0)
110ENDIF(LUA_5.0_INCLUDE_DIR)
111
112# Pick the right version
113IF(Lua_FIND_VERSION_EXACT AND NOT Lua_FIND_VERSION MATCHES "^[0-9]*$")
114  STRING(REGEX REPLACE "^([0-9]+\\.[0-9]+)(\\..*)?$" "\\1" LUA_VERSION_SELECTION ${Lua_FIND_VERSION})
115ELSE(Lua_FIND_VERSION_EXACT AND NOT Lua_FIND_VERSION MATCHES "^[0-9]*$")
116  IF(LUA_5.1_FOUND) # Prefer version 5.1 if found
117    SET(LUA_VERSION_SELECTION "5.1")
118  ELSEIF(LUA_5.0_FOUND)
119    SET(LUA_VERSION_SELECTION "5.0")
120  ENDIF(LUA_5.1_FOUND)
121ENDIF(Lua_FIND_VERSION_EXACT AND NOT Lua_FIND_VERSION MATCHES "^[0-9]*$")
122
123SET(LUA_INCLUDE_DIR "${LUA_${LUA_VERSION_SELECTION}_INCLUDE_DIR}")
124SET(LUA_LIBRARIES "${LUA_${LUA_VERSION_SELECTION}_LIBRARIES}")
125SET(LUA_VERSION_LONG "${LUA_${LUA_VERSION_SELECTION}_VERSION}")
126SET(LUA_VERSION "${LUA_VERSION_SELECTION}")
127
128FIND_PACKAGE_HANDLE_ADVANCED_ARGS(Lua DEFAULT_MSG "${LUA_VERSION_LONG}"
129  LUA_INCLUDE_DIR
130  LUA_LIBRARIES
131)
132
133# Include the math library for Unix only
134IF(LUA_FOUND)
135  IF(UNIX AND NOT APPLE)
136    FIND_LIBRARY(UNIX_MATH_LIBRARY m)
137    SET(LUA_LIBRARIES ${LUA_LIBRARIES} ${UNIX_MATH_LIBRARY})
138  ENDIF(UNIX AND NOT APPLE)
139ENDIF(LUA_FOUND)
140
141MARK_AS_ADVANCED(
142  LUA_5.0_INCLUDE_DIR
143  LUA_5.0_LUA_LIBRARY_OPTIMIZED
144  LUA_5.0_LUA_LIBRARY_DEBUG
145  LUA_5.0_LUALIB_LIBRARY_OPTIMIZED
146  LUA_5.0_LUALIB_LIBRARY_DEBUG
147  LUA_5.1_INCLUDE_DIR
148  LUA_5.1_LIBRARY_OPTIMIZED
149  LUA_5.1_LIBRARY_DEBUG
150  UNIX_MATH_LIBRARY
151)
Note: See TracBrowser for help on using the repository browser.