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