Skip to content

Commit e2ba701

Browse files
fix: allow project to build on windows
1 parent adda943 commit e2ba701

File tree

8 files changed

+55
-12
lines changed

8 files changed

+55
-12
lines changed

core/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ load("@rules_cc//cc:defs.bzl", "cc_library")
33

44
CODSPEED_VERSION = "1.1.1"
55

6+
config_setting(
7+
name = "windows",
8+
constraint_values = ["@platforms//os:windows"],
9+
)
10+
611
# Define the codspeed library
712
cc_library(
813
name = "codspeed",
914
srcs = glob(["src/**/*.cpp"] + ["src/**/*.h"]),
1015
hdrs = glob(["include/**/*.h"] + ["include/**/*.hpp"]),
1116
includes = ["include"],
17+
copts = select({
18+
":windows": ["/std:c++17"],
19+
"//conditions:default": [],
20+
}),
1221
defines = [
1322
"CODSPEED_VERSION=\\\"{}\\\"".format(CODSPEED_VERSION),
1423
] + select({

core/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ set_property(
7777
)
7878

7979
if(NOT CODSPEED_MODE STREQUAL "off")
80-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_ENABLED)
80+
target_compile_definitions(codspeed PUBLIC -DCODSPEED_ENABLED)
8181

8282
if(NOT CMAKE_BUILD_TYPE)
8383
message(
@@ -95,10 +95,10 @@ if(NOT CODSPEED_MODE STREQUAL "off")
9595
if(CODSPEED_MODE STREQUAL "instrumentation")
9696
target_compile_definitions(
9797
codspeed
98-
INTERFACE -DCODSPEED_INSTRUMENTATION
98+
PUBLIC -DCODSPEED_INSTRUMENTATION
9999
)
100100
elseif(CODSPEED_MODE STREQUAL "walltime")
101-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
101+
target_compile_definitions(codspeed PUBLIC -DCODSPEED_WALLTIME)
102102
else()
103103
message(
104104
FATAL_ERROR

core/include/codspeed.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CODSPEED_H
22
#define CODSPEED_H
33

4+
#include <cstdint>
45
#include <string>
56
#include <vector>
67

@@ -33,7 +34,7 @@ class CodSpeed {
3334
struct RawWalltimeBenchmark {
3435
std::string name;
3536
std::string uri;
36-
long iter_per_round;
37+
uint64_t iter_per_round;
3738
double mean_ns;
3839
double median_ns;
3940
double stdev_ns;

core/include/measurement.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
#include <string>
55

6+
#ifdef CODSPEED_INSTRUMENTATION
67
#include "callgrind.h"
8+
#endif
79

810
inline std::string get_version() {
911
#ifdef CODSPEED_VERSION
@@ -13,6 +15,7 @@ inline std::string get_version() {
1315
#endif
1416
}
1517

18+
#ifdef CODSPEED_INSTRUMENTATION
1619
inline bool measurement_is_instrumented() { return RUNNING_ON_VALGRIND; }
1720

1821
inline void measurement_set_metadata() {
@@ -30,5 +33,12 @@ __attribute__((always_inline)) inline void measurement_stop(
3033
CALLGRIND_STOP_INSTRUMENTATION;
3134
CALLGRIND_DUMP_STATS_AT(name.c_str());
3235
};
36+
#else
37+
// Stub implementations for non-instrumentation builds
38+
inline bool measurement_is_instrumented() { return false; }
39+
inline void measurement_set_metadata() {}
40+
inline void measurement_start() {}
41+
inline void measurement_stop(const std::string &name) { (void)name; }
42+
#endif
3343

3444
#endif // MEASUREMENT_H

core/src/codspeed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void CodSpeed::start_benchmark(const std::string &name) {
7676

7777
// Sanity check URI and add a placeholder if format is wrong
7878
if (name.find("::") == std::string::npos) {
79-
std::string uri = "unknown_file::" + name;
79+
uri = "unknown_file::" + name;
8080
std::cout << "WARNING: Benchmark name does not contain '::'. Using URI: "
8181
<< uri << std::endl;
8282
}

core/src/uri.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ std::string extract_lambda_namespace(const std::string &pretty_func) {
4646
return extract_namespace_clang(pretty_func);
4747
#elif __GNUC__
4848
return extract_namespace_gcc(pretty_func);
49+
#elif _MSC_VER
50+
// MSVC doesn't support __PRETTY_FUNCTION__ in the same way
51+
// Return empty string as fallback for Windows
52+
return {};
4953
#else
5054
#error "Unsupported compiler"
5155
#endif

examples/google_benchmark_cmake/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@ project(codspeed_picobench_compat VERSION 0.0.0 LANGUAGES CXX)
66
# Treat warnings as errors
77
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
88
add_compile_options(-Wall -Wextra -Werror)
9+
elseif(MSVC)
10+
# Set C++17 standard
11+
set(CMAKE_CXX_STANDARD 17)
12+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13+
14+
add_compile_options(
15+
/W4
16+
/WX
17+
/wd5051 # Ignore [[maybe_unused]] C++17 warnings from Google Benchmark
18+
/wd4038 # Ignore STL <filesystem> C++17 warnings
19+
)
920
endif()
1021

1122
# On the small benchmarks of this repo, most of the benches will be optimized out, but this is expected.
1223
set(CMAKE_BUILD_TYPE RelWithDebInfo)
1324

14-
option(BENCHMARK_ENABLE_GTEST_TESTS OFF)
25+
# Disable Google Benchmark tests for examples
26+
set(BENCHMARK_ENABLE_GTEST_TESTS
27+
OFF
28+
CACHE BOOL
29+
"Enable testing of the benchmark library."
30+
FORCE
31+
)
1532

1633
FetchContent_Declare(
1734
google_benchmark

google_benchmark/include/benchmark/benchmark.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,14 +1459,16 @@ class Fixture : public internal::Benchmark {
14591459
#define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
14601460
BaseClass##_##Method##_Benchmark
14611461

1462-
#define BENCHMARK_PRIVATE_DECLARE(n) \
1463-
/* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \
1464-
static ::benchmark::internal::Benchmark const* const BENCHMARK_PRIVATE_NAME( \
1465-
n) [[maybe_unused]]
1462+
#define BENCHMARK_PRIVATE_DECLARE(n) \
1463+
/* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \
1464+
static ::benchmark::internal::Benchmark const* const BENCHMARK_PRIVATE_NAME(n)
14661465

14671466
#ifdef CODSPEED_ENABLED
1468-
#define CUR_FILE \
1469-
codspeed::get_path_relative_to_workspace(__FILE__) + "::"
1467+
#define CUR_FILE codspeed::get_path_relative_to_workspace(__FILE__) + "::"
1468+
#ifdef _MSC_VER
1469+
// TODO: __PRETTY_FUNCTION__ is not available in MSVC and we dont support
1470+
#define __PRETTY_FUNCTION__ "unsupported"
1471+
#endif
14701472
#define NAMESPACE \
14711473
(([]() { return codspeed::extract_lambda_namespace(__PRETTY_FUNCTION__); })())
14721474
#define STATIC_NAMESPACE_STRING(name) static std::string name = NAMESPACE;

0 commit comments

Comments
 (0)