Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Example: C module

drahosp edited this page Mar 19, 2011 · 4 revisions

Building C modules

Writing CMake scripts for installing C modules without dependenies is very straightforward. The following steps are required:

  • Define project name - This is usually equal to the name of the module.
  • Include dist.cmake
    • This file will set up all variables that contain instalation paths recommended to be used with LuaDist.
    • Additionally it will define a helper macro for creating Lua C-based modules.
  • Create the module using a macro
  • Write commands for installing module and other files

For example here is the CMake script for building the LPeg module:

# Define projects name
# The C tells CMAKE to check for C compilers
project ( lpeg C )

# Define minimun version of CMAKE for compatibility
cmake_minimum_required ( VERSION 2.6 )

# Include dist.cmake - defines LuaDist related macros and install paths
include ( dist.cmake )

# Create and install a Lua module:
# first parameter is module's name (the name that can be required in Lua)
# second and following parameters are the source files (here only one file)
# last parameter is a .def file for exporting symbols from dll produced by MS compilers
install_lua_module ( lpeg lpeg.c lpeg.def)
install_lua_module ( re re.lua )
	
# Schedules a test, this is optional
add_lua_test ( test.lua )

# Install test ( NOTE: This does not execute testing, just installs)
install_test ( test.lua )

# Install documentation
install_doc ( re.html lpeg.html lpeg-128.gif )

Notes

If a module is build from multiple files, e.g. md5 module, the following form can be used:

# Multiple files in subdirectory with link
install_lua_module ( other.lib.core src/core.c src/util.c LINK ${SOME_LIB} ${SOME_OTHER_LIB} )

Additionally file paths can be stored as a list in a variable and passed into the macro. You can find out more abut CMake by reading the full documentation.