Sha256: 56673596b1200fcd4b7574c64a4117647737a419e125b17a0d0e71ec199496c1

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 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[String] => nil
      def cflags(value = nil)
        default = '-I%{dep}/usr/include -L%{dep}/usr/lib'
        value ||= @forge.deps.map { |x, _| (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 }
        @forge.libs += value
        nil
      end

      # Shamelessly sourced from:
      # https://blog.mayflower.de/5800-Hardening-Compiler-Flags-for-NixOS.html
      ALL_HARDEN_OPTS = {
        format: %w(-Wformat -Wformat-security -Werror=format-security),
        stackprotector: %w(-fstack-protector-strong --param ssp-buffer-size=4),
        fortify: %w(-O2 -D_FORTIFY_SOURCE=2),
        pic: '-fPIC',
        strictoverflow: '-fno-strict-overflow',
        relro: '-z=relro',
        bindnow: '-z=bindnow',
        pie: %w(-fPIE -pie)
      }.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.4.15 lib/pkgforge/components/cflags.rb