Skip to content

Add windows support #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
95 changes: 28 additions & 67 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,37 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# This warning has a false positive. See
# <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108088>.
add_compile_options(-Wno-error=free-nonheap-object)

# This warning has a false positive with clang. See
# <https://stackoverflow.com/questions/52416362>.
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-Wno-error=unused-lambda-capture)

# If we're building with clang, then use the libc++ version of the standard
# library instead of libstdc++. Better coverage of build configurations.
#
# But there's one exception: libfuzzer is built with libstdc++ on Ubuntu,
# and so won't link to libc++. So, if any of the FUZZ_* variables are set,
# keep to libstdc++ (the default on most systems).
if (NOT ${FUZZ_W3C_PROPAGATION})
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
endif ()
endif()

function(add_sanitizers)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
add_compile_options(-fsanitize=undefined)
add_link_options(-fsanitize=undefined)
endfunction()

if(FUZZ_W3C_PROPAGATION)
add_compile_options(-fsanitize=fuzzer)
add_link_options(-fsanitize=fuzzer)
add_sanitizers()
add_subdirectory(fuzz/w3c-propagation)
endif()
## Compiler
if (MSVC)
message(STATUS "Toolchain: MSVC")
include(cmake/compiler/msvc.cmake)
else ()
if (APPLE)
message(STATUS "Toolchain: clang-apple")
include(cmake/compiler/clang_apple.cmake)
else ()
message(STATUS "Toolchain: clang")
include(cmake/compiler/clang.cmake)
endif ()
endif ()

if (SANITIZE)
add_sanitizers()
endif()
## Dependencies
include(cmake/curl.cmake)

if (BUILD_TESTING)
set(CMAKE_BUILD_TYPE "Debug")
add_subdirectory(test)
endif()

include(cmake/curl.cmake)

if (APPLE)
find_library(COREFOUNDATION_LIBRARY CoreFoundation)
find_library(SYSTEMCONFIGURATION_LIBRARY SystemConfiguration)
if (FUZZ_W3C_PROPAGATION)
add_subdirectory(fuzz/w3c-propagation)
endif ()

if (MSVC)
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -pedantic -Werror)
if (BUILD_COVERAGE)
add_compile_options(-g -O0 -fprofile-arcs -ftest-coverage)
endif()
endif()
# Each example has its own build flag.
add_subdirectory(examples)

if(BUILD_COVERAGE)
set(COVERAGE_LIBRARIES gcov)
if(BUILD_BENCHMARK)
add_subdirectory(benchmark)
endif()

add_library(dd_trace_cpp-objects OBJECT)
Expand Down Expand Up @@ -207,10 +177,11 @@ target_link_libraries(dd_trace_cpp-objects
)

# Produce both shared and static versions of the library.
add_library(dd_trace_cpp-shared SHARED $<TARGET_OBJECTS:dd_trace_cpp-objects>)
set_target_properties(dd_trace_cpp-shared PROPERTIES OUTPUT_NAME "dd_trace_cpp")
add_dependencies(dd_trace_cpp-shared dd_trace_cpp-objects)
target_link_libraries(dd_trace_cpp-shared dd_trace_cpp-objects)
# FIX-ME: Issue with Ninja generator on Windows.
#add_library(dd_trace_cpp-shared SHARED $<TARGET_OBJECTS:dd_trace_cpp-objects>)
#set_target_properties(dd_trace_cpp-shared PROPERTIES OUTPUT_NAME "dd_trace_cpp")
#add_dependencies(dd_trace_cpp-shared dd_trace_cpp-objects)
#target_link_libraries(dd_trace_cpp-shared dd_trace_cpp-objects)

add_library(dd_trace_cpp-static STATIC $<TARGET_OBJECTS:dd_trace_cpp-objects>)
set_target_properties(dd_trace_cpp-static PROPERTIES OUTPUT_NAME "dd_trace_cpp")
Expand All @@ -219,16 +190,6 @@ target_link_libraries(dd_trace_cpp-static dd_trace_cpp-objects)

# When installing, install the static library, the shared library, and the
# public headers.
install(TARGETS dd_trace_cpp-static dd_trace_cpp-shared dd_trace_cpp-objects
install(TARGETS dd_trace_cpp-static dd_trace_cpp-objects
FILE_SET public_headers)

if(BUILD_TESTING)
add_subdirectory(test)
endif()

# Each example has its own build flag.
add_subdirectory(examples)

if(BUILD_BENCHMARK)
add_subdirectory(benchmark)
endif()
44 changes: 44 additions & 0 deletions cmake/compiler/clang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

function(add_sanitizers)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
add_compile_options(-fsanitize=undefined)
add_link_options(-fsanitize=undefined)
endfunction()

# This warning has a false positive. See
# <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108088>.
add_compile_options(-Wno-error=free-nonheap-object)
add_compile_options(-Wall -Wextra -pedantic) #-Werror)

# This warning has a false positive with clang. See
# <https://stackoverflow.com/questions/52416362>.
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-Wno-error=unused-lambda-capture)

# If we're building with clang, then use the libc++ version of the standard
# library instead of libstdc++. Better coverage of build configurations.
#
# But there's one exception: libfuzzer is built with libstdc++ on Ubuntu,
# and so won't link to libc++. So, if any of the FUZZ_* variables are set,
# keep to libstdc++ (the default on most systems).
if (NOT ${FUZZ_W3C_PROPAGATION})
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
endif ()
endif()

if (BUILD_COVERAGE)
set(COVERAGE_LIBRARIES gcov)
add_compile_options(-g -O0 -fprofile-arcs -ftest-coverage)
endif()

if(FUZZ_W3C_PROPAGATION)
add_compile_options(-fsanitize=fuzzer)
add_link_options(-fsanitize=fuzzer)
add_sanitizers()
endif()

if (SANITIZE)
add_sanitizers()
endif()
6 changes: 6 additions & 0 deletions cmake/compiler/clang_apple.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include(cmake/compiler/clang.cmake)

if (APPLE)
find_library(COREFOUNDATION_LIBRARY CoreFoundation)
find_library(SYSTEMCONFIGURATION_LIBRARY SystemConfiguration)
endif ()
37 changes: 37 additions & 0 deletions cmake/compiler/msvc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
macro(get_WIN32_WINNT version)
if(CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
string(REGEX MATCH "^([0-9]+)" verMajor ${ver})

# Check for Windows 10, b/c we'll need to convert to hex 'A'.
if("${verMajor}" MATCHES "10")
set(verMajor "A")
string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
endif()

string(REPLACE "." "" ver ${ver})
# Prepend each digit with a zero.
string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
endif()
endmacro()

get_WIN32_WINNT(win_ver)
add_compile_definitions(DD_TRACE_PLATFORM_WINDOWS)
add_compile_definitions(_WIN32_WINNT=${win_ver})

if (BUILD_COVERAGE)
message(FATAL_ERROR "BUILD_COVERAGE is not supported for MSVC build.")
endif ()

if (FUZZ_W3C_PROPAGATION)
message(FATAL_ERROR "Fuzzers are not support for MSVC build.")
endif ()

if (SANITIZE)
message(FATAL_ERROR "Sanitize option is not support for MSVC build")
endif ()

# Add project-wide compiler options
add_compile_options(/W4)# /WX)
26 changes: 14 additions & 12 deletions src/datadog/datadog_agent_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
namespace datadog {
namespace tracing {

constexpr StringView k_separator = "://";
constexpr StringView supported[] = {"http", "https", "unix", "http+unix",
"https+unix"};

Expected<HTTPClient::URL> DatadogAgentConfig::parse(StringView input) {
const StringView separator = "://";
const auto after_scheme = std::search(input.begin(), input.end(),
separator.begin(), separator.end());
if (after_scheme == input.end()) {
const auto after_scheme = input.find(k_separator);
if (after_scheme == StringView::npos) {
std::string message;
message += "Datadog Agent URL is missing the \"://\" separator: \"";
append(message, input);
message += '\"';
return Error{Error::URL_MISSING_SEPARATOR, std::move(message)};
}

const StringView scheme = range(input.begin(), after_scheme);
const StringView supported[] = {"http", "https", "unix", "http+unix",
"https+unix"};
const StringView scheme = input.substr(0, after_scheme);
const auto found =
std::find(std::begin(supported), std::end(supported), scheme);
if (found == std::end(supported)) {
Expand All @@ -43,7 +43,8 @@ Expected<HTTPClient::URL> DatadogAgentConfig::parse(StringView input) {
}

const StringView authority_and_path =
range(after_scheme + separator.size(), input.end());
input.substr(after_scheme + k_separator.size());
// range(after_scheme + separator.size(), input.end());
// If the scheme is for unix domain sockets, then there's no way to
// distinguish the path-to-socket from the path-to-resource. Some
// implementations require that the forward slashes in the path-to-socket
Expand Down Expand Up @@ -75,12 +76,13 @@ Expected<HTTPClient::URL> DatadogAgentConfig::parse(StringView input) {
// Again, though, we're only parsing URLs that designate the location of
// the Datadog Agent service, and so they will not have a resource
// location. Still, let's parse it properly.
const auto after_authority =
std::find(authority_and_path.begin(), authority_and_path.end(), '/');
const auto after_authority = authority_and_path.find('/');
return HTTPClient::URL{
std::string(scheme),
std::string(range(authority_and_path.begin(), after_authority)),
std::string(range(after_authority, authority_and_path.end()))};
std::string(authority_and_path.substr(0, after_authority)),
(after_authority == StringView::npos)
? ""
: std::string(authority_and_path.substr(after_authority))};
}

Expected<FinalizedDatadogAgentConfig> finalize_config(
Expand Down
12 changes: 6 additions & 6 deletions src/datadog/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Expected {
public:
Expected() = default;
Expected(const Expected&) = default;
Expected(Expected&) = default;
// Expected(Expected&) = default;
Expected(Expected&&) = default;
Expected& operator=(const Expected&) = default;
Expected& operator=(Expected&&) = default;
Expand Down Expand Up @@ -193,11 +193,11 @@ class Expected<void> {

public:
Expected() = default;
Expected(const Expected&) = default;
Expected(Expected&) = default;
Expected(Expected&&) = default;
Expected& operator=(const Expected&) = default;
Expected& operator=(Expected&&) = default;
// Expected(const Expected&) = default;
// Expected(Expected&) = default;
// Expected(Expected&&) = default;
// Expected& operator=(const Expected&) = default;
// Expected& operator=(Expected&&) = default;

template <typename Other>
Expected(Other&&);
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/msgpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ inline void pack_integer(std::string& buffer, std::int32_t value) {
}

inline Expected<void> pack_string(std::string& buffer, StringView value) {
return pack_string(buffer, value.begin(), value.size());
return pack_string(buffer, value.data(), value.size());
}

} // namespace msgpack
Expand Down
35 changes: 21 additions & 14 deletions src/datadog/parse_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ namespace {
template <typename Integer>
Expected<Integer> parse_integer(StringView input, int base, StringView kind) {
Integer value;
const auto status = std::from_chars(input.begin(), input.end(), value, base);
const auto beg = input.data();
const auto end = beg + input.size();
const auto status = std::from_chars(beg, end, value, base);
if (status.ec == std::errc::invalid_argument) {
std::string message;
message += "Is not a valid integer: \"";
append(message, input);
message += '\"';
return Error{Error::INVALID_INTEGER, std::move(message)};
} else if (status.ptr != input.end()) {
} else if (status.ptr != end) {
std::string message;
message += "Integer has trailing characters in: \"";
append(message, input);
Expand All @@ -44,19 +46,17 @@ Expected<Integer> parse_integer(StringView input, int base, StringView kind) {
} // namespace

StringView strip(StringView input) {
const auto not_whitespace = [](unsigned char ch) {
return !std::isspace(ch);
};
const char* const begin =
std::find_if(input.begin(), input.end(), not_whitespace);
const char* const end =
std::find_if(input.rbegin(), std::make_reverse_iterator(begin),
not_whitespace)
.base();
if (input.empty()) return input;

auto begin = input.data();
auto end = begin + input.size() - 1;

while (begin && std::isspace(*begin)) ++begin;
while (end && std::isspace(*end)) --end;

assert(begin <= end);

return StringView{begin, std::size_t(end - begin)};
return StringView{begin, std::size_t(end + 1 - begin)};
}

Expected<std::uint64_t> parse_uint64(StringView input, int base) {
Expand Down Expand Up @@ -103,8 +103,15 @@ bool starts_with(StringView subject, StringView prefix) {
return false;
}

return std::mismatch(subject.begin(), subject.end(), prefix.begin()).second ==
prefix.end();
auto c0 = subject.data();
auto c1 = prefix.data();
const auto prefix_end = c1 + prefix.size();
while (*c0 == *c1) {
++c0;
++c1;
}

return c1 == prefix_end;
}

void to_lower(std::string& text) {
Expand Down
Loading