Skip to content

Commit 59e1f0f

Browse files
davebenvenuticopybara-github
authored andcommitted
Ruby | Add support for a protobuf debug build (#21060)
This PR makes a small change to `extconf.rb` so we can easily build the `protobuf_c` Ruby bindings with all debug symbols included. We used [grpc](https://github.com/grpc/grpc/blob/master/src/ruby/ext/grpc/extconf.rb) as a guide, which will produce a debug build if `rake` is run with `GRPC_CONFIG=dbg`. Unlike `grpc`, the `protobuf_c` build doesn't explicitly strip symbols from the resulting library, but values will be optimized out when stepping through with `gdb`. This change also reconciles an oversight that `-fvisibility=hidden` was not set when building on non-Darwin/Linux/Freebsd platforms. Closes #21060 COPYBARA_INTEGRATE_REVIEW=#21060 from Shopify:ruby-debug-build 353b272 PiperOrigin-RevId: 744718927
1 parent d3560e7 commit 59e1f0f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

ruby/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ Then build the Gem:
8989
$ rake clobber_package gem
9090
$ gem install `ls pkg/google-protobuf-*.gem`
9191

92+
If you intend to debug the protobuf_c Ruby bindings with `gdb`, you can also
93+
build a version with debug symbols enabled by setting the `PROTOBUF_CONFIG`
94+
enviroment variable when you build the native extension:
95+
96+
```
97+
$ PROTOBUF_CONFIG=dbg rake
98+
```
99+
92100
To run the specs:
93101

94102
$ rake test

ruby/ext/google/protobuf_c/extconf.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
RbConfig::CONFIG["LD"] = RbConfig::MAKEFILE_CONFIG["LD"] = ENV["LD"]
1919
end
2020

21+
debug_enabled = ENV["PROTOBUF_CONFIG"] == "dbg"
22+
23+
additional_c_flags = debug_enabled ? "-O0 -fno-omit-frame-pointer -fvisibility=default -g" : "-O3 -DNDEBUG -fvisibility=hidden"
24+
2125
if RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/ || RUBY_PLATFORM =~ /freebsd/
22-
$CFLAGS += " -std=gnu99 -O3 -DNDEBUG -fvisibility=hidden -Wall -Wsign-compare -Wno-declaration-after-statement"
26+
$CFLAGS += " -std=gnu99 -Wall -Wsign-compare -Wno-declaration-after-statement #{additional_c_flags}"
2327
else
24-
$CFLAGS += " -std=gnu99 -O3 -DNDEBUG"
28+
$CFLAGS += " -std=gnu99 #{additional_c_flags}"
2529
end
2630

2731
if RUBY_PLATFORM =~ /linux/

0 commit comments

Comments
 (0)