package.rb: Use a function to create function placeholders (#6544)

* Update package.rb

* Update package_helpers.rb

* Update package.rb

* Update package_helpers.rb

* Update package_helpers.rb

* Update package.rb

* Update const.rb

* Update package.rb
This commit is contained in:
supechicken
2022-01-05 12:42:15 +08:00
committed by GitHub
parent 145f33f505
commit cd79481e89
3 changed files with 64 additions and 87 deletions

View File

@@ -127,20 +127,26 @@ CREW_COMMON_FNO_LTO_FLAGS = '-O2 -pipe -fno-lto -fPIC -fuse-ld=gold'
CREW_LDFLAGS = '-flto'
CREW_FNO_LTO_LDFLAGS = '-fno-lto'
CREW_ENV_OPTIONS = <<~OPT.chomp
CFLAGS='#{CREW_COMMON_FLAGS}' \
CXXFLAGS='#{CREW_COMMON_FLAGS}' \
FCFLAGS='#{CREW_COMMON_FLAGS}' \
FFLAGS='#{CREW_COMMON_FLAGS}' \
LDFLAGS='#{CREW_LDFLAGS}'
OPT
CREW_ENV_FNO_LTO_OPTIONS = <<~OPT.chomp
CFLAGS='#{CREW_COMMON_FNO_LTO_FLAGS}' \
CXXFLAGS='#{CREW_COMMON_FNO_LTO_FLAGS}' \
FCFLAGS='#{CREW_COMMON_FNO_LTO_FLAGS}' \
FFLAGS='#{CREW_COMMON_FNO_LTO_FLAGS}' \
LDFLAGS='#{CREW_FNO_LTO_LDFLAGS}'
OPT
CREW_ENV_OPTIONS_HASH = {
'CFLAGS' => CREW_COMMON_FLAGS,
'CXXFLAGS' => CREW_COMMON_FLAGS,
'FCFLAGS' => CREW_COMMON_FLAGS,
'FFLAGS' => CREW_COMMON_FLAGS,
'LDFLAGS' => CREW_LDFLAGS
}
# parse from hash to shell readable string
CREW_ENV_OPTIONS = CREW_ENV_OPTIONS_HASH.map {|k, v| "#{k}=\"#{v}\"" } .join(' ')
CREW_ENV_FNO_LTO_OPTIONS_HASH = {
'CFLAGS' => CREW_COMMON_FNO_LTO_FLAGS,
'CXXFLAGS' => CREW_COMMON_FNO_LTO_FLAGS,
'FCFLAGS' => CREW_COMMON_FNO_LTO_FLAGS,
'FFLAGS' => CREW_COMMON_FNO_LTO_FLAGS,
'LDFLAGS' => CREW_FNO_LTO_LDFLAGS
}
# parse from hash to shell readable string
CREW_ENV_FNO_LTO_OPTIONS = CREW_ENV_FNO_LTO_OPTIONS_HASH.map {|k, v| "#{k}=\"#{v}\"" } .join(' ')
CREW_OPTIONS = <<~OPT.chomp
--prefix=#{CREW_PREFIX} \
--libdir=#{CREW_LIB_PREFIX} \

View File

@@ -5,6 +5,17 @@ class Package
:binary_url, :binary_sha256, :source_url, :source_sha256,
:git_branch, :git_hashtag, :is_fake
create_placeholder :preflight, # Function for checks to see if install should occur.
:patch, # Function to perform patch operations prior to build from source.
:prebuild, # Function to perform pre-build operations prior to build from source.
:build, # Function to perform build from source.
:postbuild, # Function to perform post-build for both source build and binary distribution.
:check, # Function to perform check from source build. (executes only during `crew build`)
:preinstall, # Function to perform pre-install operations prior to install.
:install, # Function to perform install from source build.
:postinstall, # Function to perform post-install for both source build and binary distribution.
:remove # Function to perform after package removal.
class << self
attr_reader :is_fake
attr_accessor :name, :is_dep, :in_build, :build_from_source
@@ -99,58 +110,7 @@ class Package
@is_fake
end
# Function for checks to see if install should occur.
def self.preflight
end
# Function to perform patch operations prior to build from source.
def self.patch
end
# Function to perform pre-build operations prior to build from source.
def self.prebuild
end
# Function to perform build from source.
def self.build
end
# Function to perform post-build for both source build and binary distribution.
def self.postbuild
end
# Function to perform check from source build.
# This executes only during `crew build`.
def self.check
end
# Function to perform pre-install operations prior to install.
def self.preinstall
end
# Function to perform install from source build.
def self.install
end
# Function to perform post-install for both source build and binary distribution.
def self.postinstall
end
# Function to perform after package removal.
def self.remove
end
def self.system(*args)
def self.system(*args, **opt_args)
# add "-j#" argument to "make" at compile-time, if necessary
# Order of precedence to assign the number of processors:
@@ -159,30 +119,25 @@ class Package
# 3. The value of `nproc`.strip
# See lib/const.rb for more details
# extract command arguments
cmd_args = args.select {|arg| arg.is_a?(String) } .join(' ')
# extract Kernel.system options (if provided)
system_options = if args[-1].is_a?(Hash)
{ exception: true }.merge(args[-1])
# add exception option to opt_args
opt_args.merge!(exception: true)
# extract env hash
if args[0].is_a?(Hash)
env = CREW_ENV_OPTIONS_HASH.merge(args[0])
args.delete_at(0) # remove env hash from args array
else
{ exception: true }
env = CREW_ENV_OPTIONS_HASH
end
# after removing the env hash, all remaining args must be command args
cmd_args = args.join(' ')
# Add -j arg to build commands.
cmd_args.sub(/\b(?<=make)(?=\b)/, " -j#{CREW_NPROC}") unless cmd_args =~ /-j\s*\d+/
# Escape special bash characters.
cmd_args.gsub!('"','\\\\"')
cmd_args.gsub!('$','\\\\$')
cmd_args.gsub!('`','\\\\`')
cmd_args.sub!(/\b(?<=make)(?=\b)/, " -j#{CREW_NPROC}") unless cmd_args =~ /-j\s*\d+/
begin
# use bash instead of /bin/sh
@system_args = ''
[@system_args, "bash -e -c \"#{cmd_args}\""].reduce(&:concat)
# Uncomment following line for debugging.
# puts @system_args.orange
Kernel.system(@system_args, system_options)
Kernel.system(env, cmd_args, **opt_args)
rescue => e
exitstatus = $?.exitstatus
# print failed line number and error message

View File

@@ -1,7 +1,23 @@
class InstallError < RuntimeError; end
def property(*properties)
properties.each do |prop|
self.class_eval("def self.#{prop}(#{prop} = nil); @#{prop} = #{prop} if #{prop}; @#{prop}; end")
def create_placeholder (*functions)
# create_placeholder: create a placeholder for functions that will be used by crew later
functions.each do |func|
self.class_eval("def self.#{func.to_s}; end")
end
end
def property (*properties)
# property: like attr_accessor, but `=` is not needed to define a value
# Examples:
# {prop_name}('example') # set {prop_name} to 'example'
# {prop_name} # return the value of {prop_name}
properties.each do |prop|
self.class_eval <<~EOT
def self.#{prop} (#{prop} = nil)
@#{prop} = #{prop} if #{prop}
@#{prop}
end
EOT
end
end