Skip to content

Commit 86c4d70

Browse files
Add CMake script
Add CMake script to buid a static lib cld3 and unittests. No dependancy with Chrome.
1 parent 484afe9 commit 86c4d70

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

CMakeLists.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# This cmake scripts only builds a static cld3 lib and the unittests.
2+
project(cld3)
3+
4+
# Old versions of cmake dont search/find protobuf lite
5+
cmake_minimum_required(VERSION 3.9)
6+
7+
find_package(Protobuf REQUIRED)
8+
message(STATUS "Protobuf_FOUND= ${Protobuf_FOUND}")
9+
message(STATUS "Protobuf_VERSION= ${Protobuf_VERSION}")
10+
message(WARNING "Protobuf 2.5 and CLD3 seems happy together. This script does NOT check if your verison of protobuf is compatible.")
11+
message(STATUS "Protobuf_LIBRARIES= ${Protobuf_LIBRARIES}")
12+
message(STATUS "Protobuf_LITE_LIBRARIES= ${Protobuf_LITE_LIBRARIES}") # Usually /usr/lib64/libprotobuf-lite.so
13+
14+
# By default, protobuf_generate_cpp generates pb.* files directy in the cmake build dir.
15+
# But CLD3 sources have been coded using hard coded pathes to cld_3/protos/*.pb.h.
16+
# So *.pb.h must be output to cld_3/protos.
17+
# For that, let's use a custom my_protobuf_generate_cpp:
18+
include(${CMAKE_CURRENT_SOURCE_DIR}/misc/myprotobuf.cmake)
19+
my_protobuf_generate_cpp(cld_3/protos PROTO_SRCS PROTO_HDRS src/feature_extractor.proto src/sentence.proto src/task_spec.proto)
20+
message(STATUS "PROTO_HDRS= ${PROTO_HDRS}")
21+
22+
add_definitions(-fPIC) # Position Independant Code
23+
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
24+
add_definitions(-std=c++11) # Needed for std::to_string(), ...
25+
26+
include_directories(${CMAKE_CURRENT_BINARY_DIR}) # needed to include generated pb headers
27+
28+
add_library(${PROJECT_NAME}
29+
${PROTO_SRCS} ${PROTO_HDRS}
30+
src/base.cc
31+
src/embedding_feature_extractor.cc
32+
src/embedding_network.cc
33+
src/feature_extractor.cc
34+
src/feature_extractor.h
35+
src/feature_types.cc
36+
src/fml_parser.cc
37+
src/language_identifier_features.cc
38+
src/lang_id_nn_params.cc
39+
src/nnet_language_identifier.cc
40+
src/registry.cc
41+
src/relevant_script_feature.cc
42+
src/sentence_features.cc
43+
src/task_context.cc
44+
src/task_context_params.cc
45+
src/unicodetext.cc
46+
src/utils.cc
47+
src/workspace.cc
48+
49+
src/script_span/generated_entities.cc
50+
src/script_span/getonescriptspan.cc
51+
src/script_span/getonescriptspan.h
52+
src/script_span/getonescriptspan_test.cc
53+
src/script_span/utf8statetable.cc
54+
src/script_span/offsetmap.cc
55+
src/script_span/text_processing.cc
56+
src/script_span/text_processing.h
57+
src/script_span/fixunicodevalue.cc
58+
)
59+
60+
# unit tests exec:
61+
add_executable(language_identifier_main src/language_identifier_main.cc)
62+
target_link_libraries(language_identifier_main cld3 ${Protobuf_LITE_LIBRARIES})
63+
64+
add_executable(getonescriptspan_test src/script_span/getonescriptspan_test.cc)
65+
target_link_libraries(getonescriptspan_test cld3 ${Protobuf_LITE_LIBRARIES})
66+
67+
add_executable(language_identifier_features_test src/language_identifier_features_test.cc)
68+
target_link_libraries(language_identifier_features_test cld3 ${Protobuf_LITE_LIBRARIES})

misc/myprotobuf.cmake

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Special PROTOBUF_GENERATE_CPP which allows to set the output folder:
2+
# From https://stackoverflow.com/users/1600278/akira-okumura
3+
4+
function(MY_PROTOBUF_GENERATE_CPP PATH SRCS HDRS)
5+
if(NOT ARGN)
6+
message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
7+
return()
8+
endif()
9+
10+
if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
11+
# Create an include path for each file specified
12+
foreach(FIL ${ARGN})
13+
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
14+
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
15+
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
16+
if(${_contains_already} EQUAL -1)
17+
list(APPEND _protobuf_include_path -I ${ABS_PATH})
18+
endif()
19+
endforeach()
20+
else()
21+
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
22+
endif()
23+
24+
if(DEFINED PROTOBUF_IMPORT_DIRS)
25+
foreach(DIR ${PROTOBUF_IMPORT_DIRS})
26+
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
27+
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
28+
if(${_contains_already} EQUAL -1)
29+
list(APPEND _protobuf_include_path -I ${ABS_PATH})
30+
endif()
31+
endforeach()
32+
endif()
33+
34+
set(${SRCS})
35+
set(${HDRS})
36+
foreach(FIL ${ARGN})
37+
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
38+
get_filename_component(FIL_WE ${FIL} NAME_WE)
39+
40+
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${PATH}/${FIL_WE}.pb.cc")
41+
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${PATH}/${FIL_WE}.pb.h")
42+
43+
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${PATH})
44+
45+
add_custom_command(
46+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${PATH}/${FIL_WE}.pb.cc"
47+
"${CMAKE_CURRENT_BINARY_DIR}/${PATH}/${FIL_WE}.pb.h"
48+
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
49+
ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR}/${PATH} ${_protobuf_include_path} ${ABS_FIL}
50+
DEPENDS ${ABS_FIL}
51+
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
52+
VERBATIM )
53+
endforeach()
54+
55+
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
56+
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
57+
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
58+
endfunction()

src/language_identifier_features_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
#include <cmath>
1717
#include <iostream>
1818
#include <vector>
19+
#include <set>
1920

2021
#include "base.h"
2122
#include "feature_extractor.h"

0 commit comments

Comments
 (0)