CPP: Switch protobuff to lite and add Equals method. Patch contributed by philip...
[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 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 NAME LIBRARY VERSION)
52   if (PKG_CONFIG_EXECUTABLE)
53     pkg_check_modules (NAME REQUIRED ${LIBRARY}>=${VERSION})
54   endif (PKG_CONFIG_EXECUTABLE)
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 # Find all the required libraries and programs.
68 find_required_library (GTEST gtest/gtest.h gtest "Google Test framework")
69
70 find_required_library (RE2 re2/re2.h re2 "Google RE2")
71
72 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf
73                        "Google Protocol Buffers")
74 check_library_version (PC_PROTOBUF protobuf 2.4)
75
76 find_required_library (ICU unicode/unistr.h icui18n "ICU")
77 check_library_version (PC_ICU icui18n 4.4)
78
79 find_required_program (PROTOC protoc
80                        "Google Protocol Buffers compiler (protoc)")
81
82 # Add protoc (Protocol Buffers compiler) target.
83 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources")
84
85 set (
86   PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto"
87                    "${RESOURCES_DIR}/phonenumber.proto"
88 )
89
90 set (
91   PROTOBUF_OUTPUT "${CMAKE_SOURCE_DIR}/src/phonemetadata.pb.cc"
92                   "${CMAKE_SOURCE_DIR}/src/phonemetadata.pb.h"
93                   "${CMAKE_SOURCE_DIR}/src/phonenumber.pb.cc"
94                   "${CMAKE_SOURCE_DIR}/src/phonenumber.pb.h"
95 )
96
97 add_custom_command (
98   COMMAND ${PROTOC_BIN} --cpp_out="${CMAKE_SOURCE_DIR}/src"
99     --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES}
100
101   OUTPUT ${PROTOBUF_OUTPUT}
102   DEPENDS ${PROTOBUF_SOURCES}
103 )
104
105 add_custom_target (
106   generate-sources
107
108   DEPENDS ${PROTOBUF_OUTPUT}
109   COMMENT "Generating Protocol Buffers code"
110 )
111
112 # Platform independent sources.
113 set (
114   SOURCES
115   "src/base/at_exit.cc"
116   "src/base/lazy_instance.cc"
117   "src/base/string_piece.cc"
118   "src/base/synchronization/lock.cc"
119   "src/base/threading/thread_restrictions.cc"
120   "src/default_logger.cc"
121   "src/logger_adapter.cc"
122   "src/metadata.cc"
123   "src/phonemetadata.pb.cc" # Generated by Protocol Buffers.
124   "src/phonenumber.cc"
125   "src/phonenumber.pb.cc"   # Generated by Protocol Buffers.
126   "src/phonenumberutil.cc"
127   "src/re2_cache.cc"
128   "src/stringutil.cc"
129   "src/utf/rune.c"
130   "src/utf/unicodetext.cc"
131   "src/utf/unilib.cc"
132 )
133
134 if (UNIX)
135   if (CMAKE_COMPILER_IS_GNUCXX)
136     add_definitions ("-Wall -Wextra -Werror")
137
138     # The next flags are needed by base/ source files to compile low level code
139     # needed by Singleton.
140     add_definitions ("-DCOMPILER_GCC -DOS_POSIX -DOS_LINUX")
141
142     if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86.*")
143       add_definitions ("-DARCH_CPU_X86_FAMILY")
144       # Add GCC specific sources.
145       list (APPEND SOURCES "src/base/atomicops_internals_x86_gcc.cc")
146
147     elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES ".*arm.*")
148       add_definitions ("-DARCH_CPU_ARM_FAMILY")
149     endif ()
150   endif ()
151
152   # Add POSIX specific sources.
153   list (
154     APPEND SOURCES
155     "src/base/synchronization/lock_impl_posix.cc"
156     "src/base/threading/platform_thread_posix.cc"
157     "src/base/threading/thread_local_posix.cc"
158   )
159 else (WIN32)
160   # TODO: add Windows support (define COMPILER_MSVC, OS_WIN).
161   list (
162     APPEND SOURCES
163     "src/base/synchronization/lock_impl_win.cc"
164     "src/base/threading/platform_thread_win.cc"
165     "src/base/threading/thread_local_win.cc"
166   )
167   # TODO: Windows specific flags.
168 endif ()
169
170 include_directories ("src")
171 include_directories (".")
172
173 add_library (phonenumber STATIC ${SOURCES})
174 add_dependencies (phonenumber generate-sources)
175
176 target_link_libraries (phonenumber ${RE2_LIB} ${PROTOBUF_LIB} ${ICU_LIB})
177
178 # Tests.
179 set (TEST_SOURCES
180   "src/phonenumberutil_test.cc"
181   "src/re2_cache_test.cc"
182   "src/run_tests.cc"
183   "src/stringutil_test.cc"
184   "src/test_metadata.cc"
185 )
186
187 add_executable (libphonenumber_test ${TEST_SOURCES})
188 target_link_libraries (libphonenumber_test phonenumber ${GTEST_LIB} pthread)