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 NAME LIBRARY VERSION)
52 if (PKG_CONFIG_EXECUTABLE)
53 pkg_check_modules (NAME REQUIRED ${LIBRARY}>=${VERSION})
54 endif (PKG_CONFIG_EXECUTABLE)
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 # Find all the required libraries and programs.
68 find_required_library (GTEST gtest/gtest.h gtest "Google Test framework")
70 find_required_library (RE2 re2/re2.h re2 "Google RE2")
72 find_required_library (PROTOBUF google/protobuf/message_lite.h protobuf
73 "Google Protocol Buffers")
74 check_library_version (PC_PROTOBUF protobuf 2.4)
76 find_required_library (ICU unicode/unistr.h icui18n "ICU")
77 check_library_version (PC_ICU icui18n 4.4)
79 find_required_program (PROTOC protoc
80 "Google Protocol Buffers compiler (protoc)")
82 # Add protoc (Protocol Buffers compiler) target.
83 set (RESOURCES_DIR "${CMAKE_SOURCE_DIR}/../resources")
86 PROTOBUF_SOURCES "${RESOURCES_DIR}/phonemetadata.proto"
87 "${RESOURCES_DIR}/phonenumber.proto"
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"
98 COMMAND ${PROTOC_BIN} --cpp_out="${CMAKE_SOURCE_DIR}/src"
99 --proto_path=${RESOURCES_DIR} ${PROTOBUF_SOURCES}
101 OUTPUT ${PROTOBUF_OUTPUT}
102 DEPENDS ${PROTOBUF_SOURCES}
108 DEPENDS ${PROTOBUF_OUTPUT}
109 COMMENT "Generating Protocol Buffers code"
112 # Platform independent 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"
123 "src/phonemetadata.pb.cc" # Generated by Protocol Buffers.
125 "src/phonenumber.pb.cc" # Generated by Protocol Buffers.
126 "src/phonenumberutil.cc"
130 "src/utf/unicodetext.cc"
135 if (CMAKE_COMPILER_IS_GNUCXX)
136 add_definitions ("-Wall -Wextra -Werror")
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")
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")
147 elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES ".*arm.*")
148 add_definitions ("-DARCH_CPU_ARM_FAMILY")
152 # Add POSIX specific 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"
160 # TODO: add Windows support (define COMPILER_MSVC, OS_WIN).
163 "src/base/synchronization/lock_impl_win.cc"
164 "src/base/threading/platform_thread_win.cc"
165 "src/base/threading/thread_local_win.cc"
167 # TODO: Windows specific flags.
170 include_directories ("src")
171 include_directories (".")
173 add_library (phonenumber STATIC ${SOURCES})
174 add_dependencies (phonenumber generate-sources)
176 target_link_libraries (phonenumber ${RE2_LIB} ${PROTOBUF_LIB} ${ICU_LIB})
180 "src/phonenumberutil_test.cc"
181 "src/re2_cache_test.cc"
183 "src/stringutil_test.cc"
184 "src/test_metadata.cc"
187 add_executable (libphonenumber_test ${TEST_SOURCES})
188 target_link_libraries (libphonenumber_test phonenumber ${GTEST_LIB} pthread)