2014-12-23 22:35:48 +00:00
|
|
|
# -*- mode: cmake; -*-
|
2014-12-31 16:28:08 +00:00
|
|
|
# - osquery macro definitions
|
2014-12-23 22:35:48 +00:00
|
|
|
#
|
2014-12-31 16:28:08 +00:00
|
|
|
# Remove boilerplate code for linking the osquery core dependent libs
|
|
|
|
# compiling and handling static or dynamic (run time load) libs.
|
|
|
|
|
|
|
|
# osquery-specific helper macros
|
|
|
|
macro(SET_OSQUERY_COMPILE TARGET)
|
|
|
|
set(OPTIONAL_FLAGS ${ARGN})
|
|
|
|
list(LENGTH OPTIONAL_FLAGS NUM_OPTIONAL_FLAGS)
|
|
|
|
if(${NUM_OPTIONAL_FLAGS} GREATER 0)
|
|
|
|
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${OPTIONAL_FLAGS})
|
|
|
|
endif()
|
|
|
|
endmacro(SET_OSQUERY_COMPILE)
|
|
|
|
|
2015-02-03 05:21:36 +00:00
|
|
|
macro(ADD_OSQUERY_TEST IS_CORE TEST_NAME SOURCE)
|
|
|
|
if(NOT DEFINED ENV{SKIP_TESTS} AND (${IS_CORE} OR NOT OSQUERY_BUILD_SDK_ONLY))
|
2014-12-31 16:28:08 +00:00
|
|
|
add_executable(${TEST_NAME} ${SOURCE})
|
2015-02-03 05:21:36 +00:00
|
|
|
TARGET_OSQUERY_LINK_WHOLE(${TEST_NAME} libosquery)
|
|
|
|
set(TEST_LINK_ADDITIONAL ${ARGN})
|
|
|
|
if(NOT ${IS_CORE})
|
2015-01-07 20:29:24 +00:00
|
|
|
target_link_libraries(${TEST_NAME} libosquery_additional)
|
|
|
|
endif()
|
2014-12-31 16:28:08 +00:00
|
|
|
target_link_libraries(${TEST_NAME} gtest)
|
2015-02-04 03:55:16 +00:00
|
|
|
SET_OSQUERY_COMPILE(${TEST_NAME} "${OPTIONAL_FLAGS} -DGTEST_HAS_TR1_TUPLE=0")
|
2014-12-31 16:28:08 +00:00
|
|
|
add_test(${TEST_NAME} ${TEST_NAME})
|
|
|
|
endif()
|
|
|
|
endmacro(ADD_OSQUERY_TEST)
|
|
|
|
|
|
|
|
# Core/non core link helping macros (tell the build to link ALL).
|
2015-02-03 05:21:36 +00:00
|
|
|
macro(ADD_OSQUERY_LINK IS_CORE LINK)
|
|
|
|
if(${IS_CORE})
|
|
|
|
ADD_OSQUERY_LINK_INTERNAL("${LINK}" "${ARGN}" OSQUERY_LINKS)
|
|
|
|
elseif(NOT OSQUERY_BUILD_SDK_ONLY)
|
|
|
|
ADD_OSQUERY_LINK_INTERNAL("${LINK}" "${ARGN}" OSQUERY_ADDITIONAL_LINKS)
|
|
|
|
endif()
|
2014-12-31 16:28:08 +00:00
|
|
|
endmacro(ADD_OSQUERY_LINK)
|
|
|
|
|
2015-01-05 16:05:41 +00:00
|
|
|
macro(ADD_OSQUERY_LINK_INTERNAL LINK LINK_PATHS LINK_SET)
|
|
|
|
if(NOT "${LINK}" MATCHES "(^-.*)")
|
|
|
|
find_library("${LINK}_library" NAMES "lib${LINK}.a" "${LINK}" ${LINK_PATHS})
|
2015-01-07 19:02:40 +00:00
|
|
|
message("-- Found library dependency ${${LINK}_library}")
|
2015-01-05 16:05:41 +00:00
|
|
|
if("${${LINK}_library}" STREQUAL "${${LINK}_library}-NOTFOUND")
|
|
|
|
string(ASCII 27 Esc)
|
|
|
|
message(WARNING "${Esc}[31mDependent library '${LINK}' not found${Esc}[m")
|
|
|
|
list(APPEND ${LINK_SET} ${LINK})
|
|
|
|
else()
|
|
|
|
list(APPEND ${LINK_SET} "${${LINK}_library}")
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
list(APPEND ${LINK_SET} ${LINK})
|
|
|
|
endif()
|
|
|
|
set(${LINK_SET} "${${LINK_SET}}" PARENT_SCOPE)
|
|
|
|
endmacro(ADD_OSQUERY_LINK_INTERNAL)
|
|
|
|
|
2014-12-31 16:28:08 +00:00
|
|
|
# Core/non core lists of target source files.
|
2015-02-03 05:21:36 +00:00
|
|
|
macro(ADD_OSQUERY_LIBRARY IS_CORE TARGET)
|
|
|
|
if(${IS_CORE} OR NOT OSQUERY_BUILD_SDK_ONLY)
|
|
|
|
add_library(${TARGET} OBJECT ${ARGN})
|
|
|
|
add_dependencies(${TARGET} libglog)
|
|
|
|
SET_OSQUERY_COMPILE(${TARGET} "-static")
|
|
|
|
if(${IS_CORE})
|
|
|
|
list(APPEND OSQUERY_SOURCES $<TARGET_OBJECTS:${TARGET}>)
|
|
|
|
set(OSQUERY_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
list(APPEND OSQUERY_ADDITIONAL_SOURCES $<TARGET_OBJECTS:${TARGET}>)
|
|
|
|
set(OSQUERY_ADDITIONAL_SOURCES ${OSQUERY_ADDITIONAL_SOURCES} PARENT_SCOPE)
|
|
|
|
endif()
|
|
|
|
endif()
|
2014-12-31 16:28:08 +00:00
|
|
|
endmacro(ADD_OSQUERY_LIBRARY TARGET)
|
|
|
|
|
|
|
|
# Core/non core lists of target source files compiled as ObjC++.
|
2015-02-03 05:21:36 +00:00
|
|
|
macro(ADD_OSQUERY_OBJCXX_LIBRARY IS_CORE TARGET)
|
|
|
|
if(${IS_CORE} OR NOT OSQUERY_BUILD_SDK_ONLY)
|
|
|
|
add_library(${TARGET} OBJECT ${ARGN})
|
|
|
|
add_dependencies(${TARGET} libglog)
|
|
|
|
SET_OSQUERY_COMPILE(${TARGET} "${C_COMPILE_FLAGS} ${OBJCXX_COMPILE_FLAGS} -static")
|
|
|
|
if(${IS_CORE})
|
|
|
|
list(APPEND OSQUERY_SOURCES $<TARGET_OBJECTS:${TARGET}>)
|
|
|
|
set(OSQUERY_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
list(APPEND OSQUERY_ADDITIONAL_SOURCES $<TARGET_OBJECTS:${TARGET}>)
|
|
|
|
set(OSQUERY_ADDITIONAL_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
|
|
|
|
endif()
|
|
|
|
endif()
|
2014-12-31 16:28:08 +00:00
|
|
|
endmacro(ADD_OSQUERY_OBJCXX_LIBRARY TARGET)
|
|
|
|
|
|
|
|
# Helper to abstract OS/Compiler whole linking.
|
|
|
|
macro(TARGET_OSQUERY_LINK_WHOLE TARGET OSQUERY_LIB)
|
|
|
|
target_link_libraries(${TARGET} "${OS_WHOLELINK_PRE}")
|
|
|
|
target_link_libraries(${TARGET} ${OSQUERY_LIB})
|
|
|
|
target_link_libraries(${TARGET} "${OS_WHOLELINK_POST}")
|
|
|
|
endmacro(TARGET_OSQUERY_LINK_WHOLE)
|
2014-12-23 22:35:48 +00:00
|
|
|
|
2014-12-24 04:07:12 +00:00
|
|
|
set(GLOBAL PROPERTY AMALGAMATE_TARGETS "")
|
|
|
|
macro(GET_GENERATION_DEPS BASE_PATH)
|
|
|
|
# Depend on the generation code.
|
|
|
|
set(GENERATION_DEPENDENCIES "")
|
|
|
|
file(GLOB TABLE_FILES_TEMPLATES "${BASE_PATH}/osquery/tables/templates/*.in")
|
|
|
|
set(GENERATION_DEPENDENCIES
|
|
|
|
"${BASE_PATH}/tools/codegen/*.py"
|
|
|
|
"${BASE_PATH}/osquery/tables/specs/blacklist"
|
|
|
|
)
|
|
|
|
list(APPEND GENERATION_DEPENDENCIES ${TABLE_FILES_TEMPLATES})
|
|
|
|
endmacro()
|
|
|
|
|
2014-12-23 22:35:48 +00:00
|
|
|
# Find and generate table plugins from .table syntax
|
2014-12-24 04:07:12 +00:00
|
|
|
macro(GENERATE_TABLES TABLES_PATH BASE_PATH)
|
2014-12-25 20:46:59 +00:00
|
|
|
# Get all matching files for all platforms.
|
2014-12-23 22:35:48 +00:00
|
|
|
file(GLOB TABLE_FILES "${TABLES_PATH}/specs/x/*.table")
|
2014-12-25 20:46:59 +00:00
|
|
|
set(TABLE_FILES_PLATFORM "")
|
2014-12-23 22:35:48 +00:00
|
|
|
if(APPLE)
|
2014-12-24 04:07:12 +00:00
|
|
|
file(GLOB TABLE_FILES_PLATFORM "${TABLES_PATH}/specs/darwin/*.table")
|
2014-12-23 22:35:48 +00:00
|
|
|
elseif(FREEBSD)
|
2014-12-24 04:07:12 +00:00
|
|
|
file(GLOB TABLE_FILES_PLATFORM "${TABLES_PATH}/specs/freebsd/*.table")
|
2014-12-31 17:33:19 +00:00
|
|
|
else(LINUX)
|
2014-12-24 04:07:12 +00:00
|
|
|
file(GLOB TABLE_FILES_PLATFORM "${TABLES_PATH}/specs/linux/*.table")
|
2014-12-31 17:33:19 +00:00
|
|
|
if(CENTOS)
|
2015-01-11 07:24:52 +00:00
|
|
|
file(GLOB TABLE_FILES_PLATFORM_FLAVOR "${TABLES_PATH}/specs/centos/*.table")
|
2014-12-31 17:33:19 +00:00
|
|
|
elseif(UBUNTU)
|
2015-01-11 07:24:52 +00:00
|
|
|
file(GLOB TABLE_FILES_PLATFORM_FLAVOR "${TABLES_PATH}/specs/ubuntu/*.table")
|
2014-12-31 17:33:19 +00:00
|
|
|
endif()
|
2015-01-11 07:24:52 +00:00
|
|
|
list(APPEND TABLE_FILES_PLATFORM ${TABLE_FILES_PLATFORM_FLAVOR})
|
2014-12-23 22:35:48 +00:00
|
|
|
endif()
|
2014-12-24 04:07:12 +00:00
|
|
|
list(APPEND TABLE_FILES ${TABLE_FILES_PLATFORM})
|
2014-12-23 22:35:48 +00:00
|
|
|
|
2014-12-25 20:46:59 +00:00
|
|
|
get_property(TARGETS GLOBAL PROPERTY AMALGAMATE_TARGETS)
|
|
|
|
set(NEW_TARGETS "")
|
2014-12-24 04:07:12 +00:00
|
|
|
foreach(TABLE_FILE ${TABLE_FILES})
|
2014-12-25 20:46:59 +00:00
|
|
|
list(FIND TARGETS "${TABLE_FILE}" INDEX)
|
|
|
|
if (${INDEX} EQUAL -1)
|
|
|
|
# Do not set duplicate targets.
|
|
|
|
list(APPEND NEW_TARGETS "${TABLE_FILE}")
|
|
|
|
endif()
|
2014-12-24 04:07:12 +00:00
|
|
|
endforeach()
|
2014-12-25 20:46:59 +00:00
|
|
|
set_property(GLOBAL PROPERTY AMALGAMATE_TARGETS "${NEW_TARGETS}")
|
2014-12-24 04:07:12 +00:00
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(GENERATE_UTILITY TABLE_NAME)
|
|
|
|
set(TABLE_SPEC "${CMAKE_SOURCE_DIR}/osquery/tables/specs/x/${TABLE_NAME}.table")
|
|
|
|
set_property(GLOBAL APPEND PROPERTY AMALGAMATE_TARGETS "${TABLE_SPEC}")
|
|
|
|
endmacro()
|
|
|
|
|
2014-12-25 20:46:59 +00:00
|
|
|
macro(GENERATE_TABLE TABLE_FILE NAME BASE_PATH OUTPUT)
|
2014-12-24 04:38:16 +00:00
|
|
|
set(TABLE_FILE_GEN ${TABLE_FILE})
|
2015-01-07 19:02:40 +00:00
|
|
|
string(REGEX REPLACE
|
2014-12-31 17:33:19 +00:00
|
|
|
".*/specs/.*/(.*)\\.table"
|
|
|
|
"${CMAKE_BINARY_DIR}/generated/tables_${NAME}/\\1.cpp"
|
2014-12-24 04:07:12 +00:00
|
|
|
TABLE_FILE_GEN
|
|
|
|
${TABLE_FILE_GEN}
|
2014-12-23 22:35:48 +00:00
|
|
|
)
|
|
|
|
|
2014-12-24 04:07:12 +00:00
|
|
|
GET_GENERATION_DEPS(${BASE_PATH})
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT "${TABLE_FILE_GEN}"
|
|
|
|
COMMAND python "${BASE_PATH}/tools/codegen/gentable.py"
|
|
|
|
"${TABLE_FILE}" "${TABLE_FILE_GEN}" "$ENV{DISABLE_BLACKLIST}"
|
|
|
|
DEPENDS ${TABLE_FILE} ${GENERATION_DEPENDENCIES}
|
|
|
|
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
|
|
|
|
)
|
2014-12-23 22:35:48 +00:00
|
|
|
|
2014-12-24 04:07:12 +00:00
|
|
|
list(APPEND ${OUTPUT} "${TABLE_FILE_GEN}")
|
|
|
|
endmacro()
|
|
|
|
|
2014-12-24 04:53:59 +00:00
|
|
|
macro(AMALGAMATE BASE_PATH NAME OUTPUT)
|
2014-12-24 04:07:12 +00:00
|
|
|
GET_GENERATION_DEPS(${BASE_PATH})
|
|
|
|
get_property(TARGETS GLOBAL PROPERTY AMALGAMATE_TARGETS)
|
|
|
|
|
|
|
|
set(GENERATED_TARGETS "")
|
|
|
|
foreach(TARGET ${TARGETS})
|
2014-12-25 20:46:59 +00:00
|
|
|
GENERATE_TABLE(${TARGET} ${NAME} ${BASE_PATH} GENERATED_TARGETS)
|
2014-12-23 22:35:48 +00:00
|
|
|
endforeach()
|
|
|
|
|
2014-12-25 20:46:59 +00:00
|
|
|
# Include the generated folder in make clean.
|
|
|
|
set_directory_properties(PROPERTY
|
|
|
|
ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_BINARY_DIR}/generated")
|
|
|
|
|
2014-12-23 22:35:48 +00:00
|
|
|
# Append all of the code to a single amalgamation.
|
2014-12-24 04:07:12 +00:00
|
|
|
add_custom_command(
|
2014-12-25 20:46:59 +00:00
|
|
|
OUTPUT "${CMAKE_BINARY_DIR}/generated/${NAME}_amalgamation.cpp"
|
2014-12-24 04:07:12 +00:00
|
|
|
COMMAND python "${BASE_PATH}/tools/codegen/amalgamate.py"
|
2014-12-24 04:53:59 +00:00
|
|
|
"${BASE_PATH}/osquery/tables/" "${CMAKE_BINARY_DIR}/generated" "${NAME}"
|
2014-12-24 04:07:12 +00:00
|
|
|
DEPENDS ${GENERATED_TARGETS} ${GENERATION_DEPENDENCIES}
|
2014-12-23 22:35:48 +00:00
|
|
|
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
|
|
|
|
)
|
|
|
|
|
2014-12-25 20:46:59 +00:00
|
|
|
set(${OUTPUT} "${CMAKE_BINARY_DIR}/generated/${NAME}_amalgamation.cpp")
|
2014-12-24 04:07:12 +00:00
|
|
|
endmacro()
|
2015-01-08 23:53:51 +00:00
|
|
|
|
|
|
|
function(JOIN VALUES GLUE OUTPUT)
|
2015-02-04 03:55:16 +00:00
|
|
|
string(REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")
|
|
|
|
set(${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
|
2015-01-08 23:53:51 +00:00
|
|
|
endfunction(JOIN)
|