xref: /aosp_15_r20/external/icing/icing/portable/platform.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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 "icing/expand/stemming/stemmer-factory.h"
22 #include "unicode/uvernum.h"
23 
24 namespace icing {
25 namespace lib {
26 
27 // Returns true if the test was built with the CFStringTokenizer as the
28 // implementation of LanguageSegmenter.
IsCfStringTokenization()29 inline bool IsCfStringTokenization() {
30 #if defined(__APPLE__) && !defined(ICING_IOS_ICU4C_SEGMENTATION)
31   return true;
32 #endif  // defined(__APPLE__) && !defined(ICING_IOS_ICU4C_SEGMENTATION)
33   return false;
34 }
35 
IsReverseJniTokenization()36 inline bool IsReverseJniTokenization() {
37 #ifdef ICING_REVERSE_JNI_SEGMENTATION
38   return true;
39 #endif  // ICING_REVERSE_JNI_SEGMENTATION
40   return false;
41 }
42 
IsIcuTokenization()43 inline bool IsIcuTokenization() {
44   return !IsReverseJniTokenization() && !IsCfStringTokenization();
45 }
46 
47 // ICU and Reverse JNI tokenization are enabled.
IsIcuWithReverseTokenization()48 inline bool IsIcuWithReverseTokenization() {
49   return IsReverseJniTokenization() && !IsCfStringTokenization();
50 }
51 
GetIcuTokenizationVersion()52 inline int GetIcuTokenizationVersion() {
53   return (IsIcuTokenization() || IsIcuWithReverseTokenization())
54       ? U_ICU_VERSION_MAJOR_NUM : 0;
55 }
56 // Indicates whether stemming is enabled.
57 //
58 // This is false if stemmer_factory is compiled with the none-stemmer
59 // implementation and true for the snowball-stemmer implementation.
IsStemmingEnabled()60 inline bool IsStemmingEnabled() { return stemmer_factory::IsStemmingEnabled(); }
61 
62 // Whether we're running on android_x86
IsAndroidX86()63 inline bool IsAndroidX86() {
64 #if defined(__ANDROID__) && defined(__i386__)
65   return true;
66 #endif  // defined(__ANDROID__) && defined(__i386__)
67   return false;
68 }
69 
70 // Whether we're running on android_armeabi-v7a
IsAndroidArm()71 inline bool IsAndroidArm() {
72 #if defined(__ANDROID__) && defined(__arm__)
73   return true;
74 #endif  // defined(__ANDROID__) && defined(__arm__)
75   return false;
76 }
77 
78 // Whether the running test is an iOS test.
IsIosPlatform()79 inline bool IsIosPlatform() {
80 #if defined(__APPLE__)
81   return true;
82 #endif  // defined(__APPLE__)
83   return false;
84 }
85 
86 // TODO(b/259129263): verify the flag works for different platforms.
87 #if defined(__arm__) || defined(__i386__)
88 #define ICING_ARCH_BIT_32
89 #elif defined(__aarch64__) || defined(__x86_64__)
90 #define ICING_ARCH_BIT_64
91 #else
92 #define ICING_ARCH_BIT_UNKNOWN
93 #endif
94 
95 enum Architecture {
96   UNKNOWN,
97   BIT_32,
98   BIT_64,
99 };
100 
101 // Returns which architecture we're running on.
102 //
103 // Architecture macros pulled from
104 // https://developer.android.com/ndk/guides/cpu-features
GetArchitecture()105 inline Architecture GetArchitecture() {
106 #if defined(ICING_ARCH_BIT_32)
107   return BIT_32;
108 #elif defined(ICING_ARCH_BIT_64)
109   return BIT_64;
110 #else
111   return UNKNOWN;
112 #endif
113 }
114 
115 }  // namespace lib
116 }  // namespace icing
117 
118 #endif  // ICING_PORTABLE_PLATFORM_H_
119