Skip to content

Commit

Permalink
[cmake] Add support for shared Kaldi libs (kaldi-asr#4466)
Browse files Browse the repository at this point in the history
Co-authored-by: davidlin409 <[email protected]>
Co-authored-by: David2 Lin <[email protected]>
  • Loading branch information
3 people committed Mar 18, 2021
1 parent 1670662 commit 1b2399b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
30 changes: 26 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions cmake/gen_cmake_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -185,14 +186,17 @@ 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 = []
self.source_list = []
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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)):
Expand Down

0 comments on commit 1b2399b

Please sign in to comment.