From 1b2399b2dfe66740e1e4fd7ea07124ae9a220b82 Mon Sep 17 00:00:00 2001 From: davidlin409 Date: Tue, 16 Mar 2021 14:00:54 +0800 Subject: [PATCH] [cmake] Add support for shared Kaldi libs (#4466) Co-authored-by: davidlin409 Co-authored-by: David2 Lin --- CMakeLists.txt | 30 ++++++++++++++++++++++++++---- cmake/gen_cmake_skeleton.py | 13 ++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e0a246f089..bdd81dba1da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.5) project(kaldi) +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/src CACHE PATH "Install path prefix." FORCE) +endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") include(GNUInstallDirs) include(Utils) @@ -12,11 +15,18 @@ if(NOT PYTHON_EXECUTABLE) endif() message(STATUS "Running gen_cmake_skeleton.py") +option(BUILD_SHARED_LIBS "Build shared Kaldi libraries." OFF) +set(IS_LIB_SHARE "") +if(BUILD_SHARED_LIBS) + set(IS_LIB_SHARE "--shared") +endif() execute_process(COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_cmake_skeleton.py" "${CMAKE_CURRENT_SOURCE_DIR}/src" "--quiet" + ${IS_LIB_SHARE} ) +unset(IS_LIB_SHARE) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_EXTENSIONS OFF) @@ -29,7 +39,7 @@ if(BUILD_SHARED_LIBS) elseif(APPLE) set(CMAKE_INSTALL_RPATH "@loader_path") else() - set(CMAKE_INSTALL_RPATH "$ORIGIN;$ORIGIN/../lib") + set(CMAKE_INSTALL_RPATH "$ORIGIN;$ORIGIN/../lib;$ORIGIN/../../tools/openfst/lib") endif() endif() @@ -159,9 +169,21 @@ if(${KALDI_USE_PATCH_NUMBER}) set(KALDI_VERSION "${KALDI_VERSION}.${KALDI_PATCH_NUMBER}") endif() -get_third_party(openfst) -set(OPENFST_ROOT_DIR ${CMAKE_BINARY_DIR}/openfst) -include(third_party/openfst_lib_target) +# get_third_party(openfst) +# set(OPENFST_ROOT_DIR ${CMAKE_BINARY_DIR}/openfst) +# include(third_party/openfst_lib_target) +find_library(OpenFST_LIBRARY + NAMES fst + PATHS ${CMAKE_SOURCE_DIR}/tools/openfst/lib + REQUIRED) +find_path(OpenFST_INCLUDE_DIR + NAMES "fst/fst.h" + PATHS "${CMAKE_SOURCE_DIR}/tools/openfst/include" + REQUIRED) +get_filename_component(OpenFST_LIBRARY_DIR ${OpenFST_LIBRARY} DIRECTORY CACHE) +mark_as_advanced(FORCE OpenFST_INCLUDE_DIR OpenFST_LIBRARY OpenFST_LIBRARY_DIR) +include_directories(${OpenFST_INCLUDE_DIR}) +link_directories(${OpenFST_LIBRARY_DIR}) link_libraries(fst) # add all native libraries diff --git a/cmake/gen_cmake_skeleton.py b/cmake/gen_cmake_skeleton.py index c846d988c27..b6c40491f67 100644 --- a/cmake/gen_cmake_skeleton.py +++ b/cmake/gen_cmake_skeleton.py @@ -13,6 +13,7 @@ parser = argparse.ArgumentParser() parser.add_argument("working_dir") parser.add_argument("--quiet", default=False, action="store_true") +parser.add_argument("--shared", default=False, action="store_true") args = parser.parse_args() def print_wrapper(*args_, **kwargs): @@ -185,7 +186,9 @@ def gen_code(self): class CMakeListsLibrary(object): - def __init__(self, dir_name): + def __init__(self, dir_name, is_shared): + assert(type(is_shared) is bool) + self.dir_name = dir_name self.target_name = lib_dir_name_to_lib_target(self.dir_name) self.header_list = [] @@ -193,6 +196,7 @@ def __init__(self, dir_name): self.cuda_source_list = [] self.test_source_list = [] self.depends = [] + self.is_shared = is_shared def add_header(self, filename): self.header_list.append(filename) @@ -236,7 +240,10 @@ def gen_code(self): ret.append(" )") ret.append("endif()\n") - ret.append("add_library(" + self.target_name) + add_lib_line = "add_library(" + self.target_name + if self.is_shared: + add_lib_line += " SHARED" + ret.append(add_lib_line) for f in self.source_list: ret.append(" " + f) ret.append(")\n") @@ -345,7 +352,7 @@ def write_file(self): if not os.path.exists(makefile): lib = CMakeListsHeaderLibrary(dir_name) else: - lib = CMakeListsLibrary(dir_name) + lib = CMakeListsLibrary(dir_name, args.shared) lib.load_dependency_from_makefile(makefile) cmakelists.add_section(lib) for f in sorted(get_files(d)):