Skip to content

Commit

Permalink
refactor(sample): migrate sample plugin to new plugin build system
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Mar 8, 2019
1 parent 2d8ba45 commit f5d4bdd
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 71 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ option(BUILD_SHARED_LIBS "Build Rime as shared library" ON)
option(BUILD_MERGED_PLUGINS "Merge plugins into one Rime library" ON)
option(BUILD_STATIC "Build with dependencies as static libraries" OFF)
option(BUILD_DATA "Build data for Rime" OFF)
option(BUILD_SAMPLE "Build sample Rime plugin" OFF)
option(BUILD_TEST "Build and run tests" ON)
option(BUILD_SEPARATE_LIBS "Build separate rime-* libraries" OFF)
option(ENABLE_LOGGING "Enable logging with google-glog library" ON)
Expand Down Expand Up @@ -217,7 +218,11 @@ add_subdirectory(src)
if(BUILD_SHARED_LIBS)
add_subdirectory(tools)

if(GTEST_FOUND)
if(BUILD_TEST)
add_subdirectory(test)
endif()

if (BUILD_SAMPLE)
add_subdirectory(sample)
endif()
endif()
91 changes: 42 additions & 49 deletions sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,45 @@ cmake_minimum_required(VERSION 2.8)
set(SAMPLE_VERSION 1.0.0)
set(SAMPLE_SOVERSION 1)

option(BUILD_SHARED_LIBS "Build Rime as shared library" ON)
option(BUILD_STATIC "Build sample using static libraries" OFF)
option(BUILD_TEST "Build and run tests" ON)

if(WIN32)
message(FATAL_ERROR "Plugins are not supported under Windows.")
endif(WIN32)

set(RIME_SOURCE_DIR ${PROJECT_SOURCE_DIR}/..)
set(CMAKE_MODULE_PATH ${RIME_SOURCE_DIR}/cmake)

set(Gflags_STATIC ${BUILD_STATIC})
set(Glog_STATIC ${BUILD_STATIC})

find_package(Rime REQUIRED)
if(Rime_FOUND)
include_directories(${Rime_INCLUDE_DIR})
endif(Rime_FOUND)

find_package(Gflags)
if(Gflags_FOUND)
include_directories(${Gflags_INCLUDE_PATH})
endif(Gflags_FOUND)

find_package(Glog REQUIRED)
if(Glog_FOUND)
include_directories(${Glog_INCLUDE_PATH})
endif(Glog_FOUND)

if(BUILD_TEST)
find_package(GTest REQUIRED)
if(GTEST_FOUND)
enable_testing()
include_directories(${GTEST_INCLUDE_DIRS})
endif(GTEST_FOUND)
endif(BUILD_TEST)

include_directories(${RIME_SOURCE_DIR}/include)
#include_directories(${RIME_SOURCE_DIR}/thirdparty/include)
#link_directories(${RIME_SOURCE_DIR}/thirdparty/lib)
if(UNIX)
add_definitions("-std=c++11")
endif(UNIX)

add_subdirectory(src)
add_subdirectory(tools)
if(GTEST_FOUND)
add_subdirectory(test)
endif(GTEST_FOUND)
aux_source_directory(src sample_src)

set(sample_library rime-sample)
set(sample_deps ${rime_library})
set(sample_modules "sample")

if(BUILD_SAMPLE)
# to build the sample plugin, console app and tests, set BUILD_SAMPLE=ON
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

message(STATUS "${sample_library} provides modules: ${sample_modules}")
add_library(${sample_library} ${sample_src})
target_link_libraries(${sample_library} ${sample_deps})
set_target_properties(${sample_library}
PROPERTIES
VERSION ${SAMPLE_VERSION}
SOVERSION ${SAMPLE_SOVERSION})
if(XCODE_VERSION)
set_target_properties(${sample_library}
PROPERTIES
INSTALL_NAME_DIR "@rpath")
endif(XCODE_VERSION)
install(TARGETS ${sample_library} DESTINATION ${LIB_INSTALL_DIR})

add_subdirectory(tools)
if(BUILD_TEST)
add_subdirectory(test)
endif()
else()
# to build as a standard Rime plugin, link or copy the directory to plugins/
add_library(rime-sample-objs OBJECT ${sample_src})
if(BUILD_SHARED_LIBS)
set_target_properties(rime-sample-objs
PROPERTIES
POSITION_INDEPENDENT_CODE ON)
endif()

set(plugin_name ${sample_library} PARENT_SCOPE)
set(plugin_objs $<TARGET_OBJECTS:rime-sample-objs> PARENT_SCOPE)
set(plugin_deps ${sample_deps} PARENT_SCOPE)
set(plugin_modules ${sample_modules} PARENT_SCOPE)
endif()
76 changes: 76 additions & 0 deletions sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# A sample Rime plugin module

This directory offers a Rime plugin named `rime-sample`.

## Overview

Here's how a Rime plugin works:

A *plugin* can either be linked to librime or be a separate shared library,
corresponding to the `BUILD_MERGED_PLUGINS` cmake option.

When the shared library is loaded, it registers itself as *module* `sample`.
If otherwise it's built-in, the module will be automatically loaded by default.

When Rime `initialize`s via API, it loads its default modules or modules
specified by the caller in `RimeTraits::modules`.

Modules can also be loaded on demand using C++ API `rime::LoadModules()`.

When the module is loaded, the `rime_sample_initialize()` function is run,
which registers a *component* `trivial_translator`.

That component is now available for prescription in Rime schema.
It works with the Rime engine in the same way as the built-in translators.

## Build the sample plugin library

``` shell
cd librime
cmake . -Bbuild -DBUILD_SAMPLE=ON -DBUILD_SEPARATE_LIBS=ON
cmake --build build --target rime-sample
```

This outputs shared library: `build/sample/lib/rime-sample.so`

The `BUILD_SEPARATE_LIBS=ON` option is not required but builds faster because
`rime-sample` only depends on the core module of librime.

## Run unit tests

``` shell
cmake --build build --target sample_test

# run tests
build/sample/test/sample_test
```

## Play with sample_console

`trivial_translator` converts pinyin to Chinese numbers.
A sample Rime schema is set up in `build/bin/sample.schema.yaml` to utilize
the translator.

Build the console app and try it with a random number in pinyin:

``` shell
cmake --build build --target sample_console

cd build/sample/bin
echo "yibaiershisanwansiqianlingwushiliu" | ./sample_console
```

## Build as standard Rime plugin

Unlike the sample, which is built after specific rules in librime's cmake
script, standard Rime plugins are separate projects that automatically
integrate into librime's build system, without having to modify any source code
and build script.

To build the sample plugin as standard Rime plugin, link or copy the source code
directory to `plugins/sample` and turn off cmake flag `BUILD_SAMPLE=OFF`.

The cmake option `BUILD_MERGED_PLUGINS` merges all detected plugins into the
built `rime` library. Set the option off to build each plugin as a standalone
(shared) library. In the latter case, the user needs to explicitly load the
`rime-sample` library and load the `sample` module when `initialize`-ing.
8 changes: 0 additions & 8 deletions sample/src/CMakeLists.txt

This file was deleted.

18 changes: 12 additions & 6 deletions sample/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
aux_source_directory(. SAMPLE_TEST_SRC)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/test)
add_executable(sample_test ${SAMPLE_TEST_SRC})
target_link_libraries(sample_test rime-sample ${Rime_LIBRARIES} ${GTEST_LIBRARIES})
add_dependencies(sample_test rime-sample)

set(SAMPLE_TEST_EXECUTABLE ${EXECUTABLE_OUTPUT_PATH}/sample_test)
add_test(sample_test ${SAMPLE_TEST_EXECUTABLE})
aux_source_directory(. sample_test_src)
add_executable(sample_test ${sample_test_src})
target_link_libraries(sample_test
${sample_library}
${rime_library}
${GTEST_LIBRARIES})
if(BUILD_SHARED_LIBS)
target_compile_definitions(sample_test PRIVATE RIME_IMPORTS)
endif(BUILD_SHARED_LIBS)

set(sample_test_executable ${EXECUTABLE_OUTPUT_PATH}/sample_test)
add_test(sample_test ${sample_test_executable})
2 changes: 1 addition & 1 deletion sample/test/sample_test_main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <rime/setup.h>

static RIME_MODULE_LIST(test_modules, "default", "sample");
static RIME_MODULE_LIST(test_modules, "sample");

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
Expand Down
15 changes: 9 additions & 6 deletions sample/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(SAMPLE_CONSOLE_SRC "sample_console.cc")
add_executable(sample_console ${SAMPLE_CONSOLE_SRC})
target_link_libraries(sample_console rime-sample ${Rime_LIBRARIES})
add_dependencies(sample_console rime-sample)

set(sample_console_src "sample_console.cc")
add_executable(sample_console ${sample_console_src})
target_link_libraries(sample_console
${sample_library}
${rime_library}
${rime_dict_library}
${rime_gears_library}
${rime_levers_library})

file(COPY ${PROJECT_SOURCE_DIR}/tools/default.yaml
DESTINATION ${EXECUTABLE_OUTPUT_PATH})
file(COPY ${PROJECT_SOURCE_DIR}/tools/symbols.yaml
DESTINATION ${EXECUTABLE_OUTPUT_PATH})
file(COPY ${PROJECT_SOURCE_DIR}/tools/sample.schema.yaml
DESTINATION ${EXECUTABLE_OUTPUT_PATH})
Empty file removed sample/tools/symbols.yaml
Empty file.

0 comments on commit f5d4bdd

Please sign in to comment.