@@ -283,6 +283,180 @@ macro(ADD_OSQUERY_EXTENSION TARGET)
283
283
set_target_properties (${TARGET} PROPERTIES OUTPUT_NAME "${TARGET} .ext" )
284
284
endmacro (ADD_OSQUERY_EXTENSION )
285
285
286
+ function (add_osquery_extension_ex class_name extension_type extension_name ${ARGN} )
287
+ # Make sure the extension type is valid
288
+ if (NOT "${extension_type} " STREQUAL "config" AND NOT "${extension_type} " STREQUAL "table" )
289
+ message (FATAL_ERROR "Invalid extension type specified" )
290
+ endif ()
291
+
292
+ # Update the initializer list; this will be added to the main.cpp file of the extension
293
+ # group
294
+ set_property (GLOBAL APPEND_STRING
295
+ PROPERTY OSQUERY_EXTENSION_GROUP_INITIALIZERS
296
+ "REGISTER_EXTERNAL(${class_name} , \" ${extension_type} \" , \" ${extension_name} \" );\n "
297
+ )
298
+
299
+ # Loop through each argument
300
+ foreach (argument ${ARGN} )
301
+ if ("${argument} " STREQUAL "SOURCES" OR "${argument} " STREQUAL "LIBRARIES" OR
302
+ "${argument} " STREQUAL "INCLUDEDIRS" OR "${argument} " STREQUAL "MAININCLUDES" )
303
+
304
+ set (current_scope "${argument} " )
305
+ continue ()
306
+ endif ()
307
+
308
+ if ("${current_scope} " STREQUAL "SOURCES" )
309
+ if (NOT IS_ABSOLUTE "${argument} " )
310
+ set (argument "${CMAKE_CURRENT_SOURCE_DIR} /${argument} " )
311
+ endif ()
312
+
313
+ list (APPEND source_file_list "${argument} " )
314
+
315
+ elseif ("${current_scope} " STREQUAL "INCLUDEDIRS" )
316
+ if (NOT IS_ABSOLUTE "${argument} " )
317
+ set (argument "${CMAKE_CURRENT_SOURCE_DIR} /${argument} " )
318
+ endif ()
319
+
320
+ list (APPEND include_folder_list "${argument} " )
321
+
322
+ elseif ("${current_scope} " STREQUAL "LIBRARIES" )
323
+ list (APPEND library_list "${argument} " )
324
+ elseif ("${current_scope} " STREQUAL "MAININCLUDES" )
325
+ list (APPEND main_include_list "${argument} " )
326
+ else ()
327
+ message (FATAL_ERROR "Invalid scope" )
328
+ endif ()
329
+ endforeach ()
330
+
331
+ # Validate the arguments
332
+ if ("${source_file_list} " STREQUAL "" )
333
+ message (FATAL_ERROR "Source files are missing" )
334
+ endif ()
335
+
336
+ if ("${main_include_list} " STREQUAL "" )
337
+ message (FATAL_ERROR "The main include list is missing" )
338
+ endif ()
339
+
340
+ # Update the global properties
341
+ set_property (GLOBAL APPEND
342
+ PROPERTY OSQUERY_EXTENSION_GROUP_SOURCES
343
+ ${source_file_list}
344
+ )
345
+
346
+ set_property (GLOBAL APPEND
347
+ PROPERTY OSQUERY_EXTENSION_GROUP_MAIN_INCLUDES
348
+ ${main_include_list}
349
+ )
350
+
351
+ if (NOT "${library_list} " STREQUAL "" )
352
+ set_property (GLOBAL APPEND
353
+ PROPERTY OSQUERY_EXTENSION_GROUP_LIBRARIES
354
+ ${library_list}
355
+ )
356
+ endif ()
357
+
358
+ if (NOT "${include_folder_list} " STREQUAL "" )
359
+ set_property (GLOBAL APPEND
360
+ PROPERTY OSQUERY_EXTENSION_GROUP_INCLUDE_FOLDERS
361
+ ${include_folder_list}
362
+ )
363
+ endif ()
364
+ endfunction ()
365
+
366
+ # This function takes the global properties saved by add_osquery_extension_ex and generates
367
+ # a single extenion executable containing all the user code
368
+ function (generate_osquery_extension_group )
369
+ get_property (extension_source_files GLOBAL PROPERTY OSQUERY_EXTENSION_GROUP_SOURCES )
370
+ if ("${extension_source_files} " STREQUAL "" )
371
+ return ()
372
+ endif ()
373
+
374
+ # Allow the user to customize the extension name and version using
375
+ # environment variables
376
+ if (DEFINED ENV{OSQUERY_EXTENSION_GROUP_NAME} )
377
+ set (OSQUERY_EXTENSION_GROUP_NAME $ENV{OSQUERY_EXTENSION_GROUP_NAME} )
378
+ else ()
379
+ set (OSQUERY_EXTENSION_GROUP_NAME "osquery_extension_group" )
380
+ endif ()
381
+
382
+ if (DEFINED ENV{OSQUERY_EXTENSION_GROUP_VERSION} )
383
+ set (OSQUERY_EXTENSION_GROUP_VERSION $ENV{OSQUERY_EXTENSION_GROUP_VERSION} )
384
+ else ()
385
+ set (OSQUERY_EXTENSION_GROUP_VERSION "1.0" )
386
+ endif ()
387
+
388
+ # Build the include list; this contains the files required to declare
389
+ # the classes used in the REGISTER_EXTERNAL directives
390
+ #
391
+ # Note: The variables in uppercase are used by the template
392
+ get_property (main_include_list GLOBAL PROPERTY OSQUERY_EXTENSION_GROUP_MAIN_INCLUDES )
393
+ foreach (include_file ${main_include_list} )
394
+ set (OSQUERY_EXTENSION_GROUP_INCLUDES "${OSQUERY_EXTENSION_GROUP_INCLUDES} \n #include <${include_file} >" )
395
+ endforeach ()
396
+
397
+ # We need to generate the main.cpp file, containing all the required
398
+ # REGISTER_EXTERNAL directives
399
+ get_property (OSQUERY_EXTENSION_GROUP_INITIALIZERS GLOBAL PROPERTY OSQUERY_EXTENSION_GROUP_INITIALIZERS )
400
+ configure_file (
401
+ "${CMAKE_SOURCE_DIR} /tools/codegen/templates/osquery_extension_group_main.cpp.in"
402
+ "${CMAKE_CURRENT_BINARY_DIR} /osquery_extension_group_main.cpp"
403
+ )
404
+
405
+ # Extensions can no longer control which compilation flags to use here (as they are shared) so
406
+ # we are going to enforce sane defaults
407
+ if (UNIX )
408
+ set (extension_cxx_flags
409
+ -pedantic -Wall -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization
410
+ -Wformat=2 -Winit-self -Wlong-long -Wmissing-declarations -Wmissing-include-dirs -Wcomment
411
+ -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion
412
+ -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror -Wunused -Wuninitialized
413
+ -Wconversion
414
+ )
415
+
416
+ if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" )
417
+ list (APPEND extension_cxx_flags -g3 --gdwarf-2 )
418
+ endif ()
419
+ else ()
420
+ set (extension_cxx_flags /W4 )
421
+ endif ()
422
+
423
+ # Generate the extension target
424
+ add_executable ("${OSQUERY_EXTENSION_GROUP_NAME} "
425
+ "${CMAKE_CURRENT_BINARY_DIR} /osquery_extension_group_main.cpp"
426
+ ${extension_source_files}
427
+ )
428
+
429
+ set_property (TARGET "${OSQUERY_EXTENSION_GROUP_NAME} " PROPERTY INCLUDE_DIRECTORIES "" )
430
+ target_compile_features ("${OSQUERY_EXTENSION_GROUP_NAME} " PUBLIC cxx_std_14 )
431
+ target_compile_options ("${OSQUERY_EXTENSION_GROUP_NAME} " PRIVATE ${extension_cxx_flags} )
432
+
433
+ set_target_properties ("${OSQUERY_EXTENSION_GROUP_NAME} " PROPERTIES
434
+ OUTPUT_NAME "${OSQUERY_EXTENSION_GROUP_NAME} .ext"
435
+ )
436
+
437
+ # Import the core libraries; note that we are going to inherit include directories
438
+ # with the wrong scope, so we'll have to fix it
439
+ set_property (TARGET "${OSQUERY_EXTENSION_GROUP_NAME} " PROPERTY INCLUDE_DIRECTORIES "" )
440
+
441
+ get_property (include_folder_list TARGET libosquery PROPERTY INCLUDE_DIRECTORIES )
442
+ target_include_directories ("${OSQUERY_EXTENSION_GROUP_NAME} " SYSTEM PRIVATE ${include_folder_list} )
443
+
444
+ TARGET_OSQUERY_LINK_WHOLE ("${OSQUERY_EXTENSION_GROUP_NAME} " libosquery )
445
+
446
+ # Apply the user (extension) settings
447
+ get_property (library_list GLOBAL PROPERTY OSQUERY_EXTENSION_GROUP_LIBRARIES )
448
+ if (NOT "${library_list} " STREQUAL "" )
449
+ target_link_libraries ("${OSQUERY_EXTENSION_GROUP_NAME} " ${library_list} )
450
+ endif ()
451
+
452
+ get_property (include_folder_list GLOBAL PROPERTY OSQUERY_EXTENSION_GROUP_INCLUDE_FOLDERS )
453
+ if (NOT "${include_folder_list} " STREQUAL "" )
454
+ target_include_directories ("${OSQUERY_EXTENSION_GROUP_NAME} " PRIVATE
455
+ ${include_folder_list}
456
+ )
457
+ endif ()
458
+ endfunction ()
459
+
286
460
# Helper to abstract OS/Compiler whole linking.
287
461
macro (TARGET_OSQUERY_LINK_WHOLE TARGET OSQUERY_LIB )
288
462
if (WINDOWS )
0 commit comments