Skip to content

Commit

Permalink
add lua_add_executable() cmake macro for plain Lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
starwing committed Feb 19, 2016
1 parent 7635785 commit 7acadf2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ if (NOT LUA_BUILD_TYPE)
endif (NOT LUA_BUILD_TYPE)

if (WITH_LUA_ENGINE STREQUAL Lua)
add_definitions(-DLUA_USE_DLOPEN)
if (NOT WIN32)
add_definitions(-DLUA_USE_DLOPEN)
endif (NOT WIN32)
set(USE_LUAJIT OFF)
else ()
set(USE_LUAJIT ON)
Expand Down
60 changes: 52 additions & 8 deletions deps/lua.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Modfied from luajit.cmake
# Added LUAJIT_ADD_EXECUTABLE Ryan Phillips <ryan at trolocsis.com>
# Added LUA_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š
Expand All @@ -8,7 +8,7 @@

#project(Lua53 C)

SET(LUA_DIR ${CMAKE_CURRENT_LIST_DIR}/lua)
SET(LUA_DIR ${CMAKE_CURRENT_LIST_DIR}/lua CACHE PATH "location of lua sources")

SET(CMAKE_REQUIRED_INCLUDES
${LUA_DIR}
Expand Down Expand Up @@ -96,15 +96,15 @@ SET(SRC_LUACORE

IF(WITH_SHARED_LUA)
IF(WITH_AMALG)
add_library(lualib SHARED ${LUA_DIR}/../lua_one.c ${DEPS})
add_library(lualib SHARED ${LUA_DIR}/../lua_one.c)
ELSE()
add_library(lualib SHARED ${SRC_LUACORE} ${DEPS} )
add_library(lualib SHARED ${SRC_LUACORE})
ENDIF()
ELSE()
IF(WITH_AMALG)
add_library(lualib STATIC ${LUA_DIR}/../lua_one.c ${DEPS} )
add_library(lualib STATIC ${LUA_DIR}/../lua_one.c )
ELSE()
add_library(lualib STATIC ${SRC_LUACORE} ${DEPS} )
add_library(lualib STATIC ${SRC_LUACORE} )
ENDIF()
set_target_properties(lualib PROPERTIES
PREFIX "lib" IMPORT_PREFIX "lib")
Expand All @@ -118,11 +118,55 @@ IF(WIN32)
target_link_libraries(lua lualib)
ELSE()
IF(WITH_AMALG)
add_executable(lua ${LUA_DIR}/src/lua.c ${LUA_DIR}/lua_one.c ${DEPS})
add_executable(lua ${LUA_DIR}/src/lua.c ${LUA_DIR}/lua_one.c)
ELSE()
add_executable(lua ${LUA_DIR}/src/lua.c ${SRC_LUACORE} ${DEPS})
add_executable(lua ${LUA_DIR}/src/lua.c ${SRC_LUACORE})
ENDIF()
target_link_libraries(lua ${LIBS})
SET_TARGET_PROPERTIES(lua PROPERTIES ENABLE_EXPORTS ON)
ENDIF(WIN32)

MACRO(LUA_add_custom_commands luajit_target)
SET(target_srcs "")
FOREACH(file ${ARGN})
IF(${file} MATCHES ".*\\.lua$")
set(file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
set(source_file ${file})
string(LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
string(LENGTH ${file} _luajit_file_length)
math(EXPR _begin "${_luajit_source_dir_length} + 1")
math(EXPR _stripped_file_length "${_luajit_file_length} - ${_luajit_source_dir_length} - 1")
string(SUBSTRING ${file} ${_begin} ${_stripped_file_length} stripped_file)

set(generated_file "${CMAKE_BINARY_DIR}/luacode_tmp/${stripped_file}_${luajit_target}_generated.c")

add_custom_command(
OUTPUT ${generated_file}
MAIN_DEPENDENCY ${source_file}
DEPENDS lua
COMMAND lua
ARGS "${LUA_DIR}/../luac.lua"
${source_file}
${generated_file}
COMMENT "Building Lua ${source_file}: ${generated_file}"
)

get_filename_component(basedir ${generated_file} PATH)
file(MAKE_DIRECTORY ${basedir})

set(target_srcs ${target_srcs} ${generated_file})
set_source_files_properties(
${generated_file}
properties
generated true # to say that "it is OK that the obj-files do not exist before build time"
)
ELSE()
set(target_srcs ${target_srcs} ${file})
ENDIF(${file} MATCHES ".*\\.lua$")
ENDFOREACH(file)
ENDMACRO()

MACRO(LUA_ADD_EXECUTABLE luajit_target)
LUA_add_custom_commands(${luajit_target} ${ARGN})
add_executable(${luajit_target} ${target_srcs})
ENDMACRO(LUA_ADD_EXECUTABLE luajit_target)
82 changes: 82 additions & 0 deletions deps/luac.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
local src, gen = ...

This comment has been minimized.

Copy link
@creationix

creationix Jul 19, 2016

Member

@starwing Do you know the origin of this file? If you wrote it, can it be licensed the same as the rest of luvit? If you copied it from somewhere, what was the original license?

This comment has been minimized.

Copy link
@starwing

starwing Jul 19, 2016

Author Contributor

I write this file, it can be licensed the same as luvit :)


local chunk = assert(loadfile(src, nil, '@'..src))
local bytecode = string.dump(chunk)

local function basename(name)
local base = name
if base:match "[/\\]" then
base = name:match("^.*[/\\](.*)$")
end
base = base:gsub("^%.", "_")
if base:match "%." then
base = base:match("^(.*)%."):gsub("%.", "_")
end
return base
end

local function escapefn(name)
return '"'..
name:gsub('\\', '\\\\')
:gsub('\n', '\\n')
:gsub('\r', '\\r')
:gsub('"', '\\"')..'"'
end

local function write_chunk(s)
local t = { "{\n " };
local cc = 7
for i = 1, #s do
local c = string.byte(s, i, i)
local ss = (" 0x%X"):format(c)
if cc + #ss > 77 then
t[#t+1] = "\n "
t[#t+1] = ss
cc = 7 + #ss
if i ~= #s then
t[#t+1] = ","
cc = cc + 1
end
else
t[#t+1] = ss
cc = cc + #ss
if i ~= #s then
t[#t+1] = ","
cc = cc + 1
end
end
end
t[#t+1] = "\n }"
return (table.concat(t))
end

local function W(...)
io.write(...)
return W
end

io.output(gen)
W [[
/* generated source for Lua codes */
#ifndef LUA_LIB
# define LUA_LIB
#endif
#include <lua.h>
#include <lauxlib.h>
LUALIB_API int luaopen_]](basename(src))[[(lua_State *L) {
size_t len = ]](#bytecode)[[;
const char chunk[] = ]](write_chunk(bytecode))[[;
if (luaL_loadbuffer(L, chunk, len, ]](escapefn(src))[[) != 0)
lua_error(L);
lua_insert(L, 1);
lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
return lua_gettop(L);
}
]]
io.close()


7 changes: 4 additions & 3 deletions deps/luajit.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Added LUAJIT_ADD_EXECUTABLE Ryan Phillips <ryan at trolocsis.com>
# Added LUA_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š
Expand Down Expand Up @@ -401,7 +401,8 @@ MACRO(LUAJIT_add_custom_commands luajit_target)
ENDFOREACH(file)
ENDMACRO()

MACRO(LUAJIT_ADD_EXECUTABLE luajit_target)
MACRO(LUA_ADD_EXECUTABLE luajit_target)
LUAJIT_add_custom_commands(${luajit_target} ${ARGN})
add_executable(${luajit_target} ${target_srcs})
ENDMACRO(LUAJIT_ADD_EXECUTABLE luajit_target)
ENDMACRO(LUA_ADD_EXECUTABLE luajit_target)

0 comments on commit 7acadf2

Please sign in to comment.