CC as env variable

main
sandyx 11 months ago
parent 72d727abdc
commit 3b6cf68dde

@ -2,10 +2,12 @@
require 'fileutils'
RUBDIR = "#{Dir.home}/.rub/"
RUBDIR = "#{Dir.home}/.rub"
TMPLDIR = "#{RUBDIR}/rub/templates"
File.mkdir RUBDIR unless File.exist? RUBDIR
FileUtils.cp("build.rb", RUBDIR)
FileUtils.cp_r(TMPLDIR, RUBDIR)
FileUtils.cp("templates.rb", RUBDIR)
FileUtils.chmod("+x", "rub")
FileUtils.cp("rub", "/usr/local/bin")
#FileUtils.cp_r(Dir.pwd, RUBDIR)

56
rub

@ -3,18 +3,23 @@
require 'fileutils'
HELP = "rub init <name> - initialize a new project\n" \
"rub init <name> [-t, --template] <template> - initialize project with template\n" \
"rub help - display help message\n" \
"rub update - update rub\n" \
"rub meow - meows\n" \
"rub - run build.rb in current directory\n"
"rub - run build.rb in current directory\n" \
"\tif none of the above, args will be passed to build.rb :)"
ANSI_RED = "\e[31m"
ANSI_CLEAR = "\e[0m"
RUB = "#{ANSI_RED}RUB#{ANSI_CLEAR}"
RUBDIR = "#{Dir.home}/.rub/"
RUBGIT="#{RUBDIR}/rub/"
TMPLDIR = "#{RUBDIR}/templates"
RUBEDR="mousepad"
require "#{RUBDIR}/templates.rb"
def error(msg)
puts "#{RUB}: error: #{msg}"
exit 1
@ -24,25 +29,29 @@ def message(msg)
puts "#{RUB}: #{msg}"
end
def rub_init(name)
error("src directory already exists") if File.exist? "src"
error("build directory already exists") if File.exist? "build"
error(".git already exists") if File.exist? ".git"
FileUtils.mkdir "src"
FileUtils.mkdir "build"
def rub_delete
message("deleting repository"...)
buildfile = File.readlines(RUBDIR + "build.rb")
buildfile.insert(17, "APPNAME=\"#{name}\"\n")
File.write("build.rb", buildfile.join)
Dir.children(Dir.pwd).each do |x|
FileUtils.rm_r(x)
end
end
`git init`
`git branch -m main`
def rub_init(name="app", tmpl="c")
case tmpl
when "c"
TMPL::C.call(name, tmpl)
when "cpp"
TMPL::CPP.call(name, tmpl)
else
error("oops!")
end
end
def rub_install
FileUtils.mkdir RUBDIR unless File.exist? RUBDIR
FileUtils.cp("build.rb", RUBDIR)
FileUtils.cp_r("templates", RUBDIR) if File.exist? TMPLDIR
FileUtils.cp("templates.rb", RUBDIR) if File.exist? "#{RUBDIR}/templates.rb"
FileUtils.chmod("+x", "rub")
FileUtils.cp("rub", "/usr/local/bin")
end
@ -81,17 +90,32 @@ def parse_arg(arg)
exit 1
when "edit"
rub_edit
rub_edit nil
when "delete"
rub_delete
else #if none of the above, pass args onto build.rb
require "#{Dir.pwd}/build.rb"
exit 0
end
end
def parse_args(argv)
case argv[0]
when "init"
case argv[2]
when "-t"
rub_init(argv[1], argv[3])
when "--template"
rub_init(argv[1], argv[3])
else
rub_init(argv[1])
end
when "edit"
rub_edit(argv[1])
end
end
@ -107,6 +131,8 @@ when 1
parse_arg(ARGV[0])
when 2
parse_args(ARGV)
else
parse_args(ARGV)
end
#require "#{Dir.pwd}/build.rb"

@ -0,0 +1,59 @@
require 'fileutils'
module TMPL
C_MAIN = "#include <stdio.h>\n" \
"\nint main(int argc, char *argv[]) {\n" \
" printf(\"%s\\n\", \"Hello, World!\");\n" \
" return 0;\n" \
"}\n"
CPP_MAIN = "#include <cstdio>\n" \
"\nint main(int argc, char *argv[]) {\n" \
" std::printf(\"%s\\n\", \"Hello, World!\");\n" \
" return 0;\n" \
"}\n"
C = lambda { |name, tmpl|
error("template for \"#{tmpl}\" not found") unless File.exist? "#{TMPLDIR}/#{tmpl}.rb"
error("src directory already exists") if File.exist? "src"
error("build directory already exists") if File.exist? "build"
error(".git already exists") if File.exist? ".git"
FileUtils.mkdir "src"
FileUtils.mkdir "build"
buildfile = File.readlines(RUBDIR + "templates/#{tmpl}.rb")
buildfile.insert(17, "APPNAME=\"#{name}\"\n")
File.write("build.rb", buildfile.join)
c_main = File.open("src/main.c", "w")
c_main.write C_MAIN
c_main.close
`git init`
`git branch -m main`
message("set git branch to main")
}
CPP = lambda { |name, tmpl|
error("template for \"#{tmpl}\" not found") unless File.exist? "#{TMPLDIR}/#{tmpl}.rb"
error("src directory already exists") if File.exist? "src"
error("build directory already exists") if File.exist? "build"
error(".git already exists") if File.exist? ".git"
FileUtils.mkdir "src"
FileUtils.mkdir "build"
buildfile = File.readlines(RUBDIR + "templates/#{tmpl}.rb")
buildfile.insert(17, "APPNAME=\"#{name}\"\n")
File.write("build.rb", buildfile.join)
c_main = File.open("src/main.cpp", "w")
c_main.write CPP_MAIN
c_main.close
`git init`
`git branch -m main`
message("set git branch to main")
}
end

@ -0,0 +1,95 @@
#!/usr/bin/ruby
#I'll put this in here as an example
#
#should maybe use environment variables for these
CC = ENV['CC'] ? ENV['CC'] : "gcc"
CFLAGS="-O2 -march=native -std=c11"
$EFLAGS = "" #extra flags
SRCDIR="src"
BUILDDIR="build"
INCLUDE=""
LIB=""
EXT=".c" #extension of source file
#should maybe use environment variables for these
#standalone funcs
if __FILE__ == $0
ANSI_RED = "\e[31m"
ANSI_CLEAR = "\e[0m"
RUB = "#{ANSI_RED}RUB#{ANSI_CLEAR}"
def error(msg)
puts "#{RUB}: error: #{msg}"
exit 1
end
def message(msg)
puts "#{RUB}: #{msg}"
end
end
#handle argument parsing yourself
def parse(arg)
case arg
when "clean"
`rm build/*`
`rm #{APPNAME}`
exit(0)
when "debug"
$EFLAGS = "-DDEBUG"
main
else
#error "unknown argument: #{arg}"
end
end
#same as above
def args
case ARGV.count
when 0
main
when 1
parse(ARGV[0])
else
puts "too many arguments"
end
end
#main build function
def main
error("src directory empty") if Dir.children(SRCDIR).count == 0
#create a thread for each compilation unit
threads = []
Dir.each_child(SRCDIR) {|x|
threads << Thread.new {
system("#{CC} #{INCLUDE} #{CFLAGS} #{$EFLAGS} -c #{SRCDIR}/#{x} -o #{BUILDDIR}/#{File.basename(x, ".*") + ".o"}") if x.end_with?(EXT)
message "compiled: #{x}\n" if x.end_with?(EXT)
}
}
threads.each{ |t| t.join }
#wait for each thread to finish
until threads.map { |t| t.alive? }.include?(false)
end
#link
build = Dir.each_child(BUILDDIR)
o_files = []
build.each { |s|
o_files.append("#{BUILDDIR}/#{s}")
}
`#{CC} #{CFLAGS} #{$EFLAGS} #{o_files.join(" ")} -o #{APPNAME} #{LIB}`
exit(0)
end
#run some functions
args

@ -5,14 +5,14 @@
#
#should maybe use environment variables for these
$CC="gcc"
CFLAGS="-O1 -march=native -std=c11"
$CC="g++"
CFLAGS="-O2 -march=native -std=c++17"
$EFLAGS = "" #extra flags
SRCDIR="src"
BUILDDIR="build"
INCLUDE=""
LIB=""
EXT=".c" #extension of source file
EXT=".cpp" #extension of source file
#should maybe use environment variables for these
#standalone funcs

@ -0,0 +1,6 @@
ENV.each { |e|
puts e
}
puts ENV['CC']
puts ENV['CC'] ? true : false
Loading…
Cancel
Save