]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commitdiff
Make RocksDB compile for iOS
authorIgor Canadi <icanadi@fb.com>
Fri, 4 Apr 2014 20:11:44 +0000 (13:11 -0700)
committerIgor Canadi <icanadi@fb.com>
Fri, 4 Apr 2014 20:11:44 +0000 (13:11 -0700)
Summary:
I had to make number of changes to the code and Makefile:
* Add `make lib`, that will create static library without debug info. We need this to avoid growing binary too much. Currently it's 14MB.
* Remove cpuinfo() function and use __SSE4_2__ macro. We actually used the macro as part of Fast_CRC32() function.
As a result, I also accidentally fixed this issue: https://www.facebook.com/groups/rocksdb.dev/permalink/549700778461774/?stream_ref=2
* Remove __thread locals in OS_MACOSX

Test Plan: `make lib PLATFORM=IOS`

Reviewers: ljin, haobo, dhruba, sdong

Reviewed By: haobo

CC: leveldb
Differential Revision: https://reviews.facebook.net/D17475

.gitignore
INSTALL.md
Makefile
build_tools/build_detect_platform
include/rocksdb/perf_context.h
util/crc32c.cc
util/perf_context.cc
util/perf_context_imp.h

index 5e62839420a9072c69d5180cadabde0fe3e3b101..a3a70ee3113da92e35983010bbac54b883f6f4d6 100644 (file)
@@ -16,6 +16,7 @@ build_config.mk
 *.class
 *.jar
 *.*jnilib*
+*.d-e
 
 ldb
 manifest_dump
index 86934db69cffc5d40ac57ca59e5c8c460a1f498b..2a91be6974e2103c7541185f197305464217039c 100644 (file)
@@ -67,6 +67,9 @@ libraries. You are on your own.
     * Please note that some of the optimizations/features are disabled in OSX.
     We did not run any production workloads on it.
 
+* **iOS**:
+  * Run: `TARGET_OS=IOS make static_lib`
+
 ## Compilation
 `make clean; make` will compile librocksdb.a (RocksDB static library) and all
 the unit tests. You can run all unit tests with `make check`.
index e1e982f152e5deb5bb961a8db2d34d1598395a5d..a578c12c8e1ea313743430cc282edac3fe9f621c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -23,6 +23,14 @@ $(shell (export ROCKSDB_ROOT=$(CURDIR); $(CURDIR)/build_tools/build_detect_platf
 # this file is generated by the previous line to set build flags and sources
 include build_config.mk
 
+ifneq ($(PLATFORM), IOS)
+CFLAGS += -g
+CXXFLAGS += -g
+else
+# no debug info for IOS, that will make our library big
+OPT += -DNDEBUG
+endif
+
 # ASAN doesn't work well with jemalloc. If we're compiling with ASAN, we should use regular malloc.
 ifdef COMPILE_WITH_ASAN
        # ASAN compile flags
@@ -36,9 +44,9 @@ else
        PLATFORM_CCFLAGS += $(JEMALLOC_INCLUDE) -DHAVE_JEMALLOC
 endif
 
-WARNING_FLAGS = -Wall -Werror -Wno-sign-compare
-CFLAGS += -g $(WARNING_FLAGS) -I. -I./include $(PLATFORM_CCFLAGS) $(OPT)
-CXXFLAGS += -g $(WARNING_FLAGS) -I. -I./include $(PLATFORM_CXXFLAGS) $(OPT) -Woverloaded-virtual
+WARNING_FLAGS = -Wall -Werror -Wno-sign-compare -Wno-unused-const-variable
+CFLAGS += $(WARNING_FLAGS) -I. -I./include $(PLATFORM_CCFLAGS) $(OPT)
+CXXFLAGS += $(WARNING_FLAGS) -I. -I./include $(PLATFORM_CXXFLAGS) $(OPT) -Woverloaded-virtual
 
 LDFLAGS += $(PLATFORM_LDFLAGS)
 
@@ -148,11 +156,15 @@ $(SHARED3):
 endif  # PLATFORM_SHARED_EXT
 
 .PHONY: blackbox_crash_test check clean coverage crash_test ldb_tests \
-       release tags valgrind_check whitebox_crash_test format shared_lib all \
+       release tags valgrind_check whitebox_crash_test format static_lib shared_lib all \
        dbg
 
 all: $(LIBRARY) $(PROGRAMS)
 
+static_lib: $(LIBRARY)
+
+shared_lib: $(SHARED)
+
 dbg: $(LIBRARY) $(PROGRAMS)
 
 # Will also generate shared libraries.
@@ -218,8 +230,6 @@ tags:
 format:
        build_tools/format-diff.sh
 
-shared_lib: $(SHARED)
-
 # ---------------------------------------------------------------------------
 #      Unit tests and tools
 # ---------------------------------------------------------------------------
@@ -435,20 +445,20 @@ ifeq ($(PLATFORM), IOS)
 PLATFORMSROOT=/Applications/Xcode.app/Contents/Developer/Platforms
 SIMULATORROOT=$(PLATFORMSROOT)/iPhoneSimulator.platform/Developer
 DEVICEROOT=$(PLATFORMSROOT)/iPhoneOS.platform/Developer
-IOSVERSION=$(shell defaults read $(PLATFORMSROOT)/iPhoneOS.platform/versionCFBundleShortVersionString)
+IOSVERSION=$(shell defaults read $(PLATFORMSROOT)/iPhoneOS.platform/version CFBundleShortVersionString)
 
 .cc.o:
        mkdir -p ios-x86/$(dir $@)
-       $(SIMULATORROOT)/usr/bin/$(CXX) $(CXXFLAGS) -isysroot $(SIMULATORROOT)/SDKs/iPhoneSimulator$(IOSVERSION).sdk -arch i686 -c $< -o ios-x86/$@ $(COVERAGEFLAGS)
+       $(CXX) $(CXXFLAGS) -isysroot $(SIMULATORROOT)/SDKs/iPhoneSimulator$(IOSVERSION).sdk -arch i686 -arch x86_64 -c $< -o ios-x86/$@
        mkdir -p ios-arm/$(dir $@)
-       $(DEVICEROOT)/usr/bin/$(CXX) $(CXXFLAGS) -isysroot $(DEVICEROOT)/SDKs/iPhoneOS$(IOSVERSION).sdk -arch armv6 -arch armv7 -c $< -o ios-arm/$@ $(COVERAGEFLAGS)
+       xcrun -sdk iphoneos $(CXX) $(CXXFLAGS) -isysroot $(DEVICEROOT)/SDKs/iPhoneOS$(IOSVERSION).sdk -arch armv6 -arch armv7 -arch armv7s -arch arm64 -c $< -o ios-arm/$@
        lipo ios-x86/$@ ios-arm/$@ -create -output $@
 
 .c.o:
        mkdir -p ios-x86/$(dir $@)
-       $(SIMULATORROOT)/usr/bin/$(CC) $(CFLAGS) -isysroot $(SIMULATORROOT)/SDKs/iPhoneSimulator$(IOSVERSION).sdk -arch i686 -c $< -o ios-x86/$@
+       $(CC) $(CFLAGS) -isysroot $(SIMULATORROOT)/SDKs/iPhoneSimulator$(IOSVERSION).sdk -arch i686 -arch x86_64 -c $< -o ios-x86/$@
        mkdir -p ios-arm/$(dir $@)
-       $(DEVICEROOT)/usr/bin/$(CC) $(CFLAGS) -isysroot $(DEVICEROOT)/SDKs/iPhoneOS$(IOSVERSION).sdk -arch armv6 -arch armv7 -c $< -o ios-arm/$@
+       xcrun -sdk iphoneos $(CC) $(CFLAGS) -isysroot $(DEVICEROOT)/SDKs/iPhoneOS$(IOSVERSION).sdk -arch armv6 -arch armv7 -arch armv7s -arch arm64 -c $< -o ios-arm/$@
        lipo ios-x86/$@ ios-arm/$@ -create -output $@
 
 else
index 5a15aca333440754582047085e32e27e15dee62b..94aafd62efa9fc2f3aff41b43e15a17d0ab8862b 100755 (executable)
@@ -87,7 +87,7 @@ PLATFORM_SHARED_CFLAGS="-fPIC"
 PLATFORM_SHARED_VERSIONED=false
 
 # generic port files (working on all platform by #ifdef) go directly in /port
-GENERIC_PORT_FILES=`find $ROCKSDB_ROOT/port -name '*.cc' | tr "\n" " "`
+GENERIC_PORT_FILES=`cd $ROCKSDB_ROOT; find port -name '*.cc' | tr "\n" " "`
 
 # On GCC, we pick libc's memcmp over GCC's memcmp via -fno-builtin-memcmp
 case "$TARGET_OS" in
@@ -98,6 +98,13 @@ case "$TARGET_OS" in
         PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name "
         # PORT_FILES=port/darwin/darwin_specific.cc
         ;;
+    IOS)
+        PLATFORM=IOS
+        COMMON_FLAGS="$COMMON_FLAGS -DOS_MACOSX -DIOS_CROSS_COMPILE"
+        PLATFORM_SHARED_EXT=dylib
+        PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name "
+        CROSS_COMPILE=true
+        ;;
     Linux)
         PLATFORM=OS_LINUX
         COMMON_FLAGS="$COMMON_FLAGS -DOS_LINUX"
index 61adad6b73aa5343fccfb422bcb8e5a36cc2019d..45399ac2d5003ea72dd0e383a0c204830128e532 100644 (file)
@@ -64,7 +64,11 @@ struct PerfContext {
   uint64_t write_memtable_time;
 };
 
+#if defined(OS_MACOSX)
+extern PerfContext perf_context;
+#else
 extern __thread PerfContext perf_context;
+#endif
 
 }
 
index 50178ae71db4ef2a4d1d2be386d29468c91c7d9b..9500c44c1b34aacc00e3648daedf16cf356a6e37 100644 (file)
@@ -313,26 +313,12 @@ static inline void Slow_CRC32(uint64_t* l, uint8_t const **p) {
   table0_[c >> 24];
 }
 
+#ifdef __SSE4_2__
 static inline void Fast_CRC32(uint64_t* l, uint8_t const **p) {
-  #ifdef __SSE4_2__
   *l = _mm_crc32_u64(*l, LE_LOAD64(*p));
   *p += 8;
-  #else
-  Slow_CRC32(l, p);
-  #endif
-}
-
-// Detect if SS42 or not.
-static bool isSSE42() {
-  #ifdef __GNUC__
-  uint32_t c_;
-  uint32_t d_;
-  __asm__("cpuid" : "=c"(c_), "=d"(d_) : "a"(1) : "ebx");
-  return c_ & (1U << 20); // copied from CpuId.h in Folly.
-  #else
-  return false;
-  #endif
 }
+#endif
 
 template<void (*CRC32)(uint64_t*, uint8_t const**)>
 uint32_t ExtendImpl(uint32_t crc, const char* buf, size_t size) {
@@ -380,7 +366,11 @@ uint32_t ExtendImpl(uint32_t crc, const char* buf, size_t size) {
 typedef uint32_t (*Function)(uint32_t, const char*, size_t);
 
 static inline Function Choose_Extend() {
-  return isSSE42() ? ExtendImpl<Fast_CRC32> : ExtendImpl<Slow_CRC32>;
+#ifdef __SSE4_2__
+  return ExtendImpl<Fast_CRC32>;
+#else
+  return ExtendImpl<Slow_CRC32>;
+#endif
 }
 
 Function ChosenExtend = Choose_Extend();
index fc8efba645e716ad5c6066bc8cd5ff120192b45c..855e7c45ab26a5ba5f7d7f120c3d8d5509007d94 100644 (file)
 namespace rocksdb {
 
 // by default, enable counts only
+#if defined(IOS_CROSS_COMPILE)
+PerfLevel perf_level = kEnableCount;
+#else
 __thread PerfLevel perf_level = kEnableCount;
+#endif
 
 void SetPerfLevel(PerfLevel level) { perf_level = level; }
 
@@ -69,6 +73,9 @@ std::string PerfContext::ToString() const {
   return ss.str();
 }
 
+#if defined(IOS_CROSS_COMPILE)
+PerfContext perf_context;
+#else
 __thread PerfContext perf_context;
-
+#endif
 }
index 7b06e4c1dcc831352ee168649e87e9dbc662a911..ac044ca0985b27bd2d851c521d2ba9c5ba562fb2 100644 (file)
@@ -9,7 +9,13 @@
 
 namespace rocksdb {
 
+// TODO(icanadi): when calling perf_context is macro-ed (TODO ljin), make it
+// noop in case IOS_CROSS_COMPILE
+#if defined(IOS_CROSS_COMPILE)
+extern enum PerfLevel perf_level;
+#else
 extern __thread PerfLevel perf_level;
+#endif
 
 inline void StartPerfTimer(StopWatchNano* timer) {
   if (perf_level >= PerfLevel::kEnableTime) {