Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion GetArchitecture.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
# We run uname ourselves here as CMAKE by default uses -p rather than
# -m.

execute_process(COMMAND uname -m OUTPUT_VARIABLE arch OUTPUT_STRIP_TRAILING_WHITESPACE)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
# On Windows, uname may not be available; use CMake's built-in variable.
# Normalize AMD64 to x86_64 for consistency with uname -m used by
# external plugin builds running under Git Bash.
set(arch "${CMAKE_HOST_SYSTEM_PROCESSOR}")
if (arch STREQUAL "AMD64")
set(arch "x86_64")
endif ()
else ()
execute_process(COMMAND uname -m OUTPUT_VARIABLE arch OUTPUT_STRIP_TRAILING_WHITESPACE)
endif ()
set(HOST_ARCHITECTURE "${CMAKE_SYSTEM_NAME}-${arch}")
string(TOLOWER ${HOST_ARCHITECTURE} HOST_ARCHITECTURE)
14 changes: 12 additions & 2 deletions ZeekPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ function (zeek_plugin_bootstrapping)
set(_zeek_build_dir "$ENV{ZEEK_BUILD_DIR}")
endif ()
find_package(Zeek REQUIRED CONFIG NO_DEFAULT_PATH PATHS "${_zeek_build_dir}")

# Match the plugin build type to Zeek's. This ensures plugins link
# against the same CRT and are ABI-compatible with the Zeek binary.
if (ZEEK_CMAKE_BUILD_TYPE)
message(STATUS "Setting plugin CMAKE_BUILD_TYPE to ${ZEEK_CMAKE_BUILD_TYPE}")
set(CMAKE_BUILD_TYPE "${ZEEK_CMAKE_BUILD_TYPE}"
CACHE STRING "Configures the CMAKE_BUILD_TYPE for the plugin." FORCE)
endif ()

return()
endif ()
# When building plugins against an installed Zeek, this file must be installed
Expand Down Expand Up @@ -59,8 +68,9 @@ function (zeek_plugin_bootstrapping)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON
CACHE PATH "Configures whether to write a compile database." FORCE)

# When CMAKE_BUILD_TYPE is not set, use the one from Zeek.
if (NOT CMAKE_BUILD_TYPE)
# Match the plugin build type to Zeek's. This ensures plugins link against
# the same CRT and are ABI-compatible with the Zeek binary.
if (ZEEK_CMAKE_BUILD_TYPE)
message(STATUS "Setting plugin CMAKE_BUILD_TYPE to ${ZEEK_CMAKE_BUILD_TYPE}")
set(CMAKE_BUILD_TYPE "${ZEEK_CMAKE_BUILD_TYPE}"
CACHE STRING "Configures the CMAKE_BUILD_TYPE for the plugin." FORCE)
Expand Down
81 changes: 74 additions & 7 deletions ZeekPluginDynamic.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
include(GetArchitecture)
include(RequireCXXStd)

# On Windows, shell scripts (.sh) need to be invoked through bash.
if (WIN32 AND NOT ZEEK_PLUGIN_SHELL_PREFIX)
find_program(_bash_exe bash)
if (_bash_exe)
set(ZEEK_PLUGIN_SHELL_PREFIX "${_bash_exe}" CACHE INTERNAL "Shell prefix for .sh scripts")
endif ()
endif ()
if (NOT DEFINED ZEEK_PLUGIN_SHELL_PREFIX)
set(ZEEK_PLUGIN_SHELL_PREFIX "" CACHE INTERNAL "Shell prefix for .sh scripts")
endif ()

# Sets `target` to contain the CMake target name for a dynamic plugin.
macro (zeek_get_dynamic_plugin_target target ns name)
set(${target} "${ns}_${name}")
Expand Down Expand Up @@ -34,6 +45,32 @@ function (zeek_add_dynamic_plugin ns name)
set_target_properties(${target_name} PROPERTIES CXX_EXTENSIONS OFF)
endif ()

# On MSVC with static CRT, each DLL has its own stdout buffer.
# Generate a DllMain that disables stdout buffering so plugin
# printf output appears in correct order when stdout is redirected.
if (MSVC)
set(_dllmain_src "${CMAKE_CURRENT_BINARY_DIR}/zeek_plugin_dllmain.cc")
if (NOT EXISTS "${_dllmain_src}")
file(
WRITE "${_dllmain_src}"
"#include <cstdio>\n"
"// With static CRT (/MT), each DLL has its own stdout.\n"
"// Set it to unbuffered so output is correctly ordered,\n"
"// and close it at shutdown to prevent the DLL CRT's\n"
"// atexit handler from corrupting the shared fd.\n"
"namespace { struct ZeekPluginStdoutInit {\n"
" ZeekPluginStdoutInit() {\n"
" setvbuf(stdout, nullptr, _IONBF, 0);\n"
" }\n"
" ~ZeekPluginStdoutInit() {\n"
" fclose(stdout);\n"
" fclose(stderr);\n"
" }\n"
"} zeek_plugin_stdout_init; }\n")
endif ()
target_sources(${target_name} PRIVATE "${_dllmain_src}")
endif ()

# Place library file into the 'lib' directory, drop default-generated file
# prefix and override the default file name to include the architecture.
set_target_properties(
Expand Down Expand Up @@ -99,6 +136,20 @@ function (zeek_add_dynamic_plugin ns name)
# Add extra dependencies when compiling with MSVC.
if (MSVC)
target_link_libraries(${target_name} PRIVATE ws2_32)
# Force the plugin DLL to import CRT allocation functions from
# zeek.exe. With /MT, each module has its own heap; importing
# operator new/delete ensures all allocations use the same heap,
# preventing cross-module heap mismatches when objects are created
# in plugin code and freed by zeek or vice versa.
target_link_options(
${target_name}
PRIVATE
"/INCLUDE:??2@YAPEAX_K@Z"
"/INCLUDE:??_U@YAPEAX_K@Z"
"/INCLUDE:??3@YAXPEAX@Z"
"/INCLUDE:??3@YAXPEAX_K@Z"
"/INCLUDE:??_V@YAXPEAX@Z"
"/INCLUDE:??_V@YAXPEAX_K@Z")
endif ()

# Pass compiler flags, paths and dependencies to the target.
Expand Down Expand Up @@ -179,7 +230,8 @@ function (zeek_add_dynamic_plugin ns name)

add_custom_command(
OUTPUT ${dist_tarball_path}
COMMAND ${ZEEK_PLUGIN_SCRIPTS_PATH}/zeek-plugin-create-package.sh ${canon_name}
COMMAND ${ZEEK_PLUGIN_SHELL_PREFIX}
${ZEEK_PLUGIN_SCRIPTS_PATH}/zeek-plugin-create-package.sh ${canon_name}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${target_name} ${FN_ARGS_SCRIPT_FILES}
APPEND)
Expand All @@ -188,10 +240,25 @@ function (zeek_add_dynamic_plugin ns name)

# Tell CMake to install our tarball. Note: This usually runs from our
# plugin-support skeleton.
install(
CODE "execute_process(
COMMAND ${ZEEK_PLUGIN_SCRIPTS_PATH}/zeek-plugin-install-package.sh ${canon_name} \$ENV{DESTDIR}/${install_dir}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND_ECHO STDOUT
)")
if (ZEEK_PLUGIN_SHELL_PREFIX)
install(
CODE "
if (DEFINED ENV{DESTDIR})
set(_inst_dir \"\$ENV{DESTDIR}/${install_dir}\")
else ()
set(_inst_dir \"${install_dir}\")
endif ()
execute_process(
COMMAND \"${ZEEK_PLUGIN_SHELL_PREFIX}\" \"${ZEEK_PLUGIN_SCRIPTS_PATH}/zeek-plugin-install-package.sh\" ${canon_name} \"\${_inst_dir}\"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND_ECHO STDOUT
)")
else ()
install(
CODE "execute_process(
COMMAND ${ZEEK_PLUGIN_SCRIPTS_PATH}/zeek-plugin-install-package.sh ${canon_name} \$ENV{DESTDIR}/${install_dir}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND_ECHO STDOUT
)")
endif ()
endfunction ()
2 changes: 1 addition & 1 deletion zeek-plugin-create-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ tar czf "dist/${tgz}" -C dist "${name}"

rm -rf "${DIST}"

ln -s "dist/${tgz}" "${name}.tgz"
ln -s "dist/${tgz}" "${name}.tgz" 2>/dev/null || cp "dist/${tgz}" "${name}.tgz"
Loading