2014-09-25 08:45:13 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2016-02-11 19:48:58 +00:00
|
|
|
# Copyright (c) 2014-present, Facebook, Inc.
|
2014-12-18 18:50:47 +00:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This source code is licensed under the BSD-style license found in the
|
2015-01-18 01:23:41 +00:00
|
|
|
# LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
# of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
2016-04-22 18:15:26 +00:00
|
|
|
LIB_SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
|
2015-04-03 07:44:27 +00:00
|
|
|
|
2016-05-27 18:17:06 +00:00
|
|
|
# For OS X, define the distro that builds the kernel extension.
|
|
|
|
DARWIN_KERNEL_VERSION="10.11"
|
|
|
|
|
2014-09-25 08:45:13 +00:00
|
|
|
function platform() {
|
2015-04-03 07:44:27 +00:00
|
|
|
local __out=$1
|
2016-09-28 19:01:58 +00:00
|
|
|
FAMILY=$(python "$LIB_SCRIPT_DIR/get_platform.py" --family)
|
|
|
|
eval $__out=$(python "$LIB_SCRIPT_DIR/get_platform.py" --platform)
|
2014-09-25 08:45:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 07:44:27 +00:00
|
|
|
function _platform() {
|
|
|
|
platform PLATFORM
|
|
|
|
echo $PLATFORM
|
|
|
|
}
|
|
|
|
|
2014-11-12 05:34:59 +00:00
|
|
|
function distro() {
|
2015-04-03 07:44:27 +00:00
|
|
|
local __out=$2
|
2016-09-28 19:01:58 +00:00
|
|
|
eval $__out=$(python "$LIB_SCRIPT_DIR/get_platform.py" --distro)
|
2014-11-12 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 07:44:27 +00:00
|
|
|
function _distro() {
|
|
|
|
distro $1 DISTRO
|
|
|
|
echo $DISTRO
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:45:13 +00:00
|
|
|
function threads() {
|
2015-04-03 07:44:27 +00:00
|
|
|
local __out=$1
|
2014-09-25 08:45:13 +00:00
|
|
|
platform OS
|
2017-09-23 20:50:52 +00:00
|
|
|
if [[ $FAMILY = "redhat" ]] || [[ $FAMILY = "debian" ]] || [[ $FAMILY = "suse" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`cat /proc/cpuinfo | grep processor | wc -l`
|
2014-09-25 08:45:13 +00:00
|
|
|
elif [[ $OS = "darwin" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`sysctl hw.ncpu | awk '{print $2}'`
|
2014-11-12 19:15:17 +00:00
|
|
|
elif [[ $OS = "freebsd" ]]; then
|
2015-04-03 07:44:27 +00:00
|
|
|
eval $__out=`sysctl -n kern.smp.cpus`
|
2015-04-30 22:47:33 +00:00
|
|
|
else
|
2016-03-07 09:30:20 +00:00
|
|
|
eval $__out=`nproc`
|
2014-09-25 08:45:13 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function log() {
|
|
|
|
echo "[+] $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function fatal() {
|
|
|
|
echo "[!] $1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_cxx() {
|
|
|
|
export CXX=$1
|
|
|
|
export CMAKE_CXX_COMPILER=$1
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_cxx_flag() {
|
|
|
|
export CXXFLAGS="$CXXFLAGS $1"
|
|
|
|
export CMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_cc() {
|
|
|
|
export CC=$1
|
|
|
|
export CMAKE_C_COMPILER=$1
|
|
|
|
}
|
2014-10-03 08:02:58 +00:00
|
|
|
|
2016-07-31 18:32:31 +00:00
|
|
|
function do_sudo() {
|
2017-10-17 06:19:33 +00:00
|
|
|
if [[ "$OSQUERY_NOSUDO" = "True" ]]; then
|
|
|
|
$@
|
|
|
|
else
|
|
|
|
ARGS="$@"
|
|
|
|
log "requesting sudo: $ARGS"
|
|
|
|
sudo $@
|
|
|
|
fi
|
2016-07-31 18:32:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 08:02:58 +00:00
|
|
|
function contains_element() {
|
|
|
|
local e
|
|
|
|
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
|
|
|
|
return 1
|
|
|
|
}
|
2015-05-15 00:38:54 +00:00
|
|
|
|
|
|
|
function in_ec2() {
|
|
|
|
if [[ -d /home/ec2-user ]]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2016-01-12 03:32:33 +00:00
|
|
|
|
|
|
|
function build_kernel_cleanup() {
|
|
|
|
# Cleanup kernel
|
2016-07-21 00:21:20 +00:00
|
|
|
$MAKE kernel-test-unload || sudo reboot
|
2016-01-12 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 08:33:17 +00:00
|
|
|
function checkout_thirdparty() {
|
2016-01-21 10:32:53 +00:00
|
|
|
# Reset any work or artifacts from build tests in TP.
|
|
|
|
(cd third-party && git reset --hard HEAD)
|
|
|
|
git submodule init
|
|
|
|
git submodule update
|
2016-08-15 08:33:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 10:44:23 +00:00
|
|
|
function build_target() {
|
|
|
|
threads THREADS
|
|
|
|
|
|
|
|
# Clean previous build artifacts.
|
2017-06-18 21:41:05 +00:00
|
|
|
$MAKE distclean
|
2016-11-19 10:44:23 +00:00
|
|
|
|
|
|
|
# Build osquery.
|
|
|
|
if [[ -z "$RUN_TARGET" ]]; then
|
|
|
|
$MAKE -j$THREADS
|
|
|
|
else
|
|
|
|
$MAKE $RUN_TARGET -j$THREADS
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_deterministic() {
|
|
|
|
# Expect the project to have been built.
|
2016-11-24 02:55:48 +00:00
|
|
|
ALIAS=$DISTRO
|
|
|
|
if [[ "$OS" = "darwin" ]]; then
|
|
|
|
ALIAS=darwin
|
|
|
|
fi
|
|
|
|
DAEMON=build/$ALIAS/osquery/osqueryd
|
2016-11-19 10:44:23 +00:00
|
|
|
strip $DAEMON
|
|
|
|
RUN1=$(shasum -a 256 $DAEMON)
|
|
|
|
|
|
|
|
# Build again.
|
|
|
|
$MAKE distclean
|
|
|
|
build_target
|
|
|
|
|
|
|
|
strip $DAEMON
|
|
|
|
RUN2=$(shasum -a 256 $DAEMON)
|
|
|
|
echo "Initial build: $RUN1"
|
|
|
|
echo " Second build: $RUN2"
|
|
|
|
if [[ "$RUN1" = "$RUN2" ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# The build is not deterministic.
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-08-15 08:33:17 +00:00
|
|
|
function initialize() {
|
|
|
|
DISTRO=$1
|
|
|
|
checkout_thirdparty
|
2016-01-21 10:32:53 +00:00
|
|
|
|
|
|
|
# Remove any previously-cached variables
|
|
|
|
rm build/$DISTRO/CMakeCache.txt >/dev/null 2>&1 || true
|
|
|
|
}
|
|
|
|
|
2016-01-12 03:32:33 +00:00
|
|
|
function build() {
|
|
|
|
platform PLATFORM
|
|
|
|
distro $PLATFORM DISTRO
|
|
|
|
|
|
|
|
# Build kernel extension/module and tests.
|
|
|
|
BUILD_KERNEL=0
|
2016-08-17 02:12:55 +00:00
|
|
|
if [[ -z "$SKIP_KERNEL" && "$PLATFORM" = "darwin" ]]; then
|
2016-05-27 18:17:06 +00:00
|
|
|
if [[ "$DISTRO" = "$DARWIN_KERNEL_VERSION" ]]; then
|
2016-01-12 03:32:33 +00:00
|
|
|
BUILD_KERNEL=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
MAKE=make
|
|
|
|
if [[ "$PLATFORM" = "freebsd" ]]; then
|
|
|
|
MAKE=gmake
|
|
|
|
fi
|
|
|
|
|
|
|
|
RUN_TESTS=$1
|
|
|
|
|
2016-04-22 18:15:26 +00:00
|
|
|
cd $LIB_SCRIPT_DIR/../
|
2016-01-12 03:32:33 +00:00
|
|
|
|
|
|
|
# Run build host provisions and install library dependencies.
|
2016-01-21 10:32:53 +00:00
|
|
|
if [[ ! -z $RUN_BUILD_DEPS ]]; then
|
|
|
|
$MAKE deps
|
|
|
|
else
|
|
|
|
initialize $DISTRO
|
|
|
|
fi
|
2016-01-12 03:32:33 +00:00
|
|
|
|
|
|
|
# Build osquery.
|
2016-11-19 10:44:23 +00:00
|
|
|
build_target
|
2016-01-12 03:32:33 +00:00
|
|
|
|
|
|
|
if [[ $BUILD_KERNEL = 1 ]]; then
|
|
|
|
# Build osquery kernel (optional).
|
|
|
|
$MAKE kernel-build
|
|
|
|
|
|
|
|
# Setup cleanup code for catastrophic test failures.
|
|
|
|
trap build_kernel_cleanup EXIT INT TERM
|
|
|
|
|
|
|
|
# Load osquery kernel (optional).
|
2016-07-21 00:21:20 +00:00
|
|
|
$MAKE kernel-test-load
|
2016-01-12 03:32:33 +00:00
|
|
|
fi
|
|
|
|
|
2016-11-19 10:44:23 +00:00
|
|
|
if [[ ! -z "$RUN_DETERMINISTIC" ]]; then
|
|
|
|
check_deterministic
|
|
|
|
fi
|
|
|
|
|
2016-01-12 03:32:33 +00:00
|
|
|
if [[ $RUN_TESTS = true ]]; then
|
|
|
|
# Run code unit and integration tests.
|
|
|
|
$MAKE test/fast
|
|
|
|
|
|
|
|
if [[ $BUILD_KERNEL = 1 ]]; then
|
|
|
|
# Run kernel unit and integration tests (optional).
|
|
|
|
$MAKE kernel-test/fast
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|