#!/usr/bin/env python USAGE = """ fl-build: Builds files as part of the FASTlib build system. (usage: fl-build [--mode={verbose|debug|check|fast|unsafe}]) This builds the specified target rule and places links to generated files and a Makefile in the current directory. I assume that FASTlib is installed here: %s This will build code outside of the above FASTlib tree, just be sure other external rules can are referred by relative paths like "." or "..". BACKGROUND - BUILD RULES Build rules are named in the form path:name where the path is a relative path from the current directory, or absolute from the FASTlib C directory. For instance: fastlib:fastlib the 'fastlib' package in root of FASTlib tree :helper the rule 'helper' in current directory ..:parent the rule 'parent' in parent directory ../sib:sib the rule 'sib' in the sibling directory 'sib' The path specifies where the build.py specifying the rule is. Each build.py file specifies what rules are defined for that directory; nameless rules take the name of the directory. OPTIONAL COMMAND LINE OPTIONS --mode={verbose|debug|check|fast|unsafe|small} = compilation mode [verbose] verbosity messages [debug] "i need to use gdb" *[check]* assertions and fast [fast] usually fastest [unsafe] bad optimizations [small] code size --j={1|2|3...} = number of CPU's to compile with """ import os import sys import fx import buildsys import util flpath = os.path.dirname(os.path.realpath(os.path.dirname(sys.argv[0]))) cpath = os.path.realpath(flpath).rstrip(os.sep) + os.sep curdir = os.path.realpath(os.getcwd()).rstrip(os.sep) + os.sep if len(fx.extra_args) != 1: print USAGE % (cpath) sys.exit(1) if curdir.startswith(cpath): # When building inside build system, use relative paths from root # of build system. print "*** Building from within build system" #real_path = curdir[len(cpath):] real_rootpath = cpath real_path = curdir.rstrip(os.sep) fake_path = curdir[len(cpath):].replace(os.sep, "/").rstrip("/") else: # When building from outside build system, use absolute paths. # However, for generated files, we need somewhere to put them within # the bin/ directory; se use 'OUTSIDE' to signify this. print "*** Building from OUTSIDE build system... cross your fingers!" print " - I think the build system is here: %s" % cpath print " - This is where you are: %s" % curdir real_rootpath = cpath real_path = os.path.abspath(curdir).rstrip(os.sep) fake_path = "external/" + curdir.replace(os.sep, "/").strip("/") # Create build parameters fx.param_default("mode", "check") fx.param_default("compiler", "gcc") fx.param_default("j", "1") target_name = fx.extra_args[0] implicit = False if not ":" in target_name: # Target names MUST contain a colon. target_name = ":".join(os.path.split(target_name)) print "!!! No ':' is in the target rule name, assuming '%s'." % target_name if target_name.endswith(".cc"): # okay, there is actually not a build rule... let's build the library # and link to it implicit = True target_name = target_name[:-3] print "!!! Ends with .cc, assuming ad-hoc build." makefile_path = os.path.abspath("./Makefile") mode = fx.param_str("mode") compiler = fx.param_str("compiler") (kernel, nodename, release, version, arch) = os.uname() arch = util.sanitize_basename(arch) kernel = util.sanitize_basename(kernel) # Dispatch print "*** Generating Makefile in current directory" params = dict(arch=arch,kernel=kernel,mode=mode,compiler=compiler) print "*** Target Rule: '%s'" % target_name loader = buildsys.Loader(real_rootpath) if implicit: target_shortname = target_name[target_name.index(":")+1:] posttext = "binrule(name = '%s', sources=['%s.cc'], linkables = [':'])" \ % (target_shortname, target_shortname) else: posttext = "" target = loader.find_rule(target_name, real_path, fake_path, posttext) linkrule = buildsys.SymlinkRule([target], curdir) linkrule.generate(loader.thebuildsys, params) # TODO: Make symbolic link to result makefile_lines = loader.thebuildsys.to_makefile() print "*** Running Makefile" processors = fx.param_int("j") util.writelines(makefile_path, makefile_lines) sys.exit(os.spawnvp(os.P_WAIT, "make", ["make", "-j%d" % processors]))