Sha256: 59b6d420010c86ef291f9433a1480b0dabc3f48c35f43386100e4de7176c6615

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module PkgForge
  ##
  # Add cflag options to Forge
  class Forge
    attr_writer :cflags, :libs

    Contract None => ArrayOf[String]
    def cflags
      @cflags ||= []
    end

    Contract None => ArrayOf[String]
    def libs
      @libs ||= []
    end
  end

  module DSL
    ##
    # Add cflag options to Forge DSL
    class Forge
      Contract Maybe[ArrayOf[String]] => nil
      def cflags(value = nil)
        default = '-I%<dep>s/usr/include -L%<dep>s/usr/lib'
        value ||= @forge.deps.map { |x, _| format(default, dep: dep(x)).split }
        @forge.cflags += value.flatten
        nil
      end

      Contract Maybe[ArrayOf[String]] => nil
      def libs(value = nil)
        value ||= @forge.deps.keys
        value.map! { |x| '-l' + x.to_s }
        @forge.libs += value
        nil
      end

      # Shamelessly sourced from:
      # https://blog.mayflower.de/5800-Hardening-Compiler-Flags-for-NixOS.html
      # https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/
      # https://wiki.debian.org/Hardening
      ALL_HARDEN_OPTS = {
        controlflow: %w[-fcf-protection],
        format: %w[-Wformat -Wformat-security -Werror=format-security],
        fortify: %w[-D_FORTIFY_SOURCE=2],
        implicit: %w[-Werror=implicit-function-declaration],
        lazybinding: %w[-Wl,-z,now],
        optimize: %w[-O2],
        pic: %w[-fPIC -shared],
        pie: %w[-fpie -Wl,-pie],
        relro: %w[-Wl,-z,relro],
        stackclash: %w[-fstack-clash-protection],
        stackprotector: %w[-fstack-protector-strong],
        strictoverflow: %w[-fno-strict-overflow],
        underlinking: %w[-Wl,-z,defs],
        warnings: %w[-Wall]
      }.freeze

      Contract Maybe[Array[String]] => nil
      def harden(list = [])
        harden_opts = ALL_HARDEN_OPTS.reject { |k, _| list.include? k.to_s }
        @forge.cflags += harden_opts.values.flatten
        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pkgforge-0.24.0 lib/pkgforge/components/cflags.rb