Skip to content

Commit

Permalink
tweak the build script.
Browse files Browse the repository at this point in the history
now support:
- PUC lua can be built with luv.
- now can build dll library against lua/luajit (lua53.dll/lua51.dll)

so we support all combines below:
- static/dynamic luv library
- static/dynamic/system Lua library
- PUC Lua/LuaJIT implement (+1 squashed commits)

Squashed commits:

add Lua5.3 support
add missing ASM to project()
use new options in Makefile
update Lua repo version.
fix typo
  • Loading branch information
starwing committed Jun 29, 2015
1 parent 1af5a36 commit 7f6ad65
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -4,3 +4,6 @@
[submodule "luajit"]
path = deps/luajit
url = http://luajit.org/git/luajit-2.0.git
[submodule "lua"]
path = deps/lua
url = https://github.com/lua/lua
78 changes: 65 additions & 13 deletions CMakeLists.txt
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.8)

project (luv)
project (luv C ASM)

set(LUV_VERSION_MAJOR 1)
set(LUV_VERSION_MINOR 4)
Expand All @@ -10,7 +10,26 @@ set(LUV_VERSION ${LUV_VERSION_MAJOR}.${LUV_VERSION_MINOR}.${LUV_VERSION_PATCH})
option(BUILD_MODULE "Build as module" ON)
option(BUILD_SHARED_LIBS "Build shared library" OFF)
option(WITH_SHARED_LIBUV "Link to a shared libuv library instead of static linking" OFF)
option(WITH_SHARED_LUAJIT "Link to a shared LuaJIT library instead of static linking" OFF)

if (NOT WITH_LUA_ENGINE)
set(WITH_LUA_ENGINE "LuaJIT"
CACHE STRING "Link to LuaJIT or PUC Lua" FORCE)
set_property(CACHE WITH_LUA_ENGINE
PROPERTY STRINGS "Lua;LuaJIT")
endif (NOT WITH_LUA_ENGINE)

if (NOT LUA_BUILD_TYPE)
set(LUA_BUILD_TYPE "Static"
CACHE STRING "Build Lua/LuaJIT as static, dynamic libary, or use system one" FORCE)
set_property(CACHE LUA_BUILD_TYPE
PROPERTY STRINGS "Static;Dynamic;System")
endif (NOT LUA_BUILD_TYPE)

if (WITH_LUA_ENGINE STREQUAL Lua)
set(USE_LUAJIT OFF)
else ()
set(USE_LUAJIT ON)
endif ()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

Expand All @@ -28,16 +47,35 @@ else (WITH_SHARED_LIBUV)
include(deps/uv.cmake)
endif (WITH_SHARED_LIBUV)

if (WITH_SHARED_LUAJIT)
find_package(LuaJIT)
if (LUAJIT_FOUND)
include_directories(${LUAJIT_INCLUDE_DIR})
link_directories(${LUAJIT_LIBRARIES})
endif (LUAJIT_FOUND)
else (WITH_SHARED_LUAJIT)
include(deps/luajit.cmake)
include_directories(deps/luajit/src)
endif (WITH_SHARED_LUAJIT)
if (LUA_BUILD_TYPE STREQUAL System)
if (USE_LUAJIT)
find_package(LuaJIT)
if (LUAJIT_FOUND)
include_directories(${LUAJIT_INCLUDE_DIR})
link_directories(${LUAJIT_LIBRARIES})
endif (LUAJIT_FOUND)
else (USE_LUAJIT)
find_package(Lua)
if (LUA_FOUND)
include_directories(${LUA_INCLUDE_DIR})
link_directories(${LUA_LIBRARIES})
endif (LUA_FOUND)
endif (USE_LUAJIT)

else (LUA_BUILD_TYPE STREQUAL System)
if (LUA_BUILD_TYPE STREQUAL Static)
SET(WITH_SHARED_LUA OFF)
else (LUA_BUILD_TYPE STREQUAL Static)
SET(WITH_SHARED_LUA ON)
endif (LUA_BUILD_TYPE STREQUAL Static)
if (USE_LUAJIT)
include(deps/luajit.cmake)
include_directories(deps/luajit/src)
else(USE_LUAJIT)
include(deps/lua.cmake)
include_directories(deps/lua/src)
endif (USE_LUAJIT)
endif (LUA_BUILD_TYPE STREQUAL System)

if (BUILD_MODULE)
add_library(luv MODULE src/luv.c)
Expand All @@ -62,7 +100,21 @@ endif()

if(WIN32)
add_definitions(-DLUA_BUILD_AS_DLL -DLUA_LIB)
target_link_libraries(luv uv luajit-5.1)
if (USE_LUAJIT)
target_link_libraries(luv uv luajit-5.1)
else (USE_LUAJIT)
target_link_libraries(luv uv lualib)
endif (USE_LUAJIT)
# replace /MD to /MT to avoid link msvcr*.dll
set(CompilerFlags
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_RELEASE)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
target_link_libraries(luv uv rt)
else()
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Expand Up @@ -7,13 +7,15 @@ endif
BUILD_MODULE ?= ON
BUILD_SHARED_LIBS ?= OFF
WITH_SHARED_LIBUV ?= OFF
WITH_SHARED_LUAJIT ?= OFF
WITH_LUA_ENGINE ?= LuaJIT
LUA_BUILD_TYPE ?= Static

CMAKE_OPTIONS += \
-DBUILD_MODULE=$(BUILD_MODULE) \
-DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) \
-DWITH_SHARED_LIBUV=$(WITH_SHARED_LIBUV) \
-DWITH_SHARED_LUAJIT=$(WITH_SHARED_LUAJIT)
-DWITH_LUA_ENGINE=$(WITH_LUA_ENGINE) \
-DLUA_BUILD_TYPE=$(LUA_BUILD_TYPE) \

all: luv

Expand Down
1 change: 1 addition & 0 deletions deps/lua
Submodule lua added at 34c362
128 changes: 128 additions & 0 deletions deps/lua.cmake
@@ -0,0 +1,128 @@
# Modfied from luajit.cmake
# Added LUAJIT_ADD_EXECUTABLE Ryan Phillips <ryan at trolocsis.com>
# This CMakeLists.txt has been first taken from LuaDist
# Copyright (C) 2007-2011 LuaDist.
# Created by Peter Drahoš
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# Debugged and (now seriously) modified by Ronan Collobert, for Torch7

#project(Lua53 C)

SET(LUA_DIR ${CMAKE_CURRENT_LIST_DIR}/lua)

SET(CMAKE_REQUIRED_INCLUDES
${LUA_DIR}
${LUA_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}
)

OPTION(WITH_AMALG "Build eveything in one shot (needs memory)" ON)

# Ugly warnings
IF(MSVC)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
ENDIF()

# Various includes
INCLUDE(CheckLibraryExists)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckCSourceCompiles)
INCLUDE(CheckTypeSize)

CHECK_TYPE_SIZE("void*" SIZEOF_VOID_P)
IF(SIZEOF_VOID_P EQUAL 8)
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE)
ENDIF()

IF(NOT WIN32)
FIND_LIBRARY(DL_LIBRARY "dl")
IF(DL_LIBRARY)
SET(CMAKE_REQUIRED_LIBRARIES ${DL_LIBRARY})
LIST(APPEND LIBS ${DL_LIBRARY})
ENDIF(DL_LIBRARY)
CHECK_FUNCTION_EXISTS(dlopen LUA_USE_DLOPEN)
IF(NOT LUA_USE_DLOPEN)
MESSAGE(FATAL_ERROR "Cannot compile a useful lua.
Function dlopen() seems not to be supported on your platform.
Apparently you are not on a Windows platform as well.
So lua has no way to deal with shared libraries!")
ENDIF(NOT LUA_USE_DLOPEN)
ENDIF(NOT WIN32)

check_library_exists(m sin "" LUA_USE_LIBM)
if ( LUA_USE_LIBM )
list ( APPEND LIBS m )
endif ()

## SOURCES
SET(SRC_LUALIB
${LUA_DIR}/src/lbaselib.c
${LUA_DIR}/src/lcorolib.c
${LUA_DIR}/src/ldblib.c
${LUA_DIR}/src/liolib.c
${LUA_DIR}/src/lmathlib.c
${LUA_DIR}/src/loadlib.c
${LUA_DIR}/src/loslib.c
${LUA_DIR}/src/lstrlib.c
${LUA_DIR}/src/ltablib.c
${LUA_DIR}/src/lutf8lib.c)

SET(SRC_LUACORE
${LUA_DIR}/src/lauxlib.c
${LUA_DIR}/src/lapi.c
${LUA_DIR}/src/lcode.c
${LUA_DIR}/src/lctype.c
${LUA_DIR}/src/ldebug.c
${LUA_DIR}/src/ldo.c
${LUA_DIR}/src/ldump.c
${LUA_DIR}/src/lfunc.c
${LUA_DIR}/src/lgc.c
${LUA_DIR}/src/linit.c
${LUA_DIR}/src/llex.c
${LUA_DIR}/src/lmem.c
${LUA_DIR}/src/lobject.c
${LUA_DIR}/src/lopcodes.c
${LUA_DIR}/src/lparser.c
${LUA_DIR}/src/lstate.c
${LUA_DIR}/src/lstring.c
${LUA_DIR}/src/ltable.c
${LUA_DIR}/src/ltm.c
${LUA_DIR}/src/lundump.c
${LUA_DIR}/src/lvm.c
${LUA_DIR}/src/lzio.c
${SRC_LUALIB})

## GENERATE

IF(WITH_SHARED_LUA)
IF(WITH_AMALG)
add_library(lualib SHARED ${LUA_DIR}/../lua_one.c ${DEPS})
ELSE()
add_library(lualib SHARED ${SRC_LUACORE} ${DEPS} )
ENDIF()
ELSE()
IF(WITH_AMALG)
add_library(lualib STATIC ${LUA_DIR}/../lua_one.c ${DEPS} )
ELSE()
add_library(lualib STATIC ${SRC_LUACORE} ${DEPS} )
ENDIF()
set_target_properties(lualib PROPERTIES
PREFIX "lib" IMPORT_PREFIX "lib")
ENDIF()

target_link_libraries (lualib ${LIBS} )
set_target_properties (lualib PROPERTIES OUTPUT_NAME "lua53")

IF(WIN32)
add_executable(lua ${LUA_DIR}/src/lua.c)
target_link_libraries(lua lualib)
ELSE()
IF(WITH_AMALG)
add_executable(lua ${LUA_DIR}/src/lua.c ${LUA_DIR}/lua_one.c ${DEPS})
ELSE()
add_executable(lua ${LUA_DIR}/src/lua.c ${SRC_LUACORE} ${DEPS})
ENDIF()
target_link_libraries(lua ${LIBS})
SET_TARGET_PROPERTIES(lua PROPERTIES ENABLE_EXPORTS ON)
ENDIF(WIN32)

97 changes: 97 additions & 0 deletions deps/lua_one.c
@@ -0,0 +1,97 @@
/*

This comment has been minimized.

Copy link
@creationix

creationix Jul 19, 2016

Member

@starwing Do you know the licence/origin of this file?

This comment has been minimized.

Copy link
@daurnimator

daurnimator Jul 19, 2016

Contributor
* one.c -- Lua core, libraries, and interpreter in a single file
*/

/* default is to build the full interpreter */
#ifndef MAKE_LIB
#ifndef MAKE_LUAC
#ifndef MAKE_LUA
#define MAKE_LIB
#endif
#endif
#endif

/* choose suitable platform-specific features */
/* some of these may need extra libraries such as -ldl -lreadline -lncurses */
#if 0
#define LUA_USE_LINUX
#define LUA_USE_MACOSX
#define LUA_USE_POSIX
#define LUA_ANSI
#endif

/* no need to change anything below this line ----------------------------- */

/* setup for luaconf.h */
#if HAVE_LPREFIX
# include "lprefix.h"
#endif

#define LUA_CORE
#define LUA_LIB
#define ltable_c
#define lvm_c
#include "luaconf.h"

/* do not export internal symbols */
#undef LUAI_FUNC
#undef LUAI_DDEC
#undef LUAI_DDEF
#define LUAI_FUNC static
#define LUAI_DDEC static
#define LUAI_DDEF static

/* core -- used by all */
#include "lapi.c"
#include "lcode.c"
#include "lctype.c"
#include "ldebug.c"
#include "ldo.c"
#include "ldump.c"
#include "lfunc.c"
#include "lgc.c"
#include "llex.c"
#include "lmem.c"
#include "lobject.c"
#include "lopcodes.c"
#include "lparser.c"
#include "lstate.c"
#include "lstring.c"
#include "ltable.c"
#include "ltm.c"
#include "lundump.c"
#include "lvm.c"
#include "lzio.c"

/* auxiliary library -- used by all */
#include "lauxlib.c"

/* standard library -- not used by luac */
#ifndef MAKE_LUAC
#include "lbaselib.c"
#if LUA_VERSION_NUM == 502
# include "lbitlib.c"
#endif
#include "lcorolib.c"
#include "ldblib.c"
#include "liolib.c"
#include "lmathlib.c"
#include "loadlib.c"
#include "loslib.c"
#include "lstrlib.c"
#include "ltablib.c"
#if LUA_VERSION_NUM >= 503
# include "lutf8lib.c"
#endif
#include "linit.c"
#endif

/* lua */
#ifdef MAKE_LUA
#include "lua.c"
#endif

/* luac */
#ifdef MAKE_LUAC
#include "luac.c"
#endif
19 changes: 15 additions & 4 deletions deps/luajit.cmake
Expand Up @@ -5,7 +5,7 @@
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# Debugged and (now seriously) modified by Ronan Collobert, for Torch7

project(LuaJIT C ASM)
#project(LuaJIT C ASM)

SET(LUAJIT_DIR ${CMAKE_CURRENT_LIST_DIR}/luajit)

Expand Down Expand Up @@ -324,10 +324,21 @@ include_directories(
${CMAKE_CURRENT_BINARY_DIR}
)

IF(WITH_AMALG)
add_library(luajit-5.1 STATIC ${LUAJIT_DIR}/src/ljamalg.c ${DEPS} )
IF(WITH_SHARED_LUA)
IF(WITH_AMALG)
add_library(luajit-5.1 SHARED ${LUAJIT_DIR}/src/ljamalg.c ${DEPS} )
ELSE()
add_library(luajit-5.1 SHARED ${SRC_LJCORE} ${DEPS} )
ENDIF()
SET_TARGET_PROPERTIES(luajit-5.1 PROPERTIES OUTPUT_NAME "lua51")
ELSE()
add_library(luajit-5.1 STATIC ${SRC_LJCORE} ${DEPS} )
IF(WITH_AMALG)
add_library(luajit-5.1 STATIC ${LUAJIT_DIR}/src/ljamalg.c ${DEPS} )
ELSE()
add_library(luajit-5.1 STATIC ${SRC_LJCORE} ${DEPS} )
ENDIF()
SET_TARGET_PROPERTIES(luajit-5.1 PROPERTIES
PREFIX "lib" IMPORT_PREFIX "lib" OUTPUT_NAME "luajit")
ENDIF()

target_link_libraries (luajit-5.1 ${LIBS} )
Expand Down

0 comments on commit 7f6ad65

Please sign in to comment.