Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External lua option: variables unification, propagation, export. #559

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions cmake/LuaFromRocks.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# LuaFromRocks
#
# This module finds Lua either by:
# - vars set by luarocks if called from luarocks install
# - from target isntall dirs if builing our own Lua
# - FindLua() otherwise
#
# Sets variables CMake expects to be set by FindLua:
#
# LUA_LIBRARIES - the lua shared library
# LUA_INCLUDE_DIR - directory for lua includes
# LUA_FOUND - If false, don't attempt to use lua.
# LUA_VERSION_STRING - Lua version
# LUA_EXECUTABLE - Lua executable command
#
# Makes sure LuaRocks variables set even if we are not using LuaRocks:
# LUA_INCDIR - place where lua headers exist
# LUA_LIBDIR - place where lua libraries exist
# LUA_BINDIR - place where lua libraries exist
# SCRIPTS_DIR - usually, same as LUA_BINDIR
# LUADIR - LUA_PATH
# LIBDIR - LUA_CPATH
# LUALIB - the lua library to link against

INCLUDE(CheckLibraryExists)

SET(LUA_EXECUTABLE "${LUA}")

IF(LUA) # if we are using luarocks

SET(LUA_VERSION_STRING ${LUA_VERSION})

MESSAGE(STATUS "Lua: using information from luarocks: ${LUA_VERSION}")

IF(LUALIB) # present on windows platforms only
SET(LUA_LIBRARIES "${LUALIB}")
ELSE() # too bad, luarocks does not provide it (pfff...)
GET_FILENAME_COMPONENT(LUA_EXEC_NAME ${LUA_EXECUTABLE} NAME_WE)
IF(LUA_EXEC_NAME STREQUAL "luajit")
FIND_LIBRARY(LUA_LIBRARIES
NAMES luajit luajit-${LUA_VERSION} libluajit libluajit-${LUA_VERSION}
PATHS ${LUA_LIBDIR}
)
ELSEIF(LUA_EXEC_NAME STREQUAL "lua")
FIND_LIBRARY(LUA_LIBRARIES
NAMES lua liblua lua${LUA_VERSION} liblua${LUA_VERSION}
PATHS ${LUA_LIBDIR}
)
ELSE()
MESSAGE(FATAL_ERROR "You seem to have a non-standard lua installation -- are you using luajit-rocks?")
ENDIF()
MESSAGE(STATUS "Lua library guess (no info from luarocks): ${LUA_LIBRARIES}")
ENDIF()

ELSE() # standalone -- not using luarocks
IF(WITH_NATIVE_LUA)
IF(WITH_LUA51)
FIND_PACKAGE(Lua5.1 REQUIRED)
ELSE()
# To be extended
FIND_PACKAGE(Lua REQUIRED)
ENDIF()
ELSE()
IF (NOT DEFINED ${LUA_INCDIR})
SET(LUA_INCDIR ${CMAKE_INSTALL_PREFIX}/include)
MESSAGE(STATUS "LUA_INCDIR: ${LUA_INCDIR}")
ENDIF(NOT DEFINED ${LUA_INCDIR})
IF (NOT DEFINED ${LUA_LIBDIR})
SET(LUA_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
MESSAGE(STATUS "LUA_LIBDIR: ${LUA_LIBDIR}")
ENDIF(NOT DEFINED ${LUA_LIBDIR})
IF (NOT DEFINED ${LUA_BINDIR})
SET(LUA_BINDIR ${CMAKE_INSTALL_PREFIX}/bin)
MESSAGE(STATUS "LUA_BINDIR: ${LUA_BINDIR}")
ENDIF(NOT DEFINED ${LUA_BINDIR})
IF (NOT DEFINED ${LUALIB})
IF (WITH_LUAJIT21 OR WITH_LUAJIT20)
SET(LUALIB ${LIBRARY_OUTPUT_PATH}/libluajit.so)
ELSE (WITH_LUAJIT21 OR WITH_LUAJIT20)
SET(LUALIB ${LIBRARY_OUTPUT_PATH}/liblua.so)
ENDIF (WITH_LUAJIT21 OR WITH_LUAJIT20)
MESSAGE(STATUS "LUALIB: ${LUALIB}")
ENDIF(NOT DEFINED ${LUALIB})
IF (NOT DEFINED ${LUADIR})
SET(LUADIR ${CMAKE_INSTALL_PREFIX}/${INSTALL_LUA_PATH_SUBDIR})
MESSAGE(STATUS "LUADIR: ${LUADIR}")
ENDIF(NOT DEFINED ${LUADIR})
IF (NOT DEFINED ${LIBDIR})
SET(LIBDIR ${CMAKE_INSTALL_PREFIX}/${INSTALL_LUA_CPATH_SUBDIR})
MESSAGE(STATUS "LIBDIR: ${LIBDIR}")
ENDIF(NOT DEFINED ${LIBDIR})
IF (NOT DEFINED ${LUA_BINDIR})
SET(LUA_BINDIR ${CMAKE_INSTALL_PREFIX}/bin)
ENDIF(NOT DEFINED ${LUA_BINDIR})
IF (NOT DEFINED ${SCRIPTS_DIR})
SET(SCRIPTS_DIR ${CMAKE_INSTALL_PREFIX}/bin)
ENDIF(NOT DEFINED ${SCRIPTS_DIR})
IF (WITH_LUAJIT21 OR WITH_LUAJIT20)
SET(LUA "luajit")
ELSE (WITH_LUAJIT21 OR WITH_LUAJIT20)
SET(LUA "lua")
ENDIF (WITH_LUAJIT21 OR WITH_LUAJIT20)
ENDIF(WITH_NATIVE_LUA)
ENDIF()

NORMALIZE_PATH(LUA_BINDIR)
NORMALIZE_PATH(LUA_LIBDIR)
NORMALIZE_PATH(LUA_INCDIR)

NORMALIZE_PATH(LUADIR)
NORMALIZE_PATH(LIBDIR)
NORMALIZE_PATH(BINDIR)

SET(LUA_EXECUTABLE "${LUA}")
SET(LUA_INCLUDE_DIR "${LUA_INCDIR}")

MESSAGE(STATUS "LUA_BINDIR: ${LUA_BINDIR}")
MESSAGE(STATUS "SCRIPTS_DIR: ${SCRIPTS_DIR}")
MESSAGE(STATUS "Lua library: ${LUA_LIBRARIES}")

INCLUDE_DIRECTORIES(${LUA_INCDIR})

IF(LUA_LIBRARIES)
CHECK_LIBRARY_EXISTS(${LUA_LIBRARIES} luaJIT_setmode "" LUA_JIT)
ENDIF()

MARK_AS_ADVANCED(
LUA
LUA_LIBRARIES
LUA_INCLUDE_DIR
)

# MESSAGE("LUA_INCLUDE_DIR is ${LUA_INCDIR}")
# MESSAGE("LUA_VERSION_STRING is ${LUA_VERSION_STRING}")
3 changes: 3 additions & 0 deletions cmake/TorchConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ SET(Torch_INSTALL_LUA_PATH_SUBDIR "@Torch_INSTALL_LUA_PATH_SUBDIR@")
SET(Torch_INSTALL_LUA_CPATH_SUBDIR "@Torch_INSTALL_LUA_CPATH_SUBDIR@")
SET(Torch_INSTALL_CMAKE_RIDBUS "@Torch_INSTALL_CMAKE_RIDBUS@")

SET(Torch_LUA_LIBRARIES "@LUA_LIBRARIES@")
SET(Torch_LUA_INCLUDE_DIR "@LUA_INCLUDE_DIR@")

FILE(RELATIVE_PATH Torch_INSTALL_LUA_PATH_SUBDIR "${Torch_INSTALL_PREFIX}" "${CMAKE_INSTALL_PREFIX}/lua")
FILE(RELATIVE_PATH Torch_INSTALL_LUA_CPATH_SUBDIR "${Torch_INSTALL_PREFIX}" "${CMAKE_INSTALL_PREFIX}/lib")

Expand Down
25 changes: 16 additions & 9 deletions cmake/TorchPaths.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ MACRO(NORMALIZE_PATH _path_)
SET(${_path_} "${${_path_}_abs}")
ENDMACRO()

NORMALIZE_PATH(LUA_BINDIR)
NORMALIZE_PATH(LUA_LIBDIR)
NORMALIZE_PATH(LUA_INCDIR)
NORMALIZE_PATH(LUADIR)
NORMALIZE_PATH(LIBDIR)
#
# This gets standard FindLua variables
#
INCLUDE(LuaFromRocks)

GET_FILENAME_COMPONENT(CMAKE_INSTALL_PREFIX "${LUA_BINDIR}" PATH)
# This is the root of Lua tree that Luarocks used in the build is configured for
# May not be the same as Torch (current) installation directory
GET_FILENAME_COMPONENT(LUA_ROOT "${LUA_BINDIR}" PATH)

NORMALIZE_PATH(CMAKE_INSTALL_PREFIX)
SET(Torch_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
FILE(RELATIVE_PATH Torch_INSTALL_BIN_SUBDIR "${CMAKE_INSTALL_PREFIX}" "${LUA_BINDIR}")
FILE(RELATIVE_PATH Torch_INSTALL_LIB_SUBDIR "${CMAKE_INSTALL_PREFIX}" "${LUA_LIBDIR}")
FILE(RELATIVE_PATH Torch_INSTALL_INCLUDE_SUBDIR "${CMAKE_INSTALL_PREFIX}" "${LUA_INCDIR}")

# non-per-moduel install subdirectories: mimic Lua tree, possibly external
# use source Lua variables and LUA_ROOT (which may differ from Torch_INSTALL_PREFIX)
FILE(RELATIVE_PATH Torch_INSTALL_BIN_SUBDIR "${LUA_ROOT}" "${LUA_BINDIR}")
FILE(RELATIVE_PATH Torch_INSTALL_LIB_SUBDIR "${LUA_ROOT}" "${LUA_LIBDIR}")
FILE(RELATIVE_PATH Torch_INSTALL_INCLUDE_SUBDIR "${LUA_ROOT}" "${LUA_INCDIR}")

SET(Torch_INSTALL_MAN_SUBDIR "share/man" CACHE PATH
"Install dir for man pages (relative to Torch_INSTALL_PREFIX)")
Expand All @@ -28,5 +33,7 @@ SET(Torch_INSTALL_SHARE_SUBDIR "share" CACHE PATH
SET(Torch_INSTALL_CMAKE_SUBDIR "share/cmake/torch" CACHE PATH
"Install dir for .cmake files (relative to Torch_INSTALL_PREFIX)")

# Per-module install subdirectories: use target Lua variables
FILE(RELATIVE_PATH Torch_INSTALL_LUA_PATH_SUBDIR "${CMAKE_INSTALL_PREFIX}" "${LUADIR}")
FILE(RELATIVE_PATH Torch_INSTALL_LUA_CPATH_SUBDIR "${CMAKE_INSTALL_PREFIX}" "${LIBDIR}")
FILE(RELATIVE_PATH Torch_INSTALL_LUA_BINPATH_SUBDIR "${CMAKE_INSTALL_PREFIX}" "${BINDIR}")
5 changes: 2 additions & 3 deletions rocks/torch-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ dependencies = {
build = {
type = "command",
build_command = [[
cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DLUA=$(LUA) -DLUALIB=$(LUALIB) -DLUA_BINDIR="$(LUA_BINDIR)" -DLUA_INCDIR="$(LUA_INCDIR)" -DLUA_LIBDIR="$(LUA_LIBDIR)" -DLUADIR="$(LUADIR)" -DLIBDIR="$(LIBDIR)" -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE) -j$(getconf _NPROCESSORS_ONLN)
cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DLUA=$(LUA) -DLUALIB=$(LUALIB) -DLUA_BINDIR="$(LUA_BINDIR)" -DLUA_INCDIR="$(LUA_INCDIR)" -DLUA_LIBDIR="$(LUA_LIBDIR)" -DLIBDIR="$(LIBDIR)" -DLUADIR="$(LUADIR)" -DCMAKE_INSTALL_PREFIX="$(SCRIPTS_DIR)/.." && $(MAKE) -j$(getconf _NPROCESSORS_ONLN)
]],
platforms = {
windows = {
build_command = [[
cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DLUA=$(LUA) -DLUALIB=$(LUALIB) -DLUA_BINDIR="$(LUA_BINDIR)" -DLUA_INCDIR="$(LUA_INCDIR)" -DLUA_LIBDIR="$(LUA_LIBDIR)" -DLUADIR="$(LUADIR)" -DLIBDIR="$(LIBDIR)" -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)
cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DLUA=$(LUA) -DLUALIB=$(LUALIB) -DLUA_BINDIR="$(LUA_BINDIR)" -DLUA_INCDIR="$(LUA_INCDIR)" -DLUA_LIBDIR="$(LUA_LIBDIR)" -DLUADIR="$(LUADIR)" -DLIBDIR="$(LIBDIR)" -DCMAKE_INSTALL_PREFIX="$(SCRIPTS_DIR)/.." && $(MAKE)
]]
}
},
install_command = "cd build && $(MAKE) install"
}