CPP: Add AbstractRegExpFactory to allow the use of both ICU Regex and RE2.
[platform/upstream/libphonenumber.git] / cpp / CMakeLists.txt
1 # Copyright (C) 2011 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Author: Philippe Liard
16
17 cmake_minimum_required (VERSION 2.8)
18
19 project (libphonenumber)
20
21 # Helper functions dealing with finding libraries and programs this library
22 # depends on.
23
24 function (print_error DESCRIPTION FILE)
25   message (FATAL_ERROR
26     "Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.")
27 endfunction ()
28
29 # Find a library. If it has not been found, stop CMake with a fatal error
30 # message.
31 function (find_required_library NAME HEADER LIBRARY DESCRIPTION)
32   # Check the header.
33   find_path (${NAME}_INCLUDE_DIR ${HEADER})
34   set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR})
35
36   if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND")
37     print_error (${DESCRIPTION} ${HEADER})
38   endif ()
39   include_directories (${INCLUDE_DIR})
40   # Check the binary.
41   find_library (${NAME}_LIB ${LIBRARY})
42   set (LIB ${NAME}_LIB)
43
44   if (${LIB} STREQUAL "${LIB}-NOTFOUND")
45     print_error (${DESCRIPTION} ${LIBRARY})
46   endif ()
47 endfunction (find_required_library)
48
49 # Check the library version (if pkg-config available).
50 find_package (PkgConfig)
51 function (check_library_version VARNAME LIBRARY_WITH_VERSION)
52   if (PKG_CONFIG_FOUND)
53     pkg_check_modules (${VARNAME} REQUIRED ${LIBRARY_WITH_VERSION})
54   endif ()
55 endfunction ()
56
57 # Find a program. If it has not been found, stop CMake with a fatal error
58 # message.
59 function (find_required_program NAME FILENAME DESCRIPTION)
60   find_program (${NAME}_BIN NAMES ${FILENAME})
61
62   if (${NAME}_BIN STREQUAL "${${NAME}_BIN}-NOTFOUND")
63     print_error (${DESCRIPTION} ${FILENAME})
64   endif ()
65 endfunction (find_required_program)
66
67 # Options that can be passed to CMake using 'cmake -DKEY=VALUE'.
68 option ("USE_LITE_METADATA" "Use lite metadata" "OFF")
69 option ("USE_RE2" "Use RE2 instead of ICU" "OFF")
70 option ("USE_STD_MAP" "Force the use of std::map" "OFF")
71
72 # Find all the required libraries and programs.
73 find_package (Boost 1.40.0 COMPONENTS thread)
74 if (NOT Boost_FOUND)
75   print_error ("Boost Thread" "Boost")
76 endif ()
77 include_directories (${Boost_INCLUDE_DIRS})
78
79 find_required_library (GTEST gtest/gtest.h gtest "Google Test framework")
80
81 if (${USE_RE2} STREQUAL "ON")
82   find_required_library (RE2 re2/re2.h re2 "Google RE2")
83 endif ()
84
85 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf
86                        "Google Protocol Buffers")
87 check_library_version (PC_PROTOBUF protobuf>=2.4)
88
89 find_required_library (ICU_UC unicode/uchar.h icuuc "ICU")
90 check_library_version (PC_ICU_UC icu-uc>=4.4)
91
92 set (ICU_INCLUDE_DIR ${ICU_UC_INCLUDE_DIR})
93 set (ICU_LIB ${ICU_UC_LIB})
94 # If ICU regexp engine is used, use icui18n as well.
95 if (${USE_RE2} STREQUAL "OFF")
96   find_required_library (ICU_I18N unicode/regex.h icui18n "ICU")
97   check_library_version (PC_ICU_I18N icu-i18n>=4.4)
98   list (APPEND ICU_INCLUDE_DIR ${ICU_I18N_INCLUDE_DIR})
99   list (APPEND ICU_LIB ${ICU_I18N_LIB})
100 endif ()
101
102 find_required_program (PROTOC protoc
103                        "Google Protocol Buffers compiler (protoc)")
104
105 find_required_program (JAVA java
106                        "Java Runtime Environment")
107
108 if (APPLE)
109   FIND_LIBRARY (COREFOUNDATION_LIB CoreFoundation)
110   FIND_LIBRARY (FOUNDATION_LIB Foundation)
111 endif ()
112
113 if (${USE_STD_MAP} STREQUAL "OFF")
114   INCLUDE (CheckIncludeFileCXX)
115   CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP)
116   if (HAVE_CXX_TR1_UNORDERED_MAP)
117     add_definitions ("-DUSE_TR1_UNORDERED_MAP")
118   endif ()
119 endif ()
120
121 # Add protoc (Protocol Buffers compiler) target.
122 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources")
123
124 set (
125   PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto"
126                    "${RESOURCES_DIR}/phonenumber.proto"
127 )
128
129 set (
130   PROTOBUF_OUTPUT "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.cc"
131                   "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.h"
132                   "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.cc"
133                   "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.h"
134 )
135
136 add_custom_command (
137   COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_SOURCE_DIR}/src/phonenumbers/
138     --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES}
139
140   OUTPUT ${PROTOBUF_OUTPUT}
141   DEPENDS ${PROTOBUF_SOURCES}
142 )
143
144 add_custom_target (
145   generate-sources
146
147   DEPENDS ${PROTOBUF_OUTPUT}
148   COMMENT "Generating Protocol Buffers code"
149 )
150
151 set (
152   SOURCES
153   "src/base/string_piece.cc"
154   "src/phonenumbers/default_logger.cc"
155   "src/phonenumbers/logger.cc"
156   "src/phonenumbers/metadata.h"          # Generated by build tools.
157   "src/phonenumbers/phonemetadata.pb.cc" # Generated by Protocol Buffers.
158   "src/phonenumbers/phonenumber.cc"
159   "src/phonenumbers/phonenumber.pb.cc"   # Generated by Protocol Buffers.
160   "src/phonenumbers/phonenumberutil.cc"
161   "src/phonenumbers/regexp_cache.cc"
162   "src/phonenumbers/stringutil.cc"
163   "src/phonenumbers/utf/rune.c"
164   "src/phonenumbers/utf/unicodetext.cc"
165   "src/phonenumbers/utf/unilib.cc"
166 )
167
168 # Add regexp engine sources. ICU is used by default.
169 if (${USE_RE2} STREQUAL "ON")
170   # Add a flag to select the right regexp factory implementation used by
171   # regexp_factory.h and regexp_adapter_test.cc.
172   add_definitions (-DUSE_RE2)
173   list (APPEND SOURCES "src/phonenumbers/regexp_adapter_re2.cc")
174 else ()
175   list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc")
176 endif ()
177
178 # Library sources excluding the metadata files, since special metadata is used
179 # for unit-testing.
180 set (TESTING_LIBRARY_SOURCES ${SOURCES})
181
182 # Add metadata code generation targets.
183
184 # This function is invoked to create metadata, test metadata and lite metadata
185 # code generation targets.
186 function (add_metadata_gen_target TARGET_NAME
187                                   XML_FILE
188                                   METADATA_TYPE)
189   set (METADATA_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/phonenumbers")
190   set (GEN_OUTPUT "${METADATA_SOURCE_DIR}/${METADATA_TYPE}.cc"
191                   "${METADATA_SOURCE_DIR}/metadata.h")
192   set (JAR_PATH "${CMAKE_SOURCE_DIR}/../tools/java/cpp-build/target")
193   set (JAR_PATH "${JAR_PATH}/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar")
194
195   add_custom_command (
196     COMMAND ${JAVA_BIN} -jar
197       ${JAR_PATH} BuildMetadataCppFromXml ${XML_FILE}
198       ${CMAKE_SOURCE_DIR}/src/phonenumbers ${METADATA_TYPE}
199
200     OUTPUT ${GEN_OUTPUT}
201     DEPENDS ${XML_FILE}
202   )
203   add_custom_target (
204     ${TARGET_NAME}
205     DEPENDS ${GEN_OUTPUT}
206     COMMENT "Generating Metadata code"
207   )
208 endfunction (add_metadata_gen_target)
209
210 if (${USE_LITE_METADATA} STREQUAL "ON")
211   # Add the lite metadata generation target.
212   set (METADATA_TARGET "generate-lite-metadata")
213   add_metadata_gen_target (
214     ${METADATA_TARGET}
215     "${RESOURCES_DIR}/PhoneNumberMetaData.xml"
216     "lite_metadata"
217   )
218   list (APPEND SOURCES "src/phonenumbers/lite_metadata.cc")
219 else ()
220   # Add the metadata generation target.
221   set (METADATA_TARGET "generate-metadata")
222   add_metadata_gen_target (
223     ${METADATA_TARGET}
224     "${RESOURCES_DIR}/PhoneNumberMetaData.xml"
225     "metadata"
226   )
227   list (APPEND SOURCES "src/phonenumbers/metadata.cc")
228 endif ()
229
230 # Add the test metadata generation target.
231 set (TEST_METADATA_TARGET "generate-test-metadata")
232 add_metadata_gen_target (
233   ${TEST_METADATA_TARGET}
234   "${RESOURCES_DIR}/PhoneNumberMetaDataForTesting.xml"
235   "test_metadata"
236 )
237 list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/test_metadata.cc")
238 add_definitions ("-Wall -Werror")
239
240 include_directories ("src")
241
242 # Build a static library (without -fPIC).
243 add_library (phonenumber STATIC ${SOURCES})
244 add_dependencies (phonenumber generate-sources ${METADATA_TARGET})
245
246 # Build a shared library (with -fPIC).
247 set (BUILD_SHARED_LIB true)
248
249 if (${USE_RE2} STREQUAL "ON")
250   # RE2 is not always available as a shared library (e.g: package provided by
251   # Ubuntu) therefore disable the shared library build in this case.
252   if (${RE2_LIB} MATCHES ".*\\.a")
253     message (WARNING
254       "RE2 not available as a shared library, shared library build disabled")
255     set (BUILD_SHARED_LIB false)
256   endif ()
257 endif ()
258
259 if (BUILD_SHARED_LIB)
260   add_library (phonenumber-shared SHARED ${SOURCES})
261   add_dependencies (phonenumber-shared generate-sources ${METADATA_TARGET})
262   set_target_properties (phonenumber-shared
263                          PROPERTIES OUTPUT_NAME "phonenumber")
264   set_target_properties (phonenumber-shared PROPERTIES PREFIX "lib")
265 endif ()
266
267 set (LIBRARY_DEPS ${PROTOBUF_LIB} ${ICU_LIB} ${Boost_LIBRARIES})
268
269 if (${USE_RE2} STREQUAL "ON")
270   list (APPEND LIBRARY_DEPS ${RE2_LIB})
271 endif ()
272
273 if (APPLE)
274   list (APPEND LIBRARY_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB})
275 endif ()
276
277 target_link_libraries (phonenumber ${LIBRARY_DEPS})
278
279 if (BUILD_SHARED_LIB)
280   target_link_libraries (phonenumber-shared ${LIBRARY_DEPS})
281 endif ()
282
283 # Build a specific library for testing purposes.
284 add_library (phonenumber_testing STATIC ${TESTING_LIBRARY_SOURCES})
285 target_link_libraries (phonenumber_testing ${LIBRARY_DEPS})
286 add_dependencies (phonenumber_testing generate-sources ${TEST_METADATA_TARGET})
287
288 set (TEST_SOURCES
289   "src/phonenumbers/logger_test.cc"
290   "src/phonenumbers/phonenumberutil_test.cc"
291   "src/phonenumbers/regexp_adapter_test.cc"
292   "src/phonenumbers/regexp_cache_test.cc"
293   "src/phonenumbers/run_tests.cc"
294   "src/phonenumbers/stringutil_test.cc"
295   "src/phonenumbers/utf/unicodetext_test.cc"
296 )
297
298 # Build the testing binary.
299 add_executable (libphonenumber_test ${TEST_SOURCES})
300 target_link_libraries (
301   libphonenumber_test
302   phonenumber_testing ${GTEST_LIB} pthread
303 )
304
305 # Install rules.
306 install (FILES
307   "src/phonenumbers/logger.h"
308   "src/phonenumbers/phonenumber.pb.h"
309   "src/phonenumbers/phonenumberutil.h"
310   DESTINATION include/phonenumbers/
311 )
312
313 install (FILES
314   "src/base/basictypes.h"
315   "src/base/memory/singleton.h"
316   "src/base/scoped_ptr.h"
317   DESTINATION include/base/
318 )
319 install (FILES src/base/synchronization/lock.h
320          DESTINATION include/base/synchronization)
321
322 install (TARGETS phonenumber LIBRARY DESTINATION lib/ ARCHIVE DESTINATION lib/)
323
324 if (BUILD_SHARED_LIB)
325   install (TARGETS phonenumber-shared LIBRARY DESTINATION lib/ ARCHIVE
326            DESTINATION lib/)
327 endif ()