Commit df25301d authored by Yukihiro Matsumoto's avatar Yukihiro Matsumoto

Merge branch 'master' of github.com:mruby/mruby

parents f44acf7b 0dc183b8
......@@ -19,6 +19,7 @@ cscope.out
/bin
/mrblib/mrblib.c
/mrblib/*.*tmp
/build
/test/mrbtest
/test/mrbtest.c
/test/*.*tmp
......@@ -4,3 +4,4 @@ Original Authors "mruby developers" are:
Kyushu Institute of Technology
Network Applied Communication Laboratory, Inc.
Daniel Bovensiepen
Jon Maken
# CMake build system for mruby
# License: released under the same license as mruby
# Author: jonforums@gmail.com
# Author: beoran@gmail.com
#
# Usage example:
# 1. Ensure CMake, Bison, and a build toolchain are on `PATH`
# 2. Change to a build directory outside source tree, eg - `build` subdir
# 3. Create build Makefiles or project files.
# `cmake ..` (UNIX-like system)
# `cmake -G "MSYS Makefiles" ..`
# `cmake -G "Visual Studio 10" ..`
# `cmake -G "NMake Makefiles" ..`
# ** to cross-compile: add -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain/file
# ** to set install dir: add -DCMAKE_INSTALL_PREFIX=/path/to/installdir
# 4a. Build: `make` (to make noisy, add `VERBOSE=1`)
# 4b. Build and test: `make all test`
# 4c. Build, test, and install: `make all test install`
# 4d. Build, test, and package: `make all test package`
cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)
# Default build mode is Release With Debug Info unless specified
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose build type: empty Debug Release RelWithDebInfo MinSizeRel"
FORCE)
message(STATUS "Build type not set, defaulting to 'RelWithDebInfo'")
endif()
project(mruby C)
# TODO stop polluting source tree with CMakeFiles/ and CMakeCache.txt
# on build location check failure
# Make sure we are not trying to generate in in-tree build unless building
# with a MSVC IDE where it's OK.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
message(FATAL_ERROR
"\nIn-source builds are not allowed as CMake would overwrite the "
"Makefiles distributed with mruby. Please change to the 'build' "
"subdirectory and run CMake from there.")
endif()
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW) # don't split absolute link paths
cmake_policy(SET CMP0012 NEW) # recognize number & boolean literals
cmake_policy(SET CMP0015 NEW) # convert relative link paths to absolute
endif(COMMAND cmake_policy)
# Match original Makefile's default in-tree install.
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR} CACHE PATH
"Install path prefix prepended to install directories."
FORCE
)
endif()
# TODO refactor to use an option when adding shared lib support
set(BUILD_SHARED_LIBS OFF)
# Version of mruby, useful for versoning .so and .dll files.
# TODO automate by parsing src/version.h -or- extract git info?
set(MRUBY_VERSION 1.0.0dev)
string(REGEX MATCH "^[0-9]+[.][0-9]+" MRUBY_SOVERSION ${MRUBY_VERSION})
string(REPLACE "." "" MRUBY_DLL_SHORTVER ${MRUBY_SOVERSION})
# Search in the `cmake` directory for custom CMake helper modules.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
include(IntrospectSystem)
# Search for C header files in these directories.
include_directories("${CMAKE_SOURCE_DIR}/include" "${CMAKE_SOURCE_DIR}/src")
# TODO re-enable (and implement) if needed
# On some 64-bit platforms, libraries should be installed into `lib64'
# instead of `lib'. Set this to 64 to do that.
#set(LIB_SUFFIX "" CACHE STRING "Suffix for 'lib' directories, e.g. '64'")
# build the components
add_subdirectory(src)
add_subdirectory(mrblib)
add_subdirectory(tools)
add_subdirectory(test)
# install the header files
install(FILES include/mruby.h DESTINATION include)
install(FILES include/mrbconf.h DESTINATION include)
install(DIRECTORY include/mruby DESTINATION include FILES_MATCHING PATTERN "*.h")
# TODO refactor once proper versioning scheme implemented
# archive packaging
set(CPACK_GENERATOR "TGZ;ZIP")
string(TOLOWER ${CMAKE_SYSTEM_NAME} MRUBY_HOST)
if(CMAKE_C_COMPILER_VERSION)
string(REPLACE "." "" MRUBY_GCC_VERSION ${CMAKE_C_COMPILER_VERSION})
endif()
# TODO add build info suffix for non-Windows builds?
if(MINGW)
set(MRUBY_BUILD "-mingw${MRUBY_GCC_VERSION}")
elseif(MSVC)
set(MRUBY_BUILD "-msvc${MSVC_VERSION}")
endif()
set(CPACK_PACKAGE_FILE_NAME
"${CMAKE_PROJECT_NAME}-${MRUBY_VERSION}-${MRUBY_HOST}${MRUBY_BUILD}"
)
include(CPack)
# vim: ts=2 sts=2 sw=2 et
# Sample toolchain file for building for Windows from an Arch Linux system.
#
# Typical usage:
# 1) install cross compiler: `sudo pacman -S mingw32-gcc`
# 2) cp cmake/Toolchain-Arch-mingw32.cmake.sample ~/Toolchain-Arch-mingw32.cmake
# 3) tweak toolchain values as needed
# 4) cd build
# 5) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Arch-mingw32.cmake ..
# name of the target OS on which the built artifacts will run
# and the toolchain prefix
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX i486-mingw32)
# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
# set 2nd to dir containing personal cross development headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} ~/crossdev/w32)
# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# Sample toolchain file for building for Windows from an OS X system.
#
# Typical usage:
# 1) install a mingw-w64 cross compiler
# a) darwin toolchain targeting win32: http://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win32/
# b) extract toolchain into ~/mingw/w32
# 2) cp cmake/Toolchain-OSX-mingw32.cmake.sample ~/Toolchain-OSX-mingw32.cmake
# 3) tweak toolchain values as needed
# 4) cd build
# 5) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-OSX-mingw32.cmake ..
# name of the target OS on which the built artifacts will run
# and the toolchain prefix
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX i686-w64-mingw32)
# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment(s) on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
# set 2nd to dir containing personal cross development headers/libs
set(CMAKE_FIND_ROOT_PATH ~/mingw/w32/${TOOLCHAIN_PREFIX} ~/crossdev/w32)
# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# 1) install cross compiler: `sudo apt-get install mingw-w64 g++-mingw-w64`
# 2) cp cmake/Toolchain-Ubuntu-mingw32.cmake.sample ~/Toolchain-Ubuntu-mingw32.cmake
# 3) tweak toolchain values as needed
# 4) cd build
# 5) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Ubuntu-mingw32.cmake ..
# name of the target OS on which the built artifacts will run
# and the toolchain prefix
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX i686-w64-mingw32)
# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
# set 2nd to dir containing personal cross development headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} ~/crossdev/w32)
# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# system capabilities checking
# initial system defaults
if(CMAKE_COMPILER_IS_GNUCC)
set(MRUBY_DEFAULT_CFLAGS "-Wall -Werror-implicit-function-declaration")
set(CMAKE_C_FLAGS "${MRUBY_DEFAULT_CFLAGS}")
set(CMAKE_C_FLAGS_DEBUG "-O3 -ggdb")
set(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -g")
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
set(MRUBY_LIBS m)
else()
if(MSVC)
# TODO default MSVC flags
add_definitions(
-D_CRT_SECURE_NO_WARNINGS
-wd4018 # suppress 'signed/unsigned mismatch'
)
endif()
endif()
if(MSVC)
add_definitions(
-DRUBY_EXPORT # required by oniguruma.h
)
endif()
# include helpers
include(CheckIncludeFile)
include(CheckSymbolExists)
# header checks
CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
if(HAVE_STRING_H)
add_definitions(-DHAVE_STRING_H)
endif()
CHECK_INCLUDE_FILE(float.h HAVE_FLOAT_H)
if(HAVE_FLOAT_H)
add_definitions(-DHAVE_FLOAT_H)
endif()
# symbol checks
CHECK_SYMBOL_EXISTS(gettimeofday sys/time.h HAVE_GETTIMEOFDAY)
if(NOT HAVE_GETTIMEOFDAY)
add_definitions(-DNO_GETTIMEOFDAY)
endif()
# vim: ts=2 sts=2 sw=2 et
# transform mruby's standard lib into a C library
file(GLOB MRBLIB_SRC_RB "*.rb")
if(CMAKE_CROSSCOMPILING)
# create native tools and `mrblib.ctmp` required to build `mrblib.c`
include(ExternalProject)
ExternalProject_Add(mruby-native
DOWNLOAD_COMMAND ""
SOURCE_DIR "${CMAKE_SOURCE_DIR}"
CONFIGURE_COMMAND "${CMAKE_COMMAND}" "${CMAKE_SOURCE_DIR}"
INSTALL_COMMAND ""
BINARY_DIR "${CMAKE_BINARY_DIR}/native"
)
# aggregate mruby's standard library as a single C file
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrblib.c"
DEPENDS mruby-native init_mrblib.c "${CMAKE_BINARY_DIR}/native/mrblib/mrblib.ctmp"
COMMAND "${CMAKE_BINARY_DIR}/native/tools/xpcat/xpcat"
-o "${CMAKE_CURRENT_BINARY_DIR}/mrblib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/init_mrblib.c"
"${CMAKE_BINARY_DIR}/native/mrblib/mrblib.ctmp"
)
else()
# generate a single rb file from all existing ones
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrblib.rbtmp"
DEPENDS xpcat
COMMAND xpcat -o "${CMAKE_CURRENT_BINARY_DIR}/mrblib.rbtmp" ${MRBLIB_SRC_RB}
)
# mruby compile and generate C byte array representation
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrblib.ctmp"
DEPENDS mrbc "${CMAKE_CURRENT_BINARY_DIR}/mrblib.rbtmp"
COMMAND mrbc -Bmrblib_irep -o"${CMAKE_CURRENT_BINARY_DIR}/mrblib.ctmp"
"${CMAKE_CURRENT_BINARY_DIR}/mrblib.rbtmp"
)
# aggregate mruby's standard library as a single C file
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrblib.c"
DEPENDS xpcat init_mrblib.c "${CMAKE_CURRENT_BINARY_DIR}/mrblib.ctmp"
COMMAND xpcat -o "${CMAKE_CURRENT_BINARY_DIR}/mrblib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/init_mrblib.c"
"${CMAKE_CURRENT_BINARY_DIR}/mrblib.ctmp"
)
endif()
add_library(mrblib_object OBJECT mrblib.c)
# generate final static libmruby archive library
add_library(libmruby_static STATIC
$<TARGET_OBJECTS:mruby_object>
$<TARGET_OBJECTS:mrblib_object>
)
set_target_properties(libmruby_static PROPERTIES OUTPUT_NAME mruby)
install(TARGETS libmruby_static
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
# vim: ts=2 sts=2 sw=2 et
# build the core mruby C files
find_package(BISON)
bison_target(mruby parse.y "${CMAKE_CURRENT_BINARY_DIR}/parse.c")
file(GLOB MRUBY_SRC_C "*.c")
list(APPEND MRUBY_SRC_C "${CMAKE_CURRENT_BINARY_DIR}/parse.c")
add_library(mruby_object OBJECT ${MRUBY_SRC_C} ${BISON_mruby_OUTPUTS})
add_library(mruby_static STATIC EXCLUDE_FROM_ALL $<TARGET_OBJECTS:mruby_object>)
# vim: ts=2 sts=2 sw=2 et
......@@ -15,8 +15,6 @@ extern "C" {
#ifndef ONIG_RUBY_M17N
ONIG_EXTERN OnigEncoding OnigEncDefaultCharEncoding;
#define mbclen(p,e,enc) mrb_enc_mbclen((p),(e),(enc))
#endif /* ifndef ONIG_RUBY_M17N */
......
# build standalone mrbtest runner containing all *.rb tests
if(NOT CMAKE_CROSSCOMPILING)
file(GLOB MRBTEST_SRC_RB "assert.rb" "t/*.rb")
# generate a single rb file from all existing test *.rb
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.rbtmp"
DEPENDS xpcat
COMMAND xpcat -o "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.rbtmp" ${MRBTEST_SRC_RB}
)
# mruby compile and generate C byte array representation
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.ctmp"
DEPENDS mrbc "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.rbtmp"
COMMAND mrbc -Bmrbtest_irep -o"${CMAKE_CURRENT_BINARY_DIR}/mrbtest.ctmp"
"${CMAKE_CURRENT_BINARY_DIR}/mrbtest.rbtmp"
)
# aggregate mruby's *.rb test files as a single C file
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.c"
DEPENDS xpcat init_mrbtest.c "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.ctmp"
COMMAND xpcat -o "${CMAKE_CURRENT_BINARY_DIR}/mrbtest.c"
"${CMAKE_CURRENT_SOURCE_DIR}/init_mrbtest.c"
"${CMAKE_CURRENT_BINARY_DIR}/mrbtest.ctmp"
)
add_executable(mrbtest
EXCLUDE_FROM_ALL
"${CMAKE_CURRENT_SOURCE_DIR}/driver.c"
"${CMAKE_CURRENT_BINARY_DIR}/mrbtest.c"
)
target_link_libraries(mrbtest libmruby_static ${MRUBY_LIBS})
add_custom_target(test
DEPENDS mrbtest
COMMAND "${CMAKE_CURRENT_BINARY_DIR}/mrbtest"
)
endif()
# vim: ts=2 sts=2 sw=2 et
# specify the internal and external tools to be built
add_subdirectory(xpcat)
add_subdirectory(mrbc)
add_subdirectory(mruby)
add_subdirectory(mirb)
# vim: ts=2 sts=2 sw=2 et
# build tools/mirb executable
file(GLOB MIRBBIN_SRC_C "*.c")
add_executable(mirb ${MIRBBIN_SRC_C})
target_link_libraries(mirb libmruby_static ${MRUBY_LIBS})
install(TARGETS mirb RUNTIME DESTINATION bin)
# vim: ts=2 sts=2 sw=2 et
# build tools/mrbc executable
file(GLOB MRBC_SRC_C "*.c")
add_executable(mrbc ${MRBC_SRC_C})
target_link_libraries(mrbc mruby_static ${MRUBY_LIBS})
install(TARGETS mrbc RUNTIME DESTINATION bin)
# vim: ts=2 sts=2 sw=2 et
# build tools/mruby executable
file(GLOB MRUBYBIN_SRC_C "*.c")
add_executable(mruby ${MRUBYBIN_SRC_C})
target_link_libraries(mruby libmruby_static ${MRUBY_LIBS})
install(TARGETS mruby RUNTIME DESTINATION bin)
# vim: ts=2 sts=2 sw=2 et
# build tools/xpcat internal executable
add_executable(xpcat xpcat.c)
# vim: ts=2 sts=2 sw=2 et
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
usage(const char *program)
{
printf("Usage: %s -o outputfile FILE...\n", program);
}
int
main(int argc, char *argv[])
{
int i, ch;
const char *output = NULL;
FILE *infile = NULL;
FILE *outfile = NULL;
if (argc < 4) {
usage(argv[0]);
return EXIT_FAILURE;
}
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-o") == 0) {
i++;
if (i < argc)
output = argv[i];
else
return EXIT_FAILURE;
}
}
if (output) {
outfile = fopen(output, "wb");
if (!outfile) {
fprintf(stderr, "[ERROR] unable to open output file: %s\n", output);
return EXIT_FAILURE;
}
setbuf(outfile, NULL);
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-o") == 0) { i++; continue; }
infile = fopen(argv[i], "rb");
if (!infile) {
fprintf(stderr, "[ERROR] unable to open input file: %s\n", argv[i]);
return EXIT_FAILURE;
}
setbuf(infile, NULL);
while ((ch = getc(infile)) != EOF) {
if (putc(ch, outfile) == EOF) {
fprintf(stderr, "[ERROR] error writing output file: %s\n", output);
return EXIT_FAILURE;
}
}
fclose(infile);
}
}
done:
fclose(outfile);
return EXIT_SUCCESS;
}
/* vim: set ts=2 sts=2 sw=2 et: */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment