• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
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 #ifndef ICING_PORTABLE_PLATFORM_H_
16 #define ICING_PORTABLE_PLATFORM_H_
17 
18 #include "unicode/uconfig.h"  // IWYU pragma: keep
19 // clang-format: do not reorder the above include.
20 
21 #include "unicode/uvernum.h"
22 
23 namespace icing {
24 namespace lib {
25 
26 // Returns true if the test was built with the CFStringTokenizer as the
27 // implementation of LanguageSegmenter.
IsCfStringTokenization()28 inline bool IsCfStringTokenization() {
29 #if defined(__APPLE__) && !defined(ICING_IOS_ICU4C_SEGMENTATION)
30   return true;
31 #endif  // defined(__APPLE__) && !defined(ICING_IOS_ICU4C_SEGMENTATION)
32   return false;
33 }
34 
IsReverseJniTokenization()35 inline bool IsReverseJniTokenization() {
36 #ifdef ICING_REVERSE_JNI_SEGMENTATION
37   return true;
38 #endif  // ICING_REVERSE_JNI_SEGMENTATION
39   return false;
40 }
41 
IsIcuTokenization()42 inline bool IsIcuTokenization() {
43   return !IsReverseJniTokenization() && !IsCfStringTokenization();
44 }
45 
GetIcuTokenizationVersion()46 inline int GetIcuTokenizationVersion() {
47   return IsIcuTokenization() ? U_ICU_VERSION_MAJOR_NUM : 0;
48 }
49 
50 // Whether we're running on android_x86
IsAndroidX86()51 inline bool IsAndroidX86() {
52 #if defined(__ANDROID__) && defined(__i386__)
53   return true;
54 #endif  // defined(__ANDROID__) && defined(__i386__)
55   return false;
56 }
57 
58 // Whether we're running on android_armeabi-v7a
IsAndroidArm()59 inline bool IsAndroidArm() {
60 #if defined(__ANDROID__) && defined(__arm__)
61   return true;
62 #endif  // defined(__ANDROID__) && defined(__arm__)
63   return false;
64 }
65 
66 // Whether the running test is an iOS test.
IsIosPlatform()67 inline bool IsIosPlatform() {
68 #if defined(__APPLE__)
69   return true;
70 #endif  // defined(__APPLE__)
71   return false;
72 }
73 
74 // TODO(b/259129263): verify the flag works for different platforms.
75 #if defined(__arm__) || defined(__i386__)
76 #define ICING_ARCH_BIT_32
77 #elif defined(__aarch64__) || defined(__x86_64__)
78 #define ICING_ARCH_BIT_64
79 #else
80 #define ICING_ARCH_BIT_UNKNOWN
81 #endif
82 
83 enum Architecture {
84   UNKNOWN,
85   BIT_32,
86   BIT_64,
87 };
88 
89 // Returns which architecture we're running on.
90 //
91 // Architecture macros pulled from
92 // https://developer.android.com/ndk/guides/cpu-features
GetArchitecture()93 inline Architecture GetArchitecture() {
94 #if defined(ICING_ARCH_BIT_32)
95   return BIT_32;
96 #elif defined(ICING_ARCH_BIT_64)
97   return BIT_64;
98 #else
99   return UNKNOWN;
100 #endif
101 }
102 
103 }  // namespace lib
104 }  // namespace icing
105 
106 #endif  // ICING_PORTABLE_PLATFORM_H_
107