OLD | NEW |
| (Empty) |
1 # Copyright (C) 2011 Google Inc. | |
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 | |
17 cmake_minimum_required (VERSION 2.8) | |
18 | |
19 project (libphonenumber) | |
20 | |
21 # Helper functions dealing with finding libraries and programs this library | |
22 # depends on. | |
23 | |
24 function (print_error DESCRIPTION FILE) | |
25 message (FATAL_ERROR | |
26 "Can't find ${DESCRIPTION}: can't locate ${FILE}. Please read the README.") | |
27 endfunction () | |
28 | |
29 # Find a library. If it has not been found, stop CMake with a fatal error | |
30 # message. | |
31 function (find_required_library NAME HEADER LIBRARY DESCRIPTION) | |
32 # Check the header. | |
33 find_path (${NAME}_INCLUDE_DIR ${HEADER}) | |
34 set (INCLUDE_DIR ${${NAME}_INCLUDE_DIR}) | |
35 | |
36 if (${INCLUDE_DIR} STREQUAL "${INCLUDE_DIR}-NOTFOUND") | |
37 print_error (${DESCRIPTION} ${HEADER}) | |
38 endif () | |
39 include_directories (${INCLUDE_DIR}) | |
40 # Check the binary. | |
41 find_library (${NAME}_LIB ${LIBRARY}) | |
42 set (LIB ${NAME}_LIB) | |
43 | |
44 if (${LIB} STREQUAL "${LIB}-NOTFOUND") | |
45 print_error (${DESCRIPTION} ${LIBRARY}) | |
46 endif () | |
47 endfunction (find_required_library) | |
48 | |
49 # Check the library version (if pkg-config available). | |
50 find_package (PkgConfig) | |
51 function (check_library_version VARNAME LIBRARY_WITH_VERSION) | |
52 if (PKG_CONFIG_FOUND) | |
53 pkg_check_modules (${VARNAME} REQUIRED ${LIBRARY_WITH_VERSION}) | |
54 endif () | |
55 endfunction () | |
56 | |
57 # Find a program. If it has not been found, stop CMake with a fatal error | |
58 # message. | |
59 function (find_required_program NAME FILENAME DESCRIPTION) | |
60 find_program (${NAME}_BIN NAMES ${FILENAME}) | |
61 | |
62 if (${NAME}_BIN STREQUAL "${${NAME}_BIN}-NOTFOUND") | |
63 print_error (${DESCRIPTION} ${FILENAME}) | |
64 endif () | |
65 endfunction (find_required_program) | |
66 | |
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 | |
71 # Find all the required libraries and programs. | |
72 find_package (Boost 1.40.0 COMPONENTS thread) | |
73 if (NOT Boost_FOUND) | |
74 print_error ("Boost Thread" "Boost") | |
75 endif () | |
76 include_directories (${Boost_INCLUDE_DIRS}) | |
77 | |
78 find_required_library (GTEST gtest/gtest.h gtest "Google Test framework") | |
79 | |
80 if (${USE_RE2} STREQUAL "ON") | |
81 find_required_library (RE2 re2/re2.h re2 "Google RE2") | |
82 endif () | |
83 | |
84 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf | |
85 "Google Protocol Buffers") | |
86 check_library_version (PC_PROTOBUF protobuf>=2.4) | |
87 | |
88 find_required_library (ICU_UC unicode/uchar.h icuuc "ICU") | |
89 check_library_version (PC_ICU_UC icu-uc>=4.4) | |
90 | |
91 set (ICU_INCLUDE_DIR ${ICU_UC_INCLUDE_DIR}) | |
92 set (ICU_LIB ${ICU_UC_LIB}) | |
93 # If ICU regexp engine is used, use icui18n as well. | |
94 if (${USE_RE2} STREQUAL "OFF") | |
95 find_required_library (ICU_I18N unicode/regex.h icui18n "ICU") | |
96 check_library_version (PC_ICU_I18N icu-i18n>=4.4) | |
97 list (APPEND ICU_INCLUDE_DIR ${ICU_I18N_INCLUDE_DIR}) | |
98 list (APPEND ICU_LIB ${ICU_I18N_LIB}) | |
99 endif () | |
100 | |
101 find_required_program (PROTOC protoc | |
102 "Google Protocol Buffers compiler (protoc)") | |
103 | |
104 find_required_program (JAVA java | |
105 "Java Runtime Environment") | |
106 | |
107 if (APPLE) | |
108 FIND_LIBRARY (COREFOUNDATION_LIB CoreFoundation) | |
109 FIND_LIBRARY (FOUNDATION_LIB Foundation) | |
110 endif () | |
111 | |
112 INCLUDE (CheckIncludeFileCXX) | |
113 CHECK_INCLUDE_FILE_CXX ("tr1/unordered_map" HAVE_CXX_TR1_UNORDERED_MAP) | |
114 | |
115 if (HAVE_CXX_TR1_UNORDERED_MAP) | |
116 add_definitions ("-DUSE_TR1_UNORDERED_MAP") | |
117 else () | |
118 CHECK_INCLUDE_FILE_CXX (hash_map HAVE_CXX_HASH_MAP) | |
119 if (HAVE_CXX_HASH_MAP) | |
120 add_definitions ("-DUSE_HASH_MAP") | |
121 else () | |
122 print_error ("C++ map class" "tr1/unordered_map or hash_map") | |
123 endif () | |
124 endif () | |
125 | |
126 # Add protoc (Protocol Buffers compiler) target. | |
127 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources") | |
128 | |
129 set ( | |
130 PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto" | |
131 "${RESOURCES_DIR}/phonenumber.proto" | |
132 ) | |
133 | |
134 set ( | |
135 PROTOBUF_OUTPUT "${CMAKE_SOURCE_DIR}/src/phonemetadata.pb.cc" | |
136 "${CMAKE_SOURCE_DIR}/src/phonemetadata.pb.h" | |
137 "${CMAKE_SOURCE_DIR}/src/phonenumber.pb.cc" | |
138 "${CMAKE_SOURCE_DIR}/src/phonenumber.pb.h" | |
139 ) | |
140 | |
141 add_custom_command ( | |
142 COMMAND ${PROTOC_BIN} --cpp_out=${CMAKE_SOURCE_DIR}/src | |
143 --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES} | |
144 | |
145 OUTPUT ${PROTOBUF_OUTPUT} | |
146 DEPENDS ${PROTOBUF_SOURCES} | |
147 ) | |
148 | |
149 add_custom_target ( | |
150 generate-sources | |
151 | |
152 DEPENDS ${PROTOBUF_OUTPUT} | |
153 COMMENT "Generating Protocol Buffers code" | |
154 ) | |
155 | |
156 set ( | |
157 SOURCES | |
158 "src/base/string_piece.cc" | |
159 "src/default_logger.cc" | |
160 "src/logger.cc" | |
161 "src/metadata.h" # Generated by build tools. | |
162 "src/phonemetadata.pb.cc" # Generated by Protocol Buffers. | |
163 "src/phonenumber.cc" | |
164 "src/phonenumber.pb.cc" # Generated by Protocol Buffers. | |
165 "src/phonenumberutil.cc" | |
166 "src/regexp_cache.cc" | |
167 "src/stringutil.cc" | |
168 "src/utf/rune.c" | |
169 "src/utf/unicodetext.cc" | |
170 "src/utf/unilib.cc" | |
171 ) | |
172 | |
173 # Add regexp engine sources. ICU is used by default. | |
174 if (${USE_RE2} STREQUAL "ON") | |
175 list (APPEND SOURCES "src/regexp_adapter_re2.cc") | |
176 else () | |
177 list (APPEND SOURCES "src/regexp_adapter_icu.cc") | |
178 endif () | |
179 | |
180 # Library sources excluding the metadata files, since special metadata is used | |
181 # for unit-testing. | |
182 set (TESTING_LIBRARY_SOURCES ${SOURCES}) | |
183 | |
184 # Add metadata code generation targets. | |
185 | |
186 # This function is invoked to create metadata, test metadata and lite metadata | |
187 # code generation targets. | |
188 function (add_metadata_gen_target TARGET_NAME | |
189 XML_FILE | |
190 METADATA_TYPE) | |
191 set (METADATA_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src") | |
192 set (GEN_OUTPUT "${METADATA_SOURCE_DIR}/${METADATA_TYPE}.cc" | |
193 "${METADATA_SOURCE_DIR}/metadata.h") | |
194 set (JAR_PATH "${CMAKE_SOURCE_DIR}/../tools/java/cpp-build/target") | |
195 set (JAR_PATH "${JAR_PATH}/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar") | |
196 | |
197 add_custom_command ( | |
198 COMMAND ${JAVA_BIN} -jar | |
199 ${JAR_PATH} BuildMetadataCppFromXml ${XML_FILE} ${CMAKE_SOURCE_DIR}/src | |
200 ${METADATA_TYPE} | |
201 | |
202 OUTPUT ${GEN_OUTPUT} | |
203 DEPENDS ${XML_FILE} | |
204 ) | |
205 add_custom_target ( | |
206 ${TARGET_NAME} | |
207 DEPENDS ${GEN_OUTPUT} | |
208 COMMENT "Generating Metadata code" | |
209 ) | |
210 endfunction (add_metadata_gen_target) | |
211 | |
212 if (${USE_LITE_METADATA} STREQUAL "ON") | |
213 # Add the lite metadata generation target. | |
214 set (METADATA_TARGET "generate-lite-metadata") | |
215 add_metadata_gen_target ( | |
216 ${METADATA_TARGET} | |
217 "${RESOURCES_DIR}/PhoneNumberMetaData.xml" | |
218 "lite_metadata" | |
219 ) | |
220 list (APPEND SOURCES "src/lite_metadata.cc") | |
221 else () | |
222 # Add the metadata generation target. | |
223 set (METADATA_TARGET "generate-metadata") | |
224 add_metadata_gen_target ( | |
225 ${METADATA_TARGET} | |
226 "${RESOURCES_DIR}/PhoneNumberMetaData.xml" | |
227 "metadata" | |
228 ) | |
229 list (APPEND SOURCES "src/metadata.cc") | |
230 endif () | |
231 | |
232 # Add the test metadata generation target. | |
233 set (TEST_METADATA_TARGET "generate-test-metadata") | |
234 add_metadata_gen_target ( | |
235 ${TEST_METADATA_TARGET} | |
236 "${RESOURCES_DIR}/PhoneNumberMetaDataForTesting.xml" | |
237 "test_metadata" | |
238 ) | |
239 list (APPEND TESTING_LIBRARY_SOURCES "src/test_metadata.cc") | |
240 add_definitions ("-Wall -Werror") | |
241 | |
242 include_directories ("src") | |
243 | |
244 # Build a static library (without -fPIC). | |
245 add_library (phonenumber STATIC ${SOURCES}) | |
246 add_dependencies (phonenumber generate-sources ${METADATA_TARGET}) | |
247 | |
248 # Build a shared library (with -fPIC). | |
249 set (BUILD_SHARED_LIB true) | |
250 | |
251 if (${USE_RE2} STREQUAL "ON") | |
252 # RE2 is not always available as a shared library (e.g: package provided by | |
253 # Ubuntu) therefore disable the shared library build in this case. | |
254 if (${RE2_LIB} MATCHES ".*\\.a") | |
255 message (WARNING | |
256 "RE2 not available as a shared library, shared library build disabled") | |
257 set (BUILD_SHARED_LIB false) | |
258 endif () | |
259 endif () | |
260 | |
261 if (BUILD_SHARED_LIB) | |
262 add_library (phonenumber-shared SHARED ${SOURCES}) | |
263 add_dependencies (phonenumber-shared generate-sources ${METADATA_TARGET}) | |
264 set_target_properties (phonenumber-shared | |
265 PROPERTIES OUTPUT_NAME "phonenumber") | |
266 set_target_properties (phonenumber-shared PROPERTIES PREFIX "lib") | |
267 endif () | |
268 | |
269 set (LIBRARY_DEPS ${PROTOBUF_LIB} ${ICU_LIB} ${Boost_LIBRARIES}) | |
270 | |
271 if (${USE_RE2} STREQUAL "ON") | |
272 list (APPEND LIBRARY_DEPS ${RE2_LIB}) | |
273 endif () | |
274 | |
275 if (APPLE) | |
276 list (APPEND LIBRARY_DEPS ${COREFOUNDATION_LIB} ${FOUNDATION_LIB}) | |
277 endif () | |
278 | |
279 target_link_libraries (phonenumber ${LIBRARY_DEPS}) | |
280 | |
281 if (BUILD_SHARED_LIB) | |
282 target_link_libraries (phonenumber-shared ${LIBRARY_DEPS}) | |
283 endif () | |
284 | |
285 # Build a specific library for testing purposes. | |
286 add_library (phonenumber_testing STATIC ${TESTING_LIBRARY_SOURCES}) | |
287 target_link_libraries (phonenumber_testing ${LIBRARY_DEPS}) | |
288 add_dependencies (phonenumber_testing generate-sources ${TEST_METADATA_TARGET}) | |
289 | |
290 set (TEST_SOURCES | |
291 "src/logger_test.cc" | |
292 "src/phonenumberutil_test.cc" | |
293 "src/regexp_adapter_test.cc" | |
294 "src/regexp_cache_test.cc" | |
295 "src/run_tests.cc" | |
296 "src/stringutil_test.cc" | |
297 "src/utf/unicodetext_test.cc" | |
298 ) | |
299 | |
300 # Build the testing binary. | |
301 add_executable (libphonenumber_test ${TEST_SOURCES}) | |
302 target_link_libraries ( | |
303 libphonenumber_test | |
304 phonenumber_testing ${GTEST_LIB} pthread | |
305 ) | |
306 | |
307 # Install rules. | |
308 install (FILES | |
309 "src/logger.h" | |
310 "src/phonenumber.pb.h" | |
311 "src/phonenumberutil.h" | |
312 DESTINATION include/phonenumbers/ | |
313 ) | |
314 | |
315 install (FILES | |
316 "src/base/basictypes.h" | |
317 "src/base/memory/scoped_ptr.h" | |
318 "src/base/singleton.h" | |
319 DESTINATION include/phonenumbers/base/ | |
320 ) | |
321 install (FILES src/base/synchronization/lock.h | |
322 DESTINATION include/phonenumbers/base/synchronization) | |
323 | |
324 install (TARGETS phonenumber LIBRARY DESTINATION lib/ ARCHIVE DESTINATION lib/) | |
325 | |
326 if (BUILD_SHARED_LIB) | |
327 install (TARGETS phonenumber-shared LIBRARY DESTINATION lib/ ARCHIVE | |
328 DESTINATION lib/) | |
329 endif () | |
OLD | NEW |