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 # Helper functions dealing with finding libraries and programs this library
26 include (../tools/cpp/gtest.cmake)
28 function (print_error DESCRIPTION FILE)
30 "Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.")
33 # Find a library. If it has not been found, stop CMake with a fatal error
35 function (find_required_library NAME HEADER LIBRARY DESCRIPTION)
37 find_path (${NAME}_INCLUDE_DIR ${HEADER})
38 set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR})
40 if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND")
41 print_error (${DESCRIPTION} ${HEADER})
43 include_directories (${INCLUDE_DIR})
45 find_library (${NAME}_LIB ${LIBRARY})
48 if (${LIB} STREQUAL "${LIB}-NOTFOUND")
49 print_error (${DESCRIPTION} ${LIBRARY})
51 endfunction (find_required_library)
53 # Check the library version (if pkg-config available).
54 find_package (PkgConfig)
55 function (check_library_version VARNAME LIBRARY_WITH_VERSION)
57 pkg_check_modules (${VARNAME} ${LIBRARY_WITH_VERSION})
61 # Find a program. If it has not been found, stop CMake with a fatal error
63 function (find_required_program NAME FILENAME DESCRIPTION)
64 find_program (${NAME}_BIN NAMES ${FILENAME})
66 if (${NAME}_BIN STREQUAL "${${NAME}_BIN}-NOTFOUND")
67 print_error (${DESCRIPTION} ${FILENAME})
69 endfunction (find_required_program)
71 # Options that can be passed to CMake using 'cmake -DKEY=VALUE'.
72 option ("BUILD_GEOCODER" "Build the offline phone number geocoder" "ON")
73 option ("USE_ALTERNATE_FORMATS" "Use alternate formats" "ON")
74 option ("USE_BOOST" "Use Boost" "ON")
75 option ("USE_ICU_REGEXP" "Use ICU regexp engine" "ON")
76 option ("USE_LITE_METADATA" "Use lite metadata" "OFF")
77 option ("USE_RE2" "Use RE2" "OFF")
78 option ("USE_STD_MAP" "Force the use of std::map" "OFF")
80 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
81 add_definitions ("-DI18N_PHONENUMBERS_USE_ALTERNATE_FORMATS")
84 # Find all the required libraries and programs.
85 if (${USE_BOOST} STREQUAL "ON")
86 add_definitions ("-DI18N_PHONENUMBERS_USE_BOOST")
88 set (Boost_USE_STATIC_LIBS ON)
90 find_package (Boost 1.40.0 COMPONENTS date_time system thread)
92 print_error ("Boost Date_Time/System/Thread" "Boost")
94 include_directories (${Boost_INCLUDE_DIRS})
97 find_or_build_gtest ()
99 if (${USE_RE2} STREQUAL "ON")
100 find_required_library (RE2 re2/re2.h re2 "Google RE2")
103 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf
104 "Google Protocol Buffers")
105 check_library_version (PC_PROTOBUF protobuf>=2.4)
107 find_required_library (ICU_UC unicode/uchar.h icuuc "ICU")
108 check_library_version (PC_ICU_UC icu-uc>=4.4)
110 set (ICU_INCLUDE_DIR ${ICU_UC_INCLUDE_DIR})
111 set (ICU_LIB ${ICU_UC_LIB})
112 # If ICU regexp engine is used or if the geocoder is built, use icui18n as well.
113 if (${USE_ICU_REGEXP} STREQUAL "ON" OR ${BUILD_GEOCODER} STREQUAL "ON")
114 find_required_library (ICU_I18N unicode/regex.h icui18n "ICU")
115 check_library_version (PC_ICU_I18N icu-i18n>=4.4)
116 list (APPEND ICU_INCLUDE_DIR ${ICU_I18N_INCLUDE_DIR})
117 list (APPEND ICU_LIB ${ICU_I18N_LIB})
120 find_required_program (PROTOC protoc
121 "Google Protocol Buffers compiler (protoc)")
123 find_required_program (JAVA java
124 "Java Runtime Environment")
127 FIND_LIBRARY (COREFOUNDATION_LIB CoreFoundation)
128 FIND_LIBRARY (FOUNDATION_LIB Foundation)
131 if (${USE_STD_MAP} STREQUAL "OFF")
132 INCLUDE (CheckIncludeFileCXX)
133 CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP)
134 if (HAVE_CXX_TR1_UNORDERED_MAP)
135 add_definitions ("-DI18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP")
139 # Add protoc (Protocol Buffers compiler) target.
140 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources")
143 PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto"
144 "${RESOURCES_DIR}/phonenumber.proto"
148 PROTOBUF_OUTPUT "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.cc"
149 "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.h"
150 "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.cc"
151 "${CMAKE_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.h"
155 COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_SOURCE_DIR}/src/phonenumbers/
156 --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES}
158 OUTPUT ${PROTOBUF_OUTPUT}
159 DEPENDS ${PROTOBUF_SOURCES}
162 if (${BUILD_GEOCODER} STREQUAL "ON")
163 # Geocoding data cpp file generation
164 set (TOOLS_DIR "${CMAKE_CURRENT_BINARY_DIR}/tools")
165 add_subdirectory("${CMAKE_SOURCE_DIR}/../tools/cpp" "${TOOLS_DIR}")
167 set (GEOCODING_DIR "${RESOURCES_DIR}/geocoding")
168 file (GLOB_RECURSE GEOCODING_SOURCES "${GEOCODING_DIR}/*.txt")
170 set (GEOCODING_DATA_OUTPUT
171 "${CMAKE_SOURCE_DIR}/src/phonenumbers/geocoding/geocoding_data.cc"
175 COMMAND generate_geocoding_data "${GEOCODING_DIR}"
176 "${GEOCODING_DATA_OUTPUT}"
178 OUTPUT ${GEOCODING_DATA_OUTPUT}
179 DEPENDS ${GEOCODING_SOURCES}
180 generate_geocoding_data
181 COMMENT "Generating geocoding data code"
187 "src/phonenumbers/asyoutypeformatter.cc"
188 "src/phonenumbers/base/strings/string_piece.cc"
189 "src/phonenumbers/default_logger.cc"
190 "src/phonenumbers/logger.cc"
191 "src/phonenumbers/phonemetadata.pb.cc" # Generated by Protocol Buffers.
192 "src/phonenumbers/phonenumber.cc"
193 "src/phonenumbers/phonenumber.pb.cc" # Generated by Protocol Buffers.
194 "src/phonenumbers/phonenumberutil.cc"
195 "src/phonenumbers/regex_based_matcher.cc"
196 "src/phonenumbers/regexp_cache.cc"
197 "src/phonenumbers/shortnumberinfo.cc"
198 "src/phonenumbers/string_byte_sink.cc"
199 "src/phonenumbers/stringutil.cc"
200 "src/phonenumbers/unicodestring.cc"
201 "src/phonenumbers/utf/rune.c"
202 "src/phonenumbers/utf/unicodetext.cc"
203 "src/phonenumbers/utf/unilib.cc"
206 if (${BUILD_GEOCODER} STREQUAL "ON")
209 "src/phonenumbers/geocoding/area_code_map.cc"
210 "src/phonenumbers/geocoding/default_map_storage.cc"
211 "src/phonenumbers/geocoding/geocoding_data.cc"
212 "src/phonenumbers/geocoding/mapping_file_provider.cc"
213 "src/phonenumbers/geocoding/phonenumber_offline_geocoder.cc"
214 "src/phonenumbers/phonenumber.pb.h" # Forces proto buffer generation.
218 # Add regexp engine-dependent sources. ICU is used by default.
219 if (${USE_RE2} STREQUAL "ON")
220 # Add a flag to select the right regexp factory implementation used by
221 # regexp_factory.h and regexp_adapter_test.cc.
222 # When both ICU regexp and RE2 are defined, the regexp engine adapter defaults
223 # to RE2 unless the ICU implementation is instantiated explictly obviously.
224 add_definitions ("-DI18N_PHONENUMBERS_USE_RE2")
225 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_re2.cc")
228 if (${USE_ICU_REGEXP} STREQUAL "ON")
229 add_definitions ("-DI18N_PHONENUMBERS_USE_ICU_REGEXP")
230 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc")
231 # The phone number matcher needs ICU.
232 list (APPEND SOURCES "src/phonenumbers/phonenumbermatch.cc")
233 list (APPEND SOURCES "src/phonenumbers/phonenumbermatcher.cc")
234 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
235 list (APPEND SOURCES "src/phonenumbers/alternate_format.cc")
239 # Library sources excluding the metadata files, since special metadata is used
240 # for unit-testing. Note that a single testing library is built for both
241 # libphonenumber and geocoding.
242 set (TESTING_LIBRARY_SOURCES ${SOURCES})
243 if (${BUILD_GEOCODER} STREQUAL "ON")
244 list (APPEND TESTING_LIBRARY_SOURCES ${GEOCODING_SOURCES})
247 # Add metadata code generation targets.
249 # This function is invoked to create metadata, test metadata and lite metadata
250 # code generation targets.
251 function (add_metadata_gen_target TARGET_NAME
255 set (METADATA_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/phonenumbers")
256 set (GEN_OUTPUT "${METADATA_SOURCE_DIR}/${METADATA_TYPE}.cc"
257 "${METADATA_SOURCE_DIR}/${METADATA_HEADER}.h")
258 set (JAR_PATH "${CMAKE_SOURCE_DIR}/../tools/java/cpp-build/target")
259 set (JAR_PATH "${JAR_PATH}/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar")
262 COMMAND ${JAVA_BIN} -jar
263 ${JAR_PATH} BuildMetadataCppFromXml ${XML_FILE}
264 ${CMAKE_SOURCE_DIR}/src/phonenumbers ${METADATA_TYPE}
271 DEPENDS ${GEN_OUTPUT}
272 COMMENT "Generating Metadata code"
274 endfunction (add_metadata_gen_target)
276 if (${USE_LITE_METADATA} STREQUAL "ON")
277 # Add the lite metadata generation target.
278 set (METADATA_TARGET "generate-lite-metadata")
279 add_metadata_gen_target (
281 "${RESOURCES_DIR}/PhoneNumberMetadata.xml"
285 list (APPEND SOURCES "src/phonenumbers/lite_metadata.cc")
287 # Add the metadata generation target.
288 set (METADATA_TARGET "generate-metadata")
289 add_metadata_gen_target (
291 "${RESOURCES_DIR}/PhoneNumberMetadata.xml"
295 list (APPEND SOURCES "src/phonenumbers/metadata.cc")
298 # Add the test metadata generation target.
299 set (TEST_METADATA_TARGET "generate-test-metadata")
300 add_metadata_gen_target (
301 ${TEST_METADATA_TARGET}
302 "${RESOURCES_DIR}/PhoneNumberMetadataForTesting.xml"
306 list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/test_metadata.cc")
308 # Add the short metadata generation target.
309 set (SHORT_METADATA_TARGET "generate-short-number-metadata")
310 add_metadata_gen_target (
311 ${SHORT_METADATA_TARGET}
312 "${RESOURCES_DIR}/ShortNumberMetadata.xml"
316 # This is used both for the real library and for testing.
317 list (APPEND SOURCES "src/phonenumbers/short_metadata.cc")
318 list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/short_metadata.cc")
320 if (${USE_ICU_REGEXP} STREQUAL "ON")
321 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
322 # Add alternate format metadata generation for the phone number matcher.
323 set (ALT_FORMAT_METADATA_TARGET "generate-alt-format-metadata")
324 add_metadata_gen_target (
325 ${ALT_FORMAT_METADATA_TARGET}
326 "${RESOURCES_DIR}/PhoneNumberAlternateFormats.xml"
334 add_definitions ("-Wall -Werror")
337 include_directories ("src")
339 # Build a static library (without -fPIC).
340 add_library (phonenumber STATIC ${SOURCES})
341 if (${USE_ICU_REGEXP} STREQUAL "ON")
342 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
343 add_dependencies (phonenumber ${ALT_FORMAT_METADATA_TARGET})
347 if (${BUILD_GEOCODER} STREQUAL "ON")
348 add_library (geocoding STATIC ${GEOCODING_SOURCES})
349 # The geocoder doesn't use RE2 so there is no reason not to build a shared
351 add_library (geocoding-shared SHARED ${GEOCODING_SOURCES})
352 set_target_properties (geocoding-shared
354 OUTPUT_NAME "geocoding"
356 SOVERSION ${libphonenumber_VERSION_MAJOR}
357 VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR})
360 # Build a shared library (with -fPIC).
361 set (BUILD_SHARED_LIB true)
363 if (${USE_RE2} STREQUAL "ON")
364 # RE2 is not always available as a shared library (e.g: package provided by
365 # Ubuntu) therefore disable the shared library build in this case.
366 if (${RE2_LIB} MATCHES ".*\\.a")
368 "RE2 not available as a shared library, shared library build disabled")
369 set (BUILD_SHARED_LIB false)
373 if (BUILD_SHARED_LIB)
374 add_library (phonenumber-shared SHARED ${SOURCES})
375 if (${USE_ICU_REGEXP} STREQUAL "ON")
376 if (${USE_ALTERNATE_FORMATS} STREQUAL "ON")
377 add_dependencies (phonenumber ${ALT_FORMAT_METADATA_TARGET})
380 set_target_properties (phonenumber-shared
382 OUTPUT_NAME "phonenumber"
384 SOVERSION ${libphonenumber_VERSION_MAJOR}
385 VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR})
388 # Libraries used by both libphonenumber and libgeocoding.
389 set (COMMON_DEPS ${ICU_LIB})
391 set (LIBRARY_DEPS ${PROTOBUF_LIB})
393 if (${USE_BOOST} STREQUAL "ON")
394 list (APPEND LIBRARY_DEPS ${Boost_LIBRARIES})
397 if (${USE_RE2} STREQUAL "ON")
398 list (APPEND LIBRARY_DEPS ${RE2_LIB})
402 list (APPEND COMMON_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB})
405 list (APPEND LIBRARY_DEPS ${COMMON_DEPS})
407 target_link_libraries (phonenumber ${LIBRARY_DEPS})
409 if (BUILD_SHARED_LIB)
410 target_link_libraries (phonenumber-shared ${LIBRARY_DEPS})
413 if (${BUILD_GEOCODER} STREQUAL "ON")
414 list (APPEND GEOCODER_DEPS ${COMMON_DEPS})
415 # Note that the subset of base/ on which the geocoder relies is implemented
416 # on top of Boost header-only libraries (e.g. scoped_ptr.hpp).
417 target_link_libraries (geocoding ${LIBRARY_DEPS})
418 target_link_libraries (geocoding-shared ${LIBRARY_DEPS})
421 # Build a specific library for testing purposes.
422 add_library (phonenumber_testing STATIC ${TESTING_LIBRARY_SOURCES})
423 target_link_libraries (phonenumber_testing ${LIBRARY_DEPS})
425 if (${BUILD_GEOCODER} STREQUAL "ON")
426 # Test geocoding data cpp files generation.
427 set (GEOCODING_TEST_DIR "${RESOURCES_DIR}/test/geocoding")
428 file (GLOB_RECURSE GEOCODING_TEST_SOURCES "${GEOCODING_TEST_DIR}/*.txt")
430 set (GEOCODING_TEST_DATA_OUTPUT
431 "${CMAKE_SOURCE_DIR}/test/phonenumbers/geocoding/geocoding_test_data.cc"
435 COMMAND generate_geocoding_data "${GEOCODING_TEST_DIR}"
436 "${GEOCODING_TEST_DATA_OUTPUT}" "_test"
438 OUTPUT ${GEOCODING_TEST_DATA_OUTPUT}
439 DEPENDS ${GEOCODING_TEST_SOURCES} generate_geocoding_data
440 COMMENT "Generating geocoding test data code"
446 "test/phonenumbers/asyoutypeformatter_test.cc"
447 "test/phonenumbers/logger_test.cc"
448 "test/phonenumbers/phonenumberutil_test.cc"
449 "test/phonenumbers/regexp_adapter_test.cc"
450 "test/phonenumbers/regexp_cache_test.cc"
451 "test/phonenumbers/run_tests.cc"
452 "test/phonenumbers/shortnumberinfo_test.cc"
453 "test/phonenumbers/stringutil_test.cc"
454 "test/phonenumbers/test_util.cc"
455 "test/phonenumbers/unicodestring_test.cc"
456 "test/phonenumbers/utf/unicodetext_test.cc"
459 if (${BUILD_GEOCODER} STREQUAL "ON")
460 set (GEOCODING_TEST_SOURCES
461 "test/phonenumbers/geocoding/area_code_map_test.cc"
462 "test/phonenumbers/geocoding/geocoding_data_test.cc"
463 "test/phonenumbers/geocoding/geocoding_test_data.cc"
464 "test/phonenumbers/geocoding/mapping_file_provider_test.cc"
465 "test/phonenumbers/geocoding/phonenumber_offline_geocoder_test.cc"
467 list (APPEND TEST_SOURCES ${GEOCODING_TEST_SOURCES})
470 if (${USE_ICU_REGEXP} STREQUAL "ON")
471 # Add the phone number matcher tests.
472 list (APPEND TEST_SOURCES "test/phonenumbers/phonenumbermatch_test.cc")
473 list (APPEND TEST_SOURCES "test/phonenumbers/phonenumbermatcher_test.cc")
476 # Build the testing binary.
477 include_directories ("test")
478 add_executable (libphonenumber_test ${TEST_SOURCES})
479 set (TEST_LIBS phonenumber_testing ${GTEST_LIB})
482 list (APPEND TEST_LIBS pthread)
485 target_link_libraries (libphonenumber_test ${TEST_LIBS})
487 # Unfortunately add_custom_target() can't accept a single command provided as a
489 if (${BUILD_GEOCODER} STREQUAL "ON")
490 add_custom_target (test
491 COMMAND generate_geocoding_data_test
492 COMMAND libphonenumber_test
493 DEPENDS generate_geocoding_data_test libphonenumber_test
496 add_custom_target (test
497 COMMAND libphonenumber_test
498 DEPENDS libphonenumber_test
504 "src/phonenumbers/asyoutypeformatter.h"
505 "src/phonenumbers/callback.h"
506 "src/phonenumbers/logger.h"
507 "src/phonenumbers/matcher_api.h"
508 "src/phonenumbers/phonenumber.pb.h"
509 "src/phonenumbers/phonemetadata.pb.h"
510 "src/phonenumbers/phonenumberutil.h"
511 "src/phonenumbers/regexp_adapter.h"
512 "src/phonenumbers/regexp_cache.h"
513 "src/phonenumbers/shortnumberinfo.h"
514 "src/phonenumbers/unicodestring.h"
515 DESTINATION include/phonenumbers/
518 install (FILES "src/phonenumbers/utf/unicodetext.h"
519 DESTINATION include/phonenumbers/utf/)
521 if (${USE_ICU_REGEXP} STREQUAL "ON")
522 # Install the phone number matcher headers.
524 "src/phonenumbers/phonenumbermatch.h"
525 "src/phonenumbers/phonenumbermatcher.h"
526 "src/phonenumbers/regexp_adapter.h"
527 DESTINATION include/phonenumbers/
531 if (${BUILD_GEOCODER} STREQUAL "ON")
533 "src/phonenumbers/geocoding/phonenumber_offline_geocoder.h"
534 DESTINATION include/phonenumbers/geocoding
540 "src/phonenumbers/base/basictypes.h"
541 "src/phonenumbers/base/template_util.h"
542 "src/phonenumbers/base/logging.h"
543 "src/phonenumbers/base/thread_checker.h"
544 DESTINATION include/phonenumbers/base/
548 "src/phonenumbers/base/memory/scoped_ptr.h"
549 "src/phonenumbers/base/memory/singleton.h"
550 "src/phonenumbers/base/memory/singleton_posix.h"
551 DESTINATION include/phonenumbers/base/memory/
554 install (FILES "src/phonenumbers/base/synchronization/lock.h"
555 DESTINATION include/phonenumbers/base/synchronization/)
557 install (TARGETS phonenumber LIBRARY DESTINATION lib/ ARCHIVE DESTINATION lib/)
559 if (BUILD_SHARED_LIB)
560 install (TARGETS phonenumber-shared LIBRARY DESTINATION lib/ ARCHIVE
564 if (${BUILD_GEOCODER} STREQUAL "ON")
565 install (TARGETS geocoding LIBRARY DESTINATION lib/ ARCHIVE DESTINATION lib/)
566 install (TARGETS geocoding-shared LIBRARY DESTINATION lib/ ARCHIVE
570 # Build an example program using geocoding, mainly to make sure that both
571 # libraries are built properly.
572 if (${BUILD_GEOCODER} STREQUAL "ON")
574 geocoding_test_program
575 "test/phonenumbers/geocoding/geocoding_test_program.cc"
577 target_link_libraries (geocoding_test_program geocoding phonenumber)