From 9f89da6eef2c830db6fc3abb08fe755ae7ce9b6c Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 2 May 2012 20:46:09 -0400 Subject: Add native and cross compiling CMake build support --- mrblib/CMakeLists.txt | 89 +++++++++++++++++++++++++++++++++------------------ mrblib/Makefile | 73 ++++++++++++++++++++++++++++++++++++++++++ mrblib/Makefile.orig | 73 ------------------------------------------ 3 files changed, 131 insertions(+), 104 deletions(-) create mode 100644 mrblib/Makefile delete mode 100644 mrblib/Makefile.orig (limited to 'mrblib') diff --git a/mrblib/CMakeLists.txt b/mrblib/CMakeLists.txt index 30881c611..b9fa22587 100644 --- a/mrblib/CMakeLists.txt +++ b/mrblib/CMakeLists.txt @@ -1,35 +1,62 @@ -# build mrblib -# need custom commands -# Compile C source from merged mruby source +# transform mruby's standard lib into a C library file(GLOB MRBLIB_SRC_RB "*.rb") -# generate the a single rubu file from all the existing ones. -add_custom_command(OUTPUT mrblib.rbtmp -COMMAND cat ${MRBLIB_SRC_RB} > mrblib.rbtmp -DEPENDS ${MRBLIB_SRC_RB} -) - -# generate the intermediate representation in C -add_custom_command(OUTPUT mrblib_irep.c -COMMAND echo -B mrblib_irep -o mrblib_irep.c mrblib.rbtmp -COMMAND mrbc -Bmrblib_irep -omrblib_irep.c mrblib.rbtmp -DEPENDS mrblib.rbtmp -) - -# finally generate the library's c file -add_custom_command(OUTPUT mrblib.c -COMMAND cat init_mrblib.c mrblib_irep.c > mrblib.c -DEPENDS init_mrblib.c mrblib_irep.c -) - -# only use this C file to generate mrblib. -set(MRBLIB_SRC_C mrblib.c) -# add_library(mrblib_static STATIC ${MRBLIB_SRC_C}) -add_library(mrblib_object OBJECT ${MRBLIB_SRC_C}) -# target_link_libraries(mrblib ritevm ${MRUBY_LIBS}) -# target_link_libraries(mrblib_static ritevm_static ${MRUBY_LIBS}) -# install(TARGETS mrblib mrblib_static -# LIBRARY DESTINATION lib -# ARCHIVE DESTINATION lib) +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 + $ + $ + ) +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 diff --git a/mrblib/Makefile b/mrblib/Makefile new file mode 100644 index 000000000..d22c4509f --- /dev/null +++ b/mrblib/Makefile @@ -0,0 +1,73 @@ +# makefile discription. +# basic build file for mruby library (Ruby part) + +# project-specific macros +# extension of the executable-file is modifiable(.exe .out ...) +BASEDIR = . +TARGET := mrblib +MLIB := $(TARGET).o +CLIB := $(TARGET).c +DLIB := $(TARGET).ctmp +RLIB := $(TARGET).rbtmp +DEPLIB := $(TARGET).d +MRB1 := $(BASEDIR)/*.rb +MRBS := $(MRB1) +LIBR0 := ../lib/libmruby_core.a +LIBR := ../lib/libmruby.a + +# C compiler (gcc) +CC = gcc +LL = gcc +AR = ar +DEBUG_MODE = 1 +ifeq ($(DEBUG_MODE),1) +CFLAGS = -g +else +CFLAGS = -O3 +endif +INCLUDES = -I../src -I../include +ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) +ifeq ($(OS),Windows_NT) + MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" +else + MAKE_FLAGS = CC='$(CC)' LL='$(LL)' ALL_CFLAGS='$(ALL_CFLAGS)' +endif + +# mruby compiler +ifeq ($(OS),Windows_NT) +MRBC = ../bin/mrbc.exe +else +MRBC = ../bin/mrbc +endif + +############################## +# generic build targets, rules + +.PHONY : all +all : $(LIBR) + +# update libmruby.a +$(LIBR) : $(MLIB) $(LIBR0) + cp $(LIBR0) $(LIBR) + $(AR) r $(LIBR) $(MLIB) + +# Compile mrblib source +$(MLIB) : $(CLIB) + $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(CLIB) -o $(MLIB) + +# Compile C source from merged mruby source +$(CLIB) : $(RLIB) $(MRBC) + $(MRBC) -Bmrblib_irep -o$(DLIB) $(RLIB); cat init_$(TARGET).c $(DLIB) > $@ + +$(MRBC) : ../src/opcode.h ../src/codegen.c ../src/parse.y + $(MAKE) -C ../tools/mrbc $(MAKE_FLAGS) + +# merge mruby sources +$(RLIB) : $(MRBS) + cat $? > $@ + +# clean up +.PHONY : clean +clean : + @echo "make: removing targets, objects and depend files of `pwd`" + -rm -f $(MRBC) $(MLIB) $(CLIB) $(RLIB) $(DLIB) $(DEPLIB) $(LIBR) diff --git a/mrblib/Makefile.orig b/mrblib/Makefile.orig deleted file mode 100644 index d22c4509f..000000000 --- a/mrblib/Makefile.orig +++ /dev/null @@ -1,73 +0,0 @@ -# makefile discription. -# basic build file for mruby library (Ruby part) - -# project-specific macros -# extension of the executable-file is modifiable(.exe .out ...) -BASEDIR = . -TARGET := mrblib -MLIB := $(TARGET).o -CLIB := $(TARGET).c -DLIB := $(TARGET).ctmp -RLIB := $(TARGET).rbtmp -DEPLIB := $(TARGET).d -MRB1 := $(BASEDIR)/*.rb -MRBS := $(MRB1) -LIBR0 := ../lib/libmruby_core.a -LIBR := ../lib/libmruby.a - -# C compiler (gcc) -CC = gcc -LL = gcc -AR = ar -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -else -CFLAGS = -O3 -endif -INCLUDES = -I../src -I../include -ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) -ifeq ($(OS),Windows_NT) - MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" -else - MAKE_FLAGS = CC='$(CC)' LL='$(LL)' ALL_CFLAGS='$(ALL_CFLAGS)' -endif - -# mruby compiler -ifeq ($(OS),Windows_NT) -MRBC = ../bin/mrbc.exe -else -MRBC = ../bin/mrbc -endif - -############################## -# generic build targets, rules - -.PHONY : all -all : $(LIBR) - -# update libmruby.a -$(LIBR) : $(MLIB) $(LIBR0) - cp $(LIBR0) $(LIBR) - $(AR) r $(LIBR) $(MLIB) - -# Compile mrblib source -$(MLIB) : $(CLIB) - $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(CLIB) -o $(MLIB) - -# Compile C source from merged mruby source -$(CLIB) : $(RLIB) $(MRBC) - $(MRBC) -Bmrblib_irep -o$(DLIB) $(RLIB); cat init_$(TARGET).c $(DLIB) > $@ - -$(MRBC) : ../src/opcode.h ../src/codegen.c ../src/parse.y - $(MAKE) -C ../tools/mrbc $(MAKE_FLAGS) - -# merge mruby sources -$(RLIB) : $(MRBS) - cat $? > $@ - -# clean up -.PHONY : clean -clean : - @echo "make: removing targets, objects and depend files of `pwd`" - -rm -f $(MRBC) $(MLIB) $(CLIB) $(RLIB) $(DLIB) $(DEPLIB) $(LIBR) -- cgit v1.2.3