Skip to content

Instantly share code, notes, and snippets.

@Souravgoswami
Last active January 28, 2021 00:04
Show Gist options
  • Save Souravgoswami/6148c0ed2eedf64142d580d57b0e8553 to your computer and use it in GitHub Desktop.
Save Souravgoswami/6148c0ed2eedf64142d580d57b0e8553 to your computer and use it in GitHub Desktop.
Generate Makefile for generate_sources.rb
#!/usr/bin/env ruby
$-v = true
require 'etc'
BIN_DIR = 'binaries'.freeze
if File.exist?(BIN_DIR)
abort "\e[1;31mFile #{FILE} already exist!\e[0m" if File.file?(BIN_DIR)
else
Dir.mkdir(BIN_DIR)
end
files = Dir["cfiles/*.c"]
names = files.map { |x|
File.split(x)[-1].split(?.)[0..-2].join(?.).split.join(?_)
}
all = names.join(?\s)
dests = files.map.with_index { |x, i|
%Q(#{names[i]}:\n\t$(CC) $(CFLAGS) "#{x}" -o "binaries/#{names[i]}")
}.join(?\n)
makefile = <<~EOF
CC=gcc
CFLAGS="-O0"
all: #{all}
#{dests}
EOF
cpu = Etc.nprocessors.clamp(1, files.count)
puts makefile, %Q(\e[1;38;2;0;220;0mGenerated Makefile\nPlease Run "make all -j#{cpu}"\e[0m)
IO.write("Makefile", makefile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment