1# Copyright (C) 2011 The Libphonenumber Authors 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 17cmake_minimum_required (VERSION 3.11) 18 19project (libphonenumber VERSION 8.13.0) 20 21if (32BIT) 22 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") 23 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") 24 set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32") 25 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32") 26endif () 27 28# Set out-of-tree tools directory 29set (TOOLS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../tools/cpp") 30set (TOOLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/tools") 31 32# Helper functions dealing with finding libraries and programs this library 33# depends on. 34 35include (FetchContent) 36include (GNUInstallDirs) 37 38function (print_error DESCRIPTION FILE) 39 message (FATAL_ERROR 40 "Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.") 41endfunction () 42 43# Find a library. If it has not been found, stop CMake with a fatal error 44# message. 45function (find_required_library NAME HEADER LIBRARY DESCRIPTION) 46 # Check the header. 47 find_path (${NAME}_INCLUDE_DIR ${HEADER}) 48 set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR}) 49 50 if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND") 51 print_error (${DESCRIPTION} ${HEADER}) 52 endif () 53 include_directories (${INCLUDE_DIR}) 54 # Check the binary. 55 find_library (${NAME}_LIB ${LIBRARY}) 56 set (LIB ${NAME}_LIB) 57 58 if (${LIB} STREQUAL "${LIB}-NOTFOUND") 59 print_error (${DESCRIPTION} ${LIBRARY}) 60 endif () 61endfunction (find_required_library) 62 63# Check the library version (if pkg-config available). 64find_package (PkgConfig) 65function (check_library_version VARNAME LIBRARY_WITH_VERSION) 66 if (PKG_CONFIG_FOUND) 67 pkg_check_modules (${VARNAME} ${LIBRARY_WITH_VERSION}) 68 endif () 69endfunction () 70 71# Find a program. If it has not been found, stop CMake with a fatal error 72# message. 73function (find_required_program NAME FILENAME DESCRIPTION) 74 find_program (${NAME}_BIN NAMES ${FILENAME}) 75 76 if (${NAME}_BIN STREQUAL "${NAME}_BIN-NOTFOUND") 77 print_error (${DESCRIPTION} ${FILENAME}) 78 endif () 79endfunction (find_required_program) 80 81# Options that can be passed to CMake using 'cmake -DKEY=VALUE'. 82option (BUILD_GEOCODER "Build the offline phone number geocoder" ON) 83option (REGENERATE_METADATA "Regenerate metadata instead of using it from the source tree" ON) 84option (USE_ALTERNATE_FORMATS "Use alternate formats" ON) 85option (USE_PROTOBUF_LITE "Link to protobuf-lite" OFF) 86option (USE_BOOST "Use Boost" ON) 87option (USE_ICU_REGEXP "Use ICU regexp engine" ON) 88option (USE_LITE_METADATA "Use lite metadata" OFF) 89option (USE_RE2 "Use RE2" OFF) 90option (USE_STD_MAP "Force the use of std::map" OFF) 91option (BUILD_STATIC_LIB "Build static libraries" ON) 92option (BUILD_SHARED_LIBS "Build shared libraries" ON) 93option (BUILD_TESTING "Build testing" ON) 94option (BUILD_TOOLS_ONLY "Limit build to targets in ../tools/cpp" OFF) 95option (USE_STDMUTEX "Use C++ 2011 std::mutex for multi-threading" OFF) 96option (USE_POSIX_THREAD "Use Posix api for multi-threading" OFF) 97 98if (USE_ALTERNATE_FORMATS) 99 add_definitions ("-DI18N_PHONENUMBERS_USE_ALTERNATE_FORMATS") 100endif () 101 102# Find all the required libraries and programs. 103find_package(absl) 104 105if(NOT absl_FOUND) 106 # Overide abseil install rules for subprojects 107 set(ABSL_ENABLE_INSTALL ON) 108 109 # Downloading the abseil sources at particular version to not catch up 110 # with its new build requirements like min C++14 is mandated in that lib. 111 FetchContent_Declare( 112 abseil-cpp 113 GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git 114 GIT_TAG 273292d 115 ) 116 117 # Building the abseil binaries 118 FetchContent_GetProperties(abseil-cpp) 119 if (NOT abseil-cpp_POPULATED) 120 FetchContent_Populate(abseil-cpp) 121 endif () 122 123 if (NOT abseil-cpp_POPULATED) 124 message (FATAL_ERROR "Could not build abseil-cpp binaries.") 125 endif () 126 127 # Safeguarding against any potential link errors as mentioned in 128 # https://github.com/abseil/abseil-cpp/issues/225 129 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) 130 add_subdirectory(${abseil-cpp_SOURCE_DIR} ${abseil-cpp_BINARY_DIR}) 131endif() 132 133if (BUILD_TESTING) 134 include (../tools/cpp/gtest.cmake) 135 find_or_build_gtest () 136endif() 137 138if (BUILD_TOOLS_ONLY) 139 add_subdirectory("${TOOLS_DIR}" "${TOOLS_BINARY_DIR}") 140 return() 141endif() 142 143if (USE_BOOST) 144 add_definitions ("-DI18N_PHONENUMBERS_USE_BOOST") 145 if (WIN32) 146 set (Boost_USE_STATIC_LIBS ON) 147 endif () 148 find_package (Boost 1.40.0 COMPONENTS date_time system thread) 149 if (NOT Boost_FOUND) 150 print_error ("Boost Date_Time/System/Thread" "Boost") 151 endif () 152 include_directories (${Boost_INCLUDE_DIRS}) 153endif () 154 155if (USE_STDMUTEX) 156 add_definitions ("-DI18N_PHONENUMBERS_USE_STDMUTEX") 157endif () 158 159if (USE_POSIX_THREAD) 160 add_definitions ("-DI18N_PHONENUMBERS_HAVE_POSIX_THREAD") 161 find_package (Threads REQUIRED) 162endif () 163 164if ((NOT USE_BOOST) AND (NOT USE_STDMUTEX)) 165 find_package (Threads) 166endif() 167 168if (USE_RE2) 169 find_required_library (RE2 re2/re2.h re2 "Google RE2") 170endif () 171 172if (USE_PROTOBUF_LITE) 173 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf-lite 174 "Google Protocol Buffers") 175 check_library_version (PC_PROTOBUF protobuf-lite>=2.4) 176else () 177 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf 178 "Google Protocol Buffers") 179 check_library_version (PC_PROTOBUF protobuf>=2.4) 180endif () 181 182find_required_library (ICU_UC unicode/uchar.h icuuc "ICU") 183check_library_version (PC_ICU_UC icu-uc>=4.4) 184 185set (ICU_INCLUDE_DIR ${ICU_UC_INCLUDE_DIR}) 186set (ICU_LIB ${ICU_UC_LIB}) 187# If ICU regexp engine is used or if the geocoder is built, use icui18n as well. 188if (USE_ICU_REGEXP OR BUILD_GEOCODER) 189 find_required_library (ICU_I18N unicode/regex.h icui18n "ICU") 190 check_library_version (PC_ICU_I18N icu-i18n>=4.4) 191 list (APPEND ICU_INCLUDE_DIR ${ICU_I18N_INCLUDE_DIR}) 192 list (APPEND ICU_LIB ${ICU_I18N_LIB}) 193endif () 194 195find_required_program (PROTOC protoc 196 "Google Protocol Buffers compiler (protoc)") 197 198if (REGENERATE_METADATA) 199 find_required_program (JAVA java 200 "Java Runtime Environment") 201endif () 202 203if (APPLE) 204 FIND_LIBRARY (COREFOUNDATION_LIB CoreFoundation) 205 FIND_LIBRARY (FOUNDATION_LIB Foundation) 206 set (CMAKE_MACOSX_RPATH OFF) 207 set (CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS 208 "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup") 209endif () 210 211if (NOT USE_STD_MAP) 212 INCLUDE (CheckIncludeFileCXX) 213 CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP) 214 if (HAVE_CXX_TR1_UNORDERED_MAP) 215 add_definitions ("-DI18N_PHONENUMBERS_USE_TR1_UNORDERED_MAP") 216 endif () 217endif () 218 219# Add protoc (Protocol Buffers compiler) target. 220set (RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../resources") 221 222set ( 223 PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto" 224 "${RESOURCES_DIR}/phonenumber.proto" 225) 226 227set ( 228 PROTOBUF_OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.cc" 229 "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonemetadata.pb.h" 230 "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.cc" 231 "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/phonenumber.pb.h" 232) 233 234add_custom_command ( 235 COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/ 236 --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES} 237 238 OUTPUT ${PROTOBUF_OUTPUT} 239 DEPENDS ${PROTOBUF_SOURCES} 240) 241 242if (BUILD_GEOCODER) 243 # Geocoding data cpp file generation 244 add_subdirectory("${TOOLS_DIR}" "${TOOLS_BINARY_DIR}") 245 246 set (GEOCODING_DIR "${RESOURCES_DIR}/geocoding") 247 file (GLOB_RECURSE GEOCODING_SOURCES "${GEOCODING_DIR}/*.txt") 248 249 set (GEOCODING_DATA_OUTPUT 250 "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers/geocoding/geocoding_data.cc" 251 ) 252 253 add_custom_command ( 254 COMMAND generate_geocoding_data "${GEOCODING_DIR}" 255 "${GEOCODING_DATA_OUTPUT}" 256 257 OUTPUT ${GEOCODING_DATA_OUTPUT} 258 DEPENDS ${GEOCODING_SOURCES} 259 generate_geocoding_data 260 COMMENT "Generating geocoding data code" 261 ) 262endif () 263 264set ( 265 SOURCES 266 "src/phonenumbers/asyoutypeformatter.cc" 267 "src/phonenumbers/base/strings/string_piece.cc" 268 "src/phonenumbers/default_logger.cc" 269 "src/phonenumbers/logger.cc" 270 "src/phonenumbers/phonemetadata.pb.cc" # Generated by Protocol Buffers. 271 "src/phonenumbers/phonenumber.cc" 272 "src/phonenumbers/phonenumber.pb.cc" # Generated by Protocol Buffers. 273 "src/phonenumbers/phonenumberutil.cc" 274 "src/phonenumbers/regex_based_matcher.cc" 275 "src/phonenumbers/regexp_cache.cc" 276 "src/phonenumbers/shortnumberinfo.cc" 277 "src/phonenumbers/string_byte_sink.cc" 278 "src/phonenumbers/stringutil.cc" 279 "src/phonenumbers/unicodestring.cc" 280 "src/phonenumbers/utf/rune.c" 281 "src/phonenumbers/utf/unicodetext.cc" 282 "src/phonenumbers/utf/unilib.cc" 283) 284 285if (BUILD_GEOCODER) 286 set ( 287 GEOCODING_SOURCES 288 "src/phonenumbers/geocoding/area_code_map.cc" 289 "src/phonenumbers/geocoding/default_map_storage.cc" 290 "src/phonenumbers/geocoding/geocoding_data.cc" 291 "src/phonenumbers/geocoding/mapping_file_provider.cc" 292 "src/phonenumbers/geocoding/phonenumber_offline_geocoder.cc" 293 "src/phonenumbers/phonenumber.pb.h" # Forces proto buffer generation. 294 ) 295endif () 296 297# Add regexp engine-dependent sources. ICU is used by default. 298if (USE_RE2) 299 # Add a flag to select the right regexp factory implementation used by 300 # regexp_factory.h and regexp_adapter_test.cc. 301 # When both ICU regexp and RE2 are defined, the regexp engine adapter defaults 302 # to RE2 unless the ICU implementation is instantiated explictly obviously. 303 add_definitions ("-DI18N_PHONENUMBERS_USE_RE2") 304 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_re2.cc") 305endif () 306 307if (USE_ICU_REGEXP) 308 add_definitions ("-DI18N_PHONENUMBERS_USE_ICU_REGEXP") 309 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc") 310 # The phone number matcher needs ICU. 311 list (APPEND SOURCES "src/phonenumbers/phonenumbermatch.cc") 312 list (APPEND SOURCES "src/phonenumbers/phonenumbermatcher.cc") 313 if (USE_ALTERNATE_FORMATS) 314 list (APPEND SOURCES "src/phonenumbers/alternate_format.cc") 315 endif () 316endif () 317 318# Library sources excluding the metadata files, since special metadata is used 319# for unit-testing. Note that a single testing library is built for both 320# libphonenumber and geocoding. 321set (TESTING_LIBRARY_SOURCES ${SOURCES}) 322if (BUILD_GEOCODER) 323 list (APPEND TESTING_LIBRARY_SOURCES ${GEOCODING_SOURCES}) 324endif () 325 326# Add metadata code generation targets. 327 328# This function is invoked to create metadata, test metadata and lite metadata 329# code generation targets. 330function (add_metadata_gen_target TARGET_NAME XML_FILE METADATA_TYPE METADATA_HEADER) 331 set (METADATA_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers") 332 set (GEN_OUTPUT "${METADATA_SOURCE_DIR}/${METADATA_TYPE}.cc" "${METADATA_SOURCE_DIR}/${METADATA_HEADER}.h") 333 set (JAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../tools/java/cpp-build/target") 334 set (JAR_PATH "${JAR_PATH}/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar") 335 336 if (REGENERATE_METADATA) 337 add_custom_command ( 338 COMMAND ${JAVA_BIN} -jar 339 ${JAR_PATH} BuildMetadataCppFromXml ${XML_FILE} 340 ${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers ${METADATA_TYPE} 341 342 OUTPUT ${GEN_OUTPUT} 343 DEPENDS ${XML_FILE} 344 ) 345 else () 346 add_custom_command ( 347 COMMAND echo "skip metadata generation from" 348 ${XML_FILE} "to" 349 ${CMAKE_CURRENT_SOURCE_DIR}/src/phonenumbers ${METADATA_TYPE} 350 351 OUTPUT ${GEN_OUTPUT} 352 DEPENDS ${XML_FILE} 353 ) 354 endif () 355 356 add_custom_target ( 357 ${TARGET_NAME} 358 DEPENDS ${GEN_OUTPUT} 359 COMMENT "Generating Metadata code" 360 ) 361endfunction (add_metadata_gen_target) 362 363if (USE_LITE_METADATA) 364 # Add the lite metadata generation target. 365 set (METADATA_TARGET "generate-lite-metadata") 366 add_metadata_gen_target ( 367 ${METADATA_TARGET} 368 "${RESOURCES_DIR}/PhoneNumberMetadata.xml" 369 "lite_metadata" 370 "metadata" 371 ) 372 list (APPEND SOURCES "src/phonenumbers/lite_metadata.cc") 373else () 374 # Add the metadata generation target. 375 set (METADATA_TARGET "generate-metadata") 376 add_metadata_gen_target ( 377 ${METADATA_TARGET} 378 "${RESOURCES_DIR}/PhoneNumberMetadata.xml" 379 "metadata" 380 "metadata" 381 ) 382 list (APPEND SOURCES "src/phonenumbers/metadata.cc") 383endif () 384 385# Add the test metadata generation target. 386set (TEST_METADATA_TARGET "generate-test-metadata") 387add_metadata_gen_target ( 388 ${TEST_METADATA_TARGET} 389 "${RESOURCES_DIR}/PhoneNumberMetadataForTesting.xml" 390 "test_metadata" 391 "test_metadata" 392) 393list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/test_metadata.cc") 394 395# Add the short metadata generation target. 396set (SHORT_METADATA_TARGET "generate-short-number-metadata") 397add_metadata_gen_target ( 398 ${SHORT_METADATA_TARGET} 399 "${RESOURCES_DIR}/ShortNumberMetadata.xml" 400 "short_metadata" 401 "short_metadata" 402) 403# This is used both for the real library and for testing. 404list (APPEND SOURCES "src/phonenumbers/short_metadata.cc") 405list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/short_metadata.cc") 406 407if (USE_ICU_REGEXP) 408 if (USE_ALTERNATE_FORMATS) 409 # Add alternate format metadata generation for the phone number matcher. 410 set (ALT_FORMAT_METADATA_TARGET "generate-alt-format-metadata") 411 add_metadata_gen_target ( 412 ${ALT_FORMAT_METADATA_TARGET} 413 "${RESOURCES_DIR}/PhoneNumberAlternateFormats.xml" 414 "alternate_format" 415 "alternate_format" 416 ) 417 endif () 418endif () 419 420if (NOT WIN32) 421 add_definitions ("-Wall -Werror") 422endif () 423 424include_directories ("src") 425 426#---------------------------------------------------------------- 427# Collate dependencies 428#---------------------------------------------------------------- 429 430set (LIBRARY_DEPS ${ICU_LIB} ${PROTOBUF_LIB} absl::node_hash_set absl::strings absl::synchronization) 431 432if (USE_BOOST) 433 list (APPEND LIBRARY_DEPS ${Boost_LIBRARIES}) 434endif () 435 436if (USE_RE2) 437 list (APPEND LIBRARY_DEPS ${RE2_LIB}) 438endif () 439 440if (USE_POSIX_THREAD OR ((APPLE OR UNIX) AND (NOT (USE_BOOST OR USE_STDMUTEX)))) 441 if(CMAKE_USE_PTHREADS_INIT) 442 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") 443 endif() 444 if(CMAKE_THREAD_LIBS_INIT) 445 list (APPEND LIBRARY_DEPS ${CMAKE_THREAD_LIBS_INIT}) 446 endif() 447endif () 448 449if (APPLE) 450 list (APPEND LIBRARY_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB}) 451endif () 452 453#---------------------------------------------------------------- 454# Build libraries 455#---------------------------------------------------------------- 456 457if (BUILD_STATIC_LIB) 458 # Build a static library (without -fPIC). 459 add_library (phonenumber STATIC ${SOURCES}) 460 target_link_libraries (phonenumber ${LIBRARY_DEPS}) 461 target_include_directories(phonenumber PUBLIC $<INSTALL_INTERFACE:include>) 462 463 if (BUILD_GEOCODER) 464 add_library (geocoding STATIC ${GEOCODING_SOURCES}) 465 target_link_libraries (geocoding ${LIBRARY_DEPS}) 466 target_include_directories(geocoding PUBLIC $<INSTALL_INTERFACE:include>) 467 add_dependencies (geocoding generate_geocoding_data) 468 add_dependencies (phonenumber generate_geocoding_data) 469 endif () 470 471 if (USE_ICU_REGEXP AND USE_ALTERNATE_FORMATS) 472 add_dependencies (phonenumber ${ALT_FORMAT_METADATA_TARGET}) 473 endif () 474endif () 475 476 477if (BUILD_SHARED_LIBS) 478 # Build a shared library (with -fPIC). 479 add_library (phonenumber-shared SHARED ${SOURCES}) 480 target_link_libraries (phonenumber-shared ${LIBRARY_DEPS}) 481 target_include_directories(phonenumber-shared PUBLIC $<INSTALL_INTERFACE:include>) 482 483 set_target_properties (phonenumber-shared 484 PROPERTIES 485 OUTPUT_NAME "phonenumber" 486 PREFIX "lib" 487 SOVERSION ${libphonenumber_VERSION_MAJOR} 488 VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR}) 489 490 if (USE_ICU_REGEXP AND USE_ALTERNATE_FORMATS) 491 add_dependencies (phonenumber-shared ${ALT_FORMAT_METADATA_TARGET}) 492 endif () 493 494 if (BUILD_GEOCODER) 495 add_library (geocoding-shared SHARED ${GEOCODING_SOURCES}) 496 target_link_libraries (geocoding-shared ${LIBRARY_DEPS}) 497 target_include_directories(geocoding-shared PUBLIC $<INSTALL_INTERFACE:include>) 498 add_dependencies (geocoding-shared generate_geocoding_data) 499 add_dependencies (phonenumber-shared generate_geocoding_data) 500 501 set_target_properties (geocoding-shared 502 PROPERTIES 503 OUTPUT_NAME "geocoding" 504 PREFIX "lib" 505 SOVERSION ${libphonenumber_VERSION_MAJOR} 506 VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR}) 507 endif () 508endif () 509 510#---------------------------------------------------------------- 511# Build testing library 512#---------------------------------------------------------------- 513 514if(BUILD_TESTING) 515 add_library (phonenumber_testing STATIC ${TESTING_LIBRARY_SOURCES}) 516 if (BUILD_GEOCODER) 517 add_dependencies (phonenumber_testing generate_geocoding_data) 518 endif () 519 target_link_libraries (phonenumber_testing ${LIBRARY_DEPS}) 520 521 if (BUILD_GEOCODER) 522 # Test geocoding data cpp files generation. 523 set (GEOCODING_TEST_DIR "${RESOURCES_DIR}/test/geocoding") 524 file (GLOB_RECURSE GEOCODING_TEST_SOURCES "${GEOCODING_TEST_DIR}/*.txt") 525 526 set (GEOCODING_TEST_DATA_OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/test/phonenumbers/geocoding/geocoding_test_data.cc") 527 528 add_custom_command ( 529 COMMAND generate_geocoding_data "${GEOCODING_TEST_DIR}" "${GEOCODING_TEST_DATA_OUTPUT}" "_test" 530 OUTPUT ${GEOCODING_TEST_DATA_OUTPUT} 531 DEPENDS ${GEOCODING_TEST_SOURCES} generate_geocoding_data 532 COMMENT "Generating geocoding test data code" 533 ) 534 endif () 535 536 set (TEST_SOURCES 537 "test/phonenumbers/asyoutypeformatter_test.cc" 538 "test/phonenumbers/logger_test.cc" 539 "test/phonenumbers/matcher_test.cc" 540 "test/phonenumbers/phonenumberutil_test.cc" 541 "test/phonenumbers/regexp_adapter_test.cc" 542 "test/phonenumbers/regexp_cache_test.cc" 543 "test/phonenumbers/run_tests.cc" 544 "test/phonenumbers/shortnumberinfo_test.cc" 545 "test/phonenumbers/stringutil_test.cc" 546 "test/phonenumbers/test_util.cc" 547 "test/phonenumbers/unicodestring_test.cc" 548 "test/phonenumbers/utf/unicodetext_test.cc") 549 550 if (BUILD_GEOCODER) 551 set (GEOCODING_TEST_SOURCES 552 "test/phonenumbers/geocoding/area_code_map_test.cc" 553 "test/phonenumbers/geocoding/geocoding_data_test.cc" 554 "test/phonenumbers/geocoding/geocoding_test_data.cc" 555 "test/phonenumbers/geocoding/mapping_file_provider_test.cc" 556 "test/phonenumbers/geocoding/phonenumber_offline_geocoder_test.cc") 557 list (APPEND TEST_SOURCES ${GEOCODING_TEST_SOURCES}) 558 endif () 559 560 if (USE_ICU_REGEXP) 561 # Add the phone number matcher tests. 562 list (APPEND TEST_SOURCES "test/phonenumbers/phonenumbermatch_test.cc") 563 list (APPEND TEST_SOURCES "test/phonenumbers/phonenumbermatcher_test.cc") 564 endif () 565 566 # Build the testing binary. 567 include_directories ("test") 568 add_executable (libphonenumber_test ${TEST_SOURCES}) 569 set (TEST_LIBS phonenumber_testing ${GTEST_LIB}) 570 571 if (NOT WIN32) 572 list (APPEND TEST_LIBS pthread) 573 endif () 574 575 target_link_libraries (libphonenumber_test ${TEST_LIBS}) 576 577 # Unfortunately add_custom_target() can't accept a single command provided as a 578 # list of commands. 579 if (BUILD_GEOCODER) 580 add_custom_target (tests 581 COMMAND generate_geocoding_data_test 582 COMMAND libphonenumber_test 583 DEPENDS generate_geocoding_data_test libphonenumber_test 584 ) 585 else () 586 add_custom_target (tests 587 COMMAND libphonenumber_test 588 DEPENDS libphonenumber_test 589 ) 590 endif () 591 592 # Build an example program using geocoding, mainly to make sure that both 593 # libraries are built properly. 594 if (BUILD_GEOCODER) 595 add_executable (geocoding_test_program "test/phonenumbers/geocoding/geocoding_test_program.cc") 596 target_link_libraries (geocoding_test_program geocoding phonenumber) 597 endif () 598endif() 599 600#---------------------------------------------------------------- 601# Install built libraries 602#---------------------------------------------------------------- 603 604set (BUILT_LIBS) 605set(targets_export_name "${PROJECT_NAME}-targets") 606 607if (BUILD_STATIC_LIB) 608 list (APPEND BUILT_LIBS phonenumber) 609 if (BUILD_GEOCODER) 610 list (APPEND BUILT_LIBS geocoding) 611 endif() 612endif() 613 614if (BUILD_SHARED_LIBS) 615 list (APPEND BUILT_LIBS phonenumber-shared) 616 if (BUILD_GEOCODER) 617 list (APPEND BUILT_LIBS geocoding-shared) 618 endif() 619endif() 620 621install ( 622 TARGETS ${BUILT_LIBS} 623 EXPORT "${targets_export_name}" 624 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 625 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}") 626 627#---------------------------------------------------------------- 628# Install headers 629#---------------------------------------------------------------- 630 631install (FILES 632 "src/phonenumbers/asyoutypeformatter.h" 633 "src/phonenumbers/callback.h" 634 "src/phonenumbers/logger.h" 635 "src/phonenumbers/matcher_api.h" 636 "src/phonenumbers/phonenumber.pb.h" 637 "src/phonenumbers/phonemetadata.pb.h" 638 "src/phonenumbers/phonenumberutil.h" 639 "src/phonenumbers/regexp_adapter.h" 640 "src/phonenumbers/regexp_cache.h" 641 "src/phonenumbers/region_code.h" 642 "src/phonenumbers/shortnumberinfo.h" 643 "src/phonenumbers/unicodestring.h" 644 DESTINATION include/phonenumbers/ 645) 646 647install (FILES "src/phonenumbers/utf/unicodetext.h" 648 DESTINATION include/phonenumbers/utf/) 649 650if (USE_ICU_REGEXP) 651 # Install the phone number matcher headers. 652 install (FILES 653 "src/phonenumbers/phonenumbermatch.h" 654 "src/phonenumbers/phonenumbermatcher.h" 655 "src/phonenumbers/regexp_adapter.h" 656 DESTINATION include/phonenumbers/ 657 ) 658endif () 659 660if (BUILD_GEOCODER) 661 install (FILES 662 "src/phonenumbers/geocoding/phonenumber_offline_geocoder.h" 663 DESTINATION include/phonenumbers/geocoding 664 ) 665endif () 666 667install ( 668 FILES 669 "src/phonenumbers/base/basictypes.h" 670 "src/phonenumbers/base/template_util.h" 671 "src/phonenumbers/base/logging.h" 672 "src/phonenumbers/base/thread_checker.h" 673 DESTINATION include/phonenumbers/base/ 674) 675 676install (FILES 677 "src/phonenumbers/base/memory/scoped_ptr.h" 678 "src/phonenumbers/base/memory/singleton.h" 679 "src/phonenumbers/base/memory/singleton_boost.h" 680 "src/phonenumbers/base/memory/singleton_posix.h" 681 "src/phonenumbers/base/memory/singleton_stdmutex.h" 682 "src/phonenumbers/base/memory/singleton_unsafe.h" 683 "src/phonenumbers/base/memory/singleton_win32.h" 684 DESTINATION include/phonenumbers/base/memory/ 685) 686 687install (FILES 688 "src/phonenumbers/base/synchronization/lock.h" 689 "src/phonenumbers/base/synchronization/lock_boost.h" 690 "src/phonenumbers/base/synchronization/lock_posix.h" 691 "src/phonenumbers/base/synchronization/lock_stdmutex.h" 692 "src/phonenumbers/base/synchronization/lock_unsafe.h" 693 "src/phonenumbers/base/synchronization/lock_win32.h" 694 DESTINATION include/phonenumbers/base/synchronization/) 695 696#---------------------------------------------------------------- 697# Build and install cmake config files 698#---------------------------------------------------------------- 699 700include(CMakePackageConfigHelpers) 701 702set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") 703set(version_config "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake") 704set(project_config "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake") 705 706# libphonenumber-config-version.cmake 707write_basic_package_version_file("${version_config}" COMPATIBILITY SameMajorVersion) 708 709# libphonenumber-config.cmake 710configure_package_config_file("cmake/config.cmake.in" "${project_config}" INSTALL_DESTINATION "${config_install_dir}") 711 712install(FILES "${project_config}" "${version_config}" DESTINATION "${config_install_dir}") 713install(EXPORT "${targets_export_name}" NAMESPACE ${PROJECT_NAME}:: DESTINATION "${config_install_dir}") 714 715#---------------------------------------------------------------- 716# Build an RPM 717#---------------------------------------------------------------- 718 719set (CPACK_PACKAGE_VERSION ${libphonenumber_VERSION_MAJOR}.${libphonenumber_VERSION_MINOR}.${libphonenumber_VERSION_PATCH}) 720set (CPACK_GENERATOR "RPM") 721set (CPACK_PACKAGE_NAME "libphonenumber") 722set (CPACK_RPM_PACKAGE_RELEASE 1) 723set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "Google's phone number handling library") 724if (32BIT) 725 set (CPACK_RPM_PACKAGE_ARCHITECTURE i686) 726else () 727 set (CPACK_RPM_PACKAGE_ARCHITECTURE x86_64) 728endif () 729set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}.${CPACK_RPM_PACKAGE_ARCHITECTURE}") 730include (CPack) 731