<commit_before>SET(OSQUERY_LIBS
  readline
  pthread
  dl
  crypto
  ssl
)

# Check for the explicit path to determine the version of procps
if(EXISTS "/usr/lib/libprocps.a"
    OR EXISTS "/usr/lib/x86_64-linux-gnu/libprocps.a")
  set(PROCPS TRUE)
endif()

if(APPLE)
  list(APPEND OSQUERY_LIBS "boost_thread-mt")
  list(APPEND OSQUERY_LIBS "boost_system")
  list(APPEND OSQUERY_LIBS "boost_filesystem")
  list(APPEND OSQUERY_LIBS "boost_program_options")
  list(APPEND OSQUERY_LIBS "boost_regex")
  list(APPEND OSQUERY_LIBS "thrift")
  list(APPEND OSQUERY_LIBS "rocksdb")
  list(APPEND OSQUERY_LIBS "gflags")
  list(APPEND OSQUERY_LIBS "glog")
else()
  list(APPEND OSQUERY_LIBS "rt")
  list(APPEND OSQUERY_LIBS "libgflags.a")
  list(APPEND OSQUERY_LIBS "libglog.a")
  list(APPEND OSQUERY_LIBS "librocksdb.a")
  if(EXISTS "/etc/debian_version")
    list(APPEND OSQUERY_LIBS "libz.a")
    list(APPEND OSQUERY_LIBS "libbz2.a")
    list(APPEND OSQUERY_LIBS "libsnappy.a")
    list(APPEND OSQUERY_LIBS "libunwind.a")
    list(APPEND OSQUERY_LIBS "liblzma.a")
    if(PROCPS)
      list(APPEND OSQUERY_LIBS "libprocps.a")
    else()
      list(APPEND OSQUERY_LIBS "libproc.a")
    endif()
  endif()
  if(EXISTS "/etc/redhat-release")
    list(APPEND OSQUERY_LIBS "libz.so")
    list(APPEND OSQUERY_LIBS "libbz2.so")
    list(APPEND OSQUERY_LIBS "libsnappy.so")
    list(APPEND OSQUERY_LIBS "libunwind.so")
    list(APPEND OSQUERY_LIBS "liblzma.so")
    list(APPEND OSQUERY_LIBS "libproc.so")
  endif()
  list(APPEND OSQUERY_LIBS "libthrift.a")
  list(APPEND OSQUERY_LIBS "libboost_thread.a")
  list(APPEND OSQUERY_LIBS "libboost_system.a")
  list(APPEND OSQUERY_LIBS "libboost_filesystem.a")
  list(APPEND OSQUERY_LIBS "libboost_program_options.a")
  list(APPEND OSQUERY_LIBS "libboost_regex.a")
endif()

# Fill this in with objects for libosquery
set(OSQUERY_SOURCES "")
set(OSQUERY_ADDITIONAL_LINKS "")

if(NOT BUILD_SHARED)
  SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
endif()

MACRO(ADD_OSQUERY_LINK LINK)
  list(APPEND OSQUERY_ADDITIONAL_LINKS ${LINK})
  set(OSQUERY_ADDITIONAL_LINKS ${OSQUERY_ADDITIONAL_LINKS} PARENT_SCOPE)
ENDMACRO(ADD_OSQUERY_LINK)

MACRO(SET_OSQUERY_COMPILE TARGET)
  SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS ${OS_COMPILE_FLAGS})
  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)

MACRO(ADD_OSQUERY_LIBRARY TARGET)
  ADD_LIBRARY(${TARGET} OBJECT ${ARGN})
  SET_OSQUERY_COMPILE(${TARGET})
  list(APPEND OSQUERY_SOURCES $<TARGET_OBJECTS:${TARGET}>)
  set(OSQUERY_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
ENDMACRO(ADD_OSQUERY_LIBRARY TARGET)

MACRO(ADD_OSQUERY_OBJCXX_LIBRARY TARGET)
  ADD_LIBRARY(${TARGET} OBJECT ${ARGN})
  SET_OSQUERY_COMPILE(${TARGET} "-x objective-c++ -fobjc-arc")
  list(APPEND OSQUERY_SOURCES $<TARGET_OBJECTS:${TARGET}>)
  set(OSQUERY_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
ENDMACRO(ADD_OSQUERY_OBJCXX_LIBRARY TARGET)

MACRO(ADD_OSQUERY_TEST TEST_NAME SOURCE)
  ADD_EXECUTABLE(${TEST_NAME} ${SOURCE})
  TARGET_LINK_LIBRARIES(${TEST_NAME} osquery_shared)
  TARGET_LINK_LIBRARIES(${TEST_NAME} gtest)
  SET(OPTIONAL_FLAGS ${ARGN})
  SET_OSQUERY_COMPILE(${TEST_NAME} ${OPTIONAL_FLAGS})
  ADD_TEST(${TEST_NAME} ${TEST_NAME})
ENDMACRO(ADD_OSQUERY_TEST)

MACRO(ADD_OSQUERY_EXECUTABLE NAME SOURCE)
  ADD_OSQUERY_TEST(${NAME} ${SOURCE})
  TARGET_LINK_LIBRARIES(${NAME} osquery_shared)
ENDMACRO(ADD_OSQUERY_EXECUTABLE)

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)

# Table generation

file(GLOB TABLE_FILES "tables/specs/x/*.table")
if(APPLE)
  file(GLOB TABLE_FILES_DARWIN "tables/specs/darwin/*.table")
  list(APPEND TABLE_FILES ${TABLE_FILES_DARWIN})
else()
  file(GLOB TABLE_FILES_LINUX "tables/specs/linux/*.table")
  list(APPEND TABLE_FILES ${TABLE_FILES_LINUX})
endif()

SET(GENERATED_TABLE "")

foreach(TABLE_FILE ${TABLE_FILES})
  SET(TABLE_FILE_GEN ${TABLE_FILE})
  STRING(REPLACE "linux/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  STRING(REPLACE "darwin/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  STRING(REPLACE "x/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  STRING(
    REPLACE
    "${CMAKE_SOURCE_DIR}/osquery/tables/specs"
    "${CMAKE_BINARY_DIR}/generated/tables"
    TABLE_FILE_GEN
    ${TABLE_FILE_GEN}
  )
  STRING(REPLACE ".table" ".cpp" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  ADD_CUSTOM_COMMAND(
    OUTPUT ${TABLE_FILE_GEN}
    COMMAND python ${CMAKE_SOURCE_DIR}/tools/gentable.py ${TABLE_FILE} ${TABLE_FILE_GEN}
    DEPENDS ${TABLE_FILE}
    WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  )
  list(APPEND GENERATED_TABLES ${TABLE_FILE_GEN})
endforeach()
ADD_CUSTOM_TARGET(osquery_generated_tables_depends
  DEPENDS ${GENERATED_TABLES}
)
ADD_LIBRARY(osquery_generated_tables OBJECT
  ${GENERATED_TABLES}
)
ADD_DEPENDENCIES(osquery_generated_tables osquery_generated_tables_depends)
SET_OSQUERY_COMPILE(osquery_generated_tables)

# Add subdirectories

ADD_SUBDIRECTORY(config)
ADD_SUBDIRECTORY(core)
ADD_SUBDIRECTORY(database)
ADD_SUBDIRECTORY(devtools)
ADD_SUBDIRECTORY(events)
ADD_SUBDIRECTORY(examples)
ADD_SUBDIRECTORY(filesystem)
ADD_SUBDIRECTORY(logger)
ADD_SUBDIRECTORY(dispatcher)
ADD_SUBDIRECTORY(registry)
ADD_SUBDIRECTORY(scheduler)
ADD_SUBDIRECTORY(tables)

SET(OSQUERY_LIB_OBJECTS
  # Third-party objects
  $<TARGET_OBJECTS:osquery_sqlite>

  # Generated code
  $<TARGET_OBJECTS:osquery_generated_tables>

  # Build additional os-dependent libraries (may include tables)
  ${OSQUERY_SOURCES}
)

if(BUILD_SHARED)
  ADD_LIBRARY(osquery_shared SHARED main/lib.cpp ${OSQUERY_LIB_OBJECTS})
else()
  ADD_LIBRARY(osquery_static STATIC ${OSQUERY_LIB_OBJECTS})
  SET_TARGET_PROPERTIES(osquery_static PROPERTIES OUTPUT_NAME osquery_static)

  ADD_LIBRARY(osquery_shared STATIC main/lib.cpp)
  TARGET_OSQUERY_LINK_WHOLE(osquery_shared osquery_static)
endif()
TARGET_LINK_LIBRARIES(osquery_shared ${OSQUERY_ADDITIONAL_LINKS})
TARGET_LINK_LIBRARIES(osquery_shared ${OSQUERY_LIBS})
SET_TARGET_PROPERTIES(osquery_shared PROPERTIES OUTPUT_NAME osquery)

ADD_EXECUTABLE(shell main/shell.cpp)
TARGET_LINK_LIBRARIES(shell osquery_shared)
SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME osqueryi)<commit_msg>Closes #319, Install libraries and headers.<commit_after>SET(OSQUERY_LIBS
  readline
  pthread
  dl
  crypto
  ssl
)

# Check for the explicit path to determine the version of procps
if(EXISTS "/usr/lib/libprocps.a"
    OR EXISTS "/usr/lib/x86_64-linux-gnu/libprocps.a")
  set(PROCPS TRUE)
endif()

if(APPLE)
  list(APPEND OSQUERY_LIBS "boost_thread-mt")
  list(APPEND OSQUERY_LIBS "boost_system")
  list(APPEND OSQUERY_LIBS "boost_filesystem")
  list(APPEND OSQUERY_LIBS "boost_program_options")
  list(APPEND OSQUERY_LIBS "boost_regex")
  list(APPEND OSQUERY_LIBS "thrift")
  list(APPEND OSQUERY_LIBS "rocksdb")
  list(APPEND OSQUERY_LIBS "gflags")
  list(APPEND OSQUERY_LIBS "glog")
else()
  list(APPEND OSQUERY_LIBS "rt")
  list(APPEND OSQUERY_LIBS "libgflags.a")
  list(APPEND OSQUERY_LIBS "libglog.a")
  list(APPEND OSQUERY_LIBS "librocksdb.a")
  if(EXISTS "/etc/debian_version")
    list(APPEND OSQUERY_LIBS "libz.a")
    list(APPEND OSQUERY_LIBS "libbz2.a")
    list(APPEND OSQUERY_LIBS "libsnappy.a")
    list(APPEND OSQUERY_LIBS "libunwind.a")
    list(APPEND OSQUERY_LIBS "liblzma.a")
    if(PROCPS)
      list(APPEND OSQUERY_LIBS "libprocps.a")
    else()
      list(APPEND OSQUERY_LIBS "libproc.a")
    endif()
  endif()
  if(EXISTS "/etc/redhat-release")
    list(APPEND OSQUERY_LIBS "libz.so")
    list(APPEND OSQUERY_LIBS "libbz2.so")
    list(APPEND OSQUERY_LIBS "libsnappy.so")
    list(APPEND OSQUERY_LIBS "libunwind.so")
    list(APPEND OSQUERY_LIBS "liblzma.so")
    list(APPEND OSQUERY_LIBS "libproc.so")
  endif()
  list(APPEND OSQUERY_LIBS "libthrift.a")
  list(APPEND OSQUERY_LIBS "libboost_thread.a")
  list(APPEND OSQUERY_LIBS "libboost_system.a")
  list(APPEND OSQUERY_LIBS "libboost_filesystem.a")
  list(APPEND OSQUERY_LIBS "libboost_program_options.a")
  list(APPEND OSQUERY_LIBS "libboost_regex.a")
endif()

# Fill this in with objects for libosquery
set(OSQUERY_SOURCES "")
set(OSQUERY_ADDITIONAL_LINKS "")

if(NOT BUILD_SHARED)
  SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
endif()

MACRO(ADD_OSQUERY_LINK LINK)
  list(APPEND OSQUERY_ADDITIONAL_LINKS ${LINK})
  set(OSQUERY_ADDITIONAL_LINKS ${OSQUERY_ADDITIONAL_LINKS} PARENT_SCOPE)
ENDMACRO(ADD_OSQUERY_LINK)

MACRO(SET_OSQUERY_COMPILE TARGET)
  SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS ${OS_COMPILE_FLAGS})
  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)

MACRO(ADD_OSQUERY_LIBRARY TARGET)
  ADD_LIBRARY(${TARGET} OBJECT ${ARGN})
  SET_OSQUERY_COMPILE(${TARGET})
  list(APPEND OSQUERY_SOURCES $<TARGET_OBJECTS:${TARGET}>)
  set(OSQUERY_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
ENDMACRO(ADD_OSQUERY_LIBRARY TARGET)

MACRO(ADD_OSQUERY_OBJCXX_LIBRARY TARGET)
  ADD_LIBRARY(${TARGET} OBJECT ${ARGN})
  SET_OSQUERY_COMPILE(${TARGET} "-x objective-c++ -fobjc-arc")
  list(APPEND OSQUERY_SOURCES $<TARGET_OBJECTS:${TARGET}>)
  set(OSQUERY_SOURCES ${OSQUERY_SOURCES} PARENT_SCOPE)
ENDMACRO(ADD_OSQUERY_OBJCXX_LIBRARY TARGET)

MACRO(ADD_OSQUERY_TEST TEST_NAME SOURCE)
  ADD_EXECUTABLE(${TEST_NAME} ${SOURCE})
  TARGET_LINK_LIBRARIES(${TEST_NAME} osquery_shared)
  TARGET_LINK_LIBRARIES(${TEST_NAME} gtest)
  SET(OPTIONAL_FLAGS ${ARGN})
  SET_OSQUERY_COMPILE(${TEST_NAME} ${OPTIONAL_FLAGS})
  ADD_TEST(${TEST_NAME} ${TEST_NAME})
ENDMACRO(ADD_OSQUERY_TEST)

MACRO(ADD_OSQUERY_EXECUTABLE NAME SOURCE)
  ADD_OSQUERY_TEST(${NAME} ${SOURCE})
  TARGET_LINK_LIBRARIES(${NAME} osquery_shared)
ENDMACRO(ADD_OSQUERY_EXECUTABLE)

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)

# Table generation

file(GLOB TABLE_FILES "tables/specs/x/*.table")
if(APPLE)
  file(GLOB TABLE_FILES_DARWIN "tables/specs/darwin/*.table")
  list(APPEND TABLE_FILES ${TABLE_FILES_DARWIN})
else()
  file(GLOB TABLE_FILES_LINUX "tables/specs/linux/*.table")
  list(APPEND TABLE_FILES ${TABLE_FILES_LINUX})
endif()

SET(GENERATED_TABLE "")

foreach(TABLE_FILE ${TABLE_FILES})
  SET(TABLE_FILE_GEN ${TABLE_FILE})
  STRING(REPLACE "linux/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  STRING(REPLACE "darwin/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  STRING(REPLACE "x/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  STRING(
    REPLACE
    "${CMAKE_SOURCE_DIR}/osquery/tables/specs"
    "${CMAKE_BINARY_DIR}/generated/tables"
    TABLE_FILE_GEN
    ${TABLE_FILE_GEN}
  )
  STRING(REPLACE ".table" ".cpp" TABLE_FILE_GEN ${TABLE_FILE_GEN})
  ADD_CUSTOM_COMMAND(
    OUTPUT ${TABLE_FILE_GEN}
    COMMAND python ${CMAKE_SOURCE_DIR}/tools/gentable.py ${TABLE_FILE} ${TABLE_FILE_GEN}
    DEPENDS ${TABLE_FILE}
    WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  )
  list(APPEND GENERATED_TABLES ${TABLE_FILE_GEN})
endforeach()
ADD_CUSTOM_TARGET(osquery_generated_tables_depends
  DEPENDS ${GENERATED_TABLES}
)
ADD_LIBRARY(osquery_generated_tables OBJECT
  ${GENERATED_TABLES}
)
ADD_DEPENDENCIES(osquery_generated_tables osquery_generated_tables_depends)
SET_OSQUERY_COMPILE(osquery_generated_tables)

# Add subdirectories

ADD_SUBDIRECTORY(config)
ADD_SUBDIRECTORY(core)
ADD_SUBDIRECTORY(database)
ADD_SUBDIRECTORY(devtools)
ADD_SUBDIRECTORY(events)
ADD_SUBDIRECTORY(examples)
ADD_SUBDIRECTORY(filesystem)
ADD_SUBDIRECTORY(logger)
ADD_SUBDIRECTORY(dispatcher)
ADD_SUBDIRECTORY(registry)
ADD_SUBDIRECTORY(scheduler)
ADD_SUBDIRECTORY(tables)

SET(OSQUERY_LIB_OBJECTS
  # Third-party objects
  $<TARGET_OBJECTS:osquery_sqlite>

  # Generated code
  $<TARGET_OBJECTS:osquery_generated_tables>

  # Build additional os-dependent libraries (may include tables)
  ${OSQUERY_SOURCES}
)

if(BUILD_SHARED)
  ADD_LIBRARY(osquery_shared SHARED main/lib.cpp ${OSQUERY_LIB_OBJECTS})
else()
  ADD_LIBRARY(osquery_static STATIC ${OSQUERY_LIB_OBJECTS})
  SET_TARGET_PROPERTIES(osquery_static PROPERTIES OUTPUT_NAME osquery_static)

  ADD_LIBRARY(osquery_shared STATIC main/lib.cpp)
  TARGET_OSQUERY_LINK_WHOLE(osquery_shared osquery_static)
endif()
TARGET_LINK_LIBRARIES(osquery_shared ${OSQUERY_ADDITIONAL_LINKS})
TARGET_LINK_LIBRARIES(osquery_shared ${OSQUERY_LIBS})
SET_TARGET_PROPERTIES(osquery_shared PROPERTIES OUTPUT_NAME osquery)
INSTALL(TARGETS osquery_shared DESTINATION lib)
INSTALL(TARGETS osquery_static DESTINATION lib)
INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include)

ADD_EXECUTABLE(shell main/shell.cpp)
TARGET_LINK_LIBRARIES(shell osquery_shared)
SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME osqueryi)
{'fraction_non_alphanumeric': 0.10626042140366833, 'fraction_numerical': 0.0012884644535394877, 'mean_word_length': 7.377777777777778, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4974116', 'n_tokens_mistral': 5428, 'n_tokens_neox': 5284, 'n_words': 772}