summaryrefslogtreecommitdiff
path: root/private/hhvm.rb
blob: 85e78b77e574f4e12e3f9c7e5a95313720f94de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# frozen_string_literal: true

require 'pathname'
require 'digest/md5'
require 'net/http'
require_relative 'config/config'
require_relative 'lib/package'

class Package
  attr_reader :path, :version, :homepage, :requires

  def initialize(path, version:, homepage:, requires: [])
    @path = path
    @version = version
    @homepage = homepage
    @requires = requires
  end

  def name
    File.basename @path
  end

  def name_version
    "#{name}-#{@version}"
  end
end

module SlackBuilder
  extend Rake::FileUtilsExt

  def self.clone(repo, tarball, tag_prefix = 'v')
    `./bin/slackbuilder clone #{repo} #{tarball} #{tag_prefix}`
  end
end

namespace :hhvm do
  def filter_set_hhvm_third_party_source_args(tokens)
    args = tokens[0]
    allowed_arguments = tokens[1..].each_slice(2)
      .filter do |key, _value|
        !key.end_with?('_URL') && !key.end_with?('_HASH')
      end

    allowed_arguments
      .flatten
      .prepend("  #{args}")
      .join("\n  ")
  end

  def split_set_hhvm_third_party_source_args(section_content)
    section_content
      .split("\n")
      .map do |line|
        hash_index = line.index '#'
        line = line[...hash_index] unless hash_index.nil?

        line.strip
      end
  end

  def rewrite_set_hhvm_third_party_source_args(contents)
    set_hhvm_start = contents.index 'SET_HHVM_THIRD_PARTY_SOURCE_ARGS('
    return nil if set_hhvm_start.nil?

    section_contents = contents[set_hhvm_start + 'SET_HHVM_THIRD_PARTY_SOURCE_ARGS('.length..]
    set_hhvm_end = section_contents.index ')'

    lines = split_set_hhvm_third_party_source_args section_contents[...set_hhvm_end]
    new_cmake_section = filter_set_hhvm_third_party_source_args lines.reject(&:blank?).join(' ').split

    contents[...set_hhvm_start] +
      "SET_HHVM_THIRD_PARTY_SOURCE_ARGS(\n#{new_cmake_section}\n)\n" +
      section_contents[set_hhvm_end..]
  end

  desc 'Generates diffs with removed download URLs'
  task :bundled_dependencies, [:version] do |_, arguments|
    run_on_source arguments[:version] do |third_party|
      c_make_lists = third_party + 'CMakeLists.txt'
      next unless c_make_lists.exist?

      contents = c_make_lists.read
      rewritten_cmake = rewrite_set_hhvm_third_party_source_args contents
      next if rewritten_cmake.nil?

      puts Open3.capture2('diff', '-Nur', c_make_lists.to_path, '-', stdin_data: rewritten_cmake).first
    end
  end

  desc 'Generated SlackBuild code to prepare bundled dependencies'
  task :bundled_code, [:version] do |_, arguments|
    run_on_source arguments[:version] do |third_party|
      c_make_lists = third_party + 'CMakeLists.txt'
      next unless c_make_lists.exist?

      contents = c_make_lists.read
      set_hhvm_start = contents.index 'SET_HHVM_THIRD_PARTY_SOURCE_ARGS('
      next if set_hhvm_start.nil?

      set_hhvm_end = contents.index ')', set_hhvm_start
      set_hhvm_start += 'SET_HHVM_THIRD_PARTY_SOURCE_ARGS('.length
      set_hhvm_end -= 1
      contents = contents[set_hhvm_start..set_hhvm_end].split[1..].map(&:strip)

      src = Pathname.new('third-party') +
        third_party.basename +
        "bundled_#{third_party.basename}-prefix" + 'src'
      bundled = src + "bundled_#{third_party.basename}"
      archive_name = contents[1][contents[1].rindex('/') + 1..-2]

      puts "mkdir -p #{bundled}"
      puts "install -m 0644 -D $CWD/#{archive_name} #{src + archive_name}"
      puts "tar -zxvf $CWD/#{archive_name} -C #{bundled}"
      puts
    end
  end
end

private

def run_on_source(version, &block)
  package = Package.new 'development/hhvm',
    version: version,
    homepage: 'https://hhvm.com/',
    requires: %w[tbb glog libdwarf libmemcached dobule-conversion]
  repository = SlackBuilder.clone 'https://github.com/facebook/hhvm.git', package, 'HHVM-'

  (repository + 'third-party').each_child do |third_party|
    block.call third_party
  end
end