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

Commit

Permalink
CMake build for Darwin, Linux, Cygwin and MinGW Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Drahos committed Jul 19, 2011
1 parent e073839 commit 0ade929
Show file tree
Hide file tree
Showing 4 changed files with 482 additions and 0 deletions.
163 changes: 163 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Copyright (C) 2007-2011 LuaDist.
# Created by Peter Drahos
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Please note that the package source code is licensed under its own license.

project ( libffi C )
cmake_minimum_required ( VERSION 2.6 )
include ( dist.cmake )

# NOTE: For now this only supports x86 systems, mainly OSX, Linux and Windows (32/64bit)
set (TARGET X86 )

# Check headers
include ( CheckIncludeFiles )
check_include_files ( alloca.h HAVE_ALLOCA_H )
check_include_files ( dlfcn.h HAVE_DLFCN_H )
check_include_files ( inttypes.h HAVE_INTTYPES_H )
check_include_files ( memory.h HAVE_MEMORY_H )
check_include_files ( stdint.h HAVE_STDINT_H )
check_include_files ( stdlib.h HAVE_STDLIB_H )
check_include_files ( strings.h HAVE_STRINGS_H )
check_include_files ( string.h HAVE_STRING_H )
check_include_files ( sys/mman.h HAVE_SYS_MMAN_H )
check_include_files ( sys/stat.h HAVE_SYS_STAT_H )
check_include_files ( sys/types.h HAVE_SYS_TYPES_H )
check_include_files ( unistd.h HAVE_UNISTD_H )
set ( STDC_HEADERS 1 )

# Check functions
include ( CheckFunctionExists )
check_function_exists ( memcpy HAVE_MEMCPY )
check_function_exists ( mmap HAVE_MMAP )
if (HAVE_MMAP)
set( HAVE_MMAP_ANON 1)
set( HAVE_MMAP_FILE 1)
endif ()
check_function_exists ( alloca HAVE_ALLOCA )

# Check types
include ( CheckTypeSize )
check_type_size ( "long double" HAVE_LONG_DOUBLE )
check_type_size ( "double" SIZEOF_DOUBLE )
check_type_size ( "long double" SIZEOF_LONG_DOUBLE )
check_type_size ( "void*" SIZEOF_VOID_P )

# ASM related settings
set ( HAVE_AS_REGISTER_PSEUDO_OP 0 )
set ( HAVE_AS_CFI_PSEUDO_OP 0 )
set ( HAVE_AS_X86_PCREL 0 )
set ( HAVE_AS_ASCII_PSEUDO_OP 1 )
set ( HAVE_AS_STRING_PSEUDO_OP 1 )

# GCC related settings
set ( HAVE_RO_EH_FRAME 1 )
set ( EH_FRAME_FLAGS aw )
set ( HAVE_HIDDEN_VISIBILITY_ATTRIBUTE 0 )

# FFI Options
option ( FFI_CLOSURES "Enable closures" on )
option ( FFI_DEBUG "Compile with debug" off )
option ( FFI_NO_STRUCTS "Define this is you do not want support for aggregate types." off )
option ( FFI_NO_RAW_API "Define this is you do not want support for the raw API." off )
option ( USING_PURIFY "Define this if you are using Purify and want to suppress spurious messages." off )

include_directories ( ${PROJECT_BINARY_DIR} include src src/x86 )
file ( GLOB SRC_C src/*.c )

# Basic architecture detection
if ( SIZEOF_VOID_P MATCHES 8 )
set ( HAVE_64BIT 1 )
set ( HAVE_AS_X86_64_UNWIND_SECTION_TYPE 1 )
else ()
set ( HAVE_64BIT 0 )
endif ()

if ( WIN32 ) # MSVC specific
set ( FFI_CLOSURES 1 )
set ( SYMBOL_UNDERSCORE 1 )
set ( SRC_C ${SRC_C} src/x86/ffi.c )
if ( MSVC )
set ( SRC_ASM ${SRC_ASM} src/x86/sysv.S )
endif()
if ( HAVE_64BIT ) # This will probably not work on MinGW
set ( TARGET X86_WIN64 )
set ( SRC_ASM ${SRC_ASM} src/x86/win64.S )
else()
set ( TARGET X86_WIN32 )
set ( SRC_ASM ${SRC_ASM} src/x86/win32.S )
endif ()
elseif (CYGWIN)
set ( FFI_CLOSURES 1 )
set ( SYMBOL_UNDERSCORE 1 )
set ( SRC_C ${SRC_C} src/x86/ffi.c )
if ( HAVE_64BIT ) # This will probably not work yet
set ( TARGET X86_WIN64 )
set ( SRC_ASM ${SRC_ASM} src/x86/win64.S )
else()
set ( TARGET X86_WIN32 )
set ( SRC_ASM ${SRC_ASM} src/x86/win32.S )
endif ()
elseif ( APPLE )
set ( TARGET X86_DARWIN )
set ( FFI_CLOSURES 1 )
set ( FFI_EXEC_TRAMPOLINE_TABLE 1 )
set ( FFI_MMAP_EXEC_WRIT 1 )

set ( SRC_C ${SRC_C} src/x86/ffi.c )
set ( SRC_ASM ${SRC_ASM} src/x86/darwin.S )
set ( SRC_C ${SRC_C} src/x86/ffi64.c )
set ( SRC_ASM ${SRC_ASM} src/x86/darwin64.S )
else () # Other x86 systems
set ( HAVE_AS_X86_PCREL 1 )
set ( SRC_C ${SRC_C} src/x86/ffi.c )
set ( SRC_ASM ${SRC_ASM} src/x86/sysv.S )
if ( HAVE_64BIT )
set (TARGET X86_64 )
set ( SRC_C ${SRC_C} src/x86/ffi64.c )
set ( SRC_ASM ${SRC_ASM} src/x86/unix64.S )
else ()
set (TARGET X86 )
endif ()
endif ()

# Generate headers
configure_file (
"${PROJECT_SOURCE_DIR}/include/ffi.h.in"
"${PROJECT_BINARY_DIR}/ffi.h"
)
configure_file (
"${PROJECT_SOURCE_DIR}/fficonfig.cmake.in"
"${PROJECT_BINARY_DIR}/fficonfig.h"
)

# Use C to compile the assembly (this way preprocessor is used)
set_source_files_properties ( ${SRC_ASM} PROPERTIES LANGUAGE C )

# Compile libffi
add_library ( ffi SHARED ${SRC_C} ${SRC_ASM} )

# Install
install ( TARGETS ffi RUNTIME DESTINATION ${INSTALL_BIN} LIBRARY DESTINATION ${INSTALL_LIB} ARCHIVE DESTINATION ${INSTALL_LIB} )
install ( DIRECTORY doc/ DESTINATION ${INSTALL_DOC} )
install ( FILES ${PROJECT_BINARY_DIR}/ffi.h ${PROJECT_BINARY_DIR}/fficonfig.h src/x86/ffitarget.h DESTINATION ${INSTALL_INC} )
install ( FILES LICENSE README DESTINATION ${INSTALL_DATA} )

# Testing
enable_testing ()
file ( GLOB TEST_CLS testsuite/libffi.call/cls*.c )
file ( GLOB TEST_RETURN testsuite/libffi.call/return*.c )
file ( GLOB TEST_STRUCT testsuite/libffi.call/struct*.c )
file ( GLOB TEST_CLOSURE testsuite/libffi.call/closure_fn*.c )
file ( GLOB TEST_FLOAT testsuite/libffi.call/float*.c )
file ( GLOB TEST_NESTED testsuite/libffi.call/nested*.c )
file ( GLOB TEST_STRET testsuite/libffi.call/stret*.c )
set ( TESTS ${TEST_CLS} ${TEST_RETURN} ${TEST_STRUCT} ${TEST_CLOSURE} ${TEST_FLOAT} ${TEST_NESTED} ${TEST_STRET})

foreach ( TEST ${TESTS})
get_filename_component ( TEST_NAME ${TEST} NAME )
add_executable ( ${TEST_NAME} ${TEST} )
target_link_libraries ( ${TEST_NAME} ffi )
add_test ( ${TEST_NAME} ${TEST_NAME} )
endforeach ()
130 changes: 130 additions & 0 deletions dist.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# LuaDist CMake utility library.
# Provides variables and utility functions common to LuaDist CMake builds.
#
# Copyright (C) 2007-2010 LuaDist.
# by David Manura, Peter Drahos
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Please note that the package source code is licensed under its own license.

# Few convinence settings
SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH})

# Where to install module parts:
set(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
set(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
set(INSTALL_INC include CACHE PATH "Where to install headers to.")
set(INSTALL_ETC etc CACHE PATH "Where to store configuration files")
set(INSTALL_LMOD share/lua/lmod CACHE PATH "Directory to install Lua modules.")
set(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules.")
set(INSTALL_DATA share/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.")
set(INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.")
set(INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.")
set(INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.")
set(INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files")


# In MSVC, prevent warnings that can occur when using standard libraries.
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif(MSVC)

# Adds Lua shared library module target `_target`.
# Additional sources to build the module are listed after `_target`.
macro(add_lua_module _target)
find_package(Lua51 REQUIRED)
include_directories(${LUA_INCLUDE_DIR}) #2DO: somehow apply only to _target?

add_library(${_target} MODULE ${ARGN})
set_target_properties(${_target} PROPERTIES PREFIX "")
target_link_libraries(${_target} ${LUA_LIBRARY})

IF(WIN32)
set_target_properties(${_target} PROPERTIES LINK_FLAGS "-Wl,--enable-auto-import")
ENDIF()

endmacro(add_lua_module)

# Runs Lua script `_testfile` under CTest tester.
# Optional argument `_testcurrentdir` is current working directory to run test under
# (defaults to ${CMAKE_CURRENT_BINARY_DIR}).
# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}.
# Under LuaDist, set test=true in config.lua to enable testing.
macro(add_lua_test _testfile)
include(CTest)
if(BUILD_TESTING)
find_program(LUA NAMES lua lua.bat)
get_filename_component(TESTFILEABS ${_testfile} ABSOLUTE)
get_filename_component(TESTFILENAME ${_testfile} NAME)
get_filename_component(TESTFILEBASE ${_testfile} NAME_WE)

# Write wrapper script.
set(TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME})
set(TESTWRAPPERSOURCE
"package.path = '${CMAKE_CURRENT_BINARY_DIR}/?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;' .. package.path
package.cpath = '${CMAKE_CURRENT_BINARY_DIR}/?.so\;${CMAKE_CURRENT_BINARY_DIR}/?.dll\;' .. package.cpath
return dofile '${TESTFILEABS}'
" )
if(${ARGC} GREATER 1)
set(_testcurrentdir ${ARGV1})
get_filename_component(TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE)
set(TESTWRAPPERSOURCE
"require 'lfs'
lfs.chdir('${TESTCURRENTDIRABS}')
${TESTWRAPPERSOURCE}")
endif()
FILE(WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE})

add_test(${TESTFILEBASE} ${LUA} ${TESTWRAPPER})
endif(BUILD_TESTING)

# see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake
endmacro(add_lua_test)

# Converts Lua source file `_source` to binary string embedded in C source
# file `_target`. Optionally compiles Lua source to byte code (not available
# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua
# versions of bin2c [1] and luac [2] may be passed respectively as additional
# arguments.
#
# [1] http://lua-users.org/wiki/BinToCee
# [2] http://lua-users.org/wiki/LuaCompilerInLua
function(add_lua_bin2c _target _source)
find_program(LUA NAMES lua lua.bat)
execute_process(COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET)
if (NOT ${_LUA_DUMP_RESULT})
SET(HAVE_LUA_DUMP true)
endif()
message("-- string.dump=${HAVE_LUA_DUMP}")

if (ARGV2)
get_filename_component(BIN2C ${ARGV2} ABSOLUTE)
set(BIN2C ${LUA} ${BIN2C})
else()
find_program(BIN2C NAMES bin2c bin2c.bat)
endif()
if (HAVE_LUA_DUMP)
if (ARGV3)
get_filename_component(LUAC ${ARGV3} ABSOLUTE)
set(LUAC ${LUA} ${LUAC})
else()
find_program(LUAC NAMES luac luac.bat)
endif()
endif (HAVE_LUA_DUMP)
message("-- bin2c=${BIN2C}")
message("-- luac=${LUAC}")

get_filename_component(SOURCEABS ${_source} ABSOLUTE)
if (HAVE_LUA_DUMP)
get_filename_component(SOURCEBASE ${_source} NAME_WE)
add_custom_command(
OUTPUT ${_target} DEPENDS ${_source}
COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS}
COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" )
else()
add_custom_command(
OUTPUT ${_target} DEPENDS ${SOURCEABS}
COMMAND ${BIN2C} ${_source} ">${_target}" )
endif()
endfunction(add_lua_bin2c)
10 changes: 10 additions & 0 deletions dist.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- This file is part of LuaDist project

name = "libffi"
version = "3.0.9"

desc = "FFI stands for Foreign Function Interface. A foreign function interface is the popular name for the interface that allows code written in one language to call code written in another language."
author = "Anthony Green"
license = "MIT"
url = "http://sourceware.org/libffi/"
maintainer = "Peter Drahos"
Loading

0 comments on commit 0ade929

Please sign in to comment.