1 # Copyright (C) 2011 Google Inc.
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)
21 # Helper functions dealing with finding libraries and programs this library
24 function (print_error DESCRIPTION FILE)
26 "Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.")
29 # Find a library. If it has not been found, stop CMake with a fatal error
31 function (find_required_library NAME HEADER LIBRARY DESCRIPTION)
33 find_path (${NAME}_INCLUDE_DIR ${HEADER})
34 set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR})
36 if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND")
37 print_error (${DESCRIPTION} ${HEADER})
39 include_directories (${INCLUDE_DIR})
41 find_library (${NAME}_LIB ${LIBRARY})
44 if (${LIB} STREQUAL "${LIB}-NOTFOUND")
45 print_error (${DESCRIPTION} ${LIBRARY})
47 endfunction (find_required_library)
49 # Check the library version (if pkg-config available).
50 find_package (PkgConfig)
51 function (check_library_version VARNAME LIBRARY_WITH_VERSION)
53 pkg_check_modules (${VARNAME} REQUIRED ${LIBRARY_WITH_VERSION})
57 # Find a program. If it has not been found, stop CMake with a fatal error
59 function (find_required_program NAME FILENAME DESCRIPTION)
60 find_program (${NAME}_BIN NAMES ${FILENAME})
62 if (${NAME}_BIN STREQUAL "${${NAME}_BIN}-NOTFOUND")
63 print_error (${DESCRIPTION} ${FILENAME})
65 endfunction (find_required_program)
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")
72 # Find all the required libraries and programs.
73 find_package (Boost 1.40.0 COMPONENTS thread)
75 print_error ("Boost Thread" "Boost")
77 include_directories (${Boost_INCLUDE_DIRS})
79 find_required_library (GTEST gtest/gtest.h gtest "Google Test framework")
81 if (${USE_RE2} STREQUAL "ON")
82 find_required_library (RE2 re2/re2.h re2 "Google RE2")
85 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf
86 "Google Protocol Buffers")
87 check_library_version (PC_PROTOBUF protobuf>=2.4)
89 find_required_library (ICU_UC unicode/uchar.h icuuc "ICU")
90 check_library_version (PC_ICU_UC icu-uc>=4.4)
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})
102 find_required_program (PROTOC protoc
103 "Google Protocol Buffers compiler (protoc)")
105 find_required_program (JAVA java
106 "Java Runtime Environment")
109 FIND_LIBRARY (COREFOUNDATION_LIB CoreFoundation)
110 FIND_LIBRARY (FOUNDATION_LIB Foundation)
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")
121 # Add protoc (Protocol Buffers compiler) target.
122 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources")
125 PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto"
126 "${RESOURCES_DIR}/phonenumber.proto"
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"
137 COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_SOURCE_DIR}/src/phonenumbers/
138 --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES}
140 OUTPUT ${PROTOBUF_OUTPUT}
141 DEPENDS ${PROTOBUF_SOURCES}
147 DEPENDS ${PROTOBUF_OUTPUT}
148 COMMENT "Generating Protocol Buffers code"
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"
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")
175 list (APPEND SOURCES "src/phonenumbers/regexp_adapter_icu.cc")
178 # Library sources excluding the metadata files, since special metadata is used
180 set (TESTING_LIBRARY_SOURCES ${SOURCES})
182 # Add metadata code generation targets.
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
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")
196 COMMAND ${JAVA_BIN} -jar
197 ${JAR_PATH} BuildMetadataCppFromXml ${XML_FILE}
198 ${CMAKE_SOURCE_DIR}/src/phonenumbers ${METADATA_TYPE}
205 DEPENDS ${GEN_OUTPUT}
206 COMMENT "Generating Metadata code"
208 endfunction (add_metadata_gen_target)
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 (
215 "${RESOURCES_DIR}/PhoneNumberMetaData.xml"
218 list (APPEND SOURCES "src/phonenumbers/lite_metadata.cc")
220 # Add the metadata generation target.
221 set (METADATA_TARGET "generate-metadata")
222 add_metadata_gen_target (
224 "${RESOURCES_DIR}/PhoneNumberMetaData.xml"
227 list (APPEND SOURCES "src/phonenumbers/metadata.cc")
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"
237 list (APPEND TESTING_LIBRARY_SOURCES "src/phonenumbers/test_metadata.cc")
238 add_definitions ("-Wall -Werror")
240 include_directories ("src")
242 # Build a static library (without -fPIC).
243 add_library (phonenumber STATIC ${SOURCES})
244 add_dependencies (phonenumber generate-sources ${METADATA_TARGET})
246 # Build a shared library (with -fPIC).
247 set (BUILD_SHARED_LIB true)
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")
254 "RE2 not available as a shared library, shared library build disabled")
255 set (BUILD_SHARED_LIB false)
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")
267 set (LIBRARY_DEPS ${PROTOBUF_LIB} ${ICU_LIB} ${Boost_LIBRARIES})
269 if (${USE_RE2} STREQUAL "ON")
270 list (APPEND LIBRARY_DEPS ${RE2_LIB})
274 list (APPEND LIBRARY_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB})
277 target_link_libraries (phonenumber ${LIBRARY_DEPS})
279 if (BUILD_SHARED_LIB)
280 target_link_libraries (phonenumber-shared ${LIBRARY_DEPS})
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})
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"
298 # Build the testing binary.
299 add_executable (libphonenumber_test ${TEST_SOURCES})
300 target_link_libraries (
302 phonenumber_testing ${GTEST_LIB} pthread
307 "src/phonenumbers/logger.h"
308 "src/phonenumbers/phonenumber.pb.h"
309 "src/phonenumbers/phonenumberutil.h"
310 DESTINATION include/phonenumbers/
314 "src/base/basictypes.h"
315 "src/base/memory/singleton.h"
316 "src/base/scoped_ptr.h"
317 DESTINATION include/base/
319 install (FILES src/base/synchronization/lock.h
320 DESTINATION include/base/synchronization)
322 install (TARGETS phonenumber LIBRARY DESTINATION lib/ ARCHIVE DESTINATION lib/)
324 if (BUILD_SHARED_LIB)
325 install (TARGETS phonenumber-shared LIBRARY DESTINATION lib/ ARCHIVE