1 # Copyright (C) 2011 The Libphonenumber Authors
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Author: Philippe Liard
17 cmake_minimum_required (VERSION 2.8)
19 project (libphonenumber)
20 set (libphonenumber_VERSION_MAJOR 7)
21 set (libphonenumber_VERSION_MINOR 0)
23 include ( GNUInstallDirs )
25 # Helper functions dealing with finding libraries and programs this library
28 include (../tools/cpp/gtest.cmake)
30 function (print_error DESCRIPTION FILE)
32 "Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.")
35 # Find a library. If it has not been found, stop CMake with a fatal error
37 function (find_required_library NAME HEADER LIBRARY DESCRIPTION)
39 find_path (${NAME}_INCLUDE_DIR ${HEADER})
40 set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR})
42 if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND")
43 print_error (${DESCRIPTION} ${HEADER})
45 include_directories (${INCLUDE_DIR})
47 find_library (${NAME}_LIB ${LIBRARY})
50 if (${LIB} STREQUAL "${LIB}-NOTFOUND")
51 print_error (${DESCRIPTION} ${LIBRARY})
53 endfunction (find_required_library)
55 # Check the library version (if pkg-config available).
56 find_package (PkgConfig)
57 function (check_library_version VARNAME LIBRARY_WITH_VERSION)
59 pkg_check_modules (${VARNAME} ${LIBRARY_WITH_VERSION})
63 # Find a program. If it has not been found, stop CMake with a fatal error
65 function (find_required_program NAME FILENAME DESCRIPTION)
66 find_program (${NAME}_BIN NAMES ${FILENAME})
68 if (${NAME}_BIN STREQUAL "${${NAME}_BIN}-NOTFOUND")
69 print_error (${DESCRIPTION} ${FILENAME})
71 endfunction (find_required_program)
73 # Options that can be passed to CMake using 'cmake -DKEY=VALUE'.
74 option ("BUILD_GEOCODER" "Build the offline phone number geocoder" "ON")
75 option ("USE_ALTERNATE_FORMATS" "Use alternate formats" "ON")
76 option ("USE_BOOST" "Use Boost" "ON")
77 option ("USE_ICU_REGEXP" "Use ICU regexp engine" "ON")
78 option ("USE_LITE_METADATA" "Use lite metadata" "OFF")
79 option ("USE_RE2" "Use RE2" "OFF")
80 option ("USE_STD_MAP" "Force the use of std::map" "OFF")
82 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
83 add_definitions ("-DI18N_PHONENUMBERS_USE_ALTERNATE_FORMATS")
86 # Find all the required libraries and programs.
87 if (${USE_BOOST} STREQUAL "ON")
88 add_definitions ("-DI18N_PHONENUMBERS_USE_BOOST")
90 set (Boost_USE_STATIC_LIBS ON)
92 find_package (Boost 1.40.0 COMPONENTS date_time system thread)
94 print_error ("Boost Date_Time/System/Thread" "Boost")
96 include_directories (${Boost_INCLUDE_DIRS})
99 find_or_build_gtest ()
101 if (${USE_RE2} STREQUAL "ON")
102 find_required_library (RE2 re2/re2.h re2 "Google RE2")
105 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf
106 "Google Protocol Buffers")
107 check_library_version (PC_PROTOBUF protobuf>=2.4)
109 find_required_library (ICU_UC unicode/uchar.h icuuc "ICU")
110 check_library_version (PC_ICU_UC icu-uc>=4.4)
112 set (ICU_INCLUDE_DIR ${ICU_UC_INCLUDE_DIR})
113 set (ICU_LIB ${ICU_UC_LIB})
114 # If ICU regexp engine is used or if the geocoder is built, use icui18n as well.
115 if (${USE_ICU_REGEXP} STREQUAL "ON" OR ${BUILD_GEOCODER} STREQUAL "ON")
116 find_required_library (ICU_I18N unicode/regex.h icui18n "ICU")
117 check_library_version (PC_ICU_I18N icu-i18n>=4.4)
118 list (APPEND ICU_INCLUDE_DIR ${ICU_I18N_INCLUDE_DIR})
119 list (APPEND ICU_LIB ${ICU_I18N_LIB})
122 find_required_program (PROTOC protoc
123 "Google Protocol Buffers compiler (protoc)")
125 find_required_program (JAVA java
126 "Java Runtime Environment")
129 FIND_LIBRARY (COREFOUNDATION_LIB CoreFoundation)
130 FIND_LIBRARY (FOUNDATION_LIB Foundation)
133 if (${USE_STD_MAP} STREQUAL "OFF")
134 INCLUDE (CheckIncludeFileCXX)
135 CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP)
136 if (HAVE_CXX_TR1_UNORDERED_MAP)
137 add_definitions ("-DI18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP")
141 # Add protoc (Protocol Buffers compiler) target.
142 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources")
145 PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto"
146 "${RESOURCES_DIR}/phonenumber.proto"
150 PROTOBUF_OUTPUT "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.cc"
151 "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.h"
152 "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.cc"
153 "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.h"
157 COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_SOURCE_DIR}/src/phonenumbers/
158 --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES}
160 OUTPUT ${PROTOBUF_OUTPUT}
161 DEPENDS ${PROTOBUF_SOURCES}
164 if (${BUILD_GEOCODER} STREQUAL "ON")
165 # Geocoding data cpp file generation
166 set (TOOLS_DIR "${CMAKE_CURRENT_BINARY_DIR}/tools")
167 add_subdirectory("${CMAKE_SOURCE_DIR}/../tools/cpp" "${TOOLS_DIR}")
169 set (GEOCODING_DIR "${RESOURCES_DIR}/geocoding")
170 file (GLOB_RECURSE GEOCODING_SOURCES "${GEOCODING_DIR}/*.txt")
172 set (GEOCODING_DATA_OUTPUT
173 "${CMAKE_SOURCE_DIR}/src/phonenumbers/geocoding/geocoding_data.cc"
177 COMMAND generate_geocoding_data "${GEOCODING_DIR}"
178 "${GEOCODING_DATA_OUTPUT}"
180 OUTPUT ${GEOCODING_DATA_OUTPUT}
181 DEPENDS ${GEOCODING_SOURCES}
182 generate_geocoding_data
183 COMMENT "Generating geocoding data code"
189 "src/phonenumbers/asyoutypeformatter.cc"
190 "src/phonenumbers/base/strings/string_piece.cc"
191 "src/phonenumbers/default_logger.cc"
192 "src/phonenumbers/logger.cc"
193 "src/phonenumbers/phonemetadata.pb.cc" # Generated by Protocol Buffers.
194 "src/phonenumbers/phonenumber.cc"
195 "src/phonenumbers/phonenumber.pb.cc" # Generated by Protocol Buffers.
196 "src/phonenumbers/phonenumberutil.cc"
197 "src/phonenumbers/regex_based_matcher.cc"
198 "src/phonenumbers/regexp_cache.cc"
199 "src/phonenumbers/shortnumberinfo.cc"
200 "src/phonenumbers/string_byte_sink.cc"
201 "src/phonenumbers/stringutil.cc"
202 "src/phonenumbers/unicodestring.cc"
203 "src/phonenumbers/utf/rune.c"
204 "src/phonenumbers/utf/unicodetext.cc"
205 "src/phonenumbers/utf/unilib.cc"
208 if (${BUILD_GEOCODER} STREQUAL "ON")
211 "src/phonenumbers/geocoding/area_code_map.cc"
212 "src/phonenumbers/geocoding/default_map_storage.cc"
213 "src/phonenumbers/geocoding/geocoding_data.cc"
214 "src/phonenumbers/geocoding/mapping_file_provider.cc"
215 "src/phonenumbers/geocoding/phonenumber_offline_geocoder.cc"
216 "src/phonenumbers/phonenumber.pb.h" # Forces proto buffer generation.
220 # Add regexp engine-dependent sources. ICU is used by default.
221 if (${USE_RE2} STREQUAL "ON")
222 # Add a flag to select the right regexp factory implementation used by
223 # regexp_factory.h and regexp_adapter_test.cc.
224 # When both ICU regexp and RE2 are defined, the regexp engine adapter defaults
225 # to RE2 unless the ICU implementation is instantiated explictly obviously.
226 add_definitions ("-DI18N_PHONENUMBERS_USE_RE2")
227 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_re2.cc")
230 if (${USE_ICU_REGEXP} STREQUAL "ON")
231 add_definitions ("-DI18N_PHONENUMBERS_USE_ICU_REGEXP")
232 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc")
233 # The phone number matcher needs ICU.
234 list (APPEND SOURCES "src/phonenumbers/phonenumbermatch.cc")
235 list (APPEND SOURCES "src/phonenumbers/phonenumbermatcher.cc")
236 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
237 list (APPEND SOURCES "src/phonenumbers/alternate_format.cc")
241 # Library sources excluding the metadata files, since special metadata is used
242 # for unit-testing. Note that a single testing library is built for both
243 # libphonenumber and geocoding.
244 set (TESTING_LIBRARY_SOURCES ${SOURCES})
245 if (${BUILD_GEOCODER} STREQUAL "ON")
246 list (APPEND TESTING_LIBRARY_SOURCES ${GEOCODING_SOURCES})
249 # Add metadata code generation targets.
251 # This function is invoked to create metadata, test metadata and lite metadata
252 # code generation targets.
253 function (add_metadata_gen_target TARGET_NAME
257 set (METADATA_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/phonenumbers")
258 set (GEN_OUTPUT "${METADATA_SOURCE_DIR}/${METADATA_TYPE}.cc"
259 "${METADATA_SOURCE_DIR}/${METADATA_HEADER}.h")
260 set (JAR_PATH "${CMAKE_SOURCE_DIR}/../tools/java/cpp-build/target")
261 set (JAR_PATH "${JAR_PATH}/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar")
264 COMMAND ${JAVA_BIN} -jar
265 ${JAR_PATH} BuildMetadataCppFromXml ${XML_FILE}
266 ${CMAKE_SOURCE_DIR}/src/phonenumbers ${METADATA_TYPE}
273 DEPENDS ${GEN_OUTPUT}
274 COMMENT "Generating Metadata code"
276 endfunction (add_metadata_gen_target)
278 if (${USE_LITE_METADATA} STREQUAL "ON")
279 # Add the lite metadata generation target.
280 set (METADATA_TARGET "generate-lite-metadata")
281 add_metadata_gen_target (
283 "${RESOURCES_DIR}/PhoneNumberMetadata.xml"
287 list (APPEND SOURCES "src/phonenumbers/lite_metadata.cc")
289 # Add the metadata generation target.
290 set (METADATA_TARGET "generate-metadata")
291 add_metadata_gen_target (
293 "${RESOURCES_DIR}/PhoneNumberMetadata.xml"
297 list (APPEND SOURCES "src/phonenumbers/metadata.cc")
300 # Add the test metadata generation target.
301 set (TEST_METADATA_TARGET "generate-test-metadata")
302 add_metadata_gen_target (
303 ${TEST_METADATA_TARGET}
304 "${RESOURCES_DIR}/PhoneNumberMetadataForTesting.xml"
308 list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/test_metadata.cc")
310 # Add the short metadata generation target.
311 set (SHORT_METADATA_TARGET "generate-short-number-metadata")
312 add_metadata_gen_target (
313 ${SHORT_METADATA_TARGET}
314 "${RESOURCES_DIR}/ShortNumberMetadata.xml"
318 # This is used both for the real library and for testing.
319 list (APPEND SOURCES "src/phonenumbers/short_metadata.cc")
320 list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/short_metadata.cc")
322 if (${USE_ICU_REGEXP} STREQUAL "ON")
323 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
324 # Add alternate format metadata generation for the phone number matcher.
325 set (ALT_FORMAT_METADATA_TARGET "generate-alt-format-metadata")
326 add_metadata_gen_target (
327 ${ALT_FORMAT_METADATA_TARGET}
328 "${RESOURCES_DIR}/PhoneNumberAlternateFormats.xml"
336 add_definitions ("-Wall -Werror")
339 include_directories ("src")
341 # Build a static library (without -fPIC).
342 add_library (phonenumber STATIC ${SOURCES})
343 if (${USE_ICU_REGEXP} STREQUAL "ON")
344 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
345 add_dependencies (phonenumber ${ALT_FORMAT_METADATA_TARGET})
349 if (${BUILD_GEOCODER} STREQUAL "ON")
350 add_library (geocoding STATIC ${GEOCODING_SOURCES})
351 # The geocoder doesn't use RE2 so there is no reason not to build a shared
353 add_library (geocoding-shared SHARED ${GEOCODING_SOURCES})
354 set_target_properties (geocoding-shared
356 OUTPUT_NAME "geocoding"
358 SOVERSION ${libphonenumber_VERSION_MAJOR}
359 VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR})
362 # Build a shared library (with -fPIC).
363 set (BUILD_SHARED_LIB true)
365 if (${USE_RE2} STREQUAL "ON")
366 # RE2 is not always available as a shared library (e.g: package provided by
367 # Ubuntu) therefore disable the shared library build in this case.
368 if (${RE2_LIB} MATCHES ".*\\.a")
370 "RE2 not available as a shared library, shared library build disabled")
371 set (BUILD_SHARED_LIB false)
375 if (BUILD_SHARED_LIB)
376 add_library (phonenumber-shared SHARED ${SOURCES})
377 if (${USE_ICU_REGEXP} STREQUAL "ON")
378 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
379 add_dependencies (phonenumber ${ALT_FORMAT_METADATA_TARGET})
382 set_target_properties (phonenumber-shared
384 OUTPUT_NAME "phonenumber"
386 SOVERSION ${libphonenumber_VERSION_MAJOR}
387 VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR})
390 # Libraries used by both libphonenumber and libgeocoding.
391 set (COMMON_DEPS ${ICU_LIB})
393 set (LIBRARY_DEPS ${PROTOBUF_LIB})
395 if (${USE_BOOST} STREQUAL "ON")
396 list (APPEND LIBRARY_DEPS ${Boost_LIBRARIES})
399 if (${USE_RE2} STREQUAL "ON")
400 list (APPEND LIBRARY_DEPS ${RE2_LIB})
404 list (APPEND COMMON_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB})
407 list (APPEND LIBRARY_DEPS ${COMMON_DEPS})
409 target_link_libraries (phonenumber ${LIBRARY_DEPS})
411 if (BUILD_SHARED_LIB)
412 target_link_libraries (phonenumber-shared ${LIBRARY_DEPS})
415 if (${BUILD_GEOCODER} STREQUAL "ON")
416 list (APPEND GEOCODER_DEPS ${COMMON_DEPS})
417 # Note that the subset of base/ on which the geocoder relies is implemented
418 # on top of Boost header-only libraries (e.g. scoped_ptr.hpp).
419 target_link_libraries (geocoding ${LIBRARY_DEPS})
420 target_link_libraries (geocoding-shared ${LIBRARY_DEPS})
423 # Build a specific library for testing purposes.
424 add_library (phonenumber_testing STATIC ${TESTING_LIBRARY_SOURCES})
425 target_link_libraries (phonenumber_testing ${LIBRARY_DEPS})
427 if (${BUILD_GEOCODER} STREQUAL "ON")
428 # Test geocoding data cpp files generation.
429 set (GEOCODING_TEST_DIR "${RESOURCES_DIR}/test/geocoding")
430 file (GLOB_RECURSE GEOCODING_TEST_SOURCES "${GEOCODING_TEST_DIR}/*.txt")
432 set (GEOCODING_TEST_DATA_OUTPUT
433 "${CMAKE_SOURCE_DIR}/test/phonenumbers/geocoding/geocoding_test_data.cc"
437 COMMAND generate_geocoding_data "${GEOCODING_TEST_DIR}"
438 "${GEOCODING_TEST_DATA_OUTPUT}" "_test"
440 OUTPUT ${GEOCODING_TEST_DATA_OUTPUT}
441 DEPENDS ${GEOCODING_TEST_SOURCES} generate_geocoding_data
442 COMMENT "Generating geocoding test data code"
448 "test/phonenumbers/asyoutypeformatter_test.cc"
449 "test/phonenumbers/logger_test.cc"
450 "test/phonenumbers/phonenumberutil_test.cc"
451 "test/phonenumbers/regexp_adapter_test.cc"
452 "test/phonenumbers/regexp_cache_test.cc"
453 "test/phonenumbers/run_tests.cc"
454 "test/phonenumbers/shortnumberinfo_test.cc"
455 "test/phonenumbers/stringutil_test.cc"
456 "test/phonenumbers/test_util.cc"
457 "test/phonenumbers/unicodestring_test.cc"
458 "test/phonenumbers/utf/unicodetext_test.cc"
461 if (${BUILD_GEOCODER} STREQUAL "ON")
462 set (GEOCODING_TEST_SOURCES
463 "test/phonenumbers/geocoding/area_code_map_test.cc"
464 "test/phonenumbers/geocoding/geocoding_data_test.cc"
465 "test/phonenumbers/geocoding/geocoding_test_data.cc"
466 "test/phonenumbers/geocoding/mapping_file_provider_test.cc"
467 "test/phonenumbers/geocoding/phonenumber_offline_geocoder_test.cc"
469 list (APPEND TEST_SOURCES ${GEOCODING_TEST_SOURCES})
472 if (${USE_ICU_REGEXP} STREQUAL "ON")
473 # Add the phone number matcher tests.
474 list (APPEND TEST_SOURCES "test/phonenumbers/phonenumbermatch_test.cc")
475 list (APPEND TEST_SOURCES "test/phonenumbers/phonenumbermatcher_test.cc")
478 # Build the testing binary.
479 include_directories ("test")
480 add_executable (libphonenumber_test ${TEST_SOURCES})
481 set (TEST_LIBS phonenumber_testing ${GTEST_LIB})
484 list (APPEND TEST_LIBS pthread)
487 target_link_libraries (libphonenumber_test ${TEST_LIBS})
489 # Unfortunately add_custom_target() can't accept a single command provided as a
491 if (${BUILD_GEOCODER} STREQUAL "ON")
492 add_custom_target (test
493 COMMAND generate_geocoding_data_test
494 COMMAND libphonenumber_test
495 DEPENDS generate_geocoding_data_test libphonenumber_test
498 add_custom_target (test
499 COMMAND libphonenumber_test
500 DEPENDS libphonenumber_test
506 "src/phonenumbers/asyoutypeformatter.h"
507 "src/phonenumbers/callback.h"
508 "src/phonenumbers/logger.h"
509 "src/phonenumbers/matcher_api.h"
510 "src/phonenumbers/phonenumber.pb.h"
511 "src/phonenumbers/phonemetadata.pb.h"
512 "src/phonenumbers/phonenumberutil.h"
513 "src/phonenumbers/regexp_adapter.h"
514 "src/phonenumbers/regexp_cache.h"
515 "src/phonenumbers/shortnumberinfo.h"
516 "src/phonenumbers/unicodestring.h"
517 DESTINATION include/phonenumbers/
520 install (FILES "src/phonenumbers/utf/unicodetext.h"
521 DESTINATION include/phonenumbers/utf/)
523 if (${USE_ICU_REGEXP} STREQUAL "ON")
524 # Install the phone number matcher headers.
526 "src/phonenumbers/phonenumbermatch.h"
527 "src/phonenumbers/phonenumbermatcher.h"
528 "src/phonenumbers/regexp_adapter.h"
529 DESTINATION include/phonenumbers/
533 if (${BUILD_GEOCODER} STREQUAL "ON")
535 "src/phonenumbers/geocoding/phonenumber_offline_geocoder.h"
536 DESTINATION include/phonenumbers/geocoding
542 "src/phonenumbers/base/basictypes.h"
543 "src/phonenumbers/base/template_util.h"
544 "src/phonenumbers/base/logging.h"
545 "src/phonenumbers/base/thread_checker.h"
546 DESTINATION include/phonenumbers/base/
550 "src/phonenumbers/base/memory/scoped_ptr.h"
551 "src/phonenumbers/base/memory/singleton.h"
552 "src/phonenumbers/base/memory/singleton_posix.h"
553 DESTINATION include/phonenumbers/base/memory/
556 install (FILES "src/phonenumbers/base/synchronization/lock.h"
557 DESTINATION include/phonenumbers/base/synchronization/)
559 install (TARGETS phonenumber LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
561 if (BUILD_SHARED_LIB)
562 install (TARGETS phonenumber-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE
563 DESTINATION ${CMAKE_INSTALL_LIBDIR})
566 if (${BUILD_GEOCODER} STREQUAL "ON")
567 install (TARGETS geocoding LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
568 install (TARGETS geocoding-shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE
569 DESTINATION ${CMAKE_INSTALL_LIBDIR})
572 # Build an example program using geocoding, mainly to make sure that both
573 # libraries are built properly.
574 if (${BUILD_GEOCODER} STREQUAL "ON")
576 geocoding_test_program
577 "test/phonenumbers/geocoding/geocoding_test_program.cc"
579 target_link_libraries (geocoding_test_program geocoding phonenumber)