Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Support build on Windows with MSVC 15.7 (#85) #93

Merged
merged 6 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 26 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '{build}'

image: Visual Studio 2017

init:
- cmd: git config --global core.autocrlf true

platform:
- x64

configuration:
- Debug
- Release

before_build:
- cmake -H. -Bbuild -A%PLATFORM% -DBUILD_TESTING=ON

build:
project: build\opentracing-cpp.sln
parallel: false
verbosity: normal

test_script:
- ps: |
cd build
ctest -V -C $env:configuration --timeout 600 --output-on-failure
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*.out
*.app

# Cmake Build Directory
build

# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
/bazel-*
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cc_library(
name = "opentracing",
srcs = glob(["src/**/*.cpp"], exclude=["src/dynamic_load_unsupported.cpp"]),
srcs = glob(["src/**/*.cpp"], exclude=["src/dynamic_load_unsupported.cpp", "src/dynamic_load_windows.cpp"]),
hdrs = glob(["include/opentracing/**/*.h"]) + [
":include/opentracing/config.h",
":include/opentracing/version.h",
Expand Down
28 changes: 23 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ SET(CPACK_PACKAGE_VERSION_MINOR ${OPENTRACING_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${OPENTRACING_VERSION_PATCH})
include(CPack)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output)

# ==============================================================================
# Configure compilers

Expand All @@ -40,6 +44,8 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
-Wno-padded")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} -D_SCL_SECURE_NO_WARNINGS")
endif()

# ==============================================================================
Expand Down Expand Up @@ -74,9 +80,11 @@ option(BUILD_MOCKTRACER "Build mocktracer library" ON)
option(BUILD_DYNAMIC_LOADING "Build with dynamic loading support" ON)

if (BUILD_DYNAMIC_LOADING)
if (NOT SUPPORTS_WEAK_SYMBOLS OR NOT UNIX)
message(WARNING "Building without dynamic loading support.")
set(BUILD_DYNAMIC_LOADING OFF)
if (NOT WIN32)
if (NOT SUPPORTS_WEAK_SYMBOLS OR NOT UNIX)
message(WARNING "Building without dynamic loading support.")
set(BUILD_DYNAMIC_LOADING OFF)
endif()
endif()
endif()

Expand Down Expand Up @@ -116,7 +124,11 @@ set(SRCS src/propagation.cpp
src/ext/tags.cpp)

if (BUILD_DYNAMIC_LOADING)
list(APPEND SRCS src/dynamic_load_unix.cpp)
if (WIN32)
list(APPEND SRCS src/dynamic_load_windows.cpp)
else()
list(APPEND SRCS src/dynamic_load_unix.cpp)
endif()
else()
list(APPEND SRCS src/dynamic_load_unsupported.cpp)
endif()
Expand All @@ -133,7 +145,9 @@ if (BUILD_SHARED_LIBS)
target_include_directories(opentracing INTERFACE "$<INSTALL_INTERFACE:include/>")
set_target_properties(opentracing PROPERTIES VERSION ${OPENTRACING_VERSION_STRING}
SOVERSION ${OPENTRACING_VERSION_MAJOR})
target_compile_definitions(opentracing PRIVATE OPENTRACING_EXPORTS)
install(TARGETS opentracing EXPORT OpenTracingTargets
RUNTIME DESTINATION ${LIB_INSTALL_DIR}
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${LIB_INSTALL_DIR})
if (CLANG_TIDY_EXE)
Expand All @@ -145,7 +159,11 @@ endif()
if (BUILD_STATIC_LIBS)
add_library(opentracing-static STATIC ${SRCS})
target_link_libraries(opentracing-static ${LIBRARIES})
set_target_properties(opentracing-static PROPERTIES OUTPUT_NAME opentracing)
# Windows generates a lib and dll files for a shared library. using the same name will override the lib file generated by the shared target
if (NOT WIN32)
set_target_properties(opentracing-static PROPERTIES OUTPUT_NAME opentracing)
endif()
target_compile_definitions(opentracing-static PRIVATE OPENTRACING_STATIC)
target_include_directories(opentracing-static INTERFACE "$<INSTALL_INTERFACE:include/>")
install(TARGETS opentracing-static EXPORT OpenTracingTargets
ARCHIVE DESTINATION ${LIB_INSTALL_DIR})
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ In order to understand the C++ platform API, one must first be familiar with the

## Compile and install

#### Linux/MacOS

```bash
mkdir .build
cd .build
Expand All @@ -25,6 +27,32 @@ To test:
make test
```

#### Windows

```bash
mkdir .build
cd .build
cmake -G "Visual Studio 15 2017 Win64" ..
```
To build the targets in debug mode
```bash
MSBuild.exe opentracing-cpp.sln /p:Configuration=Debug /Target=Build
```
To build the targets in release mode
```bash
MSBuild.exe opentracing-cpp.sln /p:Configuration=Release /Target=Build
```

To test:
Run the below command to run the tests with the debug targets
```bash
ctest -C Debug
```
Run the below command to run the tests with the release targets
```bash
ctest -C Release
```

## API overview for those adding instrumentation

Everyday consumers of this `opentracing` package really only need to worry
Expand Down
5 changes: 5 additions & 0 deletions example/dynamic_load/dynamic_load-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#include <iterator>
#include <string>

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif

int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Usage: <tracer_library> <tracer_config_file>\n";
Expand Down
4 changes: 2 additions & 2 deletions example/tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if (BUILD_MOCKTRACER AND BUILD_SHARED_LIBS)
include_directories(../../mocktracer/include)
add_executable(tutorial-example tutorial-example.cpp)
target_link_libraries(tutorial-example opentracing_mocktracer)
add_test(tutorial-example tutorial-example)
target_link_libraries(tutorial-example opentracing_mocktracer)
add_test(NAME tutorial-example COMMAND tutorial-example)
endif()
51 changes: 42 additions & 9 deletions include/opentracing/dynamic_load.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENTRACING_DYNAMIC_LOAD_H
#define OPENTRACING_DYNAMIC_LOAD_H

#include <opentracing/symbols.h>
#include <opentracing/config.h>
#include <opentracing/tracer.h>
#include <opentracing/tracer_factory.h>
Expand Down Expand Up @@ -35,14 +36,45 @@
// assert(error_category != nullptr);
// std::error_code error{rcode, *error_category};
// }
extern "C" {
#ifdef OPENTRACING_BUILD_DYNAMIC_LOADING
int __attribute((weak))
OpenTracingMakeTracerFactory(const char* opentracing_version,
const void** error_category,
void** tracer_factory);

using OpenTracingMakeTracerFactoryType = int(const char* opentracing_version,
const void** error_category,
void** tracer_factory);

#ifdef WIN32

#define OPENTRACING_DECLARE_IMPL_FACTORY(X) \
extern "C" { \
\
__declspec(dllexport) int X(const char* opentracing_version, \
const void** error_category, \
void** tracer_factory); \
\
extern __declspec(dllexport) \
OpenTracingMakeTracerFactoryType* const OpenTracingMakeTracerFactory; \
\
__declspec(selectany) OpenTracingMakeTracerFactoryType* const \
OpenTracingMakeTracerFactory = X; \
} // extern "C"

#else

#define OPENTRACING_DECLARE_IMPL_FACTORY(X) \
extern "C" { \
\
int __attribute((weak)) \
X(const char* opentracing_version, const void** error_category, \
void** tracer_factory); \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need these declarations:

+__declspec(dllexport) int X(const char* opentracing_version,                \
+                            const void** error_category,                    \
+                            void** tracer_factory);
+  int __attribute((weak))                                                   \
+      X(const char* opentracing_version, const void** error_category,       \
+        void** tracer_factory); 

All that needs to be exposed is the variable OpenTracingMakeTracerFactory. X can be a static function in the translation unit.

Copy link
Contributor Author

@mdouaihy mdouaihy Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This declaration is useful so that

OPENTRACING_DECLARE_IMPL_FACTORY(OpenTracingMakeTracerFactoryFct);

can be declared anywhere in the translation unit.

change done.

\
__attribute((weak)) extern OpenTracingMakeTracerFactoryType* const \
OpenTracingMakeTracerFactory; \
\
OpenTracingMakeTracerFactoryType* const OpenTracingMakeTracerFactory = X; \
} // extern "C"

#endif
} // extern "C"



namespace opentracing {
BEGIN_OPENTRACING_ABI_NAMESPACE
Expand All @@ -52,7 +84,7 @@ BEGIN_OPENTRACING_ABI_NAMESPACE
// See
// http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-1.html
// https://ned14.github.io/boost.outcome/md_doc_md_03-tutorial_b.html
const std::error_category& dynamic_load_error_category();
OPENTRACING_API const std::error_category& dynamic_load_error_category();

// `dynamic_load_failure_error` occurs when dynamically loading a tracer library
// fails. Possible reasons could be the library doesn't exist or it is missing
Expand Down Expand Up @@ -117,7 +149,8 @@ class DynamicTracingLibraryHandle {
// }
//
// See DynamicTracingLibraryHandle, TracerFactory
expected<DynamicTracingLibraryHandle> DynamicallyLoadTracingLibrary(
OPENTRACING_API expected<DynamicTracingLibraryHandle>
DynamicallyLoadTracingLibrary(
const char* shared_library, std::string& error_message) noexcept;
END_OPENTRACING_ABI_NAMESPACE
} // namespace opentracing
Expand Down
7 changes: 4 additions & 3 deletions include/opentracing/ext/tags.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENTRACING_EXT_TAGS_H
#define OPENTRACING_EXT_TAGS_H

#include <opentracing/symbols.h>
#include <opentracing/string_view.h>
#include <opentracing/version.h>

Expand All @@ -19,13 +20,13 @@ namespace ext {
// ---------------------------------------------------------------------------
// span_kind hints at relationship between spans, e.g. client/server
// ---------------------------------------------------------------------------
extern const opentracing::string_view span_kind;
OPENTRACING_API extern const opentracing::string_view span_kind;

// Marks a span representing the client-side of an RPC or other remote call
extern const opentracing::string_view span_kind_rpc_client;
OPENTRACING_API extern const opentracing::string_view span_kind_rpc_client;

// Marks a span representing the server-side of an RPC or other remote call
extern const opentracing::string_view span_kind_rpc_server;
OPENTRACING_API extern const opentracing::string_view span_kind_rpc_server;

// ---------------------------------------------------------------------------
// error indicates whether a Span ended in an error state.
Expand Down
3 changes: 2 additions & 1 deletion include/opentracing/noop.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENTRACING_NOOP_H
#define OPENTRACING_NOOP_H

#include <opentracing/symbols.h>
#include <opentracing/tracer.h>
#include <opentracing/version.h>
#include <memory>
Expand All @@ -20,7 +21,7 @@ BEGIN_OPENTRACING_ABI_NAMESPACE
// (see Tracer::Global and Tracer::InitGlobal functions).
//
// WARNING: NoopTracer does not support baggage propagation.
std::shared_ptr<Tracer> MakeNoopTracer() noexcept;
OPENTRACING_API std::shared_ptr<Tracer> MakeNoopTracer() noexcept;
END_OPENTRACING_ABI_NAMESPACE
} // namespace opentracing

Expand Down
3 changes: 2 additions & 1 deletion include/opentracing/propagation.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENTRACING_PROPAGATION_H
#define OPENTRACING_PROPAGATION_H

#include <opentracing/symbols.h>
#include <opentracing/string_view.h>
#include <opentracing/util.h>
#include <opentracing/version.h>
Expand Down Expand Up @@ -62,7 +63,7 @@ enum class SpanReferenceType {
// See
// http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-1.html
// https://ned14.github.io/boost.outcome/md_doc_md_03-tutorial_b.html
const std::error_category& propagation_error_category();
OPENTRACING_API const std::error_category& propagation_error_category();

// `invalid_span_context_error` occurs when Tracer::Inject() is asked to operate
// on a SpanContext which it is not prepared to handle (for example, since it
Expand Down
34 changes: 34 additions & 0 deletions include/opentracing/symbols.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef OPENTRACING_SYMBOLS_H
#define OPENTRACING_SYMBOLS_H

#include <opentracing/config.h>

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4251)
#endif

#ifdef _MSC_VER

#define OPENTRACING_EXPORT __declspec(dllexport)

// Export if this is our own source, otherwise import:
#ifndef OPENTRACING_STATIC
#ifdef OPENTRACING_EXPORTS
#define OPENTRACING_API __declspec(dllexport)
#else // OPENTRACING_STATIC
#define OPENTRACING_API __declspec(dllimport)
#endif // OPENTRACING_EXPORTS
#endif // OPENTRACING_STATIC

#endif // _MSC_VER

#ifndef OPENTRACING_EXPORT
#define OPENTRACING_EXPORT
#endif

#ifndef OPENTRACING_API
#define OPENTRACING_API
#endif

#endif // OPENTRACING_SYMBOLS_H
3 changes: 2 additions & 1 deletion include/opentracing/tracer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENTRACING_TRACER_H
#define OPENTRACING_TRACER_H

#include <opentracing/symbols.h>
#include <opentracing/propagation.h>
#include <opentracing/span.h>
#include <opentracing/string_view.h>
Expand Down Expand Up @@ -57,7 +58,7 @@ class StartSpanOption {

// Tracer is a simple, thin interface for Span creation and SpanContext
// propagation.
class Tracer {
class OPENTRACING_API Tracer {
public:
virtual ~Tracer() = default;

Expand Down
5 changes: 3 additions & 2 deletions include/opentracing/tracer_factory.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENTRACING_TRACER_FACTORY_H
#define OPENTRACING_TRACER_FACTORY_H

#include <opentracing/symbols.h>
#include <opentracing/tracer.h>
#include <opentracing/version.h>

Expand All @@ -11,7 +12,7 @@ BEGIN_OPENTRACING_ABI_NAMESPACE
// See
// http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-1.html
// https://ned14.github.io/boost.outcome/md_doc_md_03-tutorial_b.html
const std::error_category& tracer_factory_error_category();
OPENTRACING_API const std::error_category& tracer_factory_error_category();

// `configuration_parse_error` occurs when the configuration string used to
// construct a tracer does not adhere to the expected format.
Expand All @@ -24,7 +25,7 @@ const std::error_code invalid_configuration_error(
2, tracer_factory_error_category());

// TracerFactory constructs tracers from configuration strings.
class TracerFactory {
class OPENTRACING_API TracerFactory {
public:
virtual ~TracerFactory() = default;

Expand Down
Loading