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