Skip to content

Commit

Permalink
chore(various): add cmake-format (#10820)
Browse files Browse the repository at this point in the history
Hey, so I realized we weren't really formatting our cmake files, and we
have a lot of them, so maybe we should have a consistent style?

## Checklist
- [X] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
sanchda authored Oct 1, 2024
1 parent b0fd459 commit a210c90
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 454 deletions.
105 changes: 54 additions & 51 deletions ddtrace/appsec/_iast/_taint_tracking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,85 @@ project(${APP_NAME})

set(CMAKE_CXX_STANDARD 17)

# -U_FORTIFY_SOURCE to fix a bug in alpine and pybind11
# https://github.com/pybind/pybind11/issues/1650
# -U_FORTIFY_SOURCE to fix a bug in alpine and pybind11 https://github.com/pybind/pybind11/issues/1650
# https://gitlab.alpinelinux.org/alpine/aports/-/issues/8626
add_compile_options(-fPIC -fexceptions -fvisibility=hidden -fpermissive -pthread -Wall -Wno-unknown-pragmas -U_FORTIFY_SOURCE)

if (BUILD_MACOS)
add_compile_options(
-fPIC
-fexceptions
-fvisibility=hidden
-fpermissive
-pthread
-Wall
-Wno-unknown-pragmas
-U_FORTIFY_SOURCE)

if(BUILD_MACOS)
# https://pybind11.readthedocs.io/en/stable/compiling.html#building-manually
message(STATUS "Compile options for MacOS")
add_link_options(-ldl -undefined dynamic_lookup)
else ()
else()
message(STATUS "Compile options for Linux/Win")
endif (BUILD_MACOS)
endif(BUILD_MACOS)
unset(BUILD_MACOS CACHE)

if (CMAKE_BUILD_TYPE STREQUAL "Release")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
message("Release mode: using abseil")
FetchContent_Declare(
absl
URL "https://github.com/abseil/abseil-cpp/archive/refs/tags/20230802.1.zip"
)
FetchContent_Declare(absl URL "https://github.com/abseil/abseil-cpp/archive/refs/tags/20230802.1.zip")
FetchContent_MakeAvailable(absl)
else ()
else()
message("Debug mode: not using abseil")
endif ()
endif()

include_directories(".")

file(GLOB SOURCE_FILES "*.cpp"
"Aspects/*.cpp"
"Initializer/*.cpp"
"TaintedOps/*.cpp"
"TaintTracking/*.cpp"
"Utils/*.cpp")
file(GLOB HEADER_FILES "*.h"
"Aspects/*.h"
"Initializer/*.h"
"TaintedOps/*.h"
"TaintTracking/*.h"
"Utils/*.h"
)

# Find ICU library (not needed for now)
#find_package(ICU REQUIRED COMPONENTS uc i18n) # 'uc' for the common library, 'i18n' for the internationalization library
#include_directories(${ICU_INCLUDE_DIRS})
#list(APPEND ICU_LIBS ${ICU_LIBRARIES})
file(
GLOB
SOURCE_FILES
"*.cpp"
"Aspects/*.cpp"
"Initializer/*.cpp"
"TaintedOps/*.cpp"
"TaintTracking/*.cpp"
"Utils/*.cpp")
file(
GLOB
HEADER_FILES
"*.h"
"Aspects/*.h"
"Initializer/*.h"
"TaintedOps/*.h"
"TaintTracking/*.h"
"Utils/*.h")

# Find ICU library (not needed for now) find_package(ICU REQUIRED COMPONENTS uc i18n) # 'uc' for the common library,
# 'i18n' for the internationalization library include_directories(${ICU_INCLUDE_DIRS}) list(APPEND ICU_LIBS
# ${ICU_LIBRARIES})

# Debug messages
message(STATUS "PYTHON_LIBRARIES = ${Python_LIBRARIES}")
message(STATUS "PYTHON_EXECUTABLE = ${Python_EXECUTABLE}")
message(STATUS "PYTHON_INCLUDE_DIRS = ${PYTHON_INCLUDE_DIRS}")
message(STATUS "Python_EXECUTABLE = ${Python_EXECUTABLE}")
#message(STATUS "ICU_LIBRARIES = ${ICU_LIBRARIES}")
#message(STATUS "ICU_INCLUDE_DIRS = ${ICU_INCLUDE_DIRS}")
# message(STATUS "ICU_LIBRARIES = ${ICU_LIBRARIES}") message(STATUS "ICU_INCLUDE_DIRS = ${ICU_INCLUDE_DIRS}")

add_subdirectory(_vendor/pybind11)
if (NATIVE_TESTING)
if(NATIVE_TESTING)
add_subdirectory(tests EXCLUDE_FROM_ALL)
endif ()
endif()

pybind11_add_module(_native SHARED ${SOURCE_FILES} ${HEADER_FILES})
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} DIRECTORY)
set_target_properties(
_native
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
)
set_target_properties(_native PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}")

#target_link_libraries(_native PRIVATE ${ICU_LIBS})
# target_link_libraries(_native PRIVATE ${ICU_LIBS})

if (CMAKE_BUILD_TYPE STREQUAL "Release")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_libraries(${APP_NAME} PRIVATE absl::node_hash_map)
endif ()

install(TARGETS _native DESTINATION
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
RUNTIME DESTINATION ${LIB_INSTALL_DIR}
)

endif()

install(
TARGETS _native
DESTINATION LIBRARY
DESTINATION ${LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
RUNTIME DESTINATION ${LIB_INSTALL_DIR})
21 changes: 10 additions & 11 deletions ddtrace/appsec/_iast/_taint_tracking/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
)
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip)
FetchContent_MakeAvailable(googletest)

enable_testing()
Expand All @@ -14,22 +11,24 @@ file(GLOB TEST_SOURCES "*.cpp" "*.hpp")
add_executable(native_tests ${TEST_SOURCES} ${SOURCE_FILES} ${HEADER_FILES})

set(NATIVE_TEST_LIBRARIES gtest gtest_main ${PYTHON_LIBRARIES} pybind11::module)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
list(APPEND NATIVE_TEST_LIBRARIES absl::node_hash_map)
endif ()
endif()

if (NOT MSVC AND NATIVE_TEST_COVERAGE)
if(NOT MSVC AND NATIVE_TEST_COVERAGE)
target_compile_options(native_tests PRIVATE -ggdb --coverage)
list(APPEND NATIVE_TEST_LIBRARIES gcov)
endif ()
endif()

target_link_libraries(native_tests ${NATIVE_TEST_LIBRARIES})

# Discover tests
include(GoogleTest)
gtest_discover_tests(native_tests)

add_custom_target(test_valgrind
COMMAND valgrind --leak-check=full --suppressions=../../../../../scripts/iast/valgrind-python.supp --show-reachable=yes ${CMAKE_BINARY_DIR}/tests/native_tests --gtest_filter=-TestTimer.*
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests)
add_custom_target(
test_valgrind
COMMAND valgrind --leak-check=full --suppressions=../../../../../scripts/iast/valgrind-python.supp
--show-reachable=yes ${CMAKE_BINARY_DIR}/tests/native_tests --gtest_filter=-TestTimer.*
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests)
add_dependencies(test_valgrind native_tests)
61 changes: 34 additions & 27 deletions ddtrace/internal/datadog/profiling/cmake/AnalysisFunc.cmake
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
include(CheckIPOSupported)

function(add_ddup_config target)
target_compile_options(${target} PRIVATE
"$<$<CONFIG:Debug>:-Og;-ggdb3>"
"$<$<CONFIG:RelWithDebInfo>:-Os;-ggdb3>"
"$<$<CONFIG:Release>:-Os>"
-ffunction-sections -fno-semantic-interposition
-Wall -Werror -Wextra -Wshadow -Wnon-virtual-dtor -Wold-style-cast
)
target_link_options(${target} PRIVATE
"$<$<CONFIG:Release>:-s>"
"$<$<CONFIG:RelWithDebInfo>:>"
-Wl,--as-needed -Wl,-Bsymbolic-functions -Wl,--gc-sections -Wl,-z,nodelete -Wl,--exclude-libs,ALL
)
target_compile_options(
${target}
PRIVATE "$<$<CONFIG:Debug>:-Og;-ggdb3>"
"$<$<CONFIG:RelWithDebInfo>:-Os;-ggdb3>"
"$<$<CONFIG:Release>:-Os>"
-ffunction-sections
-fno-semantic-interposition
-Wall
-Werror
-Wextra
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast)
target_link_options(
${target}
PRIVATE
"$<$<CONFIG:Release>:-s>"
"$<$<CONFIG:RelWithDebInfo>:>"
-Wl,--as-needed
-Wl,-Bsymbolic-functions
-Wl,--gc-sections
-Wl,-z,nodelete
-Wl,--exclude-libs,ALL)

# If we can IPO, then do so
check_ipo_supported(RESULT result)
if (result)
set_property(TARGET ${target} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
if(result)
set_property(TARGET ${target} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

# Propagate sanitizers
if (SANITIZE_OPTIONS)
# Some sanitizers (or the analysis--such as symbolization--tooling thereof) work better with frame
# pointers, so we include it here.
if(SANITIZE_OPTIONS)
# Some sanitizers (or the analysis--such as symbolization--tooling thereof) work better with frame pointers, so
# we include it here.
target_compile_options(${target} PRIVATE -fsanitize=${SANITIZE_OPTIONS} -fno-omit-frame-pointer)
target_link_options(${target} PRIVATE -fsanitize=${SANITIZE_OPTIONS})

# We need to do a little bit of work in order to ensure the dynamic *san libraries can be linked
# Procedure adapted from datadog/ddprof :)
# We need to do a little bit of work in order to ensure the dynamic *san libraries can be linked Procedure
# adapted from datadog/ddprof :)
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=
OUTPUT_VARIABLE LIBSAN_LIB_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY
)
set_target_properties(${target} PROPERTIES
INSTALL_RPATH ${LIBSAN_LIB_PATH}
BUILD_RPATH ${LIBSAN_LIB_PATH}
)
OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY)
set_target_properties(${target} PROPERTIES INSTALL_RPATH ${LIBSAN_LIB_PATH} BUILD_RPATH ${LIBSAN_LIB_PATH})
endif()

# If DO_FANALYZER is specified and we're using gcc, then we can use -fanalyzer
if (DO_FANALYZER AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(${target} PRIVATE -fanalyzer)
if(DO_FANALYZER AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(${target} PRIVATE -fanalyzer)
endif()
endfunction()
13 changes: 9 additions & 4 deletions ddtrace/internal/datadog/profiling/cmake/FindClangtidy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
option(DO_CLANGTIDY "Enable clang-tidy" OFF)

# Default executable for clang-tidy
if (NOT DEFINED CLANGTIDY_CMD)
if(NOT DEFINED CLANGTIDY_CMD)
find_program(CLANGTIDY_CMD NAMES clang-tidy)
endif()

function(add_clangtidy_target TARGET)
if (NOT DO_CLANGTIDY)
if(NOT DO_CLANGTIDY)
return()
endif()

if (CLANGTIDY_CMD)
set_target_properties(${TARGET} PROPERTIES CXX_CLANG_TIDY "${CLANGTIDY_CMD};--checks=bugprone-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,performance-*,readability-*,-modernize-use-trailing-return-type,-performance-avoid-endl")
if(CLANGTIDY_CMD)
set_target_properties(
${TARGET}
PROPERTIES
CXX_CLANG_TIDY
"${CLANGTIDY_CMD};--checks=bugprone-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,performance-*,readability-*,-modernize-use-trailing-return-type,-performance-avoid-endl"
)
else()
message(FATAL_ERROR "clang-tidy requested but executable not found")
endif()
Expand Down
Loading

0 comments on commit a210c90

Please sign in to comment.