Cpp:Add protobuf version check. Patch contributed by philip.liard
[platform/upstream/libphonenumber.git] / cpp / CMakeLists.txt
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
18 cmake_minimum_required (VERSION 2.8)
19
20 project (libphonenumber)
21
22 # Find Google Test library
23 find_path (GTEST_INCLUDE_DIR gtest/gtest.h)
24
25 if (GTEST_INCLUDE_DIR STREQUAL "GTEST_INCLUDE_DIR-NOTFOUND")
26   message (FATAL_ERROR
27     "Can't find Google Test framework headers. Please read README.")
28 endif ()
29
30 include_directories (${GTEST_INCLUDE_DIR})
31
32 find_library (GTEST_LIB gtest)
33
34 if (GTEST_LIB STREQUAL "GTEST_LIB-NOTFOUND")
35   message (FATAL_ERROR
36     "Can't find Google Test framework library. Please read README.")
37 endif ()
38
39
40 # Find Google RE2 library
41 find_path (RE2_INCLUDE_DIR re2/re2.h)
42
43 if (RE2_INCLUDE_DIR STREQUAL "RE2_INCLUDE_DIR-NOTFOUND")
44   message (FATAL_ERROR
45     "Can't find Google RE2 headers. Please read README.")
46 endif ()
47
48 include_directories (${RE2_INCLUDE_DIR})
49
50 find_library (RE2_LIB re2)
51
52 if (RE2_LIB STREQUAL "RE2_LIB-NOTFOUND")
53   message (FATAL_ERROR
54     "Can't find Google RE2 library. Please read README.")
55 endif ()
56
57
58 # Find Protocol Buffers compiler
59 find_program (PROTOC NAMES protoc)
60
61 if (PROTOC STREQUAL "PROTOC-NOTFOUND")
62   message (FATAL_ERROR
63       "Can't find Google Protocol Buffers compiler (protoc). "
64       "Please read README.")
65 endif ()
66
67 # Find Protocol Buffers library
68 find_path (PROTOBUF_INCLUDE_DIR google/protobuf/message_lite.h)
69
70 if (PROTOBUF_INCLUDE_DIR STREQUAL "PROTOBUF_INCLUDE_DIR-NOTFOUND")
71   message (FATAL_ERROR
72     "Can't find Google Protocol headers. Please read README.")
73 endif ()
74
75 include_directories (${PROTOBUF_INCLUDE_DIR})
76
77 find_library (PROTOBUF_LIB protobuf)
78
79 if (PROTOBUF_LIB STREQUAL "PROTOBUF_LIB-NOTFOUND")
80   message (FATAL_ERROR
81     "Can't find Google Protocol Buffers library. Please read README.")
82 endif ()
83
84 # Check Protocol Buffers version (if pkg-config available)
85 find_package (PkgConfig)
86
87 if (PKG_CONFIG_EXECUTABLE)
88   pkg_check_modules (PC_PROTOBUF REQUIRED protobuf>=2.4)
89 endif (PKG_CONFIG_EXECUTABLE)
90
91 # Find ICU
92 find_path (ICU_INCLUDE_DIR unicode/unistr.h unicode/normlzr.h)
93
94 if (ICU_INCLUDE_DIR STREQUAL "ICU_INCLUDE_DIR-NOTFOUND")
95   message (FATAL_ERROR
96     "Can't find ICU headers. Please read README.")
97 endif ()
98
99 include_directories (${ICU_INCLUDE_DIR})
100
101 find_library (ICU_LIB icui18n)
102
103 if (ICU_LIB STREQUAL "ICU_LIB-NOTFOUND")
104   message (FATAL_ERROR
105     "Can't find ICU library. Please read README.")
106 endif ()
107
108
109 # Add protoc (Protocol Buffers compiler) target
110 set (
111   PROTOBUF_SOURCES "${CMAKE_SOURCE_DIR}/src/phonemetadata.proto"
112                    "${CMAKE_SOURCE_DIR}/src/phonenumber.proto"
113
114 )
115
116 set (
117   PROTOBUF_OUTPUT "${CMAKE_SOURCE_DIR}/src/phonemetadata.pb.cc"
118                   "${CMAKE_SOURCE_DIR}/src/phonemetadata.pb.h"
119                   "${CMAKE_SOURCE_DIR}/src/phonenumber.pb.cc"
120                   "${CMAKE_SOURCE_DIR}/src/phonenumber.pb.h"
121 )
122
123 add_custom_command (
124   COMMAND ${PROTOC} --cpp_out=${CMAKE_SOURCE_DIR}
125     --proto_path=${CMAKE_SOURCE_DIR} ${PROTOBUF_SOURCES}
126
127   OUTPUT ${PROTOBUF_OUTPUT}
128   DEPENDS ${PROTOBUF_SOURCES}
129 )
130
131 add_custom_target (
132   generate-sources
133
134   DEPENDS ${PROTOBUF_OUTPUT}
135   COMMENT "Generating Protocol Buffers code"
136 )
137
138 # Platform independent sources
139 set (
140   SOURCES
141   "src/base/at_exit.cc"
142   "src/base/lazy_instance.cc"
143   "src/base/string_piece.cc"
144   "src/base/synchronization/lock.cc"
145   "src/base/threading/thread_restrictions.cc"
146   "src/default_logger.cc"
147   "src/logger_adapter.cc"
148   "src/metadata.cc"         # Generated by src/embed_binary_data.sh
149   "src/phonemetadata.pb.cc" # Generated by Protocol Buffers
150   "src/phonenumber.pb.cc"   # Generated by Protocol Buffers
151   "src/phonenumberutil.cc"
152   "src/re2_cache.cc"
153   "src/stringutil.cc"
154   "src/utf/rune.c"
155   "src/utf/unicodetext.cc"
156   "src/utf/unilib.cc"
157 )
158
159 if (UNIX)
160   if (CMAKE_COMPILER_IS_GNUCXX)
161     add_definitions ("-Wall -Wextra -Werror")
162
163     # The next flags are needed by base/ source files to compile
164     # low level code needed by Singleton
165     add_definitions ("-DCOMPILER_GCC -DOS_POSIX -DOS_LINUX")
166
167     if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86.*")
168       add_definitions ("-DARCH_CPU_X86_FAMILY")
169       # Add GCC specific sources
170       list (APPEND SOURCES "src/base/atomicops_internals_x86_gcc.cc")
171
172     elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES ".*arm.*")
173       add_definitions ("-DARCH_CPU_ARM_FAMILY")
174     endif ()
175   endif ()
176
177   # Add POSIX specific sources
178   list (
179     APPEND SOURCES
180     "src/base/synchronization/lock_impl_posix.cc"
181     "src/base/threading/platform_thread_posix.cc"
182     "src/base/threading/thread_local_posix.cc"
183   )
184 else (WIN32)
185   # TODO: add Windows support (define COMPILER_MSVC, OS_WIN)
186   list (
187     APPEND SOURCES
188     "src/base/synchronization/lock_impl_win.cc"
189     "src/base/threading/platform_thread_win.cc"
190     "src/base/threading/thread_local_win.cc"
191   )
192   # TODO: Windows specific flags
193 endif ()
194
195
196 include_directories ("src")
197 include_directories (".")
198                           
199 add_library (phonenumber STATIC ${SOURCES})
200 add_dependencies (phonenumber generate-sources)
201
202 target_link_libraries (phonenumber ${RE2_LIB} ${PROTOBUF_LIB} ${ICU_LIB})
203
204 # Tests
205
206 set (TEST_SOURCES
207   "src/phonenumberutil_test.cc"
208   "src/re2_cache_test.cc"
209   "src/run_tests.cc"
210   "src/stringutil_test.cc"
211   "src/test_metadata.cc"
212 )
213
214 add_executable (libphonenumber_test ${TEST_SOURCES})
215
216 target_link_libraries (libphonenumber_test phonenumber ${GTEST_LIB} pthread)