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

fix compilation warnings #1731

Merged
merged 6 commits into from Mar 14, 2018
Merged

Conversation

flavorjones
Copy link
Member

Trying to make compilation produce no meaningful errors. Would love to have a second pair of eyes on this.

remove a few that generate lots of warnings on ruby.h:
- -Wcast-qual
- -Wconversion

add -Wextra back (it was removed in 2008)
to fix compiler warnings.

the node->type check is polymorphic, and so a macro makes more sense to
me (so we get some compile-time checking) than force-typecasting.
xmlNewCDataBlock() takes ptr and len, so we can use RSTRING_LEN()
instead of strlen(). strlen() was introduced in commit c766f6e
to fix a segfault, but it's enough to execute StringValuePtr() and
RSTRING_LEN() in order to avoid invalid memory access.
Copy link
Member

@larskanis larskanis left a comment

Choose a reason for hiding this comment

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

Warning in rb_scan_args() is ruby intern, we can't control it in our source code. So removing -Wconversion is OK.

For -Wconst-qual I opened #1735 as an alternative approach.

IMHO the rest can't be done better than you did. Great job as usual!

…arnings

Flavorjones fix compilation warnings
@flavorjones
Copy link
Member Author

I've merged #1735 in here ... everyone OK to merge this into master?

@flavorjones flavorjones merged commit 31babad into master Mar 14, 2018
@flavorjones flavorjones deleted the flavorjones-fix-compilation-warnings branch March 22, 2018 17:52
@knu
Copy link
Member

knu commented Jun 22, 2018

Just as reported in #1772, I believe this has broken the build where the following conditions are both met:

  • Using system libraries via NOKOGIRI_USE_SYSTEM_LIBRARIES or --use-system-libraries
  • pkg-config gem is not available (e.g. during bundle install --deployment with ruby and just the bundler gem installed)

My mkmf.log has the following lines:

| pkg-config --libs libxml-2.0
"gcc -o conftest -I/rbenv/versions/2.5.1/include/ruby-2.5.0/x86_64-linux -I/rbenv/versions/2.5.1/include/ruby-2.5.0/ruby/backward -I/rbenv/versions/2.5.1/include/ruby-2.5.0 -I. -D_FORTIFY_SOURCE=2   -g -O2 -fstack-protector-strong -Wformat -Werror=format-security  -g -DXP_UNIX -Wall -Wcast-qual -Wwrite-strings -Wextra -Wmissing-noreturn -Winline conftest.c  -L. -L/rbenv/versions/2.5.1/lib/x86_64-linux-gnu -Wl,-rpath,/rbenv/versions/2.5.1/lib/x86_64-linux-gnu -L. -Wl,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic     -Wl,-rpath,/rbenv/versions/2.5.1/lib/x86_64-linux-gnu -L/rbenv/versions/2.5.1/lib/x86_64-linux-gnu -lruby-static -lxml2 -lpthread -lgmp -ldl -lcrypt -lm   -lc "
conftest.c: In function 'main':
conftest.c:3:14: warning: unused parameter 'argc' [-Wunused-parameter]
 int main(int argc, char **argv)
              ^
conftest.c:3:27: warning: unused parameter 'argv' [-Wunused-parameter]
 int main(int argc, char **argv)
                           ^
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

package configuration for libxml-2.0 is not found

The combination of -Wall -Wextra has led pkg_config() to fail because pkg_config() checks the availability of package libraries using try_ldflags() which invokes the linker with -Werror, and therefore the warnings above are considered errors.

@flavorjones
Copy link
Member Author

@knu Can you help me reproduce this? I'm on Linux, and I've tried on Ruby 2.4.1 with and without the pkg-config gem, and cannot reproduce this issue. Can you describe your environment?

@flavorjones
Copy link
Member Author

I've also tried to reproduce with Ruby 2.5.1 and I cannot. Here's my mkmf.log snippet:

| pkg-config --libs libxml-2.0
=> "-lxml2\n"
"gcc -o conftest -I/home/flavorjones/.rvm/rubies/ruby-2.5.1/include/ruby-2.5.0/x86_64-linux -I/home/flavorjones/.rvm/rubies/ruby-2.5.1/include/ruby-2.5.0/ruby/backward -I/home/flavorjones/.rvm/rubies/ruby-2.5.1/include/ruby-2.5.0 -I../../../../ext/nokogiri    -O3 -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -Wno-maybe-uninitialized  -fPIC  -g -DXP_UNIX -O3 -Wall -Wcast-qual -Wwrite-strings -Wextra -Wmissing-noreturn -Winline conftest.c  -L. -L/home/flavorjones/.rvm/rubies/ruby-2.5.1/lib -Wl,-rpath,/home/flavorjones/.rvm/rubies/ruby-2.5.1/lib -L. -fstack-protector -rdynamic -Wl,-export-dynamic     -Wl,-rpath,/home/flavorjones/.rvm/rubies/ruby-2.5.1/lib -L/home/flavorjones/.rvm/rubies/ruby-2.5.1/lib -lruby -lxml2 -lpthread -lgmp -ldl -lcrypt -lm   -lc "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

@knu
Copy link
Member

knu commented Jun 24, 2018

Sure, I've minimized my case as uploaded here: https://gist.github.com/knu/2650e7d924e818ad4441dc06017162d1

Place the three files in the same directory and run docker build . for reproduction.

@knu
Copy link
Member

knu commented Jun 24, 2018

There must be subtle differences somewhere. On environments where the build fails, it seems the compiler is invoked without warnflags added, which should include -Wno-unused-parameter.

@voxik
Copy link
Contributor

voxik commented Jul 10, 2018

Testing on Fedora Rawhide:

$ rpm -q gcc
gcc-8.1.1-4.fc29.1.x86_64

$ rpm -q pkgconf
pkgconf-1.5.1-2.fc29.x86_64

$ rpm -q ruby-devel
ruby-devel-2.5.1-93.fc29.x86_64

$ rpm -q libxml2-devel
libxml2-devel-2.9.8-2.fc29.x86_64
$ NOKOGIRI_USE_SYSTEM_LIBRARIES=yes ruby ext/nokogiri/extconf.extra.rb

... snip ...

$ cat mkmf.log 
"gcc -o conftest -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  conftest.c  -L. -L/usr/lib64 -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic  -m64   -lruby    -lc "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

"gcc -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -m64  -Werror -c conftest.c"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main() {return 0;}
/* end */

"pkg-config --exists libxml-2.0"
| pkg-config --libs libxml-2.0
=> "-lxml2 \n"
"gcc -o conftest -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -g -DXP_UNIX -Wall -Wcast-qual -Wwrite-strings -Wextra -Wmissing-noreturn -Winline conftest.c  -L. -L/usr/lib64 -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic  -m64   -lruby -lxml2   -lc "
conftest.c: In function ‘main’:
conftest.c:3:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
 int main(int argc, char **argv)
          ~~~~^~~~
conftest.c:3:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
 int main(int argc, char **argv)
                    ~~~~~~~^~~~
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

package configuration for libxml-2.0 is not found
"pkg-config --exists libxslt"
| pkg-config --libs libxslt
=> "-lxslt -lm -lxml2 \n"
"gcc -o conftest -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -g -DXP_UNIX -Wall -Wcast-qual -Wwrite-strings -Wextra -Wmissing-noreturn -Winline conftest.c  -L. -L/usr/lib64 -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic  -m64   -lruby -lxslt -lm -lxml2   -lc "
conftest.c: In function ‘main’:
conftest.c:3:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
 int main(int argc, char **argv)
          ~~~~^~~~
conftest.c:3:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
 int main(int argc, char **argv)
                    ~~~~~~~^~~~
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

package configuration for libxslt is not found
"pkg-config --exists libexslt"
| pkg-config --libs libexslt
=> "-lexslt -lxslt -lm -lgcrypt -ldl -lgpg-error -lxml2 \n"
"gcc -o conftest -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -g -DXP_UNIX -Wall -Wcast-qual -Wwrite-strings -Wextra -Wmissing-noreturn -Winline conftest.c  -L. -L/usr/lib64 -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic  -m64   -lruby -lexslt -lxslt -lm -lgcrypt -ldl -lgpg-error -lxml2   -lc "
conftest.c: In function ‘main’:
conftest.c:3:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
 int main(int argc, char **argv)
          ~~~~^~~~
conftest.c:3:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
 int main(int argc, char **argv)
                    ~~~~~~~^~~~
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

package configuration for libexslt is not found
"gcc -E -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -g -DXP_UNIX -Wall -Wcast-qual -Wwrite-strings -Wextra -Wmissing-noreturn -Winline  conftest.c -o conftest.i"
conftest.c:3:10: fatal error: libxml/xmlversion.h: No such file or directory
 #include <libxml/xmlversion.h>
          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: #include <libxml/xmlversion.h>
/* end */
$ NOKOGIRI_USE_SYSTEM_LIBRARIES=yes ruby ext/nokogiri/extconf.conversion.rb

... snip ...

$ cat mkmf.log 
"gcc -o conftest -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  conftest.c  -L. -L/usr/lib64 -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic  -m64   -lruby    -lc "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

"gcc -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -m64  -Werror -c conftest.c"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main() {return 0;}
/* end */

"pkg-config --exists libxml-2.0"
| pkg-config --libs libxml-2.0
=> "-lxml2 \n"
"gcc -o conftest -I/usr/include -I/usr/include/ruby/backward -I/usr/include -Iext/nokogiri    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC  -g -DXP_UNIX -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline conftest.c  -L. -L/usr/lib64 -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic  -m64   -lruby -lxml2   -lc "
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

| pkg-config --cflags-only-I libxml-2.0
=> "-I/usr/include/libxml2 \n"
| pkg-config --cflags-only-other libxml-2.0
=> "\n"
| pkg-config --libs-only-l libxml-2.0
=> "-lxml2 \n"
package configuration for libxml-2.0
cflags: 
ldflags: 
libs: -lxml2

... snip ...

@voxik
Copy link
Contributor

voxik commented Jul 10, 2018

The only difference in the check for "libxml-2.0" is the difference in -Wextra vs -Wconversion flags and the subsequent warnings.

@johnl
Copy link

johnl commented Jul 25, 2018

Broken for me on Ubuntu 16.04 (xenial) and 18.04 (bionic), both long term supported release of Ubuntu.

NOKOGIRI_USE_SYSTEM_LIBRARIES=true bundle exec rake compile:nokogiri
cd tmp/x86_64-linux-gnu/nokogiri/2.3.7
/usr/bin/ruby2.3 -I. ../../../../ext/nokogiri/extconf.rb 
checking if the C compiler accepts ... yes
Building nokogiri using system libraries.
pkg-config could not be used to find libxml-2.0
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

pkg-config could not be used to find libxslt
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

pkg-config could not be used to find libexslt
Please install either `pkg-config` or the pkg-config gem per

    gem install pkg-config -v "~> 1.1"

ERROR: cannot discover where libxml2 is located on your system. please make sure `pkg-config` is installed.
rake aborted!

git bisect found 77be60c to be the culprit.

xenial is ruby 2.3 and bionic is ruby 2.5.

This Dockerfile reproduces it, just put it in an empty dir and run docker build .

FROM ubuntu:xenial

RUN apt-get update -q && apt-get install ruby ruby-dev bundler libxslt-dev libxml2-dev pkg-config git -qy
RUN git clone https://github.com/sparklemotion/nokogiri.git
WORKDIR /nokogiri
RUN git checkout v1.8.3
RUN bundler install
ENV NOKOGIRI_USE_SYSTEM_LIBRARIES=true
RUN bundle exec rake compile

flavorjones added a commit that referenced this pull request Aug 15, 2018
due to conflicting gcc CFLAGS, details in #1772 and #1731.

added to both master and PR pipelines.
@flavorjones
Copy link
Member Author

Please see #1722 for details, but this will be fixed in 1.8.5, due out soon.

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

Successfully merging this pull request may close these issues.

None yet

5 participants