diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake
index bab0892aed32e81ce8d8ad79704c28ff6a270ac2..55ea1a382fa634d956395a3aa8627025f348814a 100644
--- a/cmake/compiler.cmake
+++ b/cmake/compiler.cmake
@@ -80,10 +80,9 @@ set(CMAKE_REQUIRED_FLAGS "")
 #
 # Perform build type specific configuration.
 #
-if (CMAKE_COMPILER_IS_GNUCC)
+check_c_compiler_flag("-ggdb" CC_HAS_GGDB)
+if (CC_HAS_GGDB)
     set (CC_DEBUG_OPT "-ggdb")
-else()
-    set (CC_DEBUG_OPT "-g")
 endif()
 
 set (CMAKE_C_FLAGS_DEBUG
diff --git a/cmake/luajit.cmake b/cmake/luajit.cmake
index 39b32410589bec01ba17b45bcc70fd9b1eff6f37..ff971986e0ec8263eede1d23da7ed312f49627e8 100644
--- a/cmake/luajit.cmake
+++ b/cmake/luajit.cmake
@@ -129,52 +129,56 @@ message (STATUS "Use LuaJIT includes: ${LUAJIT_INCLUDE}")
 message (STATUS "Use LuaJIT library: ${LUAJIT_LIB}")
 
 macro(luajit_build)
-    set (luajit_buildoptions BUILDMODE=static)
-    set (luajit_copt "")
-    set (luajit_xcflags "")
+    set (luajit_cc ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
+    # Cmake rules concerning strings and lists of strings are weird.
+    #   set (foo "1 2 3") defines a string, while
+    #   set (foo 1 2 3) defines a list.
+    # Use separate_arguments() to turn a string into a list (splits at ws).
+    # It appears that variable expansion rules are context-dependent.
+    # With the current arrangement add_custom_command()
+    # does the right thing. We can even handle pathnames with
+    # spaces though a path with an embeded semicolon or a quotation mark
+    # will most certainly wreak havok.
+    #
+    # This stuff is extremely fragile, proceed with caution.
+    set (luajit_cflags ${CMAKE_C_FLAGS})
+        separate_arguments(luajit_cflags)
+    set (luajut_ldflags ${CMAKE_STATIC_LINKER_FLAGS})
+        separate_arguments(luajit_ldflags)
+    # Use external unwind on all platforms.
+    set (luajit_xcflags "-DLUAJIT_UNWIND_EXTERNAL=1")
+    # We are consciously ommiting debug info in RelWithDebugInfo mode
     if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
-        set (luajit_buildoptions ${luajit_buildoptions} CCDEBUG=${CC_DEBUG_OPT})
-        set (luajit_copt ${luajit_copt} -O1)
+        set (luajit_ccopt -O0)
+        if (CC_HAS_GGDB)
+            set (luajit_ccdebug -g -ggdb)
+        else ()
+            set (luajit_ccdebug -g)
+        endif ()
         set (luajit_xcflags ${luajit_xcflags}
             -DLUA_USE_APICHECK -DLUA_USE_ASSERT)
     else ()
-        set (luajit_copt ${luajit_copt} -O2)
+        set (luajit_cсopt -O2)
+        set (luajit_ccdbebug "")
+    endif()
+    # Pass sysroot settings on OSX
+    if (NOT "${CMAKE_OSX_SYSROOT}" STREQUAL "")
+        set (luajit_cflags ${luajit_cflags} ${CMAKE_C_SYSROOT_FLAG} ${CMAKE_OSX_SYSROOT})
+        set (luajit_ldflags ${luajit_ldlags} ${CMAKE_C_SYSROOT_FLAG} ${CMAKE_OSX_SYSROOT})
     endif()
     if (ENABLE_VALGRIND)
         set (luajit_xcflags ${luajit_xcflags}
             -DLUAJIT_USE_VALGRIND -DLUAJIT_USE_SYSMALLOC)
     endif()
-    set (luajit_target_cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1} ${CMAKE_C_FLAGS}")
-    # Use external unwind on all platforms.
-    set (luajit_target_cc "${luajit_target_cc} -DLUAJIT_UNWIND_EXTERNAL=1")
-    if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL ${CMAKE_HOST_SYSTEM_PROCESSOR})
-        # Regular mode - use CMake compiler for building host utils.
-        set (luajit_host_cc ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1} ${CMAKE_C_FLAGS})
-    else()
-        # Crosscompile mode - use a host CC compiler for building host utils.
-        # Since CMake does not support cross compilation properly
-        # we have to use system CC here.
-        set (luajit_host_cc "cc")
-    endif()
-    if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64" AND
-            ${CMAKE_SIZEOF_VOID_P} EQUAL 4)
-        # The host compiler must have same pointer size as the target compiler.
-        set (luajit_host_cc "${luajit_host_cc} -m32")
-    endif()
-    set(luajit_ldflags "${CMAKE_SHARED_LINKER_FLAGS}")
-    separate_arguments(luajit_copt)
-    separate_arguments(luajit_ldflags)
-    separate_arguments(luajit_host_cc)
-    separate_arguments(luajit_target_cc)
-    set (luajit_buildoptions ${luajit_buildoptions}
-        CFLAGS=""
-        CXXFLAGS=""
+    set (luajit_buildoptions
+        BUILDMODE=static
+        CC="${luajit_cc}"
+        CFLAGS="${luajit_cflags}"
+        LDFLAGS="${luajit_ldflags}"
+        CCOPT="${luajit_ccopt}"
+        CCDEBUG="${luajit_ccdebug}"
         XCFLAGS="${luajit_xcflags}"
-        CC="${luajit_host_cc}"
-        HOST_CC="${luajit_host_cc}"
-        TARGET_CC="${luajit_target_cc}"
-        CCOPT="${luajit_copt}")
-    set (luajit_buildoptions ${luajit_buildoptions} Q='' LDFLAGS="${luajit_ldflags}")
+        Q='')
     if (${PROJECT_BINARY_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
         add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/third_party/luajit/src/libluajit.a
             WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/third_party/luajit
diff --git a/cmake/module.cmake b/cmake/module.cmake
index 8d6eafbcafe20fcdfff3a82bf68a13e751b80a2f..25cb8f4934ca5d650869ce533a2b80979f35c14e 100644
--- a/cmake/module.cmake
+++ b/cmake/module.cmake
@@ -2,6 +2,7 @@
 function(rebuild_module_api)
     set (dstfile "${CMAKE_CURRENT_BINARY_DIR}/tarantool.h")
     set (tmpfile "${dstfile}.new")
+    set (errcodefile "${CMAKE_CURRENT_BINARY_DIR}/errcode.i")
     set (headers)
     # Get absolute path for header files (required of out-of-source build)
     foreach (header ${ARGN})
@@ -12,16 +13,24 @@ function(rebuild_module_api)
         endif()
     endforeach()
 
+    set (cflags ${CMAKE_C_FLAGS})
+    separate_arguments(cflags)
+    # Pass sysroot settings on OSX
+    if (NOT "${CMAKE_OSX_SYSROOT}" STREQUAL "")
+        set (cflags ${cflags} ${CMAKE_C_SYSROOT_FLAG} ${CMAKE_OSX_SYSROOT})
+    endif()
     add_custom_command(OUTPUT ${dstfile}
         COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/tarantool_header.h > ${tmpfile}
         COMMAND cat ${headers} | ${CMAKE_SOURCE_DIR}/extra/apigen >> ${tmpfile}
         COMMAND ${CMAKE_C_COMPILER}
+            ${cflags}
             -I ${CMAKE_SOURCE_DIR}/src -I ${CMAKE_BINARY_DIR}/src
-            -E ${CMAKE_SOURCE_DIR}/src/box/errcode.h |
-            grep "enum box_error_code" >> ${tmpfile}
+            -E ${CMAKE_SOURCE_DIR}/src/box/errcode.h > ${errcodefile}
+        COMMAND
+            grep "enum box_error_code" ${errcodefile} >> ${tmpfile}
         COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/tarantool_footer.h >> ${tmpfile}
         COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmpfile} ${dstfile}
-        COMMAND ${CMAKE_COMMAND} -E remove ${tmpfile}
+        COMMAND ${CMAKE_COMMAND} -E remove ${errcodefile} ${tmpfile}
         DEPENDS ${srcfiles} ${CMAKE_SOURCE_DIR}/src/box/errcode.h
                 ${CMAKE_CURRENT_SOURCE_DIR}/tarantool_header.h
                 ${CMAKE_CURRENT_SOURCE_DIR}/tarantool_footer.h