diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..ec13429 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,7 @@ +# Codspeed C++ + +## Release + +For now, releasing is done manually, by tagging a git rev with vX.Y.Z. + +When releasing a new version, please make sure the [version](https://github.com/CodSpeedHQ/codspeed-cpp/blob/fix-walltime-pid/core/CMakeLists.txt#L3) is updated in the `core/CMakeLists.txt` file to match the pushed version diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index b6f1657..6ce84c3 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -17,20 +17,83 @@ add_library(codspeed src/codspeed.cpp src/walltime.cpp src/uri.cpp) # Version add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}") +# Specify the include directories for users of the library +target_include_directories( + codspeed + PUBLIC $ +) + # Disable valgrind compilation errors if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") # Disable the old-style-cast warning for the specific target target_compile_options(codspeed PRIVATE -Wno-old-style-cast) endif() -# Specify the include directories for users of the library -target_include_directories( +execute_process( + COMMAND git rev-parse --show-toplevel + OUTPUT_VARIABLE GIT_ROOT_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE GIT_COMMAND_RESULT +) + +if(NOT GIT_COMMAND_RESULT EQUAL 0) + message( + WARNING + "Failed to determine the git root directory.\ + Check that git is in your PATH, and that you are in a git repository.\ + Continuing, but codspeed features will not be useable" + ) + # Default to user's cmake source directory + set(GIT_ROOT_DIR ${CMAKE_SOURCE_DIR}) +endif() + +target_compile_definitions( codspeed - PUBLIC $ + INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}" +) + +set(CODSPEED_MODE_ALLOWED_VALUES "OFF" "instrumentation" "walltime") +set(CODSPEED_MODE "OFF" CACHE STRING "Build mode for Codspeed") +set_property( + CACHE CODSPEED_MODE + PROPERTY STRINGS ${CODSPEED_MODE_ALLOWED_VALUES} ) +if(NOT CODSPEED_MODE STREQUAL "OFF") + target_compile_definitions(codspeed INTERFACE -DCODSPEED_ENABLED) + + if(NOT CMAKE_BUILD_TYPE) + message( + WARNING + "CMAKE_BUILD_TYPE is not set. Consider setting it to 'RelWithDebInfo'." + ) + elseif(NOT CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") + message( + WARNING + "CMAKE_BUILD_TYPE is set to '${CMAKE_BUILD_TYPE}', but 'RelWithDebInfo' is recommended." + ) + endif() + + # Define a preprocessor macro based on the build mode + if(CODSPEED_MODE STREQUAL "instrumentation") + target_compile_definitions( + codspeed + INTERFACE -DCODSPEED_INSTRUMENTATION + ) + elseif(CODSPEED_MODE STREQUAL "walltime") + target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME) + else() + message( + FATAL_ERROR + "Invalid build mode: ${CODSPEED_MODE}. Use one of ${CODSPEED_MODE_ALLOWED_VALUES}." + ) + endif() +endif() + +message(STATUS "Codspeed mode: ${CODSPEED_MODE}") + option(ENABLE_TESTS "Enable building the unit tests which depend on gtest" OFF) if(ENABLE_TESTS) - enable_testing() + enable_testing() add_subdirectory(test) endif() diff --git a/core/src/walltime.cpp b/core/src/walltime.cpp index 6e6aa50..92bf7c0 100644 --- a/core/src/walltime.cpp +++ b/core/src/walltime.cpp @@ -8,7 +8,11 @@ #include #include #include -#include +#ifdef _WIN32 +#include +#else +#include +#endif #include const double IQR_OUTLIER_FACTOR = 1.5; @@ -105,7 +109,11 @@ void write_codspeed_benchmarks_to_json( std::string creator_name = "codspeed-cpp"; std::string creator_version = CODSPEED_VERSION; - std::thread::id creator_pid = std::this_thread::get_id(); +#ifdef _WIN32 + pid_t creator_pid = _getpid(); +#else + pid_t creator_pid = getpid(); +#endif std::string instrument_type = "walltime"; oss << "{\n"; diff --git a/examples/google_benchmark/CMakeLists.txt b/examples/google_benchmark/CMakeLists.txt index ed72751..f8f8fc4 100644 --- a/examples/google_benchmark/CMakeLists.txt +++ b/examples/google_benchmark/CMakeLists.txt @@ -3,6 +3,9 @@ include(FetchContent) project(codspeed_picobench_compat VERSION 0.0.0 LANGUAGES CXX) +# On the small benchmarks of this repo, most of the benches will be optimized out, but this is expected. +set(CMAKE_BUILD_TYPE RelWithDebInfo) + option(BENCHMARK_ENABLE_GTEST_TESTS OFF) FetchContent_Declare( diff --git a/flake.lock b/flake.lock index a73bf10..010eb84 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1739525499, - "narHash": "sha256-E9szEEULlfJLUjcacwotBx19aqcElQLR8O22fkR/YmU=", + "lastModified": 1741951873, + "narHash": "sha256-JbREpcR9+NXW0tSbVaj6GKWpNome5RpZYJ95KEhVMGs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "bdfb66032a0b71a1826d7f31391a18a39aeb80bc", + "rev": "ce52a4d87a33258646dde1a1bfcaa88074e9c821", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 0c844ef..6cb5538 100644 --- a/flake.nix +++ b/flake.nix @@ -34,6 +34,14 @@ ''; }; + clang = pkgs.mkShell { + buildInputs = with pkgs; [ clang ] ++ commonBuildInputs; + shellHook = '' + export CXX=clang++; + echo "Welcome to the C++ development environment with Clang!" + ''; + }; + lsp = pkgs.mkShell { buildInputs = with pkgs; diff --git a/google_benchmark/cmake/Codspeed.cmake b/google_benchmark/cmake/Codspeed.cmake index 958c09a..df978f6 100644 --- a/google_benchmark/cmake/Codspeed.cmake +++ b/google_benchmark/cmake/Codspeed.cmake @@ -1,44 +1 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/../core codspeed) - -execute_process( - COMMAND git rev-parse --show-toplevel - OUTPUT_VARIABLE GIT_ROOT_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE GIT_COMMAND_RESULT -) - -if(NOT GIT_COMMAND_RESULT EQUAL 0) - message( - WARNING - "Failed to determine the git root directory.\ - Check that git is in your PATH, and that you are in a git repository.\ - Continuing, but codspeed features will not be useable" - ) - # Default to user's cmake source directory - set(GIT_ROOT_DIR ${CMAKE_SOURCE_DIR}) -endif() - -target_compile_definitions( - codspeed - INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}" -) - -if(DEFINED CODSPEED_MODE) - target_compile_definitions(codspeed INTERFACE -DCODSPEED_ENABLED) - # Define a preprocessor macro based on the build mode - if(CODSPEED_MODE STREQUAL "instrumentation") - target_compile_definitions( - codspeed - INTERFACE -DCODSPEED_INSTRUMENTATION - ) - elseif(CODSPEED_MODE STREQUAL "walltime") - target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME) - else() - message( - FATAL_ERROR - "Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'." - ) - endif() -endif() - -message(STATUS "Build mode set to: ${CODSPEED_MODE}") diff --git a/google_benchmark/src/benchmark_api_internal.h b/google_benchmark/src/benchmark_api_internal.h index ce64017..0a9446b 100644 --- a/google_benchmark/src/benchmark_api_internal.h +++ b/google_benchmark/src/benchmark_api_internal.h @@ -39,6 +39,7 @@ class BenchmarkInstance { const std::vector& statistics() const { return statistics_; } int repetitions() const { #ifdef CODSPEED_INSTRUMENTATION + (void)repetitions_; return 1; #else return repetitions_;