[CMake] Questions on build lua source code with cmake

lzprgmr baiyanhuang at gmail.com
Sun Nov 4 21:39:39 EST 2012


Hi, All

After going through the cmake examples, I decide trying build lua with
cmake to get a better understanding, and got some questions.

First, the lua source code are placed in one flat directory (src/) and
will generate 3 targets: a static lib, a lua interpreter and a lua
compiler. my cmake script looks like:

	cmake_minimum_required (VERSION 2.6)
	project (lua)  # project here actually means solution in premake

	# 1. lua static library
	if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
	add_definitions( -D_CRT_SECURE_NO_WARNINGS )
	endif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")

	# how to rename library name?
	add_library (lualib STATIC lapi.c lcode.c lctype.c ldebug.c ldo.c
ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c
lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c
lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c lmathlib.c loslib.c
lstrlib.c ltablib.c loadlib.c linit.c)


	# 2. lua interpreter
	link_directories (${LUA_BINARY_DIR})
	add_executable (lua lua.c)
	target_link_libraries (lua lualib)
	if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
	target_link_libraries( lua m )
	endif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")

	# 3. lua compiler
	link_directories (${LUA_BINARY_DIR})
	add_executable (luac luac.c)
	target_link_libraries (luac lualib)
	if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
	target_link_libraries( luac m )
	endif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")

But it seems a litte bit problematic to me, questions are as below:

* It seems to me cmake are best work with source code with consistent
structure of its targets. in my case, I have to define all my targets
in one CMakeLists.txt file, the problem I can see is the property I
set for 1st target (one example is the macro definition:
-D_CRT_SECURE_NO_WARNINGS ) will also applies to the 2nd, which is not
expected. is there a way to avoid this?

* Here the final target name for the lua library and lua interpreter
are liblua.a and lua (in linux), because here I can't name both
library and executable as "lua", I would hope there is a way for me to
rename the target, like:
      add_library(lualib targetname=lua)
  and then I can depends on it using the project name:
      target_link_libraries(lua lualib)
 Is it possible to achieve this in cmake?

* I guard platform specific setting using if(${CMAKE_SYSTEM_NAME}
STREQUAL "Linux"), which seems quite tedious to me, is there a neat
way to do so?

Thanks.


More information about the CMake mailing list