Skip to content

Commit

Permalink
xcode support (#4581)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkareta committed Jun 21, 2018
1 parent 0ae3c0b commit bccc28d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 46 deletions.
114 changes: 70 additions & 44 deletions CMakeLists.txt
Expand Up @@ -228,33 +228,17 @@ endif()

# make debug (environment variable from Makefile)
if(DEFINED ENV{DEBUG})
set(DEBUG TRUE)
set(CMAKE_BUILD_TYPE "Debug")
if(WINDOWS)
# TODO(#1985): Is there a MSVC flag congruent to -fstandalone-debug?
add_compile_options(
/Zi
/Od
)
else()
add_compile_options(
-g
-O0
-fstandalone-debug
)
endif()
add_definitions(-DDEBUG)
set(CMAKE_BUILD_TYPE "DEBUG")
WARNING_LOG("Setting DEBUG build")
elseif(DEFINED ENV{SANITIZE})
set(CMAKE_BUILD_TYPE "DEBUG")
add_definitions(-DDEBUG)
# make sanitize (cannot make debug sanitize)
set(SANITIZE_BLACKLIST "${CMAKE_SOURCE_DIR}/tools/analysis/sanitize_blacklist.txt")
add_compile_options(
-g
-fstandalone-debug
-fno-omit-frame-pointer
-fno-optimize-sibling-calls
-fsanitize-blacklist=${SANITIZE_BLACKLIST}
-O1
)
if (DEFINED ENV{SANITIZE_THREAD})
add_compile_options(-fsanitize=thread)
Expand Down Expand Up @@ -288,34 +272,74 @@ elseif(DEFINED ENV{SANITIZE})
set(TEST_ARGS "--gtest_filter=-${TEST_ARGS}")
endif()
else()
set(DEBUG FALSE)
if(WINDOWS)
add_compile_options(/Ot /WX)
elseif(CLANG)
add_compile_options(-Oz)
set(CMAKE_BUILD_TYPE "RELEASE")
endif()

# Please put all xcode specific stuff to either CMAKE_XCODE_ATTRIBUTE_*
# for global project settings or
# XCODE_ATTRIBUTE_* for specific target
# all global definitions/compile options will be inherited
# using xcode $(inherited) variable
if(CMAKE_GENERATOR STREQUAL Xcode)
set(CMAKE_XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[variant=Debug] "DEBUG=1")
set(CMAKE_XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[variant=Release] "NDEBUG=1")
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS
"$(inherited)"
)
set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS
"$(inherited)"
)
else()
if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
add_definitions(-DDEBUG)
else()
add_compile_options(-Os)
add_definitions(-DNDEBUG)
endif()
add_definitions(-DNDEBUG)
# Do not enable fortify with clang: http://llvm.org/bugs/show_bug.cgi?id=16821
# Visual Studio does not support generation of PIC code
if(POSIX)
# TSAN and MSAN do not like PIE/PIC on Linux 4.1.
# To be safe, only include them when building 'release' outputs.
add_compile_options(
-g
-fno-limit-debug-info
-fPIE
-fpie
-fPIC
-fpic
)
if (CLANG AND DEPS AND NOT FREEBSD)
endif()

# osquery cmake is using CMake generator expressions to support multiconfig IDEs
# https://cmake.org/cmake/help/v3.10/manual/cmake-generator-expressions.7.html

if(WINDOWS)
# TODO(#1985): Is there a MSVC flag congruent to -fstandalone-debug?
add_compile_options(
"$<$<CONFIG:DEBUG>:/Zi>"
"$<$<CONFIG:DEBUG>:/Od>"
"$<$<CONFIG:RELEASE>:/Ot>"
"$<$<CONFIG:RELEASE>:/WX>"
)
else()
add_compile_options(
"$<$<AND:$<CXX_COMPILER_ID:Clang>,$<CONFIG:RELEASE>>:-Oz>"
"$<$<AND:$<NOT:$<CXX_COMPILER_ID:Clang>>,$<CONFIG:RELEASE>>:-Os>"
)
endif()

add_compile_options(
"$<$<CONFIG:DEBUG>:-g>"
"$<$<CONFIG:DEBUG>:-O0>"
"$<$<CONFIG:DEBUG>:-fstandalone-debug>"
)

# Do not enable fortify with clang: http://llvm.org/bugs/show_bug.cgi?id=16821
# Visual Studio does not support generation of PIC code
if (POSIX)
# TSAN and MSAN do not like PIE/PIC on Linux 4.1.
# To be safe, only include them when building 'release' outputs.
add_compile_options(
"$<$<CONFIG:RELEASE>:-g>"
"$<$<CONFIG:RELEASE>:-fno-limit-debug-info>"
"$<$<CONFIG:RELEASE>:-fPIE>"
"$<$<CONFIG:RELEASE>:-fpie>"
"$<$<CONFIG:RELEASE>:-fPIC>"
"$<$<CONFIG:RELEASE>:-fpic>"
)
if (CLANG AND DEPS AND NOT FREEBSD)
# Only enable LTO builds when using clang on Unix, for now
add_compile_options(-flto=thin)
endif()
add_compile_options("$<$<CONFIG:RELEASE>:-flto=thin>")
endif()
endif()

# Generate a compile_commands.json for static analyzer input.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Expand Down Expand Up @@ -486,8 +510,10 @@ if(OSQUERY_BUILD_VERSION STREQUAL "")
WARNING_LOG("Setting the version to: 1.0.0-unknown (beware!)")
set(OSQUERY_BUILD_VERSION "1.0.0-unknown")
endif()
if(DEFINED ENV{SDK_VERSION})
set(OSQUERY_BUILD_SDK_VERSION "${ENV{SDK_VERSION}}")

# SDK_VERSION is defined by XCode as current macOS sdk version
if(DEFINED ENV{SDK_VERSION} AND NOT (CMAKE_GENERATOR STREQUAL Xcode))
set(OSQUERY_BUILD_SDK_VERSION "$ENV{SDK_VERSION}")
else()
string(REPLACE "-" ";" OSQUERY_BUILD_SDK_VERSION ${OSQUERY_BUILD_VERSION})
list(GET OSQUERY_BUILD_SDK_VERSION 0 OSQUERY_BUILD_SDK_VERSION)
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Expand Up @@ -326,3 +326,9 @@ test: .setup
fi
@cd $(BUILD_DIR) && $(CMAKE) && \
$(DEFINES) $(MAKE) --no-print-directory $(MAKEFLAGS) $(MAKECMDGOALS)

xcode:
@# Use _xcode suffix to avoid conflict with regular "make"
@# so both "make" and "make xcode" can be used at the same time
@./tools/generate_xcode_project.sh ${BUILD_DIR}_xcode '${CMAKE}'

11 changes: 9 additions & 2 deletions osquery/CMakeLists.txt
Expand Up @@ -22,6 +22,13 @@ set(OSQUERY_BENCHMARKS "")

# The 'additional' source files are NOT included with SDK (libosquery_additional).
set(OSQUERY_ADDITIONAL_SOURCES "")

# Temporary hack for additional library, this library is sourceless
# so for some reason CMake adds "rm -f" as PostBuild step to XCode project
if(CMAKE_GENERATOR STREQUAL Xcode)
set(OSQUERY_ADDITIONAL_SOURCES main/empty.cpp)
endif()

set(OSQUERY_ADDITIONAL_LINKS "")
set(OSQUERY_ADDITIONAL_TESTS "")
set(OSQUERY_TABLES_TESTS "")
Expand Down Expand Up @@ -106,7 +113,7 @@ if(NOT SKIP_TABLES)
endif()

if(WINDOWS)
if(DEFINED ENV{DEBUG})
if(CMAKE_BUILD_TYPE EQUAL "DEBUG")
set(WB_KEY "sgd")
set(WO_KEY "_dbg")
set(WT_KEY "d_dbg")
Expand Down Expand Up @@ -202,7 +209,7 @@ if(APPLE)
ADD_OSQUERY_LINK_CORE("-Wl,-no_weak_imports")
elseif(LINUX OR FREEBSD)
ADD_OSQUERY_LINK_CORE("-Wl,-zrelro -Wl,-znow")
if(NOT DEFINED ENV{SANITIZE} AND NOT DEFINED ENV{DEBUG})
if(NOT DEFINED ENV{SANITIZE} AND (CMAKE_BUILD_TYPE STREQUAL "RELEASE"))
ADD_OSQUERY_LINK_CORE("-pie")
endif()
endif()
Expand Down
16 changes: 16 additions & 0 deletions tools/generate_xcode_project.sh
@@ -0,0 +1,16 @@
#!/bin/bash
set -e

BUILD_DIR=$1
CMAKE_COMMAND="$2 -G Xcode"

echo "Cleaning build directory: $BUILD_DIR"
rm -rf ${BUILD_DIR}
mkdir -p ${BUILD_DIR}

echo "Generating xcode project using cmake: $CMAKE_COMMAND"
cd ${BUILD_DIR}
eval ${CMAKE_COMMAND}

echo "Fixing cmake quotes around \$(inherited)"
sed -i -e "s/'\$(inherited)'/\$(inherited)/g" ./OSQUERY.xcodeproj/project.pbxproj

0 comments on commit bccc28d

Please sign in to comment.