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

Append flags from environment variables. #629

Merged
merged 1 commit into from May 31, 2023

Conversation

junaruga
Copy link
Member

@junaruga junaruga commented May 30, 2023

According to the mkmf.rb#init_mkmf, there are command line options below.

  • --with-cflags to set the cflags
  • --with-ldflags to set the ldflags

For example the following command compiles with the specified flags. Note that
MAKEFLAGS is to print the compiler command lines.

$ MAKEFLAGS="V=1" \
  bundle exec rake compile -- \
  --with-cflags="-Wundef -Werror" \
  --with-ldflags="-fstack-protector"

However, I couldn't find command line options to append the flags. And this
commit is to append the cflags and ldflags by the environment variables.

$ MAKEFLAGS="V=1" \
  RUBY_OPENSSL_EXTCFLAGS="-Wundef -Werror" \
  RUBY_OPENSSL_EXTLDFLAGS="-fstack-protector" \
  bundle exec rake compile

Details

The implementation was inspired by Nokogiri project.
https://github.com/sparklemotion/nokogiri/blob/30e965809c4c55b191c5c9f3a2cc5a289fce4681/ext/nokogiri/extconf.rb#L648-L651

# adopt environment config
append_cflags(ENV["CFLAGS"].split) unless ENV["CFLAGS"].nil?
append_cppflags(ENV["CPPFLAGS"].split) unless ENV["CPPFLAGS"].nil?
append_ldflags(ENV["LDFLAGS"].split) unless ENV["LDFLAGS"].nil?

However, I wanted to avoid the CFLAGS was loaded unintentionally. Because I see CFLAGS and LDFLAGS are already set in the build environment in Fedora project. Those are used to set the flags, not to append. So, I used the prefix RUBY_OPENSSL_. I also wanted to clarify that the value is used not to set but to append to the existing cflags coming from the rbconfig.rb. It seems the EXTLDFLAGS exists in the purpose of appendng the flags in the template/Makefile.in in ruby/ruby. So, I chose the RUBY_OPENSSL_EXTCFLAGS and RUBY_OPENSSL_EXTLDFLAGS.

You can also add your preferred debug flags like this.

$ MAKEFLAGS="V=1" \
  RUBY_OPENSSL_EXTCFLAGS="-O0 -g3 -ggdb3 -gdwarf-5" \
  bundle exec rake compile

According to the `mkmf.rb#init_mkmf`, there are command line options below.

* `--with-cflags` to set the `cflags`
* `--with-ldflags` to set the `ldflags`

For example the following command compiles with the specified flags. Note that
`MAKEFLAGS` is to print the compiler command lines.

```
$ MAKEFLAGS="V=1" \
  bundle exec rake compile -- \
  --with-cflags="-Wundef -Werror" \
  --with-ldflags="-fstack-protector"
```

However, I couldn't find command line options to append the flags. And this
commit is to append the `cflags` and `ldflags` by the environment variables.

```
$ MAKEFLAGS="V=1" \
  RUBY_OPENSSL_EXTCFLAGS="-Wundef -Werror" \
  RUBY_OPENSSL_EXTLDFLAGS="-fstack-protector" \
  bundle exec rake compile
```
@junaruga junaruga changed the title Append flags from environment variables on --with-env option. Append flags from environment variables. May 31, 2023
@junaruga
Copy link
Member Author

@MSP-Greg @ioquatix I saw your commit 4bb32c1 by the #279 appending the hard-coded cflags and ldflags in the case of mingw.

After this PR, can we remove the code below by setting it from the environment variables?

append_cflags '-D_FORTIFY_SOURCE=2'
append_ldflags '-fstack-protector'

@junaruga
Copy link
Member Author

junaruga commented May 31, 2023

Here is my test log on Fedora 37. The appended cflags and ldflags are used in the gcc commands below.

$ gcc --version
gcc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-4)
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ ruby -v
ruby 3.3.0dev (2023-05-30T12:39:26Z master 30b960ba34) [x86_64-linux]

$ bundle -v
Bundler version 2.5.0.dev

$ MAKEFLAGS="V=1" \
  RUBY_OPENSSL_EXTCFLAGS="-Wundef -Werror" \
  RUBY_OPENSSL_EXTLDFLAGS="-fstack-protector -Wl,-z,now" \
  bundle exec rake compile
mkdir -p tmp/x86_64-linux/openssl/3.3.0
cd tmp/x86_64-linux/openssl/3.3.0
/home/jaruga/.local/ruby-30b960ba34-debug/bin/ruby -I. -r.rake-compiler-siteconf.rb ../../../../ext/openssl/extconf.rb
checking for whether -Wundef is accepted as CFLAGS... yes
checking for whether -Werror is accepted as CFLAGS... yes
checking for whether -fstack-protector is accepted as LDFLAGS... yes
checking for whether -Wl,-z,now is accepted as LDFLAGS... yes
...
gcc -I. -I/home/jaruga/.local/ruby-30b960ba34-debug/include/ruby-3.3.0+0/x86_64-linux -I/home/jaruga/.local/ruby-30b960ba34-debug/include/ruby-3.3.0+0/ruby/backward -I/home/jaruga/.local/ruby-30b960ba34-debug/include/ruby-3.3.0+0 -I../../../../ext/openssl  -DRUBY_EXTCONF_H=\"extconf.h\"    -fPIC -O0 -fno-fast-math -g3 -ggdb3 -gdwarf-4 -Wall -Wextra -Wdeprecated-declarations -Wdiv-by-zero -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wold-style-definition -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable -Wundef  -fPIC -Wundef -Werror   -o openssl_missing.o -c ../../../../ext/openssl/openssl_missing.c
...
gcc -shared -o openssl.so openssl_missing.o ossl.o ossl_asn1.o ossl_bio.o ossl_bn.o ossl_cipher.o ossl_config.o ossl_digest.o ossl_engine.o ossl_hmac.o ossl_kdf.o ossl_ns_spki.o ossl_ocsp.o ossl_pkcs12.o ossl_pkcs7.o ossl_pkey.o ossl_pkey_dh.o ossl_pkey_dsa.o ossl_pkey_ec.o ossl_pkey_rsa.o ossl_rand.o ossl_ssl.o ossl_ssl_session.o ossl_ts.o ossl_x509.o ossl_x509attr.o ossl_x509cert.o ossl_x509crl.o ossl_x509ext.o ossl_x509name.o ossl_x509req.o ossl_x509revoked.o ossl_x509store.o -L. -L/home/jaruga/.local/ruby-30b960ba34-debug/lib -Wl,-rpath,/home/jaruga/.local/ruby-30b960ba34-debug/lib -L. -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,--no-as-needed -fstack-protector -Wl,-z,now  -Wl,--compress-debug-sections=zlib    -Wl,-rpath,/home/jaruga/.local/ruby-30b960ba34-debug/lib -L/home/jaruga/.local/ruby-30b960ba34-debug/lib -lruby  -lssl -lcrypto -lm -lpthread  -lc
...

@junaruga junaruga merged commit bd7b593 into ruby:master May 31, 2023
42 checks passed
@junaruga junaruga deleted the wip/env-appended-flags branch May 31, 2023 10:18
@ioquatix
Copy link
Member

ioquatix commented Jun 1, 2023

Nice work!

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

Successfully merging this pull request may close these issues.

None yet

2 participants