Skip to content
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

Initial work to add gcc support #176

Merged
merged 17 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 8 additions & 38 deletions build/Mlos.Cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
add_link_options(-Wall -Wextra -Wpedantic -Werror)

# The codegen output currently relies on __declspec(selectany) attributes to
# instruct the linker to ignore extra definitions resulting from including the
# The codegen output currently relies on asking the compiler to select one of
# our equivalent but duplicative definitions that results from including the
# same header in multiple places.
# NOTE: This option is only available with clang, not gcc.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdeclspec")
# For clang, make use of the original __declspec(selectany) attributes that msvc
# uses.
# For gcc, we use __attribute__((weak)) to achieve the same.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdeclspec")
endif()

# When compiling for Debug build, make sure that DEBUG is defined for the compiler.
# This is to mimic MSVC behavior so that our #ifdefs can remain the same rather
Expand All @@ -34,40 +38,6 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
add_compile_options(-g)
add_link_options(-g)

# TODO: Search for clang compiler and set the appropriate C/CXX compiler variables.
#if(NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
# # TODO: Add local version of clang to use?
# find_program(CLANGCPP
# NAMES clang++-10
# #PATHS ENV PATH
# )
# if(CLANGCPP)
# message(WARNING "Forcing CXX compiler to ${CLANGCPP}")
# set(CMAKE_CXX_COMPILER ${CLANGPP})
# set(CMAKE_CXX_COMPILER_ID "Clang")
# endif()
#endif()

# For now we just abort if Clang is not the compiler selected.
if(NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
message(SEND_ERROR
"MLOS currently only supports clang (not '${CMAKE_CXX_COMPILER_ID}') for compilation.\n"
"Please re-run (c)make with 'CC=clang-10 CXX=clang++-10' set.\n")
endif()

# TODO: This just finds the program, but doesn't actually enable it as a
# target. To use clang-tidy, we will need to provide a .clang-format config
# and probably reformat some code again.
find_program(CLANG_TIDY NAMES clang-tidy clang-tidy-6.0)
if(CLANG_TIDY)
add_custom_target(
clang-tidy
COMMAND ${CLANG_TIDY}
${SOURCE_FILES}
--
-I ${CMAKE_SOURCE_DIR}/include)
endif()

# https://github.com/google/sanitizers/wiki/AddressSanitizer
#
option(ADDRESS_SANITIZER "Enable Clang AddressSanitizer" OFF)
Expand Down
2 changes: 1 addition & 1 deletion source/Examples/SmartCache/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void ThrowIfFail(HRESULT hr)
}

int
__cdecl
bpkroth marked this conversation as resolved.
Show resolved Hide resolved
MLOS_CDECL_ATTR
main(
_In_ int argc,
_In_ char* argv[])
Expand Down
2 changes: 1 addition & 1 deletion source/Examples/SmartSharedChannel/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void AssertFailed(
// NOTES:
//
int
__cdecl
MLOS_CDECL_ATTR
main(
_In_ int argc,
_In_ char* argv[])
Expand Down
15 changes: 15 additions & 0 deletions source/Mlos.Core/Mlos.Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ constexpr int32_t INVALID_FD_VALUE = -1;
#define MLOS_RETAIL_ASSERT(result) { if (!result) Mlos::Core::MlosPlatform::TerminateProcess(); }
#define MLOS_UNUSED_ARG(x) (void)x

#ifdef _MSC_VER
#define MLOS_SELECTANY_LINKER_ATTR __declspec(selectany)
#define MLOS_CDECL_ATTR __cdecl
#elif __clang__
// Note: this requires compiling with -fdeclspec.
#define MLOS_SELECTANY_LINKER_ATTR __declspec(selectany)
#define MLOS_CDECL_ATTR __cdecl
#elif __GNUC__
#define MLOS_SELECTANY_LINKER_ATTR __attribute__((weak))
// cdecl is unnecessary (and emits a warning) when compiling for x86-64, which is currently the only platform we support
#define MLOS_CDECL_ATTR /* __attribute__((cdecl))__ */
#else
#warning Unhandled compiler.
#endif

#include "MlosPlatform.h"

#include "BytePtr.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override void WriteBeginFile()

// Define a global dispatch table.
//
WriteLine("__declspec(selectany) ::Mlos::Core::DispatchEntry DispatchTable[] = ");
WriteLine("MLOS_SELECTANY_LINKER_ATTR ::Mlos::Core::DispatchEntry DispatchTable[] = ");
WriteLine("{");

IndentationLevel++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override void BeginVisitType(Type sourceType)
//
string cppTypeNameAsField = $"{sourceType.Name}_Callback";

WriteLine($"__declspec(selectany) std::function<void ({cppProxyTypeFullName}&&)> {cppTypeNameAsField} = nullptr;");
WriteLine($"MLOS_SELECTANY_LINKER_ATTR std::function<void ({cppProxyTypeFullName}&&)> {cppTypeNameAsField} = nullptr;");
WriteLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void WriteBeginFile()
public override void WriteEndFile()
{
IndentationLevel--;
WriteLine("};");
WriteLine("}");
WriteLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override void WriteBeginFile()
public override void WriteEndFile()
{
IndentationLevel--;
WriteLine("};");
WriteLine("}");
WriteLine();
}

Expand Down Expand Up @@ -99,7 +99,7 @@ public override void EndVisitType(Type sourceType)
WriteLine("return totalDataSize;");
IndentationLevel--;

WriteLine("};");
WriteLine("}");
WriteLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void WriteBeginFile()
public override void WriteEndFile()
{
IndentationLevel--;
WriteLine("};");
WriteLine("}");
WriteLine();
}

Expand Down
2 changes: 1 addition & 1 deletion source/Mlos.UnitTest/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using ::testing::TestPartResult;
using ::testing::UnitTest;

int
__cdecl
MLOS_CDECL_ATTR
main(
_In_ int argc,
_In_ char* argv[])
Expand Down
6 changes: 4 additions & 2 deletions source/Mlos.UnitTest/SharedChannelTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestFlatBuffer : public BytePtr
std::array<byte, T> array = { 0 };
};

namespace
namespace SharedChannelTests
{
#ifndef DEBUG
// Verify buffer size.
Expand Down Expand Up @@ -212,7 +212,7 @@ TEST(SharedChannel, VerifySendingReceivingArrayStruct)
ChannelSynchronization sync = {};
TestSharedChannel sharedChannel(sync, buffer, 128);

Mlos::UnitTest::Line line;
Mlos::UnitTest::Line line = {};
line.Points[0] = { 3, 5 };
line.Points[1] = { 7, 9 };
line.Height = { 1.3f, 3.9f };
Expand Down Expand Up @@ -376,5 +376,7 @@ TEST(SharedChannel, StressSendReceive)
result =
resultFromReader1.get() &&
resultFromReader2.get();

EXPECT_EQ(result, true);
}
}