lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Saturday 03, David Manura wrote:
> On Thu, Oct 1, 2009 at 11:06 PM, Robert G. Jakabosky wrote:
> > Attached is a patch to add CMake build files to Lua 5.1.  I have only
> > tested them on Linux, but they should work for atleast Macs & Windows
> > systems.  Also some of the LUA_USE_* CMake options don't do anything
> > right now, but they can still be changed in src/luaconf.h
>
> With some further adjustments, I would also suggest considering adding
> this to the Lua 5.1 distributions because it handles most platforms
> relatively painlessly, including shared library builds which are
> partially lacking in the existing build files.

Thanks for the feedback.  I have updated the patch to include those changes.  
Now all the LUA_USE_* options work.

> A few comments:
>
> BUILD_STATIC had to be forced to OFF when applying the flag
> -DLUA_BUILD_AS_DLL to avoid "undefined reference to
> `__imp__lua_sethook'" linker errors.

Now BUILD_STATIC is forced OFF on WIN32 and is not provided as an option.

> In the CMAKE_SYSTEM_NAME, swap the UNIX and WIN32 tests, I think.
> Cygwin is both UNIX and WIN32, but its more UNIX.  At least that was
> required to load a lfs.so shared library module built from LuaDist.

I have added a CYGWIN section, it is just the same as the UNIX one right now.  
Also I re-worked the system detection and applied different default values to 
all the options based on the system type.

> Under gcc 3.4.6/Linux I got an 'error: unrecognized command line
> option "-fgnu89-inline"'  This had warnings though.

I just removed that flag for now.

> For comparison, see our CMakeLists.txt [1] for Lua 5.1.4 used in
> LuaDist [2], which has been tested on Linux, Cygwin, MinGW, MSVC, Mac,
> and maybe others.  Ours is a single file and simpler, but it's
> currently less configurable and doesn't yet handle CPack.  Maybe we
> can converge to a single version of the CMakeLists.txt.

I am used to using one CMakeLists.txt files for each sub-folder to keep things 
a bit more organized, and so the build file doesn't grow to be massive.

This new patch includes some of the features from LuaDist CMakeLists.txt file.

-- 
Robert G. Jakabosky
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..22037e0
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,197 @@
+#
+# Lua 5.1.x
+#
+cmake_minimum_required(VERSION 2.4 FATAL_ERROR)
+
+project(lua C)
+
+set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
+
+include(CustomMacros)
+
+enable_testing()
+
+set(COMMON_CFLAGS)
+set(COMMON_LDFLAGS)
+set(LIBS)
+#
+# Detect system type
+#
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+	set(DEFAULT_POSIX TRUE)
+	set(DEFAULT_DLOPEN ON)
+	set(DEFAULT_READLINE ON)
+	set(COMMON_LDFLAGS "${COMMON_LDFLAGS} -Wl,-E")
+else(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+	if(APPLE)
+		set(DEFAULT_POSIX TRUE)
+		set(DEFAULT_DLOPEN ON)
+		# use this on Mac OS X 10.3-
+		option(LUA_USE_MACOSX "Mac OS X 10.3-" OFF)
+	elseif(CYGWIN)
+		set(DEFAULT_POSIX TRUE)
+	elseif(UNIX)
+		set(DEFAULT_POSIX TRUE)
+	elseif(WIN32)
+		set(LUA_WIN TRUE)
+		set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_BUILD_AS_DLL")
+	else(APPLE)
+		set(DEFAULT_ANSI TRUE)
+	endif(APPLE)
+endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+
+#
+# setup config options with default values.
+#
+if(WIN32)
+	set(BUILD_STATIC OFF)
+else(WIN32)
+	option(BUILD_STATIC "build static library" ON)
+endif(WIN32)
+
+if(DEFAULT_DLOPEN)
+	option(LUA_USE_DLOPEN "Enable dlopen support." ON)
+else(DEFAULT_DLOPEN)
+	option(LUA_USE_DLOPEN "Enable dlopen support." OFF)
+endif(DEFAULT_DLOPEN)
+
+if(DEFAULT_POSIX)
+	option(LUA_USE_CURSES "Enable Curses support." ON)
+	option(LUA_USE_MKSTEMP "Use mkstemp." ON)
+	option(LUA_USE_ISATTY "Enable isatty support." ON)
+	option(LUA_USE_POPEN "Enable lua_popen support." ON)
+	option(LUA_USE_ULONGJMP "Try using _longjmp/_setjmp (more efficient)" ON)
+else(DEFAULT_POSIX)
+	option(LUA_USE_CURSES "Enable Curses support." OFF)
+	option(LUA_USE_MKSTEMP "Use mkstemp." OFF)
+	option(LUA_USE_ISATTY "Enable isatty support." OFF)
+	option(LUA_USE_POPEN "Enable lua_popen support." OFF)
+	option(LUA_USE_ULONGJMP "Try using _longjmp/_setjmp (more efficient)" OFF)
+endif(DEFAULT_POSIX)
+
+if(DEFAULT_READLINE)
+	option(LUA_USE_READLINE "Enable readline support." ON)
+else(DEFAULT_READLINE)
+	option(LUA_USE_READLINE "Enable readline support." OFF)
+endif(DEFAULT_READLINE)
+
+if(DEFAULT_ANSI)
+	option(LUA_ANSI "Disable non-ansi features." ON)
+else(DEFAULT_ANSI)
+	option(LUA_ANSI "Disable non-ansi features." OFF)
+endif(DEFAULT_ANSI)
+
+option(LUA_USE_APICHECK "Enable API checks." OFF)
+
+#
+# Lua version
+#
+set(LUA_VERSION_MAJOR 5)
+set(LUA_VERSION_MINOR 1)
+set(LUA_VERSION_PATCH 4)
+set(LUA_VERSION
+				"${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
+set(LUA_SOVERSION
+				"${LUA_VERSION_MAJOR}")
+#
+# Lua package info.
+#
+set(CPACK_PACKAGE_VERSION_MAJOR ${LUA_VERSION_MAJOR})
+set(CPACK_PACKAGE_VERSION_MINOR ${LUA_VERSION_MINOR})
+set(CPACK_PACKAGE_VERSION_PATCH ${LUA_VERSION_PATCH})
+set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYRIGHT")
+set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README")
+set(CPACK_PACKAGE_VENDOR "Lua.org, PUC-Rio.")
+set(CPACK_SOURCE_GENERATOR "TGZ")
+set(CPACK_SOURCE_IGNORE_FILES
+"/\\\\.;/\\\\.git.*/;~$;build/;CMakeFiles/;CMakeCache;Testing/;cmake_install;CPack;Dart;Makefile$")
+set(CPACK_SOURCE_PACKAGE_FILE_NAME
+	"${CMAKE_PROJECT_NAME}-${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}.${LUA_VERSION_PATCH}")
+# MUST be after CPACK_* variables.
+include(CPack)
+
+#
+# libs & cflags
+#
+set(COMMON_LDFLAGS "${COMMON_LDFLAGS} -lm ")
+
+# For "Mac OS X 10.3-"
+if(LUA_USE_MACOSX)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_MACOSX")
+	set(LUA_USE_DLOPEN FALSE)
+endif(LUA_USE_MACOSX)
+
+# enable options
+if(LUA_USE_DLOPEN)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_DLOPEN")
+	if(NOT APPLE)
+		set(COMMON_LDFLAGS "${COMMON_LDFLAGS} -ldl ")
+	endif(NOT APPLE)
+endif(LUA_USE_DLOPEN)
+if(LUA_USE_MKSTEMP)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_MKSTEMP")
+endif(LUA_USE_MKSTEMP)
+if(LUA_USE_ISATTY)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_ISATTY")
+endif(LUA_USE_ISATTY)
+if(LUA_USE_POPEN)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_POPEN")
+endif(LUA_USE_POPEN)
+if(LUA_USE_ULONGJMP)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_ULONGJMP")
+endif(LUA_USE_ULONGJMP)
+if(LUA_USE_APICHECK)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_APICHECK")
+endif(LUA_USE_APICHECK)
+if(LUA_ANSI)
+	set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_ANSI")
+endif(LUA_ANSI)
+
+# readline support
+if(LUA_USE_READLINE)
+	FIND_PATH(READLINE_INCLUDE_DIR readline/readline.h)
+	FIND_LIBRARY(READLINE_LIBRARY NAMES readline)
+	if(READLINE_LIBRARY)
+		set(COMMON_CFLAGS "${COMMON_CFLAGS} -DLUA_USE_READLINE")
+		set(COMMON_LDFLAGS "${COMMON_LDFLAGS} -lreadline -lhistory")
+		include_directories(${READLINE_INCLUDE_DIR})
+	endif(READLINE_LIBRARY)
+endif(LUA_USE_READLINE)
+
+# curses
+if(LUA_USE_CURSES)
+	include(FindCurses)
+	if(CURSES_LIBRARY)
+		include_directories(${CURSES_INCLUDE_DIR})
+		set(LIBS ${LIBS} ${CURSES_LIBRARY})
+	endif(CURSES_LIBRARY)
+endif(LUA_USE_CURSES)
+
+#
+# standard flags to use for each build type.
+#
+if(CMAKE_COMPILER_IS_GNUCC)
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -Wall -Wextra -Wshadow -W -pedantic -std=gnu99")
+	set(CMAKE_C_FLAGS_RELEASE        "${CMAKE_C_FLAGS_RELEASE}     -O2")
+	set(CMAKE_C_FLAGS_DEBUG          "${CMAKE_C_FLAGS_DEBUG}       -O0 -g")
+	set(CMAKE_C_FLAGS_PROFILE        "${CMAKE_C_FLAGS_PROFILE}     -O1 -g")
+	set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_WITHDEBINFO} -O2 -g")
+endif(CMAKE_COMPILER_IS_GNUCC)
+
+#
+# Install extra header files & manpages
+#
+install(FILES
+	etc/lua.hpp
+	DESTINATION include)
+install(FILES
+	doc/lua.1
+	doc/luac.1
+	DESTINATION man/man1)
+
+#
+# sub-folders
+#
+ADD_SUBDIRECTORY(src build)
+ADD_SUBDIRECTORY(test)
+
diff --git a/cmake/CustomMacros.cmake b/cmake/CustomMacros.cmake
new file mode 100644
index 0000000..9318ea4
--- /dev/null
+++ b/cmake/CustomMacros.cmake
@@ -0,0 +1,14 @@
+
+macro(add_target_properties _target _name)
+	set(_properties)
+	foreach(_prop ${ARGN})
+		set(_properties "${_properties} ${_prop}")
+	endforeach(_prop)
+	get_target_property(_old_properties ${_target} ${_name})
+	if(NOT _old_properties)
+		# in case it's NOTFOUND
+		set(_old_properties)
+	endif(NOT _old_properties)
+	set_target_properties(${_target} PROPERTIES ${_name} "${_old_properties} ${_properties}")
+endmacro(add_target_properties)
+
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..47fba6c
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,103 @@
+
+# Lua core source files.
+set(LUA_CORE_SRC
+	lapi.c
+	lauxlib.c
+	lbaselib.c
+	lcode.c
+	ldblib.c
+	ldebug.c
+	ldo.c
+	ldump.c
+	lfunc.c
+	lgc.c
+	linit.c
+	liolib.c
+	llex.c
+	lmathlib.c
+	lmem.c
+	loadlib.c
+	lobject.c
+	lopcodes.c
+	loslib.c
+	lparser.c
+	lstate.c
+	lstring.c
+	lstrlib.c
+	ltable.c
+	ltablib.c
+	ltm.c
+	lundump.c
+	lvm.c
+	lzio.c
+)
+set(LUA_LIB_HEADERS
+	lua.h
+	lualib.h
+	lauxlib.h
+	luaconf.h
+)
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}
+				${CMAKE_CURRENT_BINARY_DIR})
+
+#
+# Lua library.
+#
+if(BUILD_STATIC)
+	add_library(lua STATIC ${LUA_CORE_SRC})
+	add_target_properties(lua COMPILE_FLAGS "${COMMON_CFLAGS}")
+	add_target_properties(lua LINK_FLAGS "${LD_FLAGS} ${COMMON_LDFLAGS}")
+	target_link_libraries(lua ${LIBS})
+	set(LUA_STATIC_LIB lua)
+	set(LUA_LIBS lua)
+else(BUILD_STATIC)
+	add_library(lua_static STATIC ${LUA_CORE_SRC})
+	add_target_properties(lua_static COMPILE_FLAGS "${COMMON_CFLAGS}")
+	add_target_properties(lua_static LINK_FLAGS "${LD_FLAGS} ${COMMON_LDFLAGS}")
+	target_link_libraries(lua_static ${LIBS})
+	set(LUA_STATIC_LIB lua_static)
+
+	add_library(lua SHARED ${LUA_CORE_SRC})
+	add_target_properties(lua COMPILE_FLAGS "${COMMON_CFLAGS}")
+	add_target_properties(lua LINK_FLAGS "${LD_FLAGS} ${COMMON_LDFLAGS}")
+	target_link_libraries(lua ${LIBS})
+
+	set(LUA_LIBS lua_static lua)
+endif(BUILD_STATIC)
+set_target_properties(${LUA_LIBS} PROPERTIES
+	VERSION ${LUA_VERSION}
+	SOVERSION ${LUA_SOVERSION}
+	CLEAN_DIRECT_OUTPUT 1
+)
+
+#
+# Lua compiler
+#
+add_executable(luac luac.c print.c)
+add_target_properties(luac COMPILE_FLAGS "${COMMON_CFLAGS}")
+add_target_properties(luac LINK_FLAGS "${LD_FLAGS} ${COMMON_LDFLAGS}")
+target_link_libraries(luac ${LUA_STATIC_LIB} ${LIBS})
+
+#
+# Lua stand-alone interpreter
+#
+add_executable(lua.bin lua.c)
+add_target_properties(lua.bin COMPILE_FLAGS "${COMMON_CFLAGS}")
+add_target_properties(lua.bin LINK_FLAGS "${LD_FLAGS} ${COMMON_LDFLAGS}")
+target_link_libraries(lua.bin lua ${LIBS})
+# rename lub.bin to lua
+set_target_properties(lua.bin PROPERTIES OUTPUT_NAME lua)
+
+#
+# install lua/luac & library.
+#
+install(TARGETS luac lua.bin ${LUA_LIBS}
+				RUNTIME DESTINATION bin
+				LIBRARY DESTINATION lib
+				ARCHIVE DESTINATION lib)
+
+install(FILES
+	${LUA_LIB_HEADERS}
+	DESTINATION include)
+