Skip to content

Commit

Permalink
Added frontend and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
handong32 committed Oct 28, 2016
1 parent 87f90a9 commit b7563dc
Show file tree
Hide file tree
Showing 6 changed files with 642 additions and 3 deletions.
31 changes: 28 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
project("memcached-ebbrt" CXX)
project("memcached-ebbrt" C CXX)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O4 -flto -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14 -Wall -Werror")

set(BAREMETAL_SOURCES
src/Memcached.cc
Expand All @@ -12,17 +13,41 @@ set(BAREMETAL_SOURCES
set(BAREMETAL_INCLUDES
src/)

set(HOSTED_SOURCES
Memcached.cc)

# Baremetal ========================================================

if( ${CMAKE_SYSTEM_NAME} STREQUAL "EbbRT")

message(STATUS "Building baremetal image...")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14 -Wall -Werror")
message(STATUS "### BUILDING NATIVE ###")

include_directories(${BAREMETAL_INCLUDES})
add_executable(memcached.elf ${BAREMETAL_SOURCES})
add_custom_command(TARGET memcached.elf POST_BUILD
COMMAND objcopy -O elf32-i386 memcached.elf memcached.elf32 )

elseif( ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" )

message(STATUS "### BUILDING HOSTED ###")

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(EbbRT REQUIRED)
find_package(Boost 1.53.0 REQUIRED COMPONENTS
filesystem system coroutine context )
find_package(Capnp REQUIRED)
find_package(TBB REQUIRED)
find_package(Threads REQUIRED)
find_package(EbbRTCmdLine REQUIRED)


include_directories(${EBBRT_INCLUDE_DIRS})
add_executable(Memcached Memcached.cc)
target_link_libraries(Memcached ${EBBRT-CMDLINE_LIBRARIES}
${EBBRT_LIBRARIES} ${CAPNP_LIBRARIES_LITE} ${CMAKE_THREAD_LIBS_INIT}
${Boost_LIBRARIES} ${TBB_LIBRARIES}
)

else()
message(FATAL_ERROR "System name unsupported: ${CMAKE_SYSTEM_NAME}")
endif()
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
MYDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))

CD ?= cd
CP ?= cp
CMAKE ?= cmake
MAKE ?= make
MKDIR ?= mkdir
OBJCOPY ?= objcopy
RM ?= rm
STRIP ?= strip

BUILD_DIR ?= $(MYDIR)/build
NATIVE_DIR := $(BUILD_DIR)/bm

SRC_DIR := $(MYDIR)/src
SRC_CONFIG_FLAGS ?=

ifndef CMAKE_PREFIX_PATH
$(error CMAKE_PREFIX_PATH is undefined)
endif

all: hosted native

clean:
-$(RM) -r $(BUILD_DIR)

hosted: $(BUILD_DIR)/Makefile
$(MAKE) -C $(BUILD_DIR)

native: $(NATIVE_DIR) | $(NATIVE_DIR)/memcached.elf32

$(BUILD_DIR)/Makefile: | $(BUILD_DIR)
$(CD) $(BUILD_DIR) && $(CMAKE) -DCMAKE_AR=gcc-ar \
-DCMAKE_RANLIB=gcc-ranlib -DCMAKE_PREFIX_PATH=$(CMAKE_PREFIX_PATH) \
-DCMAKE_BUILD_TYPE=Release $(MYDIR)

$(BUILD_DIR):
$(MKDIR) $@

$(NATIVE_DIR): | $(BUILD_DIR)
$(MKDIR) $@

check-env:
ifndef EBBRT_SYSROOT
$(error EBBRT_SYSROOT is undefined)
endif

$(SRC_DIR): check-env
$(CD) $(NATIVE_DIR) && $(CMAKE) \
-DCMAKE_TOOLCHAIN_FILE=$(EBBRT_SYSROOT)/usr/misc/ebbrt.cmake ../../

$(NATIVE_DIR)/memcached.elf: $(SRC_DIR) | $(NATIVE_DIR)
$(MAKE) -C $(NATIVE_DIR)

%.elf.stripped: %.elf
$(STRIP) -s $< -o $@

%.elf32: %.elf.stripped
$(OBJCOPY) -O elf32-i386 $< $@

.PHONY: all check-env clean hosted linux native
43 changes: 43 additions & 0 deletions Memcached.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Boston University SESA Group 2013 - 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <signal.h>

#include <boost/filesystem.hpp>

#include <ebbrt/hosted/Context.h>
#include <ebbrt/hosted/ContextActivation.h>
#include <ebbrt/GlobalIdMap.h>
#include <ebbrt/hosted/NodeAllocator.h>
#include <ebbrt/Runtime.h>

#include <ebbrt-cmdline/CmdLineArgs.h>

enum : ebbrt::EbbId {
kCmdLineArgsId = ebbrt::kFirstStaticUserId
};

int main(int argc, char **argv) {
auto bindir = boost::filesystem::system_complete(argv[0]).parent_path() /
"/bm/memcached.elf32";
ebbrt::Runtime runtime;
ebbrt::Context c(runtime);
boost::asio::signal_set sig(c.io_service_, SIGINT);
{
ebbrt::ContextActivation activation(c);

// ensure clean quit on ctrl-c
sig.async_wait([&c](const boost::system::error_code &ec,
int signal_number) { c.io_service_.stop(); });
CmdLineArgs::Create(argc, argv, kCmdLineArgsId)
.Then([bindir](ebbrt::Future<ebbrt::EbbRef<CmdLineArgs>> f) {
f.Get();
ebbrt::node_allocator->AllocateNode(bindir.string(), 1, 1);
});
}
c.Run();

return 0;
}
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# EbbRT-memcached
EbbRT based memcached application

## Requirements
* (Optional) Set the `EBBRT_SRCDIR` environment variable to point to
EbbRT source directory: `export EBBRT_SRCDIR=~/EbbRT`
* Build and install EbbRT native toolchain
* Build and install EbbRT hosted library
* Build and install [EbRRT cmdline library](https://github.com/SESA/EbbRT/tree/master/libs/cmdline)

## Build

Set the `EBBRT_SYSROOT` environment variable to point to EbbRT native toolchain, assuming
it was installed at following location:

`export EBBRT_SYSROOT=~/sysroot/native`

Set the `CMAKE_PREFIX_PATH` environment variable to point to EbbRT hosted library, assuming
it was installed at following location:

`export CMAKE_PREFIX_PATH=~/sysroot/hosted`

To build native and hosted memcached application:

`make -j all`

To run:

`./build/Memcached`

197 changes: 197 additions & 0 deletions cmake/FindCapnp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#
# Finds the Cap'n Proto libraries, and compiles schema files.
#
# Configuration variables (optional):
# CAPNPC_OUTPUT_DIR
# Directory to place compiled schema sources (default: the same directory as the schema file).
# CAPNPC_IMPORT_DIRS
# List of additional include directories for the schema compiler.
# (CMAKE_CURRENT_SOURCE_DIR and CAPNP_INCLUDE_DIRS are always included.)
# CAPNPC_SRC_PREFIX
# Schema file source prefix (default: CMAKE_CURRENT_SOURCE_DIR).
# CAPNPC_FLAGS
# Additional flags to pass to the schema compiler.
#
# Variables that are discovered:
# CAPNP_EXECUTABLE
# Path to the `capnp` tool (can be set to override).
# CAPNPC_CXX_EXECUTABLE
# Path to the `capnpc-c++` tool (can be set to override).
# CAPNP_INCLUDE_DIRS
# Include directories for the library's headers (can be set to override).
# CANP_LIBRARIES
# The Cap'n Proto library paths.
# CAPNP_LIBRARIES_LITE
# Paths to only the 'lite' libraries.
# CAPNP_DEFINITIONS
# Compiler definitions required for building with the library.
# CAPNP_FOUND
# Set if the libraries have been located.
#
# Example usage:
#
# find_package(CapnProto REQUIRED)
# include_directories(${CAPNP_INCLUDE_DIRS})
# add_definitions(${CAPNP_DEFINITIONS})
#
# capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp)
# add_executable(a a.cc ${CAPNP_SRCS} ${CAPNP_HDRS})
# target_link_library(a ${CAPNP_LIBRARIES})
#
# For out-of-source builds:
#
# set(CAPNPC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
# include_directories(${CAPNPC_OUTPUT_DIR})
# capnp_generate_cpp(...)
#

# CAPNP_GENERATE_CPP ===========================================================

function(CAPNP_GENERATE_CPP SOURCES HEADERS)
if(NOT ARGN)
message(SEND_ERROR "CAPNP_GENERATE_CPP() called without any source files.")
endif()
if(NOT CAPNP_EXECUTABLE)
message(SEND_ERROR "Could not locate capnp executable (CAPNP_EXECUTABLE).")
endif()
if(NOT CAPNPC_CXX_EXECUTABLE)
message(SEND_ERROR "Could not locate capnpc-c++ executable (CAPNPC_CXX_EXECUTABLE).")
endif()
if(NOT CAPNP_INCLUDE_DIRS)
message(SEND_ERROR "Could not locate capnp header files (CAPNP_INCLUDE_DIRS).")
endif()

# Default compiler includes
set(include_path -I ${CMAKE_CURRENT_SOURCE_DIR} -I ${CAPNP_INCLUDE_DIRS})

if(DEFINED CAPNPC_IMPORT_DIRS)
# Append each directory as a series of '-I' flags in ${include_path}
foreach(directory ${CAPNPC_IMPORT_DIRS})
get_filename_component(absolute_path "${directory}" ABSOLUTE)
list(APPEND include_path -I ${absolute_path})
endforeach()
endif()

if(DEFINED CAPNPC_OUTPUT_DIR)
# Prepend a ':' to get the format for the '-o' flag right
set(output_dir ":${CAPNPC_OUTPUT_DIR}")
else()
set(output_dir ":.")
endif()

if(NOT DEFINED CAPNPC_SRC_PREFIX)
set(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE)

set(${SOURCES})
set(${HEADERS})
foreach(schema_file ${ARGN})
get_filename_component(file_path "${schema_file}" ABSOLUTE)
get_filename_component(file_dir "${file_path}" PATH)

# Figure out where the output files will go
if (NOT DEFINED CAPNPC_OUTPUT_DIR)
set(output_base "${file_path}")
else()
# Output files are placed in CAPNPC_OUTPUT_DIR, at a location as if they were
# relative to CAPNPC_SRC_PREFIX.
string(LENGTH "${CAPNPC_SRC_PREFIX}" prefix_len)
string(SUBSTRING "${file_path}" 0 ${prefix_len} output_prefix)
if(NOT "${CAPNPC_SRC_PREFIX}" STREQUAL "${output_prefix}")
message(SEND_ERROR "Could not determine output path for '${schema_file}' ('${file_path}') with source prefix '${CAPNPC_SRC_PREFIX}' into '${CAPNPC_OUTPUT_DIR}'.")
endif()

string(SUBSTRING "${file_path}" ${prefix_len} -1 output_path)
set(output_base "${CAPNPC_OUTPUT_DIR}${output_path}")
endif()

add_custom_command(
OUTPUT "${output_base}.c++" "${output_base}.h"
COMMAND "${CAPNP_EXECUTABLE}"
ARGS compile
-o ${CAPNPC_CXX_EXECUTABLE}${output_dir}
--src-prefix ${CAPNPC_SRC_PREFIX}
${include_path}
${CAPNPC_FLAGS}
${file_path}
DEPENDS "${schema_file}"
COMMENT "Compiling Cap'n Proto schema ${schema_file}"
VERBATIM
)
list(APPEND ${SOURCES} "${output_base}.c++")
list(APPEND ${HEADERS} "${output_base}.h")
endforeach()

set_source_files_properties(${${SOURCES}} ${${HEADERS}} PROPERTIES GENERATED TRUE)
set(${SOURCES} ${${SOURCES}} PARENT_SCOPE)
set(${HEADERS} ${${HEADERS}} PARENT_SCOPE)
endfunction()

# Find Libraries/Paths =========================================================

# Use pkg-config to get path hints and definitions
find_package(PkgConfig QUIET)
pkg_check_modules(PKGCONFIG_CAPNP capnp)
pkg_check_modules(PKGCONFIG_CAPNP_RPC capnp-rpc QUIET)
pkg_check_modules(PKGCONFIG_CAPNP_JSON capnp-json QUIET)

find_library(CAPNP_LIB_KJ kj
HINTS "${PKGCONFIG_CAPNP_LIBDIR}" ${PKGCONFIG_CAPNP_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_KJ-ASYNC kj-async
HINTS "${PKGCONFIG_CAPNP_RPC_LIBDIR}" ${PKGCONFIG_CAPNP_RPC_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_CAPNP capnp
HINTS "${PKGCONFIG_CAPNP_LIBDIR}" ${PKGCONFIG_CAPNP_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_CAPNP-RPC capnp-rpc
HINTS "${PKGCONFIG_CAPNP_RPC_LIBDIR}" ${PKGCONFIG_CAPNP_RPC_LIBRARY_DIRS}
)
find_library(CAPNP_LIB_CAPNP-JSON capnp-json
HINTS "${PKGCONFIG_CAPNP_JSON_LIBDIR}" ${PKGCONFIG_CAPNP_JSON_LIBRARY_DIRS}
)
mark_as_advanced(CAPNP_LIB_KJ CAPNP_LIB_KJ-ASYNC CAPNP_LIB_CAPNP CAPNP_LIB_CAPNP-RPC CAPNP_LIB_CAPNP-JSON)
set(CAPNP_LIBRARIES_LITE
${CAPNP_LIB_CAPNP}
${CAPNP_LIB_KJ}
)
set(CAPNP_LIBRARIES
${CAPNP_LIB_CAPNP-JSON}
${CAPNP_LIB_CAPNP-RPC}
${CAPNP_LIB_CAPNP}
${CAPNP_LIB_KJ-ASYNC}
${CAPNP_LIB_KJ}
)

# Was only the 'lite' library found?
if(CAPNP_LIB_CAPNP AND NOT CAPNP_LIB_CAPNP-RPC)
set(CAPNP_DEFINITIONS -DCAPNP_LITE)
else()
set(CAPNP_DEFINITIONS)
endif()

find_path(CAPNP_INCLUDE_DIRS capnp/generated-header-support.h
HINTS "${PKGCONFIG_CAPNP_INCLUDEDIR}" ${PKGCONFIG_CAPNP_INCLUDE_DIRS}
)

find_program(CAPNP_EXECUTABLE
NAMES capnp
DOC "Cap'n Proto Command-line Tool"
HINTS "${PKGCONFIG_CAPNP_PREFIX}/bin"
)

find_program(CAPNPC_CXX_EXECUTABLE
NAMES capnpc-c++
DOC "Capn'n Proto C++ Compiler"
HINTS "${PKGCONFIG_CAPNP_PREFIX}/bin"
)

# Only *require* the include directory, libkj, and libcapnp. If compiling with
# CAPNP_LITE, nothing else will be found.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CAPNP DEFAULT_MSG
CAPNP_INCLUDE_DIRS
CAPNP_LIB_KJ
CAPNP_LIB_CAPNP
)
Loading

0 comments on commit b7563dc

Please sign in to comment.