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

Which compile definitions are recommended for clang? #9

Closed
rbresalier opened this issue Nov 10, 2021 · 12 comments
Closed

Which compile definitions are recommended for clang? #9

rbresalier opened this issue Nov 10, 2021 · 12 comments

Comments

@rbresalier
Copy link

Which compile definitions are recommended for clang build on Linux?

I already figured out that I need to set on my cmake line:

-DASIO_GRPC_USE_BOOST_CONTAINER=1

But I wonder if there are any others?

I'm using a slightly older version of asio-grpc, it would take me a little effort to update so don't want to have to do that. I'm using commit a17b559. Don't know if this is the reason?

The reason that I ask is that when I use clang 10.0.1 I get a clang crash when trying to build hello-world-server-cpp20:

/gitworkspace/distributions/clang/10.0.1/bin/clang++ -DBOOST_ALL_NO_LIB -DCARES_STATICLIB -I/gitworkspace/rbresali/mdt/mdt_example/asio_grpc_example/native.build/src/generated -isystem /gitworkspace/rbresali/mdt/mdt_stage/native.stage/usr/local/include -stdlib=libc++ -fPIC -g -Wall -Werror -DBOOST_THREAD_VERSION=4 -save-temps=obj -std=gnu++2a -MD -MT src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o -MF src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o.d -o src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o -c /gitworkspace/rbresali/mdt/mdt_example/asio_grpc_example/src/hello-world-server-cpp20.cpp
Stack dump:
0.	Program arguments: /vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10 -cc1 -triple x86_64-unknown-linux-gnu -S -save-temps=obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello-world-server-cpp20.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -resource-dir /vol/dwdmgit_distributions/clang/10.0.1/lib64/clang/10.0.1 -Wall -Werror -std=gnu++2a -fdebug-compilation-dir /gitworkspace/rbresali-mdt_211028.110446/mdt_example/asio_grpc_example/native.build -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -faddrsig -o src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.s -x ir src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.bc 
1.	Code generation
2.	Running pass 'Function Pass Manager' on module 'src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.bc'.
3.	Running pass 'X86 DAG->DAG Instruction Selection' on function '@"_ZN5boost4asio6detail20co_spawn_entry_pointINS0_15any_io_executorEZ4mainE3$_0NS1_16detached_handlerEEENS0_9awaitableINS1_28awaitable_thread_entry_pointET_EEPNS6_IvS8_EES8_T0_T1_"'
 #0 0x00000000016b6e24 PrintStackTraceSignalHandler(void*) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x16b6e24)
 #1 0x00000000016b4b8e llvm::sys::RunSignalHandlers() (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x16b4b8e)
 #2 0x00000000016b7225 SignalHandler(int) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x16b7225)
 #3 0x00007f5f529b1630 __restore_rt (/lib64/libpthread.so.0+0xf630)
 #4 0x00000000021de359 llvm::DAGTypeLegalizer::getTableId(llvm::SDValue) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x21de359)
 #5 0x00000000021de216 llvm::DAGTypeLegalizer::RemapValue(llvm::SDValue&) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x21de216)
 #6 0x00000000021dd97f llvm::DAGTypeLegalizer::ReplaceValueWith(llvm::SDValue, llvm::SDValue) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x21dd97f)
 #7 0x00000000021e01ac llvm::DAGTypeLegalizer::DisintegrateMERGE_VALUES(llvm::SDNode*, unsigned int) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x21e01ac)
 #8 0x0000000002236e99 llvm::DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(llvm::SDNode*, unsigned int) (/vol/dwdmgit_distributions/clang/10.0.1/bin/clang-10+0x2236e99)
 #9 0x00007ffe5377a490 
clang-10: error: unable to execute command: Segmentation fault (core dumped)
clang-10: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 10.0.1 
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /gitworkspace/distributions/clang/10.0.1/bin
clang-10: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang-10: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-10: note: diagnostic msg: /gitworkspace/rbresali/tmp/hello-world-server-cpp20-31228c.cpp
clang-10: note: diagnostic msg: /gitworkspace/rbresali/tmp/hello-world-server-cpp20-31228c.sh
clang-10: note: diagnostic msg: 

********************

@Tradias
Copy link
Owner

Tradias commented Nov 10, 2021

I am surprised it even attempted to build the C++20 coroutine example without -fcoroutines-ts. You can try adding it to the compile options. On CI I have disabled the C++20 builds for Clang since I could not get them to work correctly either.

Another option is to use Boost.Coroutine, the following code shows how to do that. You will also need to change lines 61-62 in example/CMakeLists.txt

61:    target_link_libraries(asio-grpc-hello-world-server-cpp20 PRIVATE asio-grpc-example-protos Boost::coroutine
62:                                                                     Boost::thread)
#include "protos/helloworld.grpc.pb.h"

#include <agrpc/asioGrpc.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/spawn.hpp>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>

#include <optional>
#include <thread>

int main()
{
    std::optional<std::thread> shutdown_thread;

    grpc::ServerBuilder builder;
    std::unique_ptr<grpc::Server> server;
    helloworld::Greeter::AsyncService service;
    agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
    boost::asio::basic_signal_set signals{grpc_context, SIGINT, SIGTERM};
    builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
    server = builder.BuildAndStart();

    boost::asio::spawn(grpc_context,
                       [&](const boost::asio::yield_context& yield)
                       {
                           while (true)
                           {
                               grpc::ServerContext server_context;
                               helloworld::HelloRequest request;
                               grpc::ServerAsyncResponseWriter<helloworld::HelloReply> writer{&server_context};
                               bool request_ok = agrpc::request(&helloworld::Greeter::AsyncService::RequestSayHello,
                                                                service, server_context, request, writer, yield);
                               if (!request_ok)
                               {
                                   return;
                               }
                               helloworld::HelloReply response;
                               response.set_message("Hello " + request.name());
                               bool finish_ok = agrpc::finish(writer, response, grpc::Status::OK, yield);
                           }
                       });

    signals.async_wait(
        [&](auto&&, auto&&)
        {
            shutdown_thread.emplace(
                [&]
                {
                    server->Shutdown();
                });
        });

    grpc_context.run();
    shutdown_thread->join();
}

I hope that is helpful and thank you for checking out the library!

@rbresalier
Copy link
Author

Thanks for pointing out that I was missing -fcoroutines-ts, I didn't realize that. But even when I do add -fcoroutines-ts in clang is crashing :( We build our product with both clang and gcc so if clang can't support C++20 coroutines then we have to use boost coroutines even for gcc so that we have the same source code.

@rbresalier
Copy link
Author

FYI: I got the C++20 coroutine example working with latest clang-13.0.0. The build doesn't crash and the examples work!

@Tradias
Copy link
Owner

Tradias commented Nov 10, 2021

I am able to compile and run the C++20 version with Clang 13 and -stdlib=libc++ -D_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS compile options. I will try that out for CI as well.

Ah nice, I was just about to write the same :)

@rbresalier
Copy link
Author

I'm not using -D_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS and it works. But I am using ASIO_GRPC_USE_BOOST_CONTAINER. I also have -stdlib=libc++. I think for clang build that ASIO_GRPC_USE_BOOST_CONTAINER is required, you are using ASIO_GRPC_USE_BOOST_CONTAINER for your working clang 13 build?

@Tradias
Copy link
Owner

Tradias commented Nov 10, 2021

Correct, I am using that as well since libc++ does not provide <memory_resource>. For me Boost.Asio tries to use std::result_of which has been removed in C++20, that's why I needed that compile definition.

@rbresalier
Copy link
Author

To handle the boost.asio issue I used -DBOOST_ASIO_HAS_STD_INVOKE_RESULT for my boost build for clang13.

I meant to also use this for my build of the asio-grpc example but looks like I mistakenly left it out.

@rbresalier
Copy link
Author

rbresalier commented Nov 10, 2021

Actually -DBOOST_ASIO_HAS_STD_INVOKE_RESULT was also in my build of the asio-grpc example. I made an error in my last comment to say it wasn't there.

@Tradias
Copy link
Owner

Tradias commented Nov 12, 2021

Clang 10, 11 and 12 are now also building and testing with coroutines on CI: https://github.com/Tradias/asio-grpc/runs/4193734840?check_suite_focus=true

Although I do not understand the warning

clang: warning: argument unused during compilation: '-stdlib=libc++' [-Wunused-command-line-argument]

Sounds like it is cross-compiling. Do you have any idea perhaps?

@rbresalier
Copy link
Author

I don't understand your warning. Today after doing some cleanup I tried again with clang-10.0.1 and C++20 coroutines worked with clang-10.0.1! Don't know why they didn't work before? What is even more surprising is that on my first attempt I forgot the -fcoroutines-ts, but it compiled and ran anyway and worked with C++20 coroutijnes!

Here is my compilation command line with clang-10.0.1 in case it helps:

clang/10.0.1/bin/clang++ -DBOOST_ALL_NO_LIB -DCARES_STATICLIB -Iexample/asio_grpc_example/native.build/src/generated -isystem stage/native.stage/usr/local/include -fcoroutines-ts -stdlib=libc++ -fPIC -g -Wall -Werror -DBOOST_THREAD_VERSION=4 -DAGRPC_BOOST_ASIO -std=gnu++2a -MD -MT src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o -MF src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o.d -o src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o -c example/asio_grpc_example/src/hello-world-server-cpp20.cpp

And here is my link:

10.0.1/bin/clang++ -fcoroutines-ts -stdlib=libc++ -fPIC -g -nostdlib++ src/CMakeFiles/asio-grpc-example-protos-cpp20.dir/generated/protos/helloworld.pb.cc.o src/CMakeFiles/asio-grpc-example-protos-cpp20.dir/generated/protos/helloworld.grpc.pb.cc.o src/CMakeFiles/asio-grpc-example-protos-cpp20.dir/generated/protos/example.pb.cc.o src/CMakeFiles/asio-grpc-example-protos-cpp20.dir/generated/protos/example.grpc.pb.cc.o src/CMakeFiles/asio-grpc-hello-world-server-cpp20.dir/hello-world-server-cpp20.cpp.o -o src/asio-grpc-hello-world-server-cpp20  stage/native.stage/usr/local/lib/libgrpc++.a  stage/native.stage/usr/local/lib/libboost_container.a  stage/native.stage/usr/local/lib/libgrpc++.a  stage/native.stage/usr/local/lib64/libprotobuf.a  -lpthread  stage/native.stage/usr/local/lib/libgrpc.a  /usr/lib64/libz.so -lnsl -ldl  -lrt  -lm  -lpthread  -lrt  /usr/lib64/libssl.so  /usr/lib64/libcrypto.so  -Wl,--push-state,-Bstatic -lc++ -lc++abi -Wl,--pop-state -pthread 

I'm using more than your CMakeLists.txt to build these examples. I also did some slight modifications to your CMakeLists.txt to fit in our system.

I removed upper portions of paths to source/object files in the above. We also link with many more of our own libraries that aren't relevant so I left those out.

@Tradias
Copy link
Owner

Tradias commented Nov 13, 2021

Excellent, so it works for you just like it does for CI and CI uses Clang 10.0.0 even.

Do you think some of the changes you did to the CMakeLists.txt could be added upstream as well? Can you maybe list some of them? I have already added the -fcoroutines-ts option when compiling the examples with Clang, now on master branch as well.

@rbresalier
Copy link
Author

The main changes I did the CMakeLists.txt hard code some paths and options vs what is in the library. It would not be appropriate to add them to the github as they wouldn't apply. So I think just having the -fcoroutines-ts is all that is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants