Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Lua] Added Lua Cmake support files.
[simgrid.git] / buildtools / Cmake / Modules / FindLua.cmake
1 # Locate Lua library
2 # This module defines
3 #  LUA_FOUND          - if false, do not try to link to Lua
4 #  LUA_LIBRARIES      - both lua and lualib
5 #  LUA_INCLUDE_DIR    - where to find lua.h
6 #  LUA_VERSION_STRING - the version of Lua found
7 #  LUA_VERSION_MAJOR  - the major version of Lua
8 #  LUA_VERSION_MINOR  - the minor version of Lua
9 #  LUA_VERSION_PATCH  - the patch version of Lua
10 #
11 # Note that the expected include convention is
12 #  #include "lua.h"
13 # and not
14 #  #include <lua/lua.h>
15 # This is because, the lua location is not standardized and may exist
16 # in locations other than lua/
17
18 #=============================================================================
19 # CMake - Cross Platform Makefile Generator
20 # Copyright 2000-2014 Kitware, Inc.
21 # Copyright 2000-2011 Insight Software Consortium
22 # All rights reserved.
23
24 # Redistribution and use in source and binary forms, with or without
25 # modification, are permitted provided that the following conditions
26 # are met:
27
28 # * Redistributions of source code must retain the above copyright
29 # notice, this list of conditions and the following disclaimer.
30
31 # * Redistributions in binary form must reproduce the above copyright
32 # notice, this list of conditions and the following disclaimer in the
33 # documentation and/or other materials provided with the distribution.
34
35 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
36 # nor the names of their contributors may be used to endorse or promote
37 # products derived from this software without specific prior written
38 # permission.
39
40 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #=============================================================================
52 # See the copyright notice: http://www.cmake.org/licensing/
53
54 unset(_lua_include_subdirs)
55 unset(_lua_library_names)
56
57 # this is a function only to have all the variables inside go away automatically
58 function(set_lua_version_vars)
59     set(LUA_VERSIONS5 5.3 5.2 5.1 5.0)
60
61     if (Lua_FIND_VERSION_EXACT)
62         if (Lua_FIND_VERSION_COUNT GREATER 1)
63             set(lua_append_versions ${Lua_FIND_VERSION_MAJOR} ${Lua_FIND_VERSION_MINOR})
64         endif ()
65     elseif (Lua_FIND_VERSION)
66         # once there is a different major version supported this should become a loop
67         if (NOT Lua_FIND_VERSION_MAJOR GREATER 5)
68             if (Lua_FIND_VERSION_COUNT EQUAL 1)
69                 set(lua_append_versions ${LUA_VERSIONS5})
70             else ()
71                 foreach (subver IN LISTS LUA_VERSIONS5)
72                     if (NOT subver VERSION_LESS ${Lua_FIND_VERSION})
73                         list(APPEND lua_append_versions ${subver})
74                     endif ()
75                 endforeach ()
76             endif ()
77         endif ()
78     else ()
79         # once there is a different major version supported this should become a loop
80         set(lua_append_versions ${LUA_VERSIONS5})
81     endif ()
82
83     foreach (ver IN LISTS lua_append_versions)
84         string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}")
85         list(APPEND _lua_include_subdirs
86              include/lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
87              include/lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
88              include/lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
89         )
90         list(APPEND _lua_library_names
91              lua${CMAKE_MATCH_1}${CMAKE_MATCH_2}
92              lua${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
93              lua-${CMAKE_MATCH_1}.${CMAKE_MATCH_2}
94         )
95     endforeach ()
96
97     set(_lua_include_subdirs "${_lua_include_subdirs}" PARENT_SCOPE)
98     set(_lua_library_names "${_lua_library_names}" PARENT_SCOPE)
99 endfunction(set_lua_version_vars)
100
101 set_lua_version_vars()
102
103 find_path(LUA_INCLUDE_DIR lua.h
104   HINTS
105     ENV LUA_DIR
106   PATH_SUFFIXES ${_lua_include_subdirs} include/lua include
107   PATHS
108   ~/Library/Frameworks
109   /Library/Frameworks
110   /sw # Fink
111   /opt/local # DarwinPorts
112   /opt/csw # Blastwave
113   /opt
114 )
115 unset(_lua_include_subdirs)
116
117 find_library(LUA_LIBRARY
118   NAMES ${_lua_library_names} lua
119   HINTS
120     ENV LUA_DIR
121   ""PATH_SUFFIXES lib
122   PATHS
123   ~/Library/Frameworks
124   /Library/Frameworks
125   /sw
126   /opt/local
127   /opt/csw
128   /opt
129 )
130 unset(_lua_library_names)
131
132 if (LUA_LIBRARY)
133     # include the math library for Unix
134     if (UNIX AND NOT APPLE AND NOT BEOS)
135         find_library(LUA_MATH_LIBRARY m)
136         set(LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}")
137     # For Windows and Mac, don't need to explicitly include the math library
138     else ()
139         set(LUA_LIBRARIES "${LUA_LIBRARY}")
140     endif ()
141 endif ()
142
143 if (LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
144     # At least 5.[012] have different ways to express the version
145     # so all of them need to be tested. Lua 5.2 defines LUA_VERSION
146     # and LUA_RELEASE as joined by the C preprocessor, so avoid those.
147     file(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_strings
148          REGEX "^#define[ \t]+LUA_(RELEASE[ \t]+\"Lua [0-9]|VERSION([ \t]+\"Lua [0-9]|_[MR])).*")
149
150     string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MAJOR ";${lua_version_strings};")
151     if (LUA_VERSION_MAJOR MATCHES "^[0-9]+$")
152         string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_MINOR ";${lua_version_strings};")
153         string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUA_VERSION_PATCH ";${lua_version_strings};")
154         set(LUA_VERSION_STRING "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
155     else ()
156         string(REGEX REPLACE ".*;#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
157         if (NOT LUA_VERSION_STRING MATCHES "^[0-9.]+$")
158             string(REGEX REPLACE ".*;#define[ \t]+LUA_VERSION[ \t]+\"Lua ([0-9.]+)\"[ \t]*;.*" "\\1" LUA_VERSION_STRING ";${lua_version_strings};")
159         endif ()
160         string(REGEX REPLACE "^([0-9]+)\\.[0-9.]*$" "\\1" LUA_VERSION_MAJOR "${LUA_VERSION_STRING}")
161         string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[0-9.]*$" "\\1" LUA_VERSION_MINOR "${LUA_VERSION_STRING}")
162         string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUA_VERSION_PATCH "${LUA_VERSION_STRING}")
163     endif ()
164
165     unset(lua_version_strings)
166 endif()
167
168 include(FindPackageHandleStandardArgs)
169 # handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
170 # all listed variables are TRUE
171 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
172                                   REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
173                                   VERSION_VAR LUA_VERSION_STRING)
174
175
176 mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY LUA_MATH_LIBRARY)