# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'fileutils' unless find_header('funchook.h', ext_path) FUNCHOOK_DIR_NAME = 'funchook' FUNCHOOK_DIR = File.expand_path(File.join(File.dirname(File.expand_path(__FILE__)), '..', FUNCHOOK_DIR_NAME)) COMMANDS = ['./autogen.sh', './configure', 'make clean', 'make'].freeze bundler_install_target_paths = [] # .path and .paths diverge in their return type - .path returns strings, .paths returns PathSupports possible_gem_paths = Gem.path possible_gem_paths.each do |base_path| contrast_gem_dir_search = File.join(base_path, 'extensions', '**', '*', 'contrast-agent-*') extension_paths = Dir[contrast_gem_dir_search] extension_paths.map! do |extension_path| target_path = File.join(extension_path, 'shared_libraries') FileUtils.mkdir_p(target_path) unless File.exist?(target_path) target_path end bundler_install_target_paths += extension_paths end puts 'Building funchook' COMMANDS.each do |command| puts "executing: #{ command } in #{ FUNCHOOK_DIR }" Dir.chdir(FUNCHOOK_DIR) do `#{ command }` end end SOURCE_PATHS = [ File.join('include', 'funchook.h'), File.join('src', 'libfunchook.dylib'), File.join('src', 'libfunchook.so') ].freeze TARGET_PATHS = ([ File.expand_path(File.join(File.expand_path(__dir__), '..', 'shared_libraries')), File.expand_path(__dir__) ] + bundler_install_target_paths).freeze puts 'Copying required files' SOURCE_PATHS.each do |source_file| source_file_path = File.join(FUNCHOOK_DIR, source_file) unless File.exist?(source_file_path) puts "Skipping #{ source_file_path }, file doesn't exist" next end TARGET_PATHS.each do |target_path| unless File.writable?(target_path) puts "Unable to copy into #{ target_path } - directory not writable" next end puts "Copying #{ source_file_path } into #{ target_path }" FileUtils.cp(source_file_path, target_path) rescue StandardError puts "Error while copying #{ source_file } to #{ target_path }" end end end